2016-06-26 18:56:40 +03:00
|
|
|
/************************************************************************
|
|
|
|
* file name : blocks_tree_widget.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 TreeWidget and it's auxiliary classes
|
|
|
|
* : for displyaing easy_profiler blocks tree.
|
|
|
|
* ----------------- :
|
|
|
|
* change log : * 2016/06/26 Victor Zarubkin: moved sources from tree_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 <QMenu>
|
|
|
|
#include <QContextMenuEvent>
|
|
|
|
#include "blocks_tree_widget.h"
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
ProfTreeWidgetItem::ProfTreeWidgetItem(const BlocksTree* _block, QTreeWidgetItem* _parent) : QTreeWidgetItem(_parent), m_block(_block)
|
2016-06-26 18:46:51 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-26 20:54:16 +03:00
|
|
|
ProfTreeWidgetItem::~ProfTreeWidgetItem()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
const BlocksTree* ProfTreeWidgetItem::block() const
|
2016-06-26 18:46:51 +03:00
|
|
|
{
|
|
|
|
return m_block;
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
void ProfTreeWidgetItem::setTimeSmart(int _column, const ::profiler::timestamp_t& _time)
|
2016-06-26 18:46:51 +03:00
|
|
|
{
|
2016-06-26 21:14:15 +03:00
|
|
|
setData(_column, Qt::UserRole, (quint64)_time);
|
2016-06-26 18:46:51 +03:00
|
|
|
|
|
|
|
setToolTip(_column, QString("%1 ns").arg(_time));
|
|
|
|
|
|
|
|
if (_time < 1e3)
|
|
|
|
{
|
|
|
|
setText(_column, QString("%1 ns").arg(_time));
|
|
|
|
}
|
|
|
|
else if (_time < 1e6)
|
|
|
|
{
|
|
|
|
setText(_column, QString("%1 us").arg(double(_time) * 1e-3, 0, 'f', 3));
|
|
|
|
}
|
|
|
|
else if (_time < 1e9)
|
|
|
|
{
|
|
|
|
setText(_column, QString("%1 ms").arg(double(_time) * 1e-6, 0, 'f', 3));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setText(_column, QString("%1 s").arg(double(_time) * 1e-9, 0, 'f', 3));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
void ProfTreeWidgetItem::setTimeMs(int _column, const ::profiler::timestamp_t& _time)
|
2016-06-26 18:46:51 +03:00
|
|
|
{
|
2016-06-26 21:14:15 +03:00
|
|
|
setData(_column, Qt::UserRole, (quint64)_time);
|
2016-06-26 18:46:51 +03:00
|
|
|
setToolTip(_column, QString("%1 ns").arg(_time));
|
|
|
|
setText(_column, QString::number(double(_time) * 1e-6, 'g', 9));
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-26 20:54:16 +03:00
|
|
|
ProfTreeWidget::ProfTreeWidget(QWidget* _parent) : QTreeWidget(_parent), m_beginTime(-1)
|
2016-06-26 18:46:51 +03:00
|
|
|
{
|
|
|
|
setAlternatingRowColors(true);
|
|
|
|
setItemsExpandable(true);
|
|
|
|
setAnimated(true);
|
|
|
|
setSortingEnabled(false);
|
2016-06-26 20:54:16 +03:00
|
|
|
setColumnCount(8);
|
2016-06-26 18:46:51 +03:00
|
|
|
|
|
|
|
auto header = new QTreeWidgetItem();
|
|
|
|
header->setText(0, "Name");
|
|
|
|
header->setText(1, "Duration");
|
|
|
|
header->setText(2, "Begin, ms");
|
|
|
|
header->setText(3, "End, ms");
|
|
|
|
header->setText(4, "Min dur.");
|
|
|
|
header->setText(5, "Max dur.");
|
|
|
|
header->setText(6, "Average dur.");
|
|
|
|
header->setText(7, "N Calls");
|
|
|
|
setHeaderItem(header);
|
2016-06-26 20:54:16 +03:00
|
|
|
}
|
2016-06-26 18:46:51 +03:00
|
|
|
|
2016-06-26 20:54:16 +03:00
|
|
|
ProfTreeWidget::ProfTreeWidget(const thread_blocks_tree_t& _blocksTree, QWidget* _parent) : ProfTreeWidget(_parent)
|
|
|
|
{
|
2016-06-26 18:46:51 +03:00
|
|
|
setTreeInternal(_blocksTree);
|
|
|
|
|
|
|
|
setSortingEnabled(true);
|
|
|
|
sortByColumn(0, Qt::AscendingOrder);
|
|
|
|
sortByColumn(2, Qt::AscendingOrder);
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
connect(this, &QTreeWidget::itemExpanded, this, &ProfTreeWidget::onItemExpand);
|
2016-06-26 18:46:51 +03:00
|
|
|
}
|
|
|
|
|
2016-06-26 20:54:16 +03:00
|
|
|
ProfTreeWidget::~ProfTreeWidget()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
void ProfTreeWidget::setTree(const thread_blocks_tree_t& _blocksTree)
|
2016-06-26 18:46:51 +03:00
|
|
|
{
|
|
|
|
m_itemblocks.clear();
|
|
|
|
|
|
|
|
clear();
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
disconnect(this, &QTreeWidget::itemExpanded, this, &ProfTreeWidget::onItemExpand);
|
2016-06-26 18:46:51 +03:00
|
|
|
setSortingEnabled(false);
|
2016-06-26 20:54:16 +03:00
|
|
|
|
|
|
|
m_beginTime = -1;
|
2016-06-26 18:46:51 +03:00
|
|
|
setTreeInternal(_blocksTree);
|
|
|
|
|
|
|
|
setSortingEnabled(true);
|
|
|
|
sortByColumn(0, Qt::AscendingOrder);
|
|
|
|
sortByColumn(2, Qt::AscendingOrder);
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
connect(this, &QTreeWidget::itemExpanded, this, &ProfTreeWidget::onItemExpand);
|
2016-06-26 18:46:51 +03:00
|
|
|
}
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
void ProfTreeWidget::setTreeInternal(const thread_blocks_tree_t& _blocksTree)
|
2016-06-26 18:46:51 +03:00
|
|
|
{
|
|
|
|
for (const auto& threadTree : _blocksTree)
|
|
|
|
{
|
|
|
|
const auto beginTime = threadTree.second.children.front().node->block()->getBegin();
|
|
|
|
if (m_beginTime > beginTime)
|
|
|
|
{
|
|
|
|
m_beginTime = beginTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& threadTree : _blocksTree)
|
|
|
|
{
|
2016-06-26 19:06:53 +03:00
|
|
|
auto item = new ProfTreeWidgetItem(&threadTree.second);
|
2016-06-26 18:46:51 +03:00
|
|
|
item->setText(0, QString("Thread %1").arg(threadTree.first));
|
|
|
|
setTreeInternal(threadTree.second.children, item);
|
|
|
|
addTopLevelItem(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
void ProfTreeWidget::setTreeInternal(const BlocksTree::children_t& _children, ProfTreeWidgetItem* _parent)
|
2016-06-26 18:46:51 +03:00
|
|
|
{
|
|
|
|
for (const auto& child : _children)
|
|
|
|
{
|
|
|
|
const auto duration = child.node->block()->duration();
|
|
|
|
const auto beginTime = child.node->block()->getBegin() - m_beginTime;
|
|
|
|
const auto endTime = child.node->block()->getEnd() - m_beginTime;
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
auto item = new ProfTreeWidgetItem(&child, _parent);
|
2016-06-26 18:46:51 +03:00
|
|
|
item->setText(0, child.node->getBlockName());
|
|
|
|
item->setTimeSmart(1, child.node->block()->duration());
|
|
|
|
item->setTimeMs(2, child.node->block()->getBegin() - m_beginTime);
|
|
|
|
item->setTimeMs(3, child.node->block()->getEnd() - m_beginTime);
|
|
|
|
|
|
|
|
if (child.total_statistics)
|
|
|
|
{
|
|
|
|
item->setTimeSmart(4, child.total_statistics->min_duration);
|
|
|
|
item->setTimeSmart(5, child.total_statistics->max_duration);
|
|
|
|
item->setTimeSmart(6, child.total_statistics->average_duration());
|
|
|
|
item->setText(7, QString::number(child.total_statistics->calls_number));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_itemblocks[child.node] = item;
|
|
|
|
|
|
|
|
if (!child.children.empty())
|
|
|
|
{
|
|
|
|
setTreeInternal(child.children, item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
void ProfTreeWidget::contextMenuEvent(QContextMenuEvent* _event)
|
2016-06-26 18:46:51 +03:00
|
|
|
{
|
|
|
|
const auto col = currentColumn();
|
2016-06-26 19:06:53 +03:00
|
|
|
auto item = static_cast<ProfTreeWidgetItem*>(currentItem());
|
2016-06-26 18:46:51 +03:00
|
|
|
|
|
|
|
if (item == nullptr || col < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMenu menu;
|
|
|
|
|
|
|
|
auto action = new QAction("Expand all", nullptr);
|
2016-06-26 19:06:53 +03:00
|
|
|
connect(action, &QAction::triggered, this, &ProfTreeWidget::onExpandAllClicked);
|
2016-06-26 18:46:51 +03:00
|
|
|
menu.addAction(action);
|
|
|
|
|
|
|
|
action = new QAction("Collapse all", nullptr);
|
2016-06-26 19:06:53 +03:00
|
|
|
connect(action, &QAction::triggered, this, &ProfTreeWidget::onCollapseAllClicked);
|
2016-06-26 18:46:51 +03:00
|
|
|
menu.addAction(action);
|
|
|
|
|
|
|
|
switch (col)
|
|
|
|
{
|
|
|
|
case 4:
|
|
|
|
{
|
|
|
|
menu.addSeparator();
|
2016-06-26 19:06:53 +03:00
|
|
|
auto itemAction = new ProfItemAction("Jump to such item", item);
|
|
|
|
connect(itemAction, &ProfItemAction::clicked, this, &ProfTreeWidget::onJumpToMinItemClicked);
|
2016-06-26 18:46:51 +03:00
|
|
|
menu.addAction(itemAction);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
{
|
|
|
|
menu.addSeparator();
|
2016-06-26 19:06:53 +03:00
|
|
|
auto itemAction = new ProfItemAction("Jump to such item", item);
|
|
|
|
connect(itemAction, &ProfItemAction::clicked, this, &ProfTreeWidget::onJumpToMaxItemClicked);
|
2016-06-26 18:46:51 +03:00
|
|
|
menu.addAction(itemAction);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
menu.exec(QCursor::pos());
|
|
|
|
|
|
|
|
_event->accept();
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
void ProfTreeWidget::onJumpToMinItemClicked(ProfTreeWidgetItem* _item)
|
2016-06-26 18:46:51 +03:00
|
|
|
{
|
|
|
|
auto it = m_itemblocks.find(_item->block()->total_statistics->min_duration_block);
|
|
|
|
if (it != m_itemblocks.end())
|
|
|
|
{
|
|
|
|
scrollToItem(it->second, QAbstractItemView::PositionAtCenter);
|
|
|
|
setCurrentItem(it->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
void ProfTreeWidget::onJumpToMaxItemClicked(ProfTreeWidgetItem* _item)
|
2016-06-26 18:46:51 +03:00
|
|
|
{
|
|
|
|
auto it = m_itemblocks.find(_item->block()->total_statistics->max_duration_block);
|
|
|
|
if (it != m_itemblocks.end())
|
|
|
|
{
|
|
|
|
scrollToItem(it->second, QAbstractItemView::PositionAtCenter);
|
|
|
|
setCurrentItem(it->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
void ProfTreeWidget::onCollapseAllClicked(bool)
|
2016-06-26 18:46:51 +03:00
|
|
|
{
|
|
|
|
collapseAll();
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
void ProfTreeWidget::onExpandAllClicked(bool)
|
2016-06-26 18:46:51 +03:00
|
|
|
{
|
2016-06-26 19:06:53 +03:00
|
|
|
disconnect(this, &QTreeWidget::itemExpanded, this, &ProfTreeWidget::onItemExpand);
|
2016-06-26 18:46:51 +03:00
|
|
|
expandAll();
|
|
|
|
resizeColumnToContents(0);
|
2016-06-26 19:06:53 +03:00
|
|
|
connect(this, &QTreeWidget::itemExpanded, this, &ProfTreeWidget::onItemExpand);
|
2016-06-26 18:46:51 +03:00
|
|
|
}
|
|
|
|
|
2016-06-26 19:06:53 +03:00
|
|
|
void ProfTreeWidget::onItemExpand(QTreeWidgetItem*)
|
2016-06-26 18:46:51 +03:00
|
|
|
{
|
|
|
|
resizeColumnToContents(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|