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

(GraphicsView) Using GraphicsScrollbar with qreal values instead of standard QScrollBar with int values.

(GraphicsView) Does not use standard QGraphicsView::scale and QGraphicsView::centerOn methods, does not use standard QScrollBar because of it's fast value overflow. Using manual offset for all scene items instead of standard scrolling.
This commit is contained in:
Victor Zarubkin 2016-07-31 13:13:48 +03:00
parent ac484a1ae4
commit c943431104
5 changed files with 511 additions and 503 deletions

File diff suppressed because it is too large Load Diff

View File

@ -32,30 +32,23 @@
#include <QTimer>
#include <stdlib.h>
#include <vector>
#include "graphics_scrollbar.h"
#include "profiler/reader.h"
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define DRAW_METHOD 2
#pragma pack(push, 1)
struct ProfBlockItem
{
const BlocksTree* block;
qreal x;
float w;
float y;
float h;
QRgb color;
#if DRAW_METHOD > 0
unsigned int children_begin;
#endif
unsigned short totalHeight;
#if DRAW_METHOD > 0
char state;
#else
bool state;
#endif
const BlocksTree* block;
qreal x;
float w;
float y;
float h;
QRgb color;
unsigned int children_begin;
unsigned short totalHeight;
char state;
void setRect(qreal _x, float _y, float _w, float _h);
qreal left() const;
@ -67,21 +60,22 @@ struct ProfBlockItem
};
#pragma pack(pop)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class ProfGraphicsView;
class ProfGraphicsItem : public QGraphicsItem
{
typedef ::std::vector<ProfBlockItem> Children;
typedef ::std::vector<ProfBlockItem> Children;
typedef ::std::vector<unsigned int> DrawIndexes;
typedef ::std::vector<Children> Sublevels;
#if DRAW_METHOD == 2
typedef ::std::vector<unsigned int> DrawIndexes;
DrawIndexes m_levelsIndexes;
typedef ::std::vector<Children> Sublevels;
Sublevels m_levels;
#else
Children m_items;
#endif
DrawIndexes m_levelsIndexes;
Sublevels m_levels;
QRectF m_rect;
const bool m_bTest;
QRectF m_rect;
QRgb m_backgroundColor;
const bool m_bTest;
public:
@ -92,18 +86,13 @@ public:
QRectF boundingRect() const override;
void paint(QPainter* _painter, const QStyleOptionGraphicsItem* _option, QWidget* _widget = nullptr) override;
QRgb backgroundColor() const;
void setBoundingRect(qreal x, qreal y, qreal w, qreal h);
void setBoundingRect(const QRectF& _rect);
#if DRAW_METHOD == 0
void reserve(size_t _items);
const Children& items() const;
const ProfBlockItem& getItem(size_t _index) const;
ProfBlockItem& getItem(size_t _index);
size_t addItem();
size_t addItem(const ProfBlockItem& _item);
size_t addItem(ProfBlockItem&& _item);
#else
void setBackgroundColor(QRgb _color);
unsigned short levels() const;
void setLevels(unsigned short _levels);
void reserve(unsigned short _level, size_t _items);
@ -113,7 +102,10 @@ public:
size_t addItem(unsigned short _level);
size_t addItem(unsigned short _level, const ProfBlockItem& _item);
size_t addItem(unsigned short _level, ProfBlockItem&& _item);
#endif
private:
const ProfGraphicsView* view() const;
}; // END of class ProfGraphicsItem.
@ -144,8 +136,6 @@ private:
void clearSilent();
void setTree(const thread_blocks_tree_t& _blocksTree);
qreal setTree(const BlocksTree::children_t& _children, qreal& _height, qreal _y);
qreal setTree(ProfGraphicsItem* _item, const BlocksTree::children_t& _children, qreal& _height, qreal _y, unsigned short _level);
inline qreal time2position(const profiler::timestamp_t& _time) const
@ -165,17 +155,20 @@ private:
typedef ProfGraphicsView This;
QTimer m_flickerTimer;
QRectF m_visibleSceneRect;
qreal m_scale;
qreal m_scaleCoeff;
QPoint m_mousePressPos;
Qt::MouseButtons m_mouseButtons;
int m_flickerSpeed;
bool m_bUpdatingRect;
QTimer m_flickerTimer;
QRectF m_visibleSceneRect;
qreal m_scale;
qreal m_offset; ///< Have to use manual offset for all scene content instead of using scrollbars because QScrollBar::value is 32-bit integer :(
QPoint m_mousePressPos;
Qt::MouseButtons m_mouseButtons;
GraphicsHorizontalScrollbar* m_pScrollbar;
int m_flickerSpeed;
bool m_bUpdatingRect;
public:
using QGraphicsView::scale;
ProfGraphicsView(bool _test = false);
ProfGraphicsView(const thread_blocks_tree_t& _blocksTree);
virtual ~ProfGraphicsView();
@ -185,16 +178,22 @@ public:
void mouseReleaseEvent(QMouseEvent* _event) override;
void mouseMoveEvent(QMouseEvent* _event) override;
inline qreal currentScale() const
inline qreal scale() const
{
return m_scale;
}
inline qreal offset() const
{
return m_offset;
}
inline const QRectF& visibleSceneRect() const
{
return m_visibleSceneRect;
}
void setScrollbar(GraphicsHorizontalScrollbar* _scrollbar);
void setTree(const thread_blocks_tree_t& _blocksTree);
void clearSilent();
@ -204,14 +203,39 @@ private:
void initMode();
void updateVisibleSceneRect();
void updateScene();
private slots:
void onScrollbarValueChange(int);
void onGraphicsScrollbarValueChange(qreal);
void onFlickerTimeout();
}; // END of class ProfGraphicsView.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class ProfGraphicsViewWidget : public QWidget
{
Q_OBJECT
private:
ProfGraphicsView* m_view;
GraphicsHorizontalScrollbar* m_scrollbar;
public:
ProfGraphicsViewWidget(bool _test = false);
ProfGraphicsViewWidget(const thread_blocks_tree_t& _blocksTree);
virtual ~ProfGraphicsViewWidget();
ProfGraphicsView* view();
private:
}; // END of class ProfGraphicsViewWidget.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#endif // MY____GRAPHICS___VIEW_H

