From 1155601da5b52a48e4c6b9bedaba904e6b703e8a Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Fri, 23 Mar 2018 10:29:02 +0100 Subject: [PATCH] QmlProfiler: Move some methods from statistics view into model This allows us to tighten up the public interface of the model. Change-Id: Iaa0363993de7cd94c3468d2c939198e65746e829 Reviewed-by: Tobias Hunger --- .../qmlprofilerstatisticsmodel.cpp | 72 +++++++++++++++++-- .../qmlprofiler/qmlprofilerstatisticsmodel.h | 5 +- .../qmlprofiler/qmlprofilerstatisticsview.cpp | 66 +++-------------- .../qmlprofiler/qmlprofilerstatisticsview.h | 6 +- 4 files changed, 81 insertions(+), 68 deletions(-) diff --git a/src/plugins/qmlprofiler/qmlprofilerstatisticsmodel.cpp b/src/plugins/qmlprofiler/qmlprofilerstatisticsmodel.cpp index 804d68e5e2d..18c4905b8b4 100644 --- a/src/plugins/qmlprofiler/qmlprofilerstatisticsmodel.cpp +++ b/src/plugins/qmlprofiler/qmlprofilerstatisticsmodel.cpp @@ -37,6 +37,19 @@ namespace QmlProfiler { +QString QmlProfilerStatisticsModel::nameForType(RangeType typeNumber) +{ + switch (typeNumber) { + case Painting: return QmlProfilerStatisticsModel::tr("Painting"); + case Compiling: return QmlProfilerStatisticsModel::tr("Compiling"); + case Creating: return QmlProfilerStatisticsModel::tr("Creating"); + case Binding: return QmlProfilerStatisticsModel::tr("Binding"); + case HandlingSignal: return QmlProfilerStatisticsModel::tr("Handling Signal"); + case Javascript: return QmlProfilerStatisticsModel::tr("JavaScript"); + default: return QString(); + } +} + double QmlProfilerStatisticsModel::durationPercent(int typeId) const { const QmlEventStats &global = m_data[-1]; @@ -124,6 +137,60 @@ const QHash &QmlProfilerStatisticsModel::getNotes() const return m_notes; } +QStringList QmlProfilerStatisticsModel::details(int typeIndex) const +{ + QString data; + QString displayName; + if (typeIndex < 0) { + data = tr("Main Program"); + } else { + const QmlEventType &type = m_modelManager->eventTypes().at(typeIndex); + displayName = nameForType(type.rangeType()); + + const QChar ellipsisChar(0x2026); + const int maxColumnWidth = 32; + + data = type.data(); + if (data.length() > maxColumnWidth) + data = data.left(maxColumnWidth - 1) + ellipsisChar; + } + + return QStringList({ + displayName, + data, + QString::number(durationPercent(typeIndex), 'f', 2) + QLatin1Char('%') + }); +} + +QString QmlProfilerStatisticsModel::summary(const QVector &typeIds) const +{ + const double cutoff = 0.1; + const double round = 0.05; + double maximum = 0; + double sum = 0; + + for (int typeId : typeIds) { + const double percentage = durationPercent(typeId); + if (percentage > maximum) + maximum = percentage; + sum += percentage; + } + + const QLatin1Char percent('%'); + + if (sum < cutoff) + return QLatin1Char('<') + QString::number(cutoff, 'f', 1) + percent; + + if (typeIds.length() == 1) + return QLatin1Char('~') + QString::number(maximum, 'f', 1) + percent; + + // add/subtract 0.05 to avoid problematic rounding + if (maximum < cutoff) + return QChar(0x2264) + QString::number(sum + round, 'f', 1) + percent; + + return QChar(0x2265) + QString::number(qMax(maximum - round, cutoff), 'f', 1) + percent; +} + void QmlProfilerStatisticsModel::clear() { m_data.clear(); @@ -146,11 +213,6 @@ void QmlProfilerStatisticsModel::setRelativesModel(QmlProfilerStatisticsRelative m_calleesModel = relative; } -QmlProfilerModelManager *QmlProfilerStatisticsModel::modelManager() const -{ - return m_modelManager; -} - void QmlProfilerStatisticsModel::dataChanged() { if (m_modelManager->state() == QmlProfilerModelManager::ClearingData) diff --git a/src/plugins/qmlprofiler/qmlprofilerstatisticsmodel.h b/src/plugins/qmlprofiler/qmlprofilerstatisticsmodel.h index 478207255ad..092e9f8c69f 100644 --- a/src/plugins/qmlprofiler/qmlprofilerstatisticsmodel.h +++ b/src/plugins/qmlprofiler/qmlprofilerstatisticsmodel.h @@ -49,6 +49,8 @@ class QmlProfilerStatisticsModel : public QObject { Q_OBJECT public: + static QString nameForType(RangeType typeNumber); + struct QmlEventStats { qint64 duration = 0; qint64 durationSelf = 0; @@ -68,6 +70,8 @@ public: void restrictToFeatures(quint64 features); bool isRestrictedToRange() const; + QStringList details(int typeIndex) const; + QString summary(const QVector &typeIds) const; const QHash &getData() const; const QVector &getTypes() const; const QHash &getNotes() const; @@ -77,7 +81,6 @@ public: void setRelativesModel(QmlProfilerStatisticsRelativesModel *childModel, QmlProfilerStatisticsRelation relation); - QmlProfilerModelManager *modelManager() const; signals: void dataAvailable(); diff --git a/src/plugins/qmlprofiler/qmlprofilerstatisticsview.cpp b/src/plugins/qmlprofiler/qmlprofilerstatisticsview.cpp index d3d45cb7fa0..984c125c545 100644 --- a/src/plugins/qmlprofiler/qmlprofilerstatisticsview.cpp +++ b/src/plugins/qmlprofiler/qmlprofilerstatisticsview.cpp @@ -243,49 +243,12 @@ void QmlProfilerStatisticsView::clear() QString QmlProfilerStatisticsView::summary(const QVector &typeIds) const { - const double cutoff = 0.1; - const double round = 0.05; - double maximum = 0; - double sum = 0; - - for (int typeId : typeIds) { - const double percentage = m_mainView->durationPercent(typeId); - if (percentage > maximum) - maximum = percentage; - sum += percentage; - } - - const QLatin1Char percent('%'); - - if (sum < cutoff) - return QLatin1Char('<') + QString::number(cutoff, 'f', 1) + percent; - - if (typeIds.length() == 1) - return QLatin1Char('~') + QString::number(maximum, 'f', 1) + percent; - - // add/subtract 0.05 to avoid problematic rounding - if (maximum < cutoff) - return QChar(0x2264) + QString::number(sum + round, 'f', 1) + percent; - - return QChar(0x2265) + QString::number(qMax(maximum - round, cutoff), 'f', 1) + percent; + return m_mainView->summary(typeIds); } QStringList QmlProfilerStatisticsView::details(int typeId) const { - const QmlEventType &type = m_mainView->getType(typeId); - - const QChar ellipsisChar(0x2026); - const int maxColumnWidth = 32; - - QString data = type.data(); - if (data.length() > maxColumnWidth) - data = data.left(maxColumnWidth - 1) + ellipsisChar; - - return QStringList({ - QmlProfilerStatisticsMainView::nameForType(type.rangeType()), - data, - QString::number(m_mainView->durationPercent(typeId), 'f', 2) + QLatin1Char('%') - }); + return m_mainView->details(typeId); } QModelIndex QmlProfilerStatisticsView::selectedModelIndex() const @@ -493,14 +456,14 @@ bool QmlProfilerStatisticsMainView::isRestrictedToRange() const return m_model->isRestrictedToRange(); } -double QmlProfilerStatisticsMainView::durationPercent(int typeId) const +QString QmlProfilerStatisticsMainView::summary(const QVector &typeIds) const { - return m_model->durationPercent(typeId); + return m_model->summary(typeIds); } -const QmlEventType &QmlProfilerStatisticsMainView::getType(int typeId) const +QStringList QmlProfilerStatisticsMainView::details(int typeId) const { - return m_model->getTypes()[typeId]; + return m_model->details(typeId); } void QmlProfilerStatisticsMainView::parseModel() @@ -520,7 +483,7 @@ void QmlProfilerStatisticsMainView::parseModel() type.displayName().isEmpty() ? tr("") : type.displayName(), type.displayName()); - QString typeString = QmlProfilerStatisticsMainView::nameForType(type.rangeType()); + QString typeString = QmlProfilerStatisticsModel::nameForType(type.rangeType()); newRow << new StatisticsViewItem(typeString, typeString); const double percent = m_model->durationPercent(typeIndex); @@ -582,19 +545,6 @@ QStandardItem *QmlProfilerStatisticsMainView::itemFromIndex(const QModelIndex &i return m_standardItemModel->item(index.row()); } -QString QmlProfilerStatisticsMainView::nameForType(RangeType typeNumber) -{ - switch (typeNumber) { - case Painting: return QmlProfilerStatisticsMainView::tr("Painting"); - case Compiling: return QmlProfilerStatisticsMainView::tr("Compiling"); - case Creating: return QmlProfilerStatisticsMainView::tr("Creating"); - case Binding: return QmlProfilerStatisticsMainView::tr("Binding"); - case HandlingSignal: return QmlProfilerStatisticsMainView::tr("Handling Signal"); - case Javascript: return QmlProfilerStatisticsMainView::tr("JavaScript"); - default: return QString(); - } -} - int QmlProfilerStatisticsMainView::selectedTypeId() const { QModelIndex index = selectedModelIndex(); @@ -753,7 +703,7 @@ void QmlProfilerStatisticsRelativesView::rebuildTree( newRow << new StatisticsViewItem( type.displayName().isEmpty() ? tr("") : type.displayName(), type.displayName()); - const QString typeName = QmlProfilerStatisticsMainView::nameForType(type.rangeType()); + const QString typeName = QmlProfilerStatisticsModel::nameForType(type.rangeType()); newRow << new StatisticsViewItem(typeName, typeName); newRow << new StatisticsViewItem(Timeline::formatTime(stats.duration), stats.duration); diff --git a/src/plugins/qmlprofiler/qmlprofilerstatisticsview.h b/src/plugins/qmlprofiler/qmlprofilerstatisticsview.h index 957ac190155..351147d0e78 100644 --- a/src/plugins/qmlprofiler/qmlprofilerstatisticsview.h +++ b/src/plugins/qmlprofiler/qmlprofilerstatisticsview.h @@ -117,8 +117,6 @@ public: void copyTableToClipboard() const; void copyRowToClipboard() const; - static QString nameForType(RangeType typeNumber); - int selectedTypeId() const; void setShowExtendedStatistics(bool); @@ -132,9 +130,9 @@ public: void restrictToFeatures(quint64 features); bool isRestrictedToRange() const; - double durationPercent(int typeId) const; - const QmlEventType &getType(int typeId) const; + QString summary(const QVector &typeIds) const; + QStringList details(int typeId) const; signals: void gotoSourceLocation(const QString &fileName, int lineNumber, int columnNumber);