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 "callgrind/callgrindfunction.h"
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
#include <QLabel>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
@@ -43,50 +44,16 @@ namespace Constants { const char CALLGRIND_TEXT_MARK_CATEGORY[] = "Callgrind.Tex
|
|||||||
|
|
||||||
CallgrindTextMark::CallgrindTextMark(const QPersistentModelIndex &index,
|
CallgrindTextMark::CallgrindTextMark(const QPersistentModelIndex &index,
|
||||||
const FilePath &fileName, int lineNumber)
|
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)
|
, m_modelIndex(index)
|
||||||
{
|
{
|
||||||
setPriority(TextEditor::TextMark::HighPriority);
|
setPriority(TextEditor::TextMark::HighPriority);
|
||||||
}
|
const Function *f = function();
|
||||||
|
const QString inclusiveCost = QLocale::system().toString(f->inclusiveCost(0));
|
||||||
void CallgrindTextMark::paintIcon(QPainter *painter, const QRect &paintRect) const
|
setLineAnnotation(tr("%1 (Called: %2; Incl. Cost: %3)")
|
||||||
{
|
.arg(CallgrindHelper::toPercent(costs() * 100.0f))
|
||||||
if (!m_modelIndex.isValid())
|
.arg(f->called())
|
||||||
return;
|
.arg(inclusiveCost));
|
||||||
|
|
||||||
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 *CallgrindTextMark::function() const
|
const Function *CallgrindTextMark::function() const
|
||||||
@@ -97,3 +64,25 @@ const Function *CallgrindTextMark::function() const
|
|||||||
return m_modelIndex.data(DataModel::FunctionRole).value<const Function *>();
|
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;
|
const Valgrind::Callgrind::Function *function() const;
|
||||||
|
|
||||||
void paintIcon(QPainter *painter, const QRect &paintRect) const override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
bool addToolTipContent(QLayout *target) const;
|
||||||
|
qreal costs() const;
|
||||||
|
|
||||||
QPersistentModelIndex m_modelIndex;
|
QPersistentModelIndex m_modelIndex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user