Skip to content

Commit

Permalink
#106 [UI] Rising a tooltip when passing cursor over a bookmark; #0 [U…
Browse files Browse the repository at this point in the history
…I] Minor fixes
  • Loading branch information
cas4ey committed Jun 13, 2018
1 parent 0ae4304 commit c85d15a
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 54 deletions.
137 changes: 113 additions & 24 deletions profiler_gui/blocks_graphics_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ BoldLabel::~BoldLabel()
//////////////////////////////////////////////////////////////////////////

BackgroundItem::BackgroundItem() : AuxItem()
, m_bookmark(std::numeric_limits<size_t>::max())
, m_tooltip(nullptr)
, m_bookmark(profiler_gui::numeric_max<decltype(m_bookmark)>())
, m_bButtonPressed(false)
{
m_bookmarkSign.lineTo(px(BOOKMARK_WIDTH), 0);
Expand All @@ -151,6 +152,11 @@ BackgroundItem::BackgroundItem() : AuxItem()
connect(&m_idleTimer, &QTimer::timeout, this, &BackgroundItem::onIdleTimeout);
}

BackgroundItem::~BackgroundItem()
{
delete m_tooltip;
}

void BackgroundItem::paint(QPainter* _painter, const QStyleOptionGraphicsItem*, QWidget*)
{
auto const sceneView = static_cast<BlocksGraphicsView*>(scene()->parent());
Expand Down Expand Up @@ -295,27 +301,32 @@ void BackgroundItem::paint(QPainter* _painter, const QStyleOptionGraphicsItem*,
pen.setColor(Qt::black);
pen.setWidth(px(1));

_painter->setPen(borderColor);
_painter->setBrush(Qt::transparent);
_painter->setRenderHint(QPainter::Antialiasing);

const int minWidth = px(BOOKMARK_WIDTH);
const int half = minWidth >> 1;
QBrush brush(Qt::transparent);
qreal prevPos = -offset * currentScale;
const int half = px(BOOKMARK_WIDTH) >> 1;
auto color = QColor(Qt::transparent).rgb();
qreal prevPos = -1e300;
for (auto it = first_it; it != bookmarks.cend() && it->pos <= endTime; ++it)
{
const qreal pos =
(PROF_MICROSECONDS(it->pos - EASY_GLOBALS.begin_time) - offset) * currentScale - half;

const bool isSelectedBookmark = m_bookmark == static_cast<size_t>(std::distance(bookmarks.cbegin(), it));

const auto delta = fabs(pos - prevPos);
if (delta < minWidth)
if (delta < half && !isSelectedBookmark)
continue;

if (brush.color().rgb() != it->color)
brush.setColor(QColor::fromRgb(it->color));
if (color != it->color)
_painter->setBrush(QColor::fromRgb(it->color));

const auto path = m_bookmarkSign.translated(pos, h);
_painter->drawPath(path);

_painter->fillPath(m_bookmarkSign.translated(pos, h), brush);
if (m_bookmark == static_cast<size_t>(std::distance(bookmarks.cbegin(), it)))
_painter->strokePath(m_bookmarkSign.translated(pos, h), pen);
if (isSelectedBookmark)
_painter->strokePath(path, pen);

prevPos = pos;
}
Expand All @@ -330,13 +341,16 @@ bool BackgroundItem::mouseMove(const QPointF& scenePos)
{
const auto prev = m_bookmark;

delete m_tooltip;
m_tooltip = nullptr;

auto& bookmarks = EASY_GLOBALS.bookmarks;
if (bookmarks.empty())
{
if (m_idleTimer.isActive())
m_idleTimer.stop();

m_bookmark = std::numeric_limits<size_t>::max();
profiler_gui::set_max(m_bookmark);
if (prev != m_bookmark)
{
qApp->restoreOverrideCursor();
Expand All @@ -357,7 +371,7 @@ bool BackgroundItem::mouseMove(const QPointF& scenePos)
if (m_idleTimer.isActive())
m_idleTimer.stop();

m_bookmark = std::numeric_limits<size_t>::max();
profiler_gui::set_max(m_bookmark);
if (prev != m_bookmark)
{
qApp->restoreOverrideCursor();
Expand All @@ -374,7 +388,7 @@ bool BackgroundItem::mouseMove(const QPointF& scenePos)

if (!m_bButtonPressed)
{
m_bookmark = std::numeric_limits<size_t>::max();
profiler_gui::set_max(m_bookmark);

auto first_it = std::lower_bound(bookmarks.cbegin(), bookmarks.cend(), timestamp,
[](const profiler::Bookmark& bookmark, profiler::timestamp_t value)
Expand Down Expand Up @@ -442,6 +456,10 @@ bool BackgroundItem::mouseMove(const QPointF& scenePos)
bool BackgroundItem::mousePress(const QPointF& scenePos)
{
m_bButtonPressed = m_bookmark < EASY_GLOBALS.bookmarks.size() && contains(scenePos);

delete m_tooltip;
m_tooltip = nullptr;

return false;
}

Expand Down Expand Up @@ -508,6 +526,23 @@ bool BackgroundItem::mouseDoubleClick(const QPointF& scenePos)
return true;
}

void BackgroundItem::mouseLeave()
{
delete m_tooltip;
m_tooltip = nullptr;

if (m_idleTimer.isActive())
m_idleTimer.stop();

if (m_bookmark < EASY_GLOBALS.bookmarks.size())
{
profiler_gui::set_max(m_bookmark);
qApp->restoreOverrideCursor();
emit bookmarkChanged(m_bookmark);
update();
}
}

bool BackgroundItem::contains(const QPointF& scenePos) const
{
auto const sceneView = static_cast<BlocksGraphicsView*>(scene()->parent());
Expand All @@ -520,14 +555,40 @@ void BackgroundItem::onIdleTimeout()
{
if (m_bookmark < EASY_GLOBALS.bookmarks.size())
{
/// TODO: show tooltip
delete m_tooltip;
m_tooltip = nullptr;

const auto& text = EASY_GLOBALS.bookmarks[m_bookmark].text;
if (text.empty())
return;

auto parent = static_cast<QWidget*>(scene()->parent());
m_tooltip = new QLabel(QString::fromStdString(text),
parent, Qt::ToolTip | Qt::WindowTransparentForInput);

if (m_tooltip == nullptr)
return;

const auto delta = px(10);

m_tooltip->setObjectName(QStringLiteral("BookmarkPopup"));
m_tooltip->setAttribute(Qt::WA_ShowWithoutActivating, true);
m_tooltip->setFocusPolicy(Qt::NoFocus);
m_tooltip->setWordWrap(true);
m_tooltip->move(QCursor::pos() + QPoint(delta >> 1, delta));
m_tooltip->show();

const int bottom = m_tooltip->mapToParent(m_tooltip->pos()).y() + m_tooltip->height();
const int parentBottom = parent->y() + parent->height();
if (bottom > parentBottom)
m_tooltip->move(m_tooltip->pos() - QPoint(delta >> 1, m_tooltip->height() + delta));
}
}

//////////////////////////////////////////////////////////////////////////

ForegroundItem::ForegroundItem() : AuxItem()
, m_bookmark(std::numeric_limits<size_t>::max())
, m_bookmark(profiler_gui::numeric_max<decltype(m_bookmark)>())
{

}
Expand Down Expand Up @@ -592,7 +653,7 @@ void ForegroundItem::onMoved()

BlocksGraphicsView::BlocksGraphicsView(QWidget* _parent)
: Parent(_parent)
, m_beginTime(std::numeric_limits<decltype(m_beginTime)>::max())
, m_beginTime(profiler_gui::numeric_max<decltype(m_beginTime)>())
, m_sceneWidth(0)
, m_scale(1)
, m_offset(0)
Expand Down Expand Up @@ -696,7 +757,7 @@ void BlocksGraphicsView::clear()
m_selectedBlocks.clear();
m_backgroundItem = nullptr;

m_beginTime = std::numeric_limits<decltype(m_beginTime)>::max(); // reset begin time
profiler_gui::set_max(m_beginTime); // reset begin time
m_scale = 1; // scale back to initial 100% scale
m_timelineStep = 1;
m_offset = 0; // scroll back to the beginning of the scene
Expand Down Expand Up @@ -757,7 +818,7 @@ void BlocksGraphicsView::notifyVisibleRegionPosChange()

void BlocksGraphicsView::notifyVisibleRegionPosChange(qreal _pos)
{
if (m_sceneWidth < m_visibleRegionWidth)
if (m_sceneWidth <= m_visibleRegionWidth)
m_offset = 0;
else
m_offset = estd::clamp(0., _pos, m_sceneWidth - m_visibleRegionWidth);
Expand Down Expand Up @@ -889,11 +950,11 @@ void BlocksGraphicsView::setTree(const profiler::thread_blocks_tree_t& _blocksTr
EASY_GLOBALS.scene.empty = false;

// Center view on the beginning of the scene
updateVisibleSceneRect();
//setScrollbar(m_pScrollbar);
const int vbar_width = updateVisibleSceneRect();
const auto windowWidth = (m_visibleSceneRect.width() + vbar_width) / m_scale;

notifySceneSizeChange();
notifyVisibleRegionSizeChange();
notifyVisibleRegionSizeChange(windowWidth);

// Create new chronometer item (previous item was destroyed by scene on scene()->clear()).
// It will be shown on mouse right button click.
Expand Down Expand Up @@ -925,12 +986,38 @@ void BlocksGraphicsView::setTree(const profiler::thread_blocks_tree_t& _blocksTr

scrollTo(longestItem);
m_pScrollbar->setHistogramSource(longestItem->threadId(), longestItem->items(0));

if (!longestItem->items(0).empty())
{
notifyVisibleRegionPosChange(longestItem->items(0).front().left() - m_visibleRegionWidth * 0.25);

// Scale to fit all items
const auto right = longestItem->items(0).back().right() - m_offset;
const auto currentScale = m_scale + std::numeric_limits<decltype(m_scale)>::epsilon();

auto scale = m_scale;
while (scale < MAX_SCALE && right < (m_visibleSceneRect.width() + vbar_width) / scale)
{
m_scale = scale;
scale *= profiler_gui::SCALING_COEFFICIENT;
}

if (currentScale < m_scale)
scaleTo(m_scale);
}
}

if (m_bHovered && !m_idleTimer.isActive())
m_idleTimer.start();

// Workaround for valid scene painting after setting a new tree
QTimer::singleShot(0, this, &This::revalidateOffset);
}

void BlocksGraphicsView::revalidateOffset()
{
notifyVisibleRegionPosChange(m_offset);
repaintScene();
}

const BlocksGraphicsView::Items &BlocksGraphicsView::getItems() const
Expand Down Expand Up @@ -1150,7 +1237,6 @@ void BlocksGraphicsView::scaleTo(qreal _scale)
notifyVisibleRegionSizeChange(windowWidth);

updateTimelineStep(windowWidth);
repaintScene();
}

//////////////////////////////////////////////////////////////////////////
Expand All @@ -1175,6 +1261,9 @@ void BlocksGraphicsView::leaveEvent(QEvent* _event)

if (!needToIgnoreMouseEvent())
removePopup();

if (m_backgroundItem != nullptr)
m_backgroundItem->mouseLeave();
}

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1241,7 +1330,7 @@ void BlocksGraphicsView::onWheel(qreal _scenePos, int _wheelDelta)
notifyVisibleRegionSizeChange();

// Calculate new offset to simulate QGraphicsView::AnchorUnderMouse scaling behavior
if (m_sceneWidth < m_visibleRegionWidth)
if (m_sceneWidth <= m_visibleRegionWidth)
m_offset = 0;
else
m_offset = clamp(0., initialPosition - _scenePos / m_scale, m_sceneWidth - m_visibleRegionWidth);
Expand Down
6 changes: 5 additions & 1 deletion profiler_gui/blocks_graphics_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,22 @@ class BackgroundItem : public AuxItem

QTimer m_idleTimer;
QPainterPath m_bookmarkSign;
QLabel* m_tooltip;
size_t m_bookmark;
bool m_bButtonPressed;

public:

explicit BackgroundItem();
~BackgroundItem() override {}
~BackgroundItem() override;

void paint(QPainter* _painter, const QStyleOptionGraphicsItem* _option, QWidget* _widget = nullptr) override;

bool mouseMove(const QPointF& scenePos);
bool mousePress(const QPointF& scenePos);
bool mouseRelease(const QPointF& scenePos);
bool mouseDoubleClick(const QPointF& scenePos);
void mouseLeave();

bool contains(const QPointF& scenePos) const;

Expand Down Expand Up @@ -291,6 +293,8 @@ class BlocksGraphicsView : public QGraphicsView
void onWheel(qreal _scenePos, int _wheelDelta);
qreal setTree(GraphicsBlockItem* _item, const ::profiler::BlocksTree::children_t& _children, qreal& _height, uint32_t& _maxDepthChild, qreal _y, short _level);

void revalidateOffset();

void addSelectionToHierarchy();

private slots:
Expand Down
7 changes: 6 additions & 1 deletion profiler_gui/blocks_tree_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,6 @@ HierarchyWidget::HierarchyWidget(QWidget* _parent) : Parent(_parent)
{
loadSettings();

m_searchBox->setFixedWidth(300);
m_searchBox->setContentsMargins(5, 0, 0, 0);
m_searchBox->setClearButtonEnabled(true);
m_searchBox->setPlaceholderText("Search by name");
Expand Down Expand Up @@ -1316,6 +1315,12 @@ void HierarchyWidget::contextMenuEvent(QContextMenuEvent* _event)
m_tree->contextMenuEvent(_event);
}

void HierarchyWidget::showEvent(QShowEvent* event)
{
Parent::showEvent(event);
m_searchBox->setFixedWidth(px(300));
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

BlocksTreeWidget* HierarchyWidget::tree()
Expand Down
6 changes: 6 additions & 0 deletions profiler_gui/blocks_tree_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ class HierarchyWidget : public QWidget
void contextMenuEvent(QContextMenuEvent* _event) override;
void dragEnterEvent(QDragEnterEvent*) override {}

protected:

// Protected virtual methods

void showEvent(QShowEvent* event) override;

public:

// Public non-virtual methods
Expand Down
Loading

0 comments on commit c85d15a

Please sign in to comment.