tree_widget_item.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /************************************************************************
  2. * file name : tree_widget_item.cpp
  3. * ----------------- :
  4. * creation time : 2016/08/18
  5. * author : Victor Zarubkin
  6. * email : [email protected]
  7. * ----------------- :
  8. * description : The file contains implementation of EasyTreeWidgetItem.
  9. * ----------------- :
  10. * change log : * 2016/08/18 Victor Zarubkin: Moved sources from blocks_tree_widget.cpp
  11. * : and renamed classes from Prof* to Easy*.
  12. * :
  13. * : *
  14. * ----------------- :
  15. * license : Lightweight profiler library for c++
  16. * : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin
  17. * :
  18. * : Licensed under either of
  19. * : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT)
  20. * : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  21. * : at your option.
  22. * :
  23. * : The MIT License
  24. * :
  25. * : Permission is hereby granted, free of charge, to any person obtaining a copy
  26. * : of this software and associated documentation files (the "Software"), to deal
  27. * : in the Software without restriction, including without limitation the rights
  28. * : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  29. * : of the Software, and to permit persons to whom the Software is furnished
  30. * : to do so, subject to the following conditions:
  31. * :
  32. * : The above copyright notice and this permission notice shall be included in all
  33. * : copies or substantial portions of the Software.
  34. * :
  35. * : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  36. * : INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  37. * : PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  38. * : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  39. * : TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  40. * : USE OR OTHER DEALINGS IN THE SOFTWARE.
  41. * :
  42. * : The Apache License, Version 2.0 (the "License")
  43. * :
  44. * : You may not use this file except in compliance with the License.
  45. * : You may obtain a copy of the License at
  46. * :
  47. * : http://www.apache.org/licenses/LICENSE-2.0
  48. * :
  49. * : Unless required by applicable law or agreed to in writing, software
  50. * : distributed under the License is distributed on an "AS IS" BASIS,
  51. * : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  52. * : See the License for the specific language governing permissions and
  53. * : limitations under the License.
  54. ************************************************************************/
  55. #include "tree_widget_item.h"
  56. #include "globals.h"
  57. //////////////////////////////////////////////////////////////////////////
  58. EasyTreeWidgetItem::EasyTreeWidgetItem(const ::profiler::block_index_t _treeBlock, Parent* _parent)
  59. : Parent(_parent)
  60. , m_block(_treeBlock)
  61. , m_customBGColor(0)
  62. , m_customTextColor(0)
  63. {
  64. }
  65. EasyTreeWidgetItem::~EasyTreeWidgetItem()
  66. {
  67. }
  68. bool EasyTreeWidgetItem::operator < (const Parent& _other) const
  69. {
  70. const auto col = treeWidget()->sortColumn();
  71. switch (col)
  72. {
  73. //case COL_UNKNOWN:
  74. case COL_NAME:
  75. {
  76. if (parent() == nullptr)
  77. return false; // Do not sort topLevelItems by name
  78. return Parent::operator < (_other);
  79. }
  80. case COL_NCALLS_PER_THREAD:
  81. case COL_NCALLS_PER_PARENT:
  82. case COL_NCALLS_PER_FRAME:
  83. {
  84. return data(col, Qt::UserRole).toUInt() < _other.data(col, Qt::UserRole).toUInt();
  85. }
  86. case COL_SELF_DURATION_PERCENT:
  87. case COL_PERCENT_PER_PARENT:
  88. case COL_PERCENT_PER_FRAME:
  89. case COL_PERCENT_SUM_PER_PARENT:
  90. case COL_PERCENT_SUM_PER_FRAME:
  91. case COL_PERCENT_SUM_PER_THREAD:
  92. {
  93. return data(col, Qt::UserRole).toInt() < _other.data(col, Qt::UserRole).toInt();
  94. }
  95. case COL_ACTIVE_PERCENT:
  96. {
  97. return data(col, Qt::UserRole).toDouble() < _other.data(col, Qt::UserRole).toDouble();
  98. }
  99. default:
  100. {
  101. // durations min, max, average
  102. return data(col, Qt::UserRole).toULongLong() < _other.data(col, Qt::UserRole).toULongLong();
  103. }
  104. }
  105. return false;
  106. }
  107. ::profiler::block_index_t EasyTreeWidgetItem::block_index() const
  108. {
  109. return m_block;
  110. }
  111. ::profiler_gui::EasyBlock& EasyTreeWidgetItem::guiBlock()
  112. {
  113. return easyBlock(m_block);
  114. }
  115. const ::profiler::BlocksTree& EasyTreeWidgetItem::block() const
  116. {
  117. return blocksTree(m_block);
  118. }
  119. ::profiler::timestamp_t EasyTreeWidgetItem::duration() const
  120. {
  121. if (parent() != nullptr)
  122. return block().node->duration();
  123. return data(COL_DURATION, Qt::UserRole).toULongLong();
  124. }
  125. ::profiler::timestamp_t EasyTreeWidgetItem::selfDuration() const
  126. {
  127. return data(COL_SELF_DURATION, Qt::UserRole).toULongLong();
  128. }
  129. void EasyTreeWidgetItem::setTimeSmart(int _column, ::profiler_gui::TimeUnits _units, const ::profiler::timestamp_t& _time, const QString& _prefix)
  130. {
  131. const ::profiler::timestamp_t nanosecondsTime = PROF_NANOSECONDS(_time);
  132. setData(_column, Qt::UserRole, (quint64)nanosecondsTime);
  133. setToolTip(_column, QString("%1 ns").arg(nanosecondsTime));
  134. setText(_column, QString("%1%2").arg(_prefix).arg(::profiler_gui::timeStringRealNs(_units, nanosecondsTime, 3)));
  135. // if (_time < 1e3)
  136. // {
  137. // setText(_column, QString("%1%2 ns").arg(_prefix).arg(nanosecondsTime));
  138. // }
  139. // else if (_time < 1e6)
  140. // {
  141. // setText(_column, QString("%1%2 us").arg(_prefix).arg(double(nanosecondsTime) * 1e-3, 0, 'f', 3));
  142. // }
  143. // else if (_time < 1e9)
  144. // {
  145. // setText(_column, QString("%1%2 ms").arg(_prefix).arg(double(nanosecondsTime) * 1e-6, 0, 'f', 3));
  146. // }
  147. // else
  148. // {
  149. // setText(_column, QString("%1%2 s").arg(_prefix).arg(double(nanosecondsTime) * 1e-9, 0, 'f', 3));
  150. // }
  151. }
  152. void EasyTreeWidgetItem::setTimeSmart(int _column, ::profiler_gui::TimeUnits _units, const ::profiler::timestamp_t& _time)
  153. {
  154. const ::profiler::timestamp_t nanosecondsTime = PROF_NANOSECONDS(_time);
  155. setData(_column, Qt::UserRole, (quint64)nanosecondsTime);
  156. setToolTip(_column, QString("%1 ns").arg(nanosecondsTime));
  157. setText(_column, ::profiler_gui::timeStringRealNs(_units, nanosecondsTime, 3));
  158. }
  159. void EasyTreeWidgetItem::setTimeMs(int _column, const ::profiler::timestamp_t& _time)
  160. {
  161. const ::profiler::timestamp_t nanosecondsTime = PROF_NANOSECONDS(_time);
  162. setData(_column, Qt::UserRole, (quint64)nanosecondsTime);
  163. setToolTip(_column, QString("%1 ns").arg(nanosecondsTime));
  164. setText(_column, QString::number(double(nanosecondsTime) * 1e-6, 'g', 9));
  165. }
  166. void EasyTreeWidgetItem::setTimeMs(int _column, const ::profiler::timestamp_t& _time, const QString& _prefix)
  167. {
  168. const ::profiler::timestamp_t nanosecondsTime = PROF_NANOSECONDS(_time);
  169. setData(_column, Qt::UserRole, (quint64)nanosecondsTime);
  170. setToolTip(_column, QString("%1 ns").arg(nanosecondsTime));
  171. setText(_column, QString("%1%2").arg(_prefix).arg(double(nanosecondsTime) * 1e-6, 0, 'g', 9));
  172. }
  173. void EasyTreeWidgetItem::setBackgroundColor(QRgb _color)
  174. {
  175. m_customBGColor = _color;
  176. }
  177. void EasyTreeWidgetItem::setTextColor(QRgb _color)
  178. {
  179. m_customTextColor = _color;
  180. }
  181. void EasyTreeWidgetItem::colorize(bool _colorize)
  182. {
  183. if (_colorize)
  184. {
  185. for (int i = 0; i < COL_COLUMNS_NUMBER; ++i)
  186. {
  187. setBackground(i, QColor::fromRgb(m_customBGColor));
  188. setForeground(i, QColor::fromRgb(m_customTextColor));
  189. }
  190. }
  191. else
  192. {
  193. const QBrush nobrush;
  194. for (int i = 0; i < COL_COLUMNS_NUMBER; ++i)
  195. {
  196. setBackground(i, nobrush);
  197. setForeground(i, nobrush);
  198. }
  199. }
  200. }
  201. void EasyTreeWidgetItem::collapseAll()
  202. {
  203. for (int i = 0, childrenNumber = childCount(); i < childrenNumber; ++i)
  204. {
  205. static_cast<EasyTreeWidgetItem*>(child(i))->collapseAll();
  206. }
  207. setExpanded(false);
  208. if (parent() != nullptr)
  209. guiBlock().expanded = false;
  210. }
  211. void EasyTreeWidgetItem::expandAll()
  212. {
  213. for (int i = 0, childrenNumber = childCount(); i < childrenNumber; ++i)
  214. {
  215. static_cast<EasyTreeWidgetItem*>(child(i))->expandAll();
  216. }
  217. setExpanded(true);
  218. if (parent() != nullptr)
  219. guiBlock().expanded = true;
  220. }
  221. //////////////////////////////////////////////////////////////////////////