From abccfe064641021ba6bdc08f8a0749b44915ba4c Mon Sep 17 00:00:00 2001 From: David Schulz Date: Wed, 27 Apr 2022 14:29:26 +0200 Subject: [PATCH] Callgrind: modernize callgrind marks Use annotions instead of custom icons on marks and show tooltip on annotations Change-Id: I153caefb997e9688902c0ec1a8090ff741416431 Reviewed-by: hjk --- src/plugins/valgrind/callgrindtextmark.cpp | 71 +++++++++------------- src/plugins/valgrind/callgrindtextmark.h | 5 +- 2 files changed, 33 insertions(+), 43 deletions(-) diff --git a/src/plugins/valgrind/callgrindtextmark.cpp b/src/plugins/valgrind/callgrindtextmark.cpp index 3ba65a3e003..21c9bc201cd 100644 --- a/src/plugins/valgrind/callgrindtextmark.cpp +++ b/src/plugins/valgrind/callgrindtextmark.cpp @@ -31,6 +31,7 @@ #include "callgrind/callgrindfunction.h" #include +#include #include #include @@ -43,50 +44,16 @@ namespace Constants { const char CALLGRIND_TEXT_MARK_CATEGORY[] = "Callgrind.Tex CallgrindTextMark::CallgrindTextMark(const QPersistentModelIndex &index, const FilePath &fileName, int lineNumber) - : TextEditor::TextMark(fileName, lineNumber, Constants::CALLGRIND_TEXT_MARK_CATEGORY, 4.0) + : TextEditor::TextMark(fileName, lineNumber, Constants::CALLGRIND_TEXT_MARK_CATEGORY) , m_modelIndex(index) { setPriority(TextEditor::TextMark::HighPriority); -} - -void CallgrindTextMark::paintIcon(QPainter *painter, const QRect &paintRect) const -{ - if (!m_modelIndex.isValid()) - return; - - bool ok; - qreal costs = m_modelIndex.data(RelativeTotalCostRole).toReal(&ok); - QTC_ASSERT(ok, return); - QTC_ASSERT(costs >= 0.0 && costs <= 100.0, return); - - painter->save(); - - // set up - painter->setPen(Qt::black); - - // draw bar - QRect fillRect = paintRect; - fillRect.setWidth(paintRect.width() * costs); - painter->fillRect(paintRect, Qt::white); - painter->fillRect(fillRect, CallgrindHelper::colorForCostRatio(costs)); - painter->drawRect(paintRect); - - // draw text - const QTextOption flags = Qt::AlignHCenter | Qt::AlignVCenter; - const QString text = CallgrindHelper::toPercent(costs * 100.0f); - - // decrease font size if paint rect is too small (very unlikely, but may happen) - QFont font = painter->font(); - QFontMetrics fm = QFontMetrics(font); - while (fm.boundingRect(text).width() > paintRect.width()) { - font.setPointSize(font.pointSize() - 1); - fm = QFontMetrics(font); - } - painter->setFont(font); - - painter->drawText(paintRect, text, flags); - - painter->restore(); + const Function *f = function(); + const QString inclusiveCost = QLocale::system().toString(f->inclusiveCost(0)); + setLineAnnotation(tr("%1 (Called: %2; Incl. Cost: %3)") + .arg(CallgrindHelper::toPercent(costs() * 100.0f)) + .arg(f->called()) + .arg(inclusiveCost)); } const Function *CallgrindTextMark::function() const @@ -97,3 +64,25 @@ const Function *CallgrindTextMark::function() const return m_modelIndex.data(DataModel::FunctionRole).value(); } +bool CallgrindTextMark::addToolTipContent(QLayout *target) const +{ + if (!m_modelIndex.isValid()) + return false; + + const QString tooltip = m_modelIndex.data(Qt::ToolTipRole).toString(); + if (tooltip.isEmpty()) + return false; + + target->addWidget(new QLabel(tooltip)); + return true; +} + +qreal CallgrindTextMark::costs() const +{ + bool ok; + qreal costs = m_modelIndex.data(RelativeTotalCostRole).toReal(&ok); + QTC_ASSERT(ok, return 0.0); + QTC_ASSERT(costs >= 0.0 && costs <= 100.0, return 0.0); + + return costs; +} diff --git a/src/plugins/valgrind/callgrindtextmark.h b/src/plugins/valgrind/callgrindtextmark.h index e92f16ce23e..11eb7026ecb 100644 --- a/src/plugins/valgrind/callgrindtextmark.h +++ b/src/plugins/valgrind/callgrindtextmark.h @@ -49,9 +49,10 @@ public: const Valgrind::Callgrind::Function *function() const; - void paintIcon(QPainter *painter, const QRect &paintRect) const override; - private: + bool addToolTipContent(QLayout *target) const; + qreal costs() const; + QPersistentModelIndex m_modelIndex; };