From a6acd3e8d9c3ab0758b96815d79235aa371f2208 Mon Sep 17 00:00:00 2001 From: hjk Date: Wed, 27 Jul 2016 18:08:56 +0200 Subject: [PATCH] TreeModel: Introduce a TreeItem::indexInParent() convenience function To shorten user code and hide the ugly const_cast. Change-Id: I798bd105932004ea17cb95b243fc38ccfcda0a2c Reviewed-by: Christian Stenger --- src/libs/utils/treemodel.cpp | 5 +++++ src/libs/utils/treemodel.h | 1 + .../clangstaticanalyzerdiagnosticmodel.cpp | 2 +- src/plugins/projectexplorer/projectwindow.cpp | 2 +- src/plugins/valgrind/xmlprotocol/errorlistmodel.cpp | 2 +- 5 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/libs/utils/treemodel.cpp b/src/libs/utils/treemodel.cpp index 3acc9dbcdfc..fc8b55272db 100644 --- a/src/libs/utils/treemodel.cpp +++ b/src/libs/utils/treemodel.cpp @@ -766,6 +766,11 @@ int TreeItem::level() const return l; } +int TreeItem::indexInParent() const +{ + return m_parent ? m_parent->m_children.indexOf(const_cast(this)) : -1; +} + QModelIndex TreeItem::index() const { QTC_ASSERT(m_model, return QModelIndex()); diff --git a/src/libs/utils/treemodel.h b/src/libs/utils/treemodel.h index 080263a19fa..6532caaf1e9 100644 --- a/src/libs/utils/treemodel.h +++ b/src/libs/utils/treemodel.h @@ -65,6 +65,7 @@ public: int level() const; int childCount() const { return m_children.size(); } + int indexInParent() const; TreeItem *childAt(int index) const; QVector children() const { return m_children; } QModelIndex index() const; diff --git a/src/plugins/clangstaticanalyzer/clangstaticanalyzerdiagnosticmodel.cpp b/src/plugins/clangstaticanalyzer/clangstaticanalyzerdiagnosticmodel.cpp index af5eab2d67c..3ee6060d5c3 100644 --- a/src/plugins/clangstaticanalyzer/clangstaticanalyzerdiagnosticmodel.cpp +++ b/src/plugins/clangstaticanalyzer/clangstaticanalyzerdiagnosticmodel.cpp @@ -272,7 +272,7 @@ QVariant ExplainingStepItem::data(int column, int role) const case ClangStaticAnalyzerDiagnosticModel::DiagnosticRole: return QVariant::fromValue(static_cast(parent())->diagnostic()); case Qt::DisplayRole: { - const int row = parent()->children().indexOf(const_cast(this)) + 1; + const int row = indexInParent() + 1; const int padding = static_cast(std::log10(parent()->childCount())) - static_cast(std::log10(row)); return QString::fromLatin1("%1%2: %3") diff --git a/src/plugins/projectexplorer/projectwindow.cpp b/src/plugins/projectexplorer/projectwindow.cpp index c9e0b5b2953..bec0664a467 100644 --- a/src/plugins/projectexplorer/projectwindow.cpp +++ b/src/plugins/projectexplorer/projectwindow.cpp @@ -361,7 +361,7 @@ void ProjectWindow::deregisterProject(Project *project) void ProjectWindow::startupProjectChanged(Project *project) { if (ProjectItem *projectItem = itemForProject(project)) { - int index = projectItem->parent()->children().indexOf(projectItem); + int index = projectItem->indexInParent(); QTC_ASSERT(index != -1, return); m_projectSelection->setCurrentIndex(index); m_selectorModel->rootItem()->m_currentProjectIndex = index; diff --git a/src/plugins/valgrind/xmlprotocol/errorlistmodel.cpp b/src/plugins/valgrind/xmlprotocol/errorlistmodel.cpp index a6ff62ebdf8..f574f89c243 100644 --- a/src/plugins/valgrind/xmlprotocol/errorlistmodel.cpp +++ b/src/plugins/valgrind/xmlprotocol/errorlistmodel.cpp @@ -278,7 +278,7 @@ QVariant FrameItem::data(int column, int role) const case ErrorListModel::ErrorRole: return QVariant::fromValue(getErrorItem()->error()); case Qt::DisplayRole: { - const int row = parent()->children().indexOf(const_cast(this)) + 1; + const int row = indexInParent() + 1; const int padding = static_cast(std::log10(parent()->childCount())) - static_cast(std::log10(row)); return QString::fromLatin1("%1%2: %3")