0
0
mirror of https://github.com/yse/easy_profiler.git synced 2024-12-27 16:51:22 +08:00

Flicker functionality for profiler gui on QWidgets

This commit is contained in:
Victor Zarubkin 2016-07-27 22:52:13 +03:00
parent 03fb3f271f
commit 5b7364b0bd
2 changed files with 61 additions and 3 deletions

View File

@ -875,7 +875,13 @@ qreal ProfGraphicsScene::setTree(ProfGraphicsItem* _item, const BlocksTree::chil
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ProfGraphicsView::ProfGraphicsView(bool _test) : QGraphicsView(), m_scale(1), m_scaleCoeff(1.25), m_mouseButtons(Qt::NoButton), m_bUpdatingRect(false) ProfGraphicsView::ProfGraphicsView(bool _test)
: QGraphicsView()
, m_scale(1)
, m_scaleCoeff(1.25)
, m_mouseButtons(Qt::NoButton)
, m_bUpdatingRect(false)
, m_flickerSpeed(0)
{ {
initMode(); initMode();
setScene(new ProfGraphicsScene(this, _test)); setScene(new ProfGraphicsScene(this, _test));
@ -883,7 +889,13 @@ ProfGraphicsView::ProfGraphicsView(bool _test) : QGraphicsView(), m_scale(1), m_
updateVisibleSceneRect(); updateVisibleSceneRect();
} }
ProfGraphicsView::ProfGraphicsView(const thread_blocks_tree_t& _blocksTree) : QGraphicsView(), m_scale(1), m_scaleCoeff(1.25), m_mouseButtons(Qt::NoButton), m_bUpdatingRect(false) ProfGraphicsView::ProfGraphicsView(const thread_blocks_tree_t& _blocksTree)
: QGraphicsView()
, m_scale(1)
, m_scaleCoeff(1.25)
, m_mouseButtons(Qt::NoButton)
, m_bUpdatingRect(false)
, m_flickerSpeed(0)
{ {
initMode(); initMode();
setScene(new ProfGraphicsScene(_blocksTree, this)); setScene(new ProfGraphicsScene(_blocksTree, this));
@ -956,6 +968,14 @@ void ProfGraphicsView::mouseMoveEvent(QMouseEvent* _event)
// because if scrollbar does not emit valueChanged signal then viewport does not move // because if scrollbar does not emit valueChanged signal then viewport does not move
updateVisibleSceneRect(); // Update scene visible rect only once updateVisibleSceneRect(); // Update scene visible rect only once
// Update flicker speed
m_flickerSpeed += delta.x() >> 1;
if (!m_flickerTimer.isActive())
{
// If flicker timer is not started, then start it
m_flickerTimer.start(20);
}
} }
//_event->accept(); //_event->accept();
@ -974,6 +994,7 @@ void ProfGraphicsView::initMode()
connect(horizontalScrollBar(), &QScrollBar::valueChanged, this, &This::onScrollbarValueChange); connect(horizontalScrollBar(), &QScrollBar::valueChanged, this, &This::onScrollbarValueChange);
connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &This::onScrollbarValueChange); connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &This::onScrollbarValueChange);
connect(&m_flickerTimer, &QTimer::timeout, this, &This::onFlickerTimeout);
} }
void ProfGraphicsView::updateVisibleSceneRect() void ProfGraphicsView::updateVisibleSceneRect()
@ -1019,4 +1040,37 @@ void ProfGraphicsView::onScrollbarValueChange(int)
updateVisibleSceneRect(); updateVisibleSceneRect();
} }
void ProfGraphicsView::onFlickerTimeout()
{
if (m_mouseButtons & Qt::LeftButton)
{
// Fast slow-down and stop if mouse button is pressed, no flicking.
m_flickerSpeed >>= 1;
}
else
{
// Flick when mouse button is not pressed
auto hbar = horizontalScrollBar();
m_bUpdatingRect = true; // Block scrollbars from updating scene rect to make it possible to do it only once
hbar->setValue(hbar->value() - m_flickerSpeed);
m_bUpdatingRect = false;
// Seems like an ugly stub, but QSignalBlocker is also a bad decision
// because if scrollbar does not emit valueChanged signal then viewport does not move
updateVisibleSceneRect(); // Update scene visible rect only once
static const auto sign = [](int _value) { return _value < 0 ? -1 : 1; };
static const auto absmin = [](int _a, int _b) { return abs(_a) < abs(_b) ? _a : _b; };
m_flickerSpeed -= absmin(3 * sign(m_flickerSpeed), m_flickerSpeed);
}
if (m_flickerSpeed == 0)
{
// Flicker stopped, no timer needed.
m_flickerTimer.stop();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -29,6 +29,7 @@
#include <QGraphicsPolygonItem> #include <QGraphicsPolygonItem>
#include <QGraphicsSimpleTextItem> #include <QGraphicsSimpleTextItem>
#include <QPoint> #include <QPoint>
#include <QTimer>
#include <stdlib.h> #include <stdlib.h>
#include <vector> #include <vector>
#include "profiler/reader.h" #include "profiler/reader.h"
@ -128,7 +129,7 @@ private:
typedef ProfGraphicsScene This; typedef ProfGraphicsScene This;
::profiler::timestamp_t m_beginTime; ::profiler::timestamp_t m_beginTime;
public: public:
@ -164,11 +165,13 @@ private:
typedef ProfGraphicsView This; typedef ProfGraphicsView This;
QTimer m_flickerTimer;
QRectF m_visibleSceneRect; QRectF m_visibleSceneRect;
qreal m_scale; qreal m_scale;
qreal m_scaleCoeff; qreal m_scaleCoeff;
QPoint m_mousePressPos; QPoint m_mousePressPos;
Qt::MouseButtons m_mouseButtons; Qt::MouseButtons m_mouseButtons;
int m_flickerSpeed;
bool m_bUpdatingRect; bool m_bUpdatingRect;
public: public:
@ -205,6 +208,7 @@ private:
private slots: private slots:
void onScrollbarValueChange(int); void onScrollbarValueChange(int);
void onFlickerTimeout();
}; // END of class ProfGraphicsView. }; // END of class ProfGraphicsView.