easy_graphics_item.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /************************************************************************
  2. * file name : easy_graphics_item.h
  3. * ----------------- :
  4. * creation time : 2016/09/15
  5. * author : Victor Zarubkin
  6. * email : [email protected]
  7. * ----------------- :
  8. * description : The file contains declaration of EasyGraphicsItem - an item
  9. * : used to draw profiler blocks on graphics scene.
  10. * ----------------- :
  11. * change log : * 2016/09/15 Victor Zarubkin: moved sources from blocks_graphics_view.h/.cpp
  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. #ifndef EASY_GRAPHICS_ITEM_H
  56. #define EASY_GRAPHICS_ITEM_H
  57. #include <stdlib.h>
  58. #include <QGraphicsItem>
  59. #include <QRectF>
  60. #include <QString>
  61. #include <easy/reader.h>
  62. #include "common_types.h"
  63. //////////////////////////////////////////////////////////////////////////
  64. //////////////////////////////////////////////////////////////////////////
  65. class EasyGraphicsView;
  66. class EasyGraphicsItem : public QGraphicsItem
  67. {
  68. typedef ::profiler_gui::EasyItems Children;
  69. typedef ::std::vector<uint32_t> DrawIndexes;
  70. typedef ::std::vector<qreal> RightBounds;
  71. typedef ::std::vector<Children> Sublevels;
  72. DrawIndexes m_levelsIndexes; ///< Indexes of first item on each level from which we must start painting
  73. RightBounds m_rightBounds; ///<
  74. Sublevels m_levels; ///< Arrays of items for each level
  75. QRectF m_boundingRect; ///< boundingRect (see QGraphicsItem)
  76. QString m_threadName; ///<
  77. const ::profiler::BlocksTreeRoot* m_pRoot; ///< Pointer to the root profiler block (thread block). Used by ProfTreeWidget to restore hierarchy.
  78. uint8_t m_index; ///< This item's index in the list of items of EasyGraphicsView
  79. public:
  80. explicit EasyGraphicsItem(uint8_t _index, const::profiler::BlocksTreeRoot& _root);
  81. virtual ~EasyGraphicsItem();
  82. // Public virtual methods
  83. QRectF boundingRect() const override;
  84. void paint(QPainter* _painter, const QStyleOptionGraphicsItem* _option, QWidget* _widget = nullptr) override;
  85. public:
  86. // Public non-virtual methods
  87. void validateName();
  88. const ::profiler::BlocksTreeRoot* root() const;
  89. const QString& threadName() const;
  90. QRect getRect() const;
  91. void setBoundingRect(qreal x, qreal y, qreal w, qreal h);
  92. void setBoundingRect(const QRectF& _rect);
  93. ::profiler::thread_id_t threadId() const;
  94. ///< Returns number of levels
  95. uint8_t levels() const;
  96. float levelY(uint8_t _level) const;
  97. /** \brief Sets number of levels.
  98. \note Must be set before doing anything else.
  99. \param _levels Desired number of levels */
  100. void setLevels(uint8_t _levels);
  101. /** \brief Reserves memory for desired number of items on specified level.
  102. \param _level Index of the level
  103. \param _items Desired number of items on this level */
  104. void reserve(uint8_t _level, unsigned int _items);
  105. /**\brief Returns reference to the array of items of specified level.
  106. \param _level Index of the level */
  107. const Children& items(uint8_t _level) const;
  108. /**\brief Returns reference to the item with required index on specified level.
  109. \param _level Index of the level
  110. \param _index Index of required item */
  111. const ::profiler_gui::EasyBlockItem& getItem(uint8_t _level, unsigned int _index) const;
  112. /**\brief Returns reference to the item with required index on specified level.
  113. \param _level Index of the level
  114. \param _index Index of required item */
  115. ::profiler_gui::EasyBlockItem& getItem(uint8_t _level, unsigned int _index);
  116. /** \brief Adds new item to required level.
  117. \param _level Index of the level
  118. \retval Index of the new created item */
  119. unsigned int addItem(uint8_t _level);
  120. /** \brief Finds top-level blocks which are intersects with required selection zone.
  121. \note Found blocks will be added into the array of selected blocks.
  122. \param _left Left bound of the selection zone
  123. \param _right Right bound of the selection zone
  124. \param _blocks Reference to the array of selected blocks */
  125. void getBlocks(qreal _left, qreal _right, ::profiler_gui::TreeBlocks& _blocks) const;
  126. const ::profiler_gui::EasyBlock* intersect(const QPointF& _pos, ::profiler::block_index_t& _blockIndex) const;
  127. const ::profiler_gui::EasyBlock* intersectEvent(const QPointF& _pos) const;
  128. private:
  129. ///< Returns pointer to the EasyGraphicsView widget.
  130. const EasyGraphicsView* view() const;
  131. #ifdef EASY_GRAPHICS_ITEM_RECURSIVE_PAINT
  132. void paintChildren(const float _minWidth, const int _narrowSizeHalf, const uint8_t _levelsNumber, QPainter* _painter, struct EasyPainterInformation& p, ::profiler_gui::EasyBlockItem& _item, const ::profiler_gui::EasyBlock& _itemBlock, RightBounds& _rightBounds, uint8_t _level, int8_t _mode);
  133. #endif
  134. public:
  135. // Public inline methods
  136. ///< Returns this item's index in the list of graphics items of EasyGraphicsView
  137. inline uint8_t index() const
  138. {
  139. return m_index;
  140. }
  141. }; // END of class EasyGraphicsItem.
  142. //////////////////////////////////////////////////////////////////////////
  143. //////////////////////////////////////////////////////////////////////////
  144. #endif // EASY_GRAPHICS_ITEM_H