View File

@ -15,12 +15,20 @@
* license : TODO: add license text
************************************************************************/
#include <QScrollBar>
#include <algorithm>
#include <QWheelEvent>
#include <QMouseEvent>
#include <QResizeEvent>
#include "graphics_scrollbar.h"
//////////////////////////////////////////////////////////////////////////
auto const clamp = [](double _minValue, double _value, double _maxValue)
const qreal SCALING_COEFFICIENT = 1.25;
const qreal SCALING_COEFFICIENT_INV = 1.0 / SCALING_COEFFICIENT;
//////////////////////////////////////////////////////////////////////////
auto const clamp = [](qreal _minValue, qreal _value, qreal _maxValue)
{
return (_value < _minValue ? _minValue : (_value > _maxValue ? _maxValue : _value));
};
@ -38,32 +46,58 @@ GraphicsHorizontalSlider::~GraphicsHorizontalSlider()
}
double GraphicsHorizontalSlider::width() const
void GraphicsHorizontalSlider::paint(QPainter* _painter, const QStyleOptionGraphicsItem* _option, QWidget* _widget)
{
//Parent::paint(_painter, _option, _widget);
const auto currentScale = static_cast<const GraphicsHorizontalScrollbar*>(scene()->parent())->getWindowScale();
const auto br = rect();
qreal w = width() * currentScale;
qreal dx = 0;
if (w < 1.0)
{
dx = (w - 1.0) * 0.5;
w = 1.0;
}
_painter->save();
_painter->setTransform(QTransform::fromScale(1.0 / currentScale, 1), true);
_painter->setBrush(brush());
_painter->setPen(Qt::NoPen);
_painter->drawRect(QRectF(dx + br.left() * currentScale, br.top(), w, br.height()));
_painter->restore();
}
qreal GraphicsHorizontalSlider::width() const
{
return m_halfwidth * 2.0;
}
double GraphicsHorizontalSlider::halfwidth() const
qreal GraphicsHorizontalSlider::halfwidth() const
{
return m_halfwidth;
}
void GraphicsHorizontalSlider::setWidth(double _width)
void GraphicsHorizontalSlider::setWidth(qreal _width)
{
m_halfwidth = _width * 0.5;
setPolygon(QRectF(-m_halfwidth, -10, _width, 20));
setRect(-m_halfwidth, -10, _width, 20);
}
void GraphicsHorizontalSlider::setHalfwidth(double _halfwidth)
void GraphicsHorizontalSlider::setHalfwidth(qreal _halfwidth)
{
m_halfwidth = _halfwidth;
setPolygon(QRectF(-m_halfwidth, -10, m_halfwidth * 2.0, 20));
setRect(-m_halfwidth, -10, m_halfwidth * 2.0, 20);
}
void GraphicsHorizontalSlider::setColor(QRgb _color)
{
QColor c(_color);
c.setAlpha((_color & 0xff000000) >> 24);
auto b = brush();
b.setColor(_color);
b.setColor(c);
setBrush(b);
}
@ -74,7 +108,10 @@ GraphicsHorizontalScrollbar::GraphicsHorizontalScrollbar(QWidget* _parent)
, m_minimumValue(0)
, m_maximumValue(500)
, m_value(10)
, m_windowScale(1)
, m_mouseButtons(Qt::NoButton)
, m_slider(nullptr)
, m_bScrolling(false)
{
setCacheMode(QGraphicsView::CacheNone);
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
@ -93,10 +130,10 @@ GraphicsHorizontalScrollbar::GraphicsHorizontalScrollbar(QWidget* _parent)
m_slider = new GraphicsHorizontalSlider();
m_slider->setPos(10, 0);
m_slider->setColor(0xffff0404);
m_slider->setColor(0x80e00000);
selfScene->addItem(m_slider);
centerOn(10, 0);
centerOn(0, 0);
}
GraphicsHorizontalScrollbar::~GraphicsHorizontalScrollbar()
@ -106,34 +143,120 @@ GraphicsHorizontalScrollbar::~GraphicsHorizontalScrollbar()
//////////////////////////////////////////////////////////////////////////
double GraphicsHorizontalScrollbar::minimum() const
qreal GraphicsHorizontalScrollbar::getWindowScale() const
{
return m_windowScale;
}
qreal GraphicsHorizontalScrollbar::minimum() const
{
return m_minimumValue;
}
double GraphicsHorizontalScrollbar::maximum() const
qreal GraphicsHorizontalScrollbar::maximum() const
{
return m_maximumValue;
}
double GraphicsHorizontalScrollbar::value() const
qreal GraphicsHorizontalScrollbar::range() const
{
return m_maximumValue - m_minimumValue;
}
qreal GraphicsHorizontalScrollbar::value() const
{
return m_value;
}
//////////////////////////////////////////////////////////////////////////
void GraphicsHorizontalScrollbar::setValue(double _value)
void GraphicsHorizontalScrollbar::setValue(qreal _value)
{
m_value = clamp(m_minimumValue + m_slider->halfwidth(), _value, m_maximumValue - m_slider->halfwidth());
m_value = clamp(m_minimumValue, _value, m_maximumValue - m_slider->width());
m_slider->setX(m_value + m_slider->halfwidth());
emit valueChanged(m_value);
}
void GraphicsHorizontalScrollbar::setRange(double _minValue, double _maxValue)
void GraphicsHorizontalScrollbar::setRange(qreal _minValue, qreal _maxValue)
{
const auto oldRange = range();
const auto oldValue = oldRange < 1e-3 ? 0.0 : m_value / oldRange;
m_minimumValue = _minValue;
m_maximumValue = _maxValue;
scene()->setSceneRect(_minValue, -10, _maxValue - _minValue, 20);
emit rangeChanged();
setValue(_minValue + oldValue * range());
onWindowWidthChange(width());
}
void GraphicsHorizontalScrollbar::setSliderWidth(qreal _width)
{
m_slider->setWidth(_width);
setValue(m_value);
}
//////////////////////////////////////////////////////////////////////////
void GraphicsHorizontalScrollbar::mousePressEvent(QMouseEvent* _event)
{
m_mouseButtons = _event->buttons();
if (m_mouseButtons & Qt::LeftButton)
{
m_bScrolling = true;
m_mousePressPos = _event->pos();
setValue(mapToScene(m_mousePressPos).x() - m_minimumValue);
}
_event->accept();
//QGraphicsView::mousePressEvent(_event);
}
void GraphicsHorizontalScrollbar::mouseReleaseEvent(QMouseEvent* _event)
{
m_mouseButtons = _event->buttons();
m_bScrolling = false;
_event->accept();
//QGraphicsView::mouseReleaseEvent(_event);
}
void GraphicsHorizontalScrollbar::mouseMoveEvent(QMouseEvent* _event)
{
if (m_mouseButtons & Qt::LeftButton)
{
const auto pos = _event->pos();
const auto delta = pos - m_mousePressPos;
m_mousePressPos = pos;
if (m_bScrolling)
{
setValue(m_value + delta.x() / m_windowScale);
}
}
}
void GraphicsHorizontalScrollbar::resizeEvent(QResizeEvent* _event)
{
onWindowWidthChange(_event->size().width());
}
void GraphicsHorizontalScrollbar::onWindowWidthChange(qreal _width)
{
const auto oldScale = m_windowScale;
const auto scrollingRange = range();
if (scrollingRange < 1e-3)
{
m_windowScale = 1;
}
else
{
m_windowScale = _width / scrollingRange;
}
scale(m_windowScale / oldScale, 1);
}
//////////////////////////////////////////////////////////////////////////

