Callgrind: modernize callgrind marks

Use annotions instead of custom icons on marks and show tooltip on
annotations

Change-Id: I153caefb997e9688902c0ec1a8090ff741416431
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
David Schulz
2022-04-27 14:29:26 +02:00
parent 30f9f68b15
commit abccfe0646
2 changed files with 33 additions and 43 deletions

View File

@@ -31,6 +31,7 @@
#include "callgrind/callgrindfunction.h"
#include <QDebug>
#include <QLabel>
#include <QPainter>
#include <utils/qtcassert.h>
@@ -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<const Function *>();
}
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;
}

View File

@@ -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;
};