0
0
mirror of https://github.com/yse/easy_profiler.git synced 2025-01-14 16:47:55 +08:00

added main window with both graphics view and tree widget;

* further improvement of graphics view: highly improved scene scaling and added scene drag with left mouse button pressed.
This commit is contained in:
Victor Zarubkin 2016-06-26 20:54:16 +03:00
parent 98035ae705
commit e6c105561a
8 changed files with 423 additions and 126 deletions

View File

@ -19,6 +19,8 @@ add_executable(${PROJECT_NAME}
blocks_graphics_view.cpp blocks_graphics_view.cpp
blocks_tree_widget.h blocks_tree_widget.h
blocks_tree_widget.cpp blocks_tree_widget.cpp
main_window.h
main_window.cpp
) )
target_link_libraries(${PROJECT_NAME} Qt5::Widgets easy_profiler) target_link_libraries(${PROJECT_NAME} Qt5::Widgets easy_profiler)

View File

@ -17,57 +17,71 @@
************************************************************************/ ************************************************************************/
#include <QWheelEvent> #include <QWheelEvent>
#include <QMouseEvent>
#include <QSignalBlocker>
#include <QScrollBar>
#include "blocks_graphics_view.h" #include "blocks_graphics_view.h"
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const ProfViewGlobalSignals GLOBALS; ProfGraphicsPolygonItem::ProfGraphicsPolygonItem(bool _drawBorders, QGraphicsItem* _parent)
: QGraphicsPolygonItem(_parent)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// , m_bDrawBorders(_drawBorders)
ProfGraphicsPolygonItem::ProfGraphicsPolygonItem(QGraphicsItem* _parent) : QGraphicsPolygonItem(_parent)
{ {
if (!_drawBorders)
{
setPen(QPen(Qt::NoPen));
}
} }
ProfGraphicsPolygonItem::~ProfGraphicsPolygonItem() ProfGraphicsPolygonItem::~ProfGraphicsPolygonItem()
{ {
} }
void ProfGraphicsPolygonItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
if (m_bDrawBorders)
{
const auto currentScale = static_cast<ProfGraphicsView*>(scene()->parent())->currentScale();
if (boundingRect().width() * currentScale < 5)
{
auto linePen = pen();
if (linePen.style() != Qt::NoPen)
{
linePen.setStyle(Qt::NoPen);
setPen(linePen);
}
}
else
{
auto linePen = pen();
linePen.setWidthF(1. / currentScale);
setPen(linePen);
}
}
QGraphicsPolygonItem::paint(painter, option, widget);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ProfGraphicsTextItem::ProfGraphicsTextItem(const char* _text, QGraphicsItem* _parent) : QGraphicsSimpleTextItem(_text, _parent), QObject() ProfGraphicsTextItem::ProfGraphicsTextItem(const char* _text, QGraphicsItem* _parent)
: QGraphicsSimpleTextItem(_text, _parent)
{ {
connect(&GLOBALS, &ProfViewGlobalSignals::scaleIncreased, this, &ProfGraphicsTextItem::onScaleIncrease);
connect(&GLOBALS, &ProfViewGlobalSignals::scaleDecreased, this, &ProfGraphicsTextItem::onScaleDecrease);
} }
ProfGraphicsTextItem::~ProfGraphicsTextItem() ProfGraphicsTextItem::~ProfGraphicsTextItem()
{ {
} }
void ProfGraphicsTextItem::onScaleIncrease(qreal _scale) void ProfGraphicsTextItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{ {
setScale(100.0 / _scale); const auto currentScale = static_cast<ProfGraphicsView*>(parentItem()->scene()->parent())->currentScale();
if (boundingRect().width() < parentItem()->boundingRect().width() * currentScale)
if (!isVisible())
{ {
if (boundingRect().width() * 100.0 < parentItem()->boundingRect().width() * _scale) painter->setTransform(QTransform::fromScale(1. / currentScale, 1), true);
{ //setScale(1. / currentScale);
show(); QGraphicsSimpleTextItem::paint(painter, option, widget);
}
}
}
void ProfGraphicsTextItem::onScaleDecrease(qreal _scale)
{
setScale(100.0 / _scale);
if (isVisible())
{
if (boundingRect().width() * 100.0 > parentItem()->boundingRect().width() * _scale)
{
hide();
}
} }
} }
@ -77,15 +91,18 @@ const qreal LEFT = -2500000;
const qreal WIDTH = 5000000; const qreal WIDTH = 5000000;
const int N_ITEMS = 200000; const int N_ITEMS = 200000;
ProfGraphicsScene::ProfGraphicsScene(QGraphicsView* _parent) : QGraphicsScene(_parent), m_start(0) ProfGraphicsScene::ProfGraphicsScene(QGraphicsView* _parent, bool _test) : QGraphicsScene(_parent), m_start(-1)
{ {
setSceneRect(QRectF(LEFT, -200, WIDTH, 220)); if (_test)
test(); {
setSceneRect(QRectF(LEFT, -200, WIDTH, 220));
test();
}
} }
ProfGraphicsScene::ProfGraphicsScene(const thread_blocks_tree_t& _blocksTree, QGraphicsView* _parent) : QGraphicsScene(_parent), m_start(0) ProfGraphicsScene::ProfGraphicsScene(const thread_blocks_tree_t& _blocksTree, QGraphicsView* _parent) : QGraphicsScene(_parent), m_start(-1)
{ {
setTree(_blocksTree); setTreeInternal(_blocksTree);
} }
ProfGraphicsScene::~ProfGraphicsScene() ProfGraphicsScene::~ProfGraphicsScene()
@ -97,7 +114,7 @@ void ProfGraphicsScene::test()
QPolygonF poly(QRectF(-5, -180, 10, 180)); QPolygonF poly(QRectF(-5, -180, 10, 180));
for (int i = 0; i < N_ITEMS; ++i) for (int i = 0; i < N_ITEMS; ++i)
{ {
ProfGraphicsPolygonItem* item = new ProfGraphicsPolygonItem(); ProfGraphicsPolygonItem* item = new ProfGraphicsPolygonItem(true);
int h = 50 + rand() % 131; int h = 50 + rand() % 131;
item->setPolygon(QRectF(-5, -h, 10, h)); item->setPolygon(QRectF(-5, -h, 10, h));
item->setPos(LEFT + i * 10, 0); item->setPos(LEFT + i * 10, 0);
@ -106,13 +123,28 @@ void ProfGraphicsScene::test()
} }
} }
void ProfGraphicsScene::clearSilent()
{
const QSignalBlocker b(this); // block all scene signals (otherwise clear() would be extremely slow!)
clear(); // clear would be VERY SLOW if signals would not be blocked!
}
void ProfGraphicsScene::setTree(const thread_blocks_tree_t& _blocksTree) void ProfGraphicsScene::setTree(const thread_blocks_tree_t& _blocksTree)
{ {
// delete all items created for previous session
clearSilent();
// fill scene with new items
m_start = -1; m_start = -1;
setTreeInternal(_blocksTree);
}
void ProfGraphicsScene::setTreeInternal(const thread_blocks_tree_t& _blocksTree)
{
// calculate scene size
profiler::timestamp_t finish = 0; profiler::timestamp_t finish = 0;
for (const auto& threadTree : _blocksTree) for (const auto& threadTree : _blocksTree)
{ {
++items_couinter;
const auto timestart = threadTree.second.children.front().node->block()->getBegin(); const auto timestart = threadTree.second.children.front().node->block()->getBegin();
const auto timefinish = threadTree.second.children.back().node->block()->getEnd(); const auto timefinish = threadTree.second.children.back().node->block()->getEnd();
if (m_start > timestart) m_start = timestart; if (m_start > timestart) m_start = timestart;
@ -122,58 +154,56 @@ void ProfGraphicsScene::setTree(const thread_blocks_tree_t& _blocksTree)
const qreal endX = time2position(finish + 1000000); const qreal endX = time2position(finish + 1000000);
setSceneRect(QRectF(0, 0, endX, 110 * _blocksTree.size())); setSceneRect(QRectF(0, 0, endX, 110 * _blocksTree.size()));
// fill scene with items
qreal y = 0; qreal y = 0;
for (const auto& threadTree : _blocksTree) for (const auto& threadTree : _blocksTree)
{ {
setTree(threadTree.second.children, y); setTreeInternal(threadTree.second.children, y);
y += 110; y += 110; // each thread is shifted to 110 points down
} }
} }
void ProfGraphicsScene::setTree(const BlocksTree::children_t& _children, qreal _y, int _level) void ProfGraphicsScene::setTreeInternal(const BlocksTree::children_t& _children, qreal _y, int _level)
{ {
for (const auto& child : _children) for (const auto& child : _children)
{ {
++items_couinter;
ProfGraphicsPolygonItem* item = new ProfGraphicsPolygonItem();
const qreal xbegin = time2position(child.node->block()->getBegin()); const qreal xbegin = time2position(child.node->block()->getBegin());
const qreal height = 100 - _level * 5; const qreal height = 100 - _level * 5;
qreal duration = time2position(child.node->block()->getEnd()) - xbegin; qreal duration = time2position(child.node->block()->getEnd()) - xbegin;
if (duration < 1)
duration = 1;
const bool drawBorders = duration > 1;
if (!drawBorders)
{
duration = 1;
}
ProfGraphicsPolygonItem* item = new ProfGraphicsPolygonItem(drawBorders);
item->setPolygon(QRectF(0, _level * 5, duration, height)); item->setPolygon(QRectF(0, _level * 5, duration, height));
item->setPos(xbegin, _y); item->setPos(xbegin, _y);
item->setZValue(_level); item->setZValue(_level);
const auto color = child.node->block()->getColor(); 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->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); addItem(item);
ProfGraphicsTextItem* text = new ProfGraphicsTextItem(child.node->getBlockName(), item); ProfGraphicsTextItem* text = new ProfGraphicsTextItem(child.node->getBlockName(), item);
QRectF textRect = text->boundingRect();
text->setPos(0, _level * 5); text->setPos(0, _level * 5);
QRectF itemRect = item->boundingRect(); setTreeInternal(child.children, _y, _level + 1);
if (textRect.width() > itemRect.width())
text->hide();
setTree(child.children, _y, _level + 1);
} }
} }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ProfGraphicsView::ProfGraphicsView() : QGraphicsView(), m_scale(100), m_scaleCoeff(1.25) ProfGraphicsView::ProfGraphicsView(bool _test) : QGraphicsView(), m_scale(1), m_scaleCoeff(1.25), m_mouseButtons(Qt::NoButton)
{ {
initMode(); initMode();
setScene(new ProfGraphicsScene(this)); setScene(new ProfGraphicsScene(this, _test));
centerOn(0, 0); centerOn(0, 0);
} }
ProfGraphicsView::ProfGraphicsView(const thread_blocks_tree_t& _blocksTree) : QGraphicsView(), m_scale(100), m_scaleCoeff(1.25) ProfGraphicsView::ProfGraphicsView(const thread_blocks_tree_t& _blocksTree) : QGraphicsView(), m_scale(1), m_scaleCoeff(1.25), m_mouseButtons(Qt::NoButton)
{ {
initMode(); initMode();
setScene(new ProfGraphicsScene(_blocksTree, this)); setScene(new ProfGraphicsScene(_blocksTree, this));
@ -184,29 +214,81 @@ ProfGraphicsView::~ProfGraphicsView()
{ {
} }
void ProfGraphicsView::wheelEvent(QWheelEvent* _event)
{
qreal scaleCoeff;
if (_event->delta() > 0)
{
scaleCoeff = m_scaleCoeff;
}
else
{
scaleCoeff = 1. / m_scaleCoeff;
}
scale(scaleCoeff, 1);// scaleCoeff);
m_scale *= scaleCoeff;
_event->accept();
//QGraphicsView::wheelEvent(_event);
}
void ProfGraphicsView::mousePressEvent(QMouseEvent* _event)
{
m_mouseButtons = _event->buttons();
if (m_mouseButtons & Qt::LeftButton)
{
m_mousePressPos = _event->globalPos();
}
_event->accept();
//QGraphicsView::mousePressEvent(_event);
}
void ProfGraphicsView::mouseReleaseEvent(QMouseEvent* _event)
{
m_mouseButtons = _event->buttons();
_event->accept();
//QGraphicsView::mouseReleaseEvent(_event);
}
void ProfGraphicsView::mouseMoveEvent(QMouseEvent* _event)
{
if (m_mouseButtons & Qt::LeftButton)
{
const auto pos = _event->globalPos();
const auto delta = pos - m_mousePressPos;
m_mousePressPos = pos;
verticalScrollBar()->setValue(verticalScrollBar()->value() - delta.y()); horizontalScrollBar()->setValue(horizontalScrollBar()->value() - delta.x());
}
//_event->accept();
QGraphicsView::mouseMoveEvent(_event); // This is to be able to use setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
}
void ProfGraphicsView::initMode() void ProfGraphicsView::initMode()
{ {
setCacheMode(QGraphicsView::CacheBackground); setCacheMode(QGraphicsView::CacheBackground);
setTransformationAnchor(QGraphicsView::AnchorUnderMouse); setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
//setTransformationAnchor(QGraphicsView::AnchorViewCenter);
setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
} }
void ProfGraphicsView::wheelEvent(QWheelEvent* _event) void ProfGraphicsView::setTree(const thread_blocks_tree_t& _blocksTree)
{ {
//ProfGraphicsScene* myscene = (ProfGraphicsScene*)scene(); // block all signals for faster scene recreation
if (_event->delta() > 0) const QSignalBlocker b(this);
{
scale(m_scaleCoeff, m_scaleCoeff); // scale back to initial 100% scale
m_scale *= m_scaleCoeff; scale(1. / m_scale, 1);
emit GLOBALS.scaleIncreased(m_scale); m_scale = 1;
}
else // set new blocks tree
{ static_cast<ProfGraphicsScene*>(scene())->setTree(_blocksTree);
const qreal scaleCoeff = 1.0 / m_scaleCoeff;
scale(scaleCoeff, scaleCoeff); // center view on the beginning of the scene
m_scale *= scaleCoeff; centerOn(0, 0);
emit GLOBALS.scaleDecreased(m_scale);
}
} }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -23,54 +23,35 @@
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QGraphicsPolygonItem> #include <QGraphicsPolygonItem>
#include <QGraphicsSimpleTextItem> #include <QGraphicsSimpleTextItem>
#include <QPoint>
#include <stdlib.h> #include <stdlib.h>
#include "profiler/reader.h" #include "profiler/reader.h"
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class ProfViewGlobalSignals : public QObject
{
Q_OBJECT
public:
ProfViewGlobalSignals() : QObject() {}
virtual ~ProfViewGlobalSignals() {}
signals:
void scaleIncreased(qreal _scale) const;
void scaleDecreased(qreal _scale) const;
}; // END of class ProfViewGlobalSignals.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class ProfGraphicsPolygonItem : public QGraphicsPolygonItem class ProfGraphicsPolygonItem : public QGraphicsPolygonItem
{ {
bool m_bDrawBorders;
public: public:
ProfGraphicsPolygonItem(QGraphicsItem* _parent = nullptr); ProfGraphicsPolygonItem(bool _drawBorders, QGraphicsItem* _parent = nullptr);
virtual ~ProfGraphicsPolygonItem(); virtual ~ProfGraphicsPolygonItem();
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = Q_NULLPTR) override;
}; // END of class ProfGraphicsPolygonItem. }; // END of class ProfGraphicsPolygonItem.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class ProfGraphicsTextItem : public QObject, public QGraphicsSimpleTextItem class ProfGraphicsTextItem : public QGraphicsSimpleTextItem
{ {
Q_OBJECT
public: public:
ProfGraphicsTextItem(const char* _text, QGraphicsItem* _parent = nullptr); ProfGraphicsTextItem(const char* _text, QGraphicsItem* _parent = nullptr);
virtual ~ProfGraphicsTextItem(); virtual ~ProfGraphicsTextItem();
private slots: void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = Q_NULLPTR) override;
void onScaleIncrease(qreal _scale);
void onScaleDecrease(qreal _scale);
}; // END of class ProfGraphicsTextItem. }; // END of class ProfGraphicsTextItem.
@ -78,6 +59,8 @@ private slots:
class ProfGraphicsScene : public QGraphicsScene class ProfGraphicsScene : public QGraphicsScene
{ {
friend class ProfGraphicsView;
Q_OBJECT Q_OBJECT
private: private:
@ -86,25 +69,26 @@ private:
public: public:
int items_couinter = 0; ProfGraphicsScene(QGraphicsView* _parent, bool _test = false);
ProfGraphicsScene(QGraphicsView* _parent);
ProfGraphicsScene(const thread_blocks_tree_t& _blocksTree, QGraphicsView* _parent); ProfGraphicsScene(const thread_blocks_tree_t& _blocksTree, QGraphicsView* _parent);
virtual ~ProfGraphicsScene(); virtual ~ProfGraphicsScene();
private:
void test(); void test();
void clearSilent();
void setTree(const thread_blocks_tree_t& _blocksTree); void setTree(const thread_blocks_tree_t& _blocksTree);
private: void setTreeInternal(const thread_blocks_tree_t& _blocksTree);
void setTreeInternal(const BlocksTree::children_t& _children, qreal _y, int _level = 0);
inline qreal time2position(const profiler::timestamp_t& _time) const inline qreal time2position(const profiler::timestamp_t& _time) const
{ {
return qreal(_time - m_start) * 1e-6; return qreal(_time - m_start) * 1e-6;
} }
void setTree(const BlocksTree::children_t& _children, qreal _y, int _level = 0);
}; // END of class ProfGraphicsScene. }; // END of class ProfGraphicsScene.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -115,18 +99,30 @@ class ProfGraphicsView : public QGraphicsView
private: private:
qreal m_scale; qreal m_scale;
qreal m_scaleCoeff; qreal m_scaleCoeff;
QPoint m_mousePressPos;
Qt::MouseButtons m_mouseButtons;
public: public:
ProfGraphicsView(); ProfGraphicsView(bool _test = false);
ProfGraphicsView(const thread_blocks_tree_t& _blocksTree); ProfGraphicsView(const thread_blocks_tree_t& _blocksTree);
virtual ~ProfGraphicsView(); virtual ~ProfGraphicsView();
void initMode(); void wheelEvent(QWheelEvent* _event) override;
void mousePressEvent(QMouseEvent* _event) override;
void mouseReleaseEvent(QMouseEvent* _event) override;
void mouseMoveEvent(QMouseEvent* _event) override;
void wheelEvent(QWheelEvent* _event); inline qreal currentScale() const
{
return m_scale;
}
void setTree(const thread_blocks_tree_t& _blocksTree);
void initMode();
}; // END of class ProfGraphicsView. }; // END of class ProfGraphicsView.

View File

@ -26,6 +26,10 @@ ProfTreeWidgetItem::ProfTreeWidgetItem(const BlocksTree* _block, QTreeWidgetItem
{ {
} }
ProfTreeWidgetItem::~ProfTreeWidgetItem()
{
}
const BlocksTree* ProfTreeWidgetItem::block() const const BlocksTree* ProfTreeWidgetItem::block() const
{ {
return m_block; return m_block;
@ -64,13 +68,13 @@ void ProfTreeWidgetItem::setTimeMs(int _column, const ::profiler::timestamp_t& _
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
ProfTreeWidget::ProfTreeWidget(const thread_blocks_tree_t& _blocksTree, QWidget* _parent) : QTreeWidget(_parent), m_beginTime(-1) ProfTreeWidget::ProfTreeWidget(QWidget* _parent) : QTreeWidget(_parent), m_beginTime(-1)
{ {
setAlternatingRowColors(true); setAlternatingRowColors(true);
setItemsExpandable(true); setItemsExpandable(true);
setAnimated(true); setAnimated(true);
setSortingEnabled(false); setSortingEnabled(false);
setColumnCount(10); setColumnCount(8);
auto header = new QTreeWidgetItem(); auto header = new QTreeWidgetItem();
header->setText(0, "Name"); header->setText(0, "Name");
@ -82,7 +86,10 @@ ProfTreeWidget::ProfTreeWidget(const thread_blocks_tree_t& _blocksTree, QWidget*
header->setText(6, "Average dur."); header->setText(6, "Average dur.");
header->setText(7, "N Calls"); header->setText(7, "N Calls");
setHeaderItem(header); setHeaderItem(header);
}
ProfTreeWidget::ProfTreeWidget(const thread_blocks_tree_t& _blocksTree, QWidget* _parent) : ProfTreeWidget(_parent)
{
setTreeInternal(_blocksTree); setTreeInternal(_blocksTree);
setSortingEnabled(true); setSortingEnabled(true);
@ -92,6 +99,10 @@ ProfTreeWidget::ProfTreeWidget(const thread_blocks_tree_t& _blocksTree, QWidget*
connect(this, &QTreeWidget::itemExpanded, this, &ProfTreeWidget::onItemExpand); connect(this, &QTreeWidget::itemExpanded, this, &ProfTreeWidget::onItemExpand);
} }
ProfTreeWidget::~ProfTreeWidget()
{
}
void ProfTreeWidget::setTree(const thread_blocks_tree_t& _blocksTree) void ProfTreeWidget::setTree(const thread_blocks_tree_t& _blocksTree)
{ {
m_itemblocks.clear(); m_itemblocks.clear();
@ -100,6 +111,8 @@ void ProfTreeWidget::setTree(const thread_blocks_tree_t& _blocksTree)
disconnect(this, &QTreeWidget::itemExpanded, this, &ProfTreeWidget::onItemExpand); disconnect(this, &QTreeWidget::itemExpanded, this, &ProfTreeWidget::onItemExpand);
setSortingEnabled(false); setSortingEnabled(false);
m_beginTime = -1;
setTreeInternal(_blocksTree); setTreeInternal(_blocksTree);
setSortingEnabled(true); setSortingEnabled(true);

View File

@ -80,28 +80,23 @@ class ProfTreeWidgetItem : public QTreeWidgetItem
public: public:
ProfTreeWidgetItem(const BlocksTree* _block, QTreeWidgetItem* _parent = nullptr); ProfTreeWidgetItem(const BlocksTree* _block, QTreeWidgetItem* _parent = nullptr);
virtual ~ProfTreeWidgetItem();
const BlocksTree* block() const; const BlocksTree* block() const;
inline bool operator < (const QTreeWidgetItem& _other) const inline bool operator < (const QTreeWidgetItem& _other) const
{ {
const auto col = treeWidget()->sortColumn(); const auto col = treeWidget()->sortColumn();
//switch (col) if (col > 0 && col < 7)
//{ {
// case 1:
// case 2:
// case 3:
if (col > 0 && col < 7)
{
#ifndef _DEBUG #ifndef _DEBUG
return data(col, Qt::UserRole).toULongLong() < _other.data(col, Qt::UserRole).toULongLong(); return data(col, Qt::UserRole).toULongLong() < _other.data(col, Qt::UserRole).toULongLong();
#else #else
const auto selfdata = data(col, Qt::UserRole).toULongLong(); const auto selfdata = data(col, Qt::UserRole).toULongLong();
const auto otherdata = _other.data(col, Qt::UserRole).toULongLong(); const auto otherdata = _other.data(col, Qt::UserRole).toULongLong();
return selfdata < otherdata; return selfdata < otherdata;
#endif #endif
} }
//}
return QTreeWidgetItem::operator < (_other); return QTreeWidgetItem::operator < (_other);
} }
@ -129,6 +124,10 @@ public:
connect(this, &QAction::triggered, this, &ProfItemAction::onToggle); connect(this, &QAction::triggered, this, &ProfItemAction::onToggle);
} }
virtual ~ProfItemAction()
{
}
private: private:
void onToggle(bool) void onToggle(bool)
@ -156,7 +155,9 @@ protected:
public: public:
ProfTreeWidget(QWidget* _parent = nullptr);
ProfTreeWidget(const thread_blocks_tree_t& _blocksTree, QWidget* _parent = nullptr); ProfTreeWidget(const thread_blocks_tree_t& _blocksTree, QWidget* _parent = nullptr);
virtual ~ProfTreeWidget();
void setTree(const thread_blocks_tree_t& _blocksTree); void setTree(const thread_blocks_tree_t& _blocksTree);
@ -166,7 +167,7 @@ protected:
void setTreeInternal(const BlocksTree::children_t& _children, ProfTreeWidgetItem* _parent); void setTreeInternal(const BlocksTree::children_t& _children, ProfTreeWidgetItem* _parent);
void contextMenuEvent(QContextMenuEvent* _event); void contextMenuEvent(QContextMenuEvent* _event) override;
private slots: private slots:

View File

@ -4,13 +4,13 @@
#include "treemodel.h" #include "treemodel.h"
#include "blocks_graphics_view.h" #include "blocks_graphics_view.h"
#include "blocks_tree_widget.h" #include "blocks_tree_widget.h"
#include "main_window.h"
#include "profiler/reader.h" #include "profiler/reader.h"
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
QApplication app(argc, argv); QApplication app(argc, argv);
//QFileSystemModel *model = new QFileSystemModel; //QFileSystemModel *model = new QFileSystemModel;
//model->setRootPath(QDir::currentPath()); //model->setRootPath(QDir::currentPath());
// const char* filename = 0; // const char* filename = 0;
@ -31,7 +31,7 @@ int main(int argc, char **argv)
// //
// tree->show(); // tree->show();
int mode = 1; int mode = 2;
switch (mode) switch (mode)
{ {
@ -46,7 +46,7 @@ int main(int argc, char **argv)
srand(*rseed); srand(*rseed);
delete rseed; delete rseed;
ProfGraphicsView gview; ProfGraphicsView gview(true);
gview.show(); gview.show();
return app.exec(); return app.exec();
@ -71,6 +71,13 @@ int main(int argc, char **argv)
return app.exec(); return app.exec();
} }
case 2:
{
ProfMainWindow window;
window.show();
return app.exec();
}
} }
return -1; return -1;

View File

@ -0,0 +1,140 @@
/************************************************************************
* file name : main_window.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 MainWindow for easy_profiler GUI.
* ----------------- :
* change log : * 2016/06/26 Victor Zarubkin: initial commit.
* : *
* ----------------- :
* license : TODO: add license text
************************************************************************/
#include <QStatusBar>
#include <QDockWidget>
#include <QFileDialog>
#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QApplication>
#include <thread>
#include "main_window.h"
#include "blocks_tree_widget.h"
#include "blocks_graphics_view.h"
//////////////////////////////////////////////////////////////////////////
void graphicsViewDeleterThread(ProfGraphicsView* _widget)
{
delete _widget;
}
void treeDeleterThread(ProfTreeWidget* _widget)
{
delete _widget;
}
//////////////////////////////////////////////////////////////////////////
ProfMainWindow::ProfMainWindow() : QMainWindow(), m_treeWidget(nullptr), m_graphicsView(nullptr)
{
setObjectName("ProfilerGUI_MainWindow");
setWindowTitle("easy_profiler reader");
setDockNestingEnabled(true);
resize(800, 600);
setStatusBar(new QStatusBar());
auto graphicsView = new ProfGraphicsView();
m_graphicsView = new QDockWidget("Blocks diagram");
m_graphicsView->setMinimumHeight(50);
m_graphicsView->setAllowedAreas(Qt::AllDockWidgetAreas);
m_graphicsView->setWidget(graphicsView);
auto treeWidget = new ProfTreeWidget();
m_treeWidget = new QDockWidget("Blocks hierarchy");
m_treeWidget->setMinimumHeight(50);
m_treeWidget->setAllowedAreas(Qt::AllDockWidgetAreas);
m_treeWidget->setWidget(treeWidget);
addDockWidget(Qt::TopDockWidgetArea, m_graphicsView);
addDockWidget(Qt::BottomDockWidgetArea, m_treeWidget);
auto actionOpen = new QAction("Open", nullptr);
connect(actionOpen, &QAction::triggered, this, &ProfMainWindow::onOpenFileClicked);
auto actionReload = new QAction("Reload", nullptr);
connect(actionReload, &QAction::triggered, this, &ProfMainWindow::onReloadFileClicked);
auto actionExit = new QAction("Exit", nullptr);
connect(actionExit, &QAction::triggered, this, &ProfMainWindow::onExitClicked);
auto menuFile = new QMenu("File");
menuFile->addAction(actionOpen);
menuFile->addAction(actionReload);
menuFile->addSeparator();
menuFile->addAction(actionExit);
menuBar()->addMenu(menuFile);
}
ProfMainWindow::~ProfMainWindow()
{
}
//////////////////////////////////////////////////////////////////////////
void ProfMainWindow::onOpenFileClicked(bool)
{
auto filename = QFileDialog::getOpenFileName(this, "Open profiler log", m_lastFile.c_str(), "Profiler Log File (*.prof);;All Files (*.*)");
auto stdfilename = filename.toStdString();
thread_blocks_tree_t prof_blocks;
auto nblocks = fillTreesFromFile(stdfilename.c_str(), prof_blocks, true);
if (nblocks > 0)
{
m_lastFile.swap(stdfilename);
m_currentProf.swap(prof_blocks);
static_cast<ProfTreeWidget*>(m_treeWidget->widget())->setTree(m_currentProf);
static_cast<ProfGraphicsView*>(m_graphicsView->widget())->setTree(m_currentProf);
}
}
//////////////////////////////////////////////////////////////////////////
void ProfMainWindow::onReloadFileClicked(bool)
{
if (m_lastFile.empty())
{
return;
}
thread_blocks_tree_t prof_blocks;
auto nblocks = fillTreesFromFile(m_lastFile.c_str(), prof_blocks, true);
if (nblocks > 0)
{
m_currentProf.swap(prof_blocks);
static_cast<ProfTreeWidget*>(m_treeWidget->widget())->setTree(m_currentProf);
static_cast<ProfGraphicsView*>(m_graphicsView->widget())->setTree(m_currentProf);
}
}
//////////////////////////////////////////////////////////////////////////
void ProfMainWindow::onExitClicked(bool)
{
close();
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

View File

@ -0,0 +1,56 @@
/************************************************************************
* file name : main_window.h
* ----------------- :
* creation time : 2016/06/26
* copyright : (c) 2016 Victor Zarubkin
* author : Victor Zarubkin
* email : v.s.zarubkin@gmail.com
* ----------------- :
* description : The file contains declaration of MainWindow for easy_profiler GUI.
* ----------------- :
* change log : * 2016/06/26 Victor Zarubkin: initial commit.
* : *
* ----------------- :
* license : TODO: add license text
************************************************************************/
#ifndef EASY_PROFILER_GUI__MAIN_WINDOW__H
#define EASY_PROFILER_GUI__MAIN_WINDOW__H
#include <QMainWindow>
#include <string>
#include "profiler/reader.h"
//////////////////////////////////////////////////////////////////////////
class ProfTreeWidget;
class ProfGraphicsView;
class QDockWidget;
class ProfMainWindow : public QMainWindow
{
Q_OBJECT
protected:
::std::string m_lastFile;
thread_blocks_tree_t m_currentProf;
QDockWidget* m_treeWidget;
QDockWidget* m_graphicsView;
public:
ProfMainWindow();
virtual ~ProfMainWindow();
protected slots:
void onOpenFileClicked(bool);
void onReloadFileClicked(bool);
void onExitClicked(bool);
}; // END of class ProfMainWindow.
//////////////////////////////////////////////////////////////////////////
#endif // EASY_PROFILER_GUI__MAIN_WINDOW__H