2016-08-08 22:45:57 +03:00
/************************************************************************
* file name : blocks_graphics_view . h
* - - - - - - - - - - - - - - - - - :
* creation time : 2016 / 06 / 26
* author : Victor Zarubkin
* email : v . s . zarubkin @ gmail . com
* - - - - - - - - - - - - - - - - - :
* description : The file contains declaration of GraphicsScene and GraphicsView and
* : it ' s auxiliary classes for displyaing easy_profiler blocks tree .
* - - - - - - - - - - - - - - - - - :
* change log : * 2016 / 06 / 26 Victor Zarubkin : moved sources from graphics_view . h
* : and renamed classes from My * to Prof * .
* :
* : * 2016 / 06 / 29 Victor Zarubkin : Highly optimized painting performance and memory consumption .
* :
* : * 2016 / 06 / 30 Victor Zarubkin : Replaced doubles with floats ( in ProfBlockItem ) for less memory consumption .
* :
2018-01-28 20:52:17 +03:00
* : * 2016 / 09 / 15 Victor Zarubkin : Moved sources of BlocksGraphicsItem and GraphicsRulerItem to separate files .
2016-09-15 22:30:32 +03:00
* :
2016-08-08 22:45:57 +03:00
* : *
* - - - - - - - - - - - - - - - - - :
2016-09-06 21:49:32 +03:00
* license : Lightweight profiler library for c + +
2018-01-29 23:29:43 +03:00
* : Copyright ( C ) 2016 - 2018 Sergey Yagovtsev , Victor Zarubkin
2016-09-06 21:49:32 +03:00
* :
2017-03-30 06:14:23 +03:00
* : Licensed under either of
* : * MIT license ( LICENSE . MIT or http : //opensource.org/licenses/MIT)
* : * Apache License , Version 2.0 , ( LICENSE . APACHE or http : //www.apache.org/licenses/LICENSE-2.0)
* : at your option .
2016-11-13 16:39:59 +03:00
* :
2017-03-30 06:14:23 +03:00
* : The MIT License
* :
* : Permission is hereby granted , free of charge , to any person obtaining a copy
* : of this software and associated documentation files ( the " Software " ) , to deal
* : in the Software without restriction , including without limitation the rights
* : to use , copy , modify , merge , publish , distribute , sublicense , and / or sell copies
* : of the Software , and to permit persons to whom the Software is furnished
* : to do so , subject to the following conditions :
* :
* : The above copyright notice and this permission notice shall be included in all
* : copies or substantial portions of the Software .
* :
* : THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR IMPLIED ,
* : INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY , FITNESS FOR A PARTICULAR
* : PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* : LIABLE FOR ANY CLAIM , DAMAGES OR OTHER LIABILITY , WHETHER IN AN ACTION OF CONTRACT ,
* : TORT OR OTHERWISE , ARISING FROM , OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* : USE OR OTHER DEALINGS IN THE SOFTWARE .
* :
* : The Apache License , Version 2.0 ( the " License " )
* :
* : You may not use this file except in compliance with the License .
2016-11-13 16:39:59 +03:00
* : You may obtain a copy of the License at
* :
* : http : //www.apache.org/licenses/LICENSE-2.0
* :
* : Unless required by applicable law or agreed to in writing , software
* : distributed under the License is distributed on an " AS IS " BASIS ,
* : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* : See the License for the specific language governing permissions and
* : limitations under the License .
2016-06-26 18:56:40 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2016-06-26 18:46:51 +03:00
2017-03-05 22:59:03 +03:00
# ifndef EASY_GRAPHICS_VIEW_H
# define EASY_GRAPHICS_VIEW_H
2016-06-26 18:46:51 +03:00
2016-09-15 22:30:32 +03:00
# include <stdlib.h>
# include <unordered_set>
2017-03-05 23:50:38 +03:00
2016-06-26 18:46:51 +03:00
# include <QGraphicsView>
2016-07-31 18:48:41 +03:00
# include <QGraphicsItem>
2016-06-26 20:54:16 +03:00
# include <QPoint>
2016-09-15 22:30:32 +03:00
# include <QRectF>
2016-07-27 22:52:13 +03:00
# include <QTimer>
2016-12-14 21:47:33 +03:00
# include <QLabel>
2017-03-05 23:50:38 +03:00
# include <easy/reader.h>
2017-12-07 22:36:53 +03:00
# include "common_functions.h"
2016-06-26 18:46:51 +03:00
2016-07-31 18:48:41 +03:00
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
2016-06-26 18:46:51 +03:00
2016-09-15 22:30:32 +03:00
class QGraphicsProxyWidget ;
2018-01-28 20:52:17 +03:00
class BlocksGraphicsView ;
class BlocksGraphicsItem ;
class BlocksGraphicsScrollbar ;
class GraphicsRulerItem ;
2016-07-31 18:48:41 +03:00
//////////////////////////////////////////////////////////////////////////
2016-06-26 18:46:51 +03:00
2016-08-10 22:08:27 +03:00
# define EASY_QGRAPHICSITEM(ClassName) \
class ClassName : public QGraphicsItem { \
QRectF m_boundingRect ; \
public : \
ClassName ( ) : QGraphicsItem ( ) { } \
virtual ~ ClassName ( ) { } \
void paint ( QPainter * _painter , const QStyleOptionGraphicsItem * _option , QWidget * _widget = nullptr ) override ; \
QRectF boundingRect ( ) const override { return m_boundingRect ; } \
void setBoundingRect ( qreal x , qreal y , qreal w , qreal h ) { m_boundingRect . setRect ( x , y , w , h ) ; } \
void setBoundingRect ( const QRectF & _rect ) { m_boundingRect = _rect ; } \
}
2018-01-28 20:52:17 +03:00
EASY_QGRAPHICSITEM ( BackgroundItem ) ;
EASY_QGRAPHICSITEM ( TimelineIndicatorItem ) ;
EASY_QGRAPHICSITEM ( ThreadNameItem ) ;
2016-08-10 22:08:27 +03:00
# undef EASY_QGRAPHICSITEM
//////////////////////////////////////////////////////////////////////////
2018-01-28 20:52:17 +03:00
struct BoldLabel : public QLabel {
BoldLabel ( const QString & _text , QWidget * _parent = nullptr ) ;
~ BoldLabel ( ) override ;
2016-12-14 21:47:33 +03:00
} ;
//////////////////////////////////////////////////////////////////////////
2018-01-28 20:52:17 +03:00
class BlocksGraphicsView : public QGraphicsView
2016-06-26 18:46:51 +03:00
{
Q_OBJECT
private :
2017-12-28 00:23:14 +03:00
using Parent = QGraphicsView ;
2018-01-28 20:52:17 +03:00
using This = BlocksGraphicsView ;
using Items = : : std : : vector < BlocksGraphicsItem * > ;
2017-12-28 00:23:14 +03:00
//using Keys = ::std::unordered_set<int, ::estd::hash<int> >;
2016-09-09 00:07:27 +03:00
2018-01-28 20:52:17 +03:00
Items m_items ; ///< Array of all BlocksGraphicsItem items
2017-06-06 20:46:06 +03:00
//Keys m_keys; ///< Pressed keyboard keys
2018-01-28 20:52:17 +03:00
: : profiler_gui : : TreeBlocks m_selectedBlocks ; ///< Array of items which were selected by selection zone (GraphicsRulerItem)
2016-09-09 00:07:27 +03:00
QTimer m_flickerTimer ; ///< Timer for flicking behavior
2016-09-13 23:03:01 +03:00
QTimer m_idleTimer ; ///<
2016-09-09 00:07:27 +03:00
QRectF m_visibleSceneRect ; ///< Visible scene rectangle
: : profiler : : timestamp_t m_beginTime ; ///< Begin time of profiler session. Used to reduce values of all begin and end times of profiler blocks.
2016-09-25 11:50:57 +03:00
qreal m_sceneWidth ; ///<
2016-09-09 00:07:27 +03:00
qreal m_scale ; ///< Current scale
qreal m_offset ; ///< Have to use manual offset for all scene content instead of using scrollbars because QScrollBar::value is 32-bit integer :(
2018-01-21 19:37:44 +03:00
qreal m_visibleRegionWidth ; ///< Visible scene rectangle in scene coordinates + width of vertical scrollbar (if visible)
2016-09-09 00:07:27 +03:00
qreal m_timelineStep ; ///<
2016-09-13 23:03:01 +03:00
uint64_t m_idleTime ; ///<
2016-09-09 00:07:27 +03:00
QPoint m_mousePressPos ; ///< Last mouse global position (used by mousePressEvent and mouseMoveEvent)
QPoint m_mouseMovePath ; ///< Mouse move path between press and release of any button
Qt : : MouseButtons m_mouseButtons ; ///< Pressed mouse buttons
2018-01-28 20:52:17 +03:00
BlocksGraphicsScrollbar * m_pScrollbar ; ///< Pointer to the graphics scrollbar widget
GraphicsRulerItem * m_selectionItem ; ///< Pointer to the GraphicsRulerItem which is displayed when you press right mouse button and move mouse left or right. This item is used to select blocks to display in tree widget.
GraphicsRulerItem * m_rulerItem ; ///< Pointer to the GraphicsRulerItem which is displayed when you double click left mouse button and move mouse left or right. This item is used only to measure time.
2018-03-29 01:56:21 +03:00
QWidget * m_popupWidget ; ///<
2016-09-09 00:07:27 +03:00
int m_flickerSpeedX ; ///< Current flicking speed x
int m_flickerSpeedY ; ///< Current flicking speed y
2016-09-13 23:03:01 +03:00
int m_flickerCounterX ;
int m_flickerCounterY ;
2016-09-09 00:07:27 +03:00
bool m_bDoubleClick ; ///< Is mouse buttons double clicked
bool m_bUpdatingRect ; ///< Stub flag which is used to avoid excess calculations on some scene update (flicking, scaling and so on)
bool m_bEmpty ; ///< Indicates whether scene is empty and has no items
2018-03-29 01:56:21 +03:00
bool m_isArbitraryValueTooltip ;
2018-03-31 19:01:47 +03:00
bool m_bHovered ;
2016-06-26 18:46:51 +03:00
public :
2018-01-28 20:52:17 +03:00
explicit BlocksGraphicsView ( QWidget * _parent = nullptr ) ;
~ BlocksGraphicsView ( ) override ;
2016-06-26 18:46:51 +03:00
2016-08-01 22:21:59 +03:00
// Public virtual methods
2018-03-31 19:01:47 +03:00
void enterEvent ( QEvent * _event ) override ;
void leaveEvent ( QEvent * _event ) override ;
2016-06-26 20:54:16 +03:00
void wheelEvent ( QWheelEvent * _event ) override ;
void mousePressEvent ( QMouseEvent * _event ) override ;
2016-08-06 14:50:31 +03:00
void mouseDoubleClickEvent ( QMouseEvent * _event ) override ;
2016-06-26 20:54:16 +03:00
void mouseReleaseEvent ( QMouseEvent * _event ) override ;
void mouseMoveEvent ( QMouseEvent * _event ) override ;
2016-09-09 00:07:27 +03:00
void keyPressEvent ( QKeyEvent * _event ) override ;
2016-07-31 18:48:41 +03:00
void resizeEvent ( QResizeEvent * _event ) override ;
2016-06-26 20:54:16 +03:00
2016-11-19 05:39:41 +03:00
void dragEnterEvent ( QDragEnterEvent * ) override { }
2016-08-01 22:21:59 +03:00
public :
2016-06-30 02:57:57 +03:00
2016-08-01 22:21:59 +03:00
// Public non-virtual methods
2016-07-31 18:48:41 +03:00
2016-09-25 11:50:57 +03:00
qreal sceneWidth ( ) const ;
2016-09-15 22:30:32 +03:00
qreal chronoTime ( ) const ;
qreal chronoTimeAux ( ) const ;
2018-01-28 20:52:17 +03:00
void setScrollbar ( BlocksGraphicsScrollbar * _scrollbar ) ;
2016-09-21 22:09:04 +03:00
void clear ( ) ;
2016-06-30 02:57:57 +03:00
2016-08-03 23:00:04 +03:00
void setTree ( const : : profiler : : thread_blocks_tree_t & _blocksTree ) ;
2016-07-31 18:48:41 +03:00
2016-08-09 00:45:45 +03:00
const Items & getItems ( ) const ;
2018-04-22 03:34:26 +03:00
bool getSelectionRegionForSaving ( profiler : : timestamp_t & _beginTime , profiler : : timestamp_t & _endTime ) const ;
2018-03-18 03:12:10 +03:00
void inspectCurrentView ( bool _strict ) {
onInspectCurrentView ( _strict ) ;
}
2016-07-31 18:48:41 +03:00
signals :
2016-08-01 22:21:59 +03:00
// Signals
2016-09-09 00:07:27 +03:00
void sceneUpdated ( ) ;
2016-09-08 22:42:35 +03:00
void treeChanged ( ) ;
2016-08-03 23:00:04 +03:00
void intervalChanged ( const : : profiler_gui : : TreeBlocks & _blocks , : : profiler : : timestamp_t _session_begin_time , : : profiler : : timestamp_t _left , : : profiler : : timestamp_t _right , bool _strict ) ;
2016-06-30 02:57:57 +03:00
private :
2016-06-26 20:54:16 +03:00
2016-08-01 22:21:59 +03:00
// Private non-virtual methods
2018-01-21 19:37:44 +03:00
void notifySceneSizeChange ( ) ;
void notifyVisibleRegionSizeChange ( ) ;
void notifyVisibleRegionSizeChange ( qreal _size ) ;
void notifyVisibleRegionPosChange ( ) ;
void notifyVisibleRegionPosChange ( qreal _pos ) ;
2018-03-29 01:56:21 +03:00
void removePopup ( ) ;
bool needToIgnoreMouseEvent ( ) const ;
2017-06-06 20:46:06 +03:00
2018-01-28 20:52:17 +03:00
GraphicsRulerItem * createChronometer ( bool _main = true ) ;
bool moveChrono ( GraphicsRulerItem * _chronometerItem , qreal _mouseX ) ;
2016-06-26 20:54:16 +03:00
void initMode ( ) ;
2016-12-21 22:40:06 +03:00
int updateVisibleSceneRect ( ) ;
2016-08-10 22:08:27 +03:00
void updateTimelineStep ( qreal _windowWidth ) ;
2016-08-01 22:21:59 +03:00
void scaleTo ( qreal _scale ) ;
2018-01-28 20:52:17 +03:00
void scrollTo ( const BlocksGraphicsItem * _item ) ;
2018-01-25 23:21:56 +03:00
qreal mapToDiagram ( qreal x ) const ;
void onWheel ( qreal _scenePos , int _wheelDelta ) ;
2018-01-28 20:52:17 +03:00
qreal setTree ( BlocksGraphicsItem * _item , const : : profiler : : BlocksTree : : children_t & _children , qreal & _height , uint32_t & _maxDepthChild , qreal _y , short _level ) ;
2016-06-30 02:57:57 +03:00
2018-03-18 03:12:10 +03:00
void addSelectionToHierarchy ( ) ;
2016-06-30 02:57:57 +03:00
private slots :
2016-08-01 22:21:59 +03:00
// Private Slots
2016-09-09 00:07:27 +03:00
void repaintScene ( ) ;
2018-01-25 23:21:56 +03:00
void onGraphicsScrollbarWheel ( qreal _scenePos , int _wheelDelta ) ;
2016-06-30 02:57:57 +03:00
void onScrollbarValueChange ( int ) ;
2016-07-31 13:13:48 +03:00
void onGraphicsScrollbarValueChange ( qreal ) ;
2016-07-27 22:52:13 +03:00
void onFlickerTimeout ( ) ;
2016-09-13 23:03:01 +03:00
void onIdleTimeout ( ) ;
2016-11-20 21:01:33 +03:00
void onHierarchyFlagChange ( bool _value ) ;
2016-08-03 23:00:04 +03:00
void onSelectedThreadChange ( : : profiler : : thread_id_t _id ) ;
2016-08-08 22:17:56 +03:00
void onSelectedBlockChange ( unsigned int _block_index ) ;
2016-11-30 21:59:40 +03:00
void onRefreshRequired ( ) ;
2017-06-05 21:26:10 +03:00
void onThreadViewChanged ( ) ;
2018-03-18 03:12:10 +03:00
void onZoomSelection ( ) ;
void onInspectCurrentView ( bool _strict ) ;
2016-06-26 18:46:51 +03:00
2016-08-01 22:21:59 +03:00
public :
// Public inline methods
2018-01-21 19:37:44 +03:00
qreal scale ( ) const
2016-08-01 22:21:59 +03:00
{
return m_scale ;
}
2018-01-21 19:37:44 +03:00
qreal offset ( ) const
2016-08-01 22:21:59 +03:00
{
return m_offset ;
}
2018-01-21 19:37:44 +03:00
const QRectF & visibleSceneRect ( ) const
2016-08-01 22:21:59 +03:00
{
return m_visibleSceneRect ;
}
2018-01-21 19:37:44 +03:00
qreal timelineStep ( ) const
2016-08-10 22:08:27 +03:00
{
return m_timelineStep ;
}
2018-01-21 19:37:44 +03:00
qreal time2position ( const profiler : : timestamp_t & _time ) const
2016-08-01 22:21:59 +03:00
{
return PROF_MICROSECONDS ( qreal ( _time - m_beginTime ) ) ;
//return PROF_MILLISECONDS(qreal(_time - m_beginTime));
}
2018-01-21 19:37:44 +03:00
: : profiler : : timestamp_t position2time ( qreal _pos ) const
2016-08-01 22:21:59 +03:00
{
return PROF_FROM_MICROSECONDS ( _pos ) ;
//return PROF_FROM_MILLISECONDS(_pos);
}
2018-01-28 20:52:17 +03:00
} ; // END of class BlocksGraphicsView.
2016-06-26 18:46:51 +03:00
2016-08-18 23:26:41 +03:00
//////////////////////////////////////////////////////////////////////////
2018-01-28 20:52:17 +03:00
class ThreadNamesWidget : public QGraphicsView
2016-08-09 00:45:45 +03:00
{
Q_OBJECT
2016-09-08 22:42:35 +03:00
2016-08-09 00:45:45 +03:00
private :
2018-01-21 19:37:44 +03:00
using Parent = QGraphicsView ;
2018-01-28 20:52:17 +03:00
using This = ThreadNamesWidget ;
2016-09-08 22:42:35 +03:00
2017-06-06 20:46:06 +03:00
QTimer m_idleTimer ; ///<
uint64_t m_idleTime ; ///<
2018-01-28 20:52:17 +03:00
BlocksGraphicsView * m_view ; ///<
2017-06-06 20:46:06 +03:00
QGraphicsProxyWidget * m_popupWidget ; ///<
int m_maxLength ; ///<
const int m_additionalHeight ; ///<
2016-08-09 00:45:45 +03:00
public :
2016-09-08 22:42:35 +03:00
2018-01-28 20:52:17 +03:00
explicit ThreadNamesWidget ( BlocksGraphicsView * _view , int _additionalHeight , QWidget * _parent = nullptr ) ;
~ ThreadNamesWidget ( ) override ;
2016-09-08 22:42:35 +03:00
2016-09-09 00:07:27 +03:00
void mousePressEvent ( QMouseEvent * _event ) override ;
void mouseDoubleClickEvent ( QMouseEvent * _event ) override ;
void mouseReleaseEvent ( QMouseEvent * _event ) override ;
void mouseMoveEvent ( QMouseEvent * _event ) override ;
void keyPressEvent ( QKeyEvent * _event ) override ;
void keyReleaseEvent ( QKeyEvent * _event ) override ;
2016-12-01 22:27:25 +03:00
void wheelEvent ( QWheelEvent * _event ) override ;
2016-09-08 22:42:35 +03:00
2016-11-19 05:39:41 +03:00
void dragEnterEvent ( QDragEnterEvent * ) override { }
2016-09-21 22:09:04 +03:00
void clear ( ) ;
2018-01-28 20:52:17 +03:00
const BlocksGraphicsView * view ( ) const
2016-09-09 00:07:27 +03:00
{
return m_view ;
}
2016-09-08 22:42:35 +03:00
2017-06-06 20:46:06 +03:00
private :
void removePopup ( bool _removeFromScene = false ) ;
2016-09-08 22:42:35 +03:00
private slots :
2016-09-09 00:07:27 +03:00
void setVerticalScrollbarRange ( int _minValue , int _maxValue ) ;
void onTreeChange ( ) ;
2017-06-06 20:46:06 +03:00
void onIdleTimeout ( ) ;
2016-09-09 00:07:27 +03:00
void repaintScene ( ) ;
2016-09-08 22:42:35 +03:00
2018-01-28 20:52:17 +03:00
} ; // END of class ThreadNamesWidget.
2016-08-09 00:45:45 +03:00
2016-07-31 18:48:41 +03:00
//////////////////////////////////////////////////////////////////////////
2016-06-26 18:46:51 +03:00
2018-01-28 20:52:17 +03:00
class DiagramWidget : public QWidget
2016-07-31 13:13:48 +03:00
{
Q_OBJECT
private :
2018-01-28 20:52:17 +03:00
class QSplitter * m_splitter ;
BlocksGraphicsScrollbar * m_scrollbar ;
BlocksGraphicsView * m_view ;
ThreadNamesWidget * m_threadNamesWidget ;
2016-07-31 13:13:48 +03:00
public :
2018-01-28 20:52:17 +03:00
explicit DiagramWidget ( QWidget * _parent = nullptr ) ;
~ DiagramWidget ( ) override ;
2016-07-31 13:13:48 +03:00
2018-01-28 20:52:17 +03:00
BlocksGraphicsView * view ( ) ;
2016-09-21 22:09:04 +03:00
void clear ( ) ;
2016-07-31 13:13:48 +03:00
2018-01-21 19:37:44 +03:00
void save ( class QSettings & settings ) ;
void restore ( class QSettings & settings ) ;
2016-07-31 13:13:48 +03:00
private :
2016-08-09 00:45:45 +03:00
void initWidget ( ) ;
2018-01-28 20:52:17 +03:00
} ; // END of class DiagramWidget.
2016-08-09 00:45:45 +03:00
2016-07-31 18:48:41 +03:00
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
2016-07-31 13:13:48 +03:00
2017-03-05 22:59:03 +03:00
# endif // EASY_GRAPHICS_VIEW_H