View File

@ -21,29 +21,31 @@
#include <stdlib.h>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsPolygonItem>
#include <QGraphicsRectItem>
//////////////////////////////////////////////////////////////////////////
class GraphicsHorizontalSlider : public QGraphicsPolygonItem
class GraphicsHorizontalSlider : public QGraphicsRectItem
{
typedef QGraphicsPolygonItem Parent;
typedef QGraphicsRectItem Parent;
typedef GraphicsHorizontalSlider This;
private:
double m_halfwidth;
qreal m_halfwidth;
public:
GraphicsHorizontalSlider();
virtual ~GraphicsHorizontalSlider();
double width() const;
double halfwidth() const;
void paint(QPainter* _painter, const QStyleOptionGraphicsItem* _option, QWidget* _widget = nullptr) override;
void setWidth(double _width);
void setHalfwidth(double _halfwidth);
qreal width() const;
qreal halfwidth() const;
void setWidth(qreal _width);
void setHalfwidth(qreal _halfwidth);
void setColor(QRgb _color);
@ -60,24 +62,44 @@ private:
typedef QGraphicsView Parent;
typedef GraphicsHorizontalScrollbar This;
double m_minimumValue, m_maximumValue, m_value;
GraphicsHorizontalSlider* m_slider;
qreal m_minimumValue;
qreal m_maximumValue;
qreal m_value;
qreal m_windowScale;
QPoint m_mousePressPos;
Qt::MouseButtons m_mouseButtons;
GraphicsHorizontalSlider* m_slider;
bool m_bScrolling;
public:
GraphicsHorizontalScrollbar(QWidget* _parent = nullptr);
virtual ~GraphicsHorizontalScrollbar();
double minimum() const;
double maximum() const;
double value() const;
void mousePressEvent(QMouseEvent* _event) override;
void mouseReleaseEvent(QMouseEvent* _event) override;
void mouseMoveEvent(QMouseEvent* _event) override;
void resizeEvent(QResizeEvent* _event) override;
void setValue(double _value);
void setRange(double _minValue, double _maxValue);
qreal getWindowScale() const;
qreal minimum() const;
qreal maximum() const;
qreal range() const;
qreal value() const;
void setValue(qreal _value);
void setRange(qreal _minValue, qreal _maxValue);
void setSliderWidth(qreal _width);
signals:
void valueChanged(double _value) const;
void rangeChanged();
void valueChanged(qreal _value);
private:
void onWindowWidthChange(qreal _width);
}; // END of class GraphicsHorizontalScrollbar.

