0
0
mirror of https://github.com/yse/easy_profiler.git synced 2024-12-28 01:04:41 +08:00
easy_profiler/profiler_gui/blocks_graphics_view.cpp

213 lines
6.8 KiB
C++
Raw Normal View History

2016-06-26 18:56:40 +03:00
/************************************************************************
* file name : blocks_graphics_view.cpp
* ----------------- :
* creation time : 2016/06/26
* copyright : (c) 2016 Victor Zarubkin
* author : Victor Zarubkin
* email : v.s.zarubkin@gmail.com
* ----------------- :
* description : The file contains implementation 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
2016-06-26 19:06:53 +03:00
* : and renamed classes from My* to Prof*.
2016-06-26 18:56:40 +03:00
* : *
* ----------------- :
* license : TODO: add license text
************************************************************************/
2016-06-26 18:46:51 +03:00
#include <QWheelEvent>
#include "blocks_graphics_view.h"
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2016-06-26 19:06:53 +03:00
const ProfViewGlobalSignals GLOBALS;
2016-06-26 18:46:51 +03:00
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2016-06-26 19:06:53 +03:00
ProfGraphicsPolygonItem::ProfGraphicsPolygonItem(QGraphicsItem* _parent) : QGraphicsPolygonItem(_parent)
2016-06-26 18:46:51 +03:00
{
}
2016-06-26 19:06:53 +03:00
ProfGraphicsPolygonItem::~ProfGraphicsPolygonItem()
2016-06-26 18:46:51 +03:00
{
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2016-06-26 19:06:53 +03:00
ProfGraphicsTextItem::ProfGraphicsTextItem(const char* _text, QGraphicsItem* _parent) : QGraphicsSimpleTextItem(_text, _parent), QObject()
2016-06-26 18:46:51 +03:00
{
2016-06-26 19:06:53 +03:00
connect(&GLOBALS, &ProfViewGlobalSignals::scaleIncreased, this, &ProfGraphicsTextItem::onScaleIncrease);
connect(&GLOBALS, &ProfViewGlobalSignals::scaleDecreased, this, &ProfGraphicsTextItem::onScaleDecrease);
2016-06-26 18:46:51 +03:00
}
2016-06-26 19:06:53 +03:00
ProfGraphicsTextItem::~ProfGraphicsTextItem()
2016-06-26 18:46:51 +03:00
{
}
2016-06-26 19:06:53 +03:00
void ProfGraphicsTextItem::onScaleIncrease(qreal _scale)
2016-06-26 18:46:51 +03:00
{
setScale(100.0 / _scale);
if (!isVisible())
{
if (boundingRect().width() * 100.0 < parentItem()->boundingRect().width() * _scale)
{
show();
}
}
}
2016-06-26 19:06:53 +03:00
void ProfGraphicsTextItem::onScaleDecrease(qreal _scale)
2016-06-26 18:46:51 +03:00
{
setScale(100.0 / _scale);
if (isVisible())
{
if (boundingRect().width() * 100.0 > parentItem()->boundingRect().width() * _scale)
{
hide();
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const qreal LEFT = -2500000;
const qreal WIDTH = 5000000;
const int N_ITEMS = 200000;
2016-06-26 19:06:53 +03:00
ProfGraphicsScene::ProfGraphicsScene(QGraphicsView* _parent) : QGraphicsScene(_parent), m_start(0)
2016-06-26 18:46:51 +03:00
{
setSceneRect(QRectF(LEFT, -200, WIDTH, 220));
test();
}
2016-06-26 19:06:53 +03:00
ProfGraphicsScene::ProfGraphicsScene(const thread_blocks_tree_t& _blocksTree, QGraphicsView* _parent) : QGraphicsScene(_parent), m_start(0)
2016-06-26 18:46:51 +03:00
{
setTree(_blocksTree);
}
2016-06-26 19:06:53 +03:00
ProfGraphicsScene::~ProfGraphicsScene()
2016-06-26 18:46:51 +03:00
{
}
2016-06-26 19:06:53 +03:00
void ProfGraphicsScene::test()
2016-06-26 18:46:51 +03:00
{
QPolygonF poly(QRectF(-5, -180, 10, 180));
for (int i = 0; i < N_ITEMS; ++i)
{
2016-06-26 19:06:53 +03:00
ProfGraphicsPolygonItem* item = new ProfGraphicsPolygonItem();
2016-06-26 18:46:51 +03:00
int h = 50 + rand() % 131;
item->setPolygon(QRectF(-5, -h, 10, h));
item->setPos(LEFT + i * 10, 0);
item->setBrush(QBrush(QColor(30 + rand() % 225, 30 + rand() % 225, 30 + rand() % 225)));
addItem(item);
}
}
2016-06-26 19:06:53 +03:00
void ProfGraphicsScene::setTree(const thread_blocks_tree_t& _blocksTree)
2016-06-26 18:46:51 +03:00
{
m_start = -1;
profiler::timestamp_t finish = 0;
for (const auto& threadTree : _blocksTree)
{
++items_couinter;
const auto timestart = threadTree.second.children.front().node->block()->getBegin();
const auto timefinish = threadTree.second.children.back().node->block()->getEnd();
if (m_start > timestart) m_start = timestart;
if (finish < timefinish) finish = timefinish;
}
const qreal endX = time2position(finish + 1000000);
setSceneRect(QRectF(0, 0, endX, 110 * _blocksTree.size()));
qreal y = 0;
for (const auto& threadTree : _blocksTree)
{
setTree(threadTree.second.children, y);
y += 110;
}
}
2016-06-26 19:06:53 +03:00
void ProfGraphicsScene::setTree(const BlocksTree::children_t& _children, qreal _y, int _level)
2016-06-26 18:46:51 +03:00
{
for (const auto& child : _children)
{
++items_couinter;
2016-06-26 19:06:53 +03:00
ProfGraphicsPolygonItem* item = new ProfGraphicsPolygonItem();
2016-06-26 18:46:51 +03:00
const qreal xbegin = time2position(child.node->block()->getBegin());
const qreal height = 100 - _level * 5;
qreal duration = time2position(child.node->block()->getEnd()) - xbegin;
if (duration < 1)
duration = 1;
item->setPolygon(QRectF(0, _level * 5, duration, height));
item->setPos(xbegin, _y);
item->setZValue(_level);
const auto color = child.node->block()->getColor();
item->setBrush(QBrush(QColor(profiler::colors::get_red(color), profiler::colors::get_green(color), profiler::colors::get_blue(color))));
item->setPen(QPen(Qt::NoPen)); // disable borders painting
addItem(item);
2016-06-26 19:06:53 +03:00
ProfGraphicsTextItem* text = new ProfGraphicsTextItem(child.node->getBlockName(), item);
2016-06-26 18:46:51 +03:00
QRectF textRect = text->boundingRect();
text->setPos(0, _level * 5);
QRectF itemRect = item->boundingRect();
if (textRect.width() > itemRect.width())
text->hide();
setTree(child.children, _y, _level + 1);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2016-06-26 19:06:53 +03:00
ProfGraphicsView::ProfGraphicsView() : QGraphicsView(), m_scale(100), m_scaleCoeff(1.25)
2016-06-26 18:46:51 +03:00
{
initMode();
2016-06-26 19:06:53 +03:00
setScene(new ProfGraphicsScene(this));
2016-06-26 18:46:51 +03:00
centerOn(0, 0);
}
2016-06-26 19:06:53 +03:00
ProfGraphicsView::ProfGraphicsView(const thread_blocks_tree_t& _blocksTree) : QGraphicsView(), m_scale(100), m_scaleCoeff(1.25)
2016-06-26 18:46:51 +03:00
{
initMode();
2016-06-26 19:06:53 +03:00
setScene(new ProfGraphicsScene(_blocksTree, this));
2016-06-26 18:46:51 +03:00
centerOn(0, 0);
}
2016-06-26 19:06:53 +03:00
ProfGraphicsView::~ProfGraphicsView()
2016-06-26 18:46:51 +03:00
{
}
2016-06-26 19:06:53 +03:00
void ProfGraphicsView::initMode()
2016-06-26 18:46:51 +03:00
{
setCacheMode(QGraphicsView::CacheBackground);
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
}
2016-06-26 19:06:53 +03:00
void ProfGraphicsView::wheelEvent(QWheelEvent* _event)
2016-06-26 18:46:51 +03:00
{
2016-06-26 19:06:53 +03:00
//ProfGraphicsScene* myscene = (ProfGraphicsScene*)scene();
2016-06-26 18:46:51 +03:00
if (_event->delta() > 0)
{
scale(m_scaleCoeff, m_scaleCoeff);
m_scale *= m_scaleCoeff;
emit GLOBALS.scaleIncreased(m_scale);
}
else
{
const qreal scaleCoeff = 1.0 / m_scaleCoeff;
scale(scaleCoeff, scaleCoeff);
m_scale *= scaleCoeff;
emit GLOBALS.scaleDecreased(m_scale);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////