forked from qt-creator/qt-creator
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:
@@ -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;
|
||||
}
|
||||
|
@@ -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;
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user