View File

@ -31,7 +31,6 @@
#include "main_window.h"
#include "blocks_tree_widget.h"
#include "blocks_graphics_view.h"
//#include "graphics_scrollbar.h"
//////////////////////////////////////////////////////////////////////////
@ -44,7 +43,7 @@ ProfMainWindow::ProfMainWindow() : QMainWindow(), m_treeWidget(nullptr), m_graph
setStatusBar(new QStatusBar());
auto graphicsView = new ProfGraphicsView(false);
auto graphicsView = new ProfGraphicsViewWidget(false);
m_graphicsView = new QDockWidget("Blocks diagram");
m_graphicsView->setMinimumHeight(50);
m_graphicsView->setAllowedAreas(Qt::AllDockWidgetAreas);
@ -56,14 +55,7 @@ ProfMainWindow::ProfMainWindow() : QMainWindow(), m_treeWidget(nullptr), m_graph
m_treeWidget->setAllowedAreas(Qt::AllDockWidgetAreas);
m_treeWidget->setWidget(treeWidget);
//auto bar = new GraphicsHorizontalScrollbar();
//auto dock = new QDockWidget();
//dock->setMinimumHeight(20);
//dock->setAllowedAreas(Qt::AllDockWidgetAreas);
//dock->setWidget(bar);
addDockWidget(Qt::TopDockWidgetArea, m_graphicsView);
//addDockWidget(Qt::TopDockWidgetArea, dock);
addDockWidget(Qt::BottomDockWidgetArea, m_treeWidget);
auto actionOpen = new QAction("Open", nullptr);
@ -120,8 +112,8 @@ void ProfMainWindow::loadFile(const std::string& stdfilename)
{
m_lastFile = stdfilename;
m_currentProf.swap(prof_blocks);
static_cast<ProfTreeWidget*>(m_treeWidget->widget())->setTree(nblocks, m_currentProf);
static_cast<ProfGraphicsView*>(m_graphicsView->widget())->setTree(m_currentProf);
//static_cast<ProfTreeWidget*>(m_treeWidget->widget())->setTree(nblocks, m_currentProf);
static_cast<ProfGraphicsViewWidget*>(m_graphicsView->widget())->view()->setTree(m_currentProf);
}
}
@ -140,8 +132,8 @@ void ProfMainWindow::onReloadFileClicked(bool)
if (nblocks != 0)
{
m_currentProf.swap(prof_blocks);
static_cast<ProfTreeWidget*>(m_treeWidget->widget())->setTree(nblocks, m_currentProf);
static_cast<ProfGraphicsView*>(m_graphicsView->widget())->setTree(m_currentProf);
//static_cast<ProfTreeWidget*>(m_treeWidget->widget())->setTree(nblocks, m_currentProf);
static_cast<ProfGraphicsViewWidget*>(m_graphicsView->widget())->view()->setTree(m_currentProf);
}
}
@ -158,7 +150,7 @@ void ProfMainWindow::onTestViewportClicked(bool)
{
static_cast<ProfTreeWidget*>(m_treeWidget->widget())->clearSilent();
auto view = static_cast<ProfGraphicsView*>(m_graphicsView->widget());
auto view = static_cast<ProfGraphicsViewWidget*>(m_graphicsView->widget())->view();
view->clearSilent();
m_currentProf.clear();