From 4bcc896eaad4f35ea8a9e4d033971eecad2d340c Mon Sep 17 00:00:00 2001 From: Oleg Shparber Date: Sun, 16 Jun 2024 14:43:38 -0400 Subject: [PATCH] refactor(registry): use class enum for index level --- src/libs/registry/listmodel.cpp | 55 +++++++++++++++++---------------- src/libs/registry/listmodel.h | 16 +++++----- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/libs/registry/listmodel.cpp b/src/libs/registry/listmodel.cpp index 71fc732..8644158 100644 --- a/src/libs/registry/listmodel.cpp +++ b/src/libs/registry/listmodel.cpp @@ -49,20 +49,21 @@ ListModel::~ListModel() QVariant ListModel::data(const QModelIndex &index, int role) const { - if (!index.isValid()) + if (!index.isValid()) { return QVariant(); + } switch (role) { case Qt::DecorationRole: switch (indexLevel(index)) { - case Level::DocsetLevel: + case IndexLevel::Docset: return itemInRow(index.row())->docset->icon(); - case Level::GroupLevel: { + case IndexLevel::Group: { auto docsetItem = static_cast(index.internalPointer()); const QString symbolType = docsetItem->groups.at(index.row())->symbolType; return docsetItem->docset->symbolTypeIcon(symbolType); } - case Level::SymbolLevel: { + case IndexLevel::Symbol: { auto groupItem = static_cast(index.internalPointer()); return groupItem->docsetItem->docset->symbolTypeIcon(groupItem->symbolType); } @@ -71,15 +72,15 @@ QVariant ListModel::data(const QModelIndex &index, int role) const } case Qt::DisplayRole: switch (indexLevel(index)) { - case Level::DocsetLevel: + case IndexLevel::Docset: return itemInRow(index.row())->docset->title(); - case Level::GroupLevel: { + case IndexLevel::Group: { auto docsetItem = static_cast(index.internalPointer()); const QString symbolType = docsetItem->groups.at(index.row())->symbolType; return QStringLiteral("%1 (%2)").arg(pluralize(symbolType), QString::number(docsetItem->docset->symbolCount(symbolType))); } - case Level::SymbolLevel: { + case IndexLevel::Symbol: { auto groupItem = static_cast(index.internalPointer()); auto it = groupItem->docsetItem->docset->symbols(groupItem->symbolType).cbegin(); std::advance(it, index.row()); @@ -90,7 +91,7 @@ QVariant ListModel::data(const QModelIndex &index, int role) const } case Qt::ToolTipRole: switch (indexLevel(index)) { - case Level::DocsetLevel: { + case IndexLevel::Docset: { const auto docset = itemInRow(index.row())->docset; return tr("Version: %1r%2").arg(docset->version()).arg(docset->revision()); } @@ -99,9 +100,9 @@ QVariant ListModel::data(const QModelIndex &index, int role) const } case ItemDataRole::UrlRole: switch (indexLevel(index)) { - case Level::DocsetLevel: + case IndexLevel::Docset: return itemInRow(index.row())->docset->indexFileUrl(); - case Level::SymbolLevel: { + case IndexLevel::Symbol: { auto groupItem = static_cast(index.internalPointer()); auto it = groupItem->docsetItem->docset->symbols(groupItem->symbolType).cbegin(); std::advance(it, index.row()); @@ -125,15 +126,16 @@ QVariant ListModel::data(const QModelIndex &index, int role) const QModelIndex ListModel::index(int row, int column, const QModelIndex &parent) const { - if (!hasIndex(row, column, parent)) + if (!hasIndex(row, column, parent)) { return {}; + } switch (indexLevel(parent)) { - case Level::RootLevel: + case IndexLevel::Root: return createIndex(row, column); - case Level::DocsetLevel: + case IndexLevel::Docset: return createIndex(row, column, static_cast(itemInRow(parent.row()))); - case Level::GroupLevel: { + case IndexLevel::Group: { auto docsetItem = static_cast(parent.internalPointer()); return createIndex(row, column, docsetItem->groups.at(parent.row())); } @@ -145,7 +147,7 @@ QModelIndex ListModel::index(int row, int column, const QModelIndex &parent) con QModelIndex ListModel::parent(const QModelIndex &child) const { switch (indexLevel(child)) { - case Level::GroupLevel: { + case IndexLevel::Group: { auto item = static_cast(child.internalPointer()); auto it = std::find_if(m_docsetItems.cbegin(), m_docsetItems.cend(), [item](const auto &pair) { @@ -160,7 +162,7 @@ QModelIndex ListModel::parent(const QModelIndex &child) const const int row = static_cast(std::distance(m_docsetItems.begin(), it)); return createIndex(row, 0); } - case SymbolLevel: { + case IndexLevel::Symbol: { auto item = static_cast(child.internalPointer()); return createIndex(item->docsetItem->groups.indexOf(item), 0, item->docsetItem); } @@ -177,15 +179,16 @@ int ListModel::columnCount(const QModelIndex &parent) const int ListModel::rowCount(const QModelIndex &parent) const { - if (parent.column() > 0) + if (parent.column() > 0) { return 0; + } switch (indexLevel(parent)) { - case Level::RootLevel: + case IndexLevel::Root: return static_cast(m_docsetItems.size()); - case Level::DocsetLevel: + case IndexLevel::Docset: return itemInRow(parent.row())->docset->symbolCounts().count(); - case Level::GroupLevel: { + case IndexLevel::Group: { auto docsetItem = static_cast(parent.internalPointer()); return docsetItem->docset->symbolCount(docsetItem->groups.at(parent.row())->symbolType); } @@ -242,21 +245,21 @@ QString ListModel::pluralize(const QString &s) return s + (s.endsWith('s') ? QLatin1String("es") : QLatin1String("s")); } -ListModel::Level ListModel::indexLevel(const QModelIndex &index) +ListModel::IndexLevel ListModel::indexLevel(const QModelIndex &index) { if (!index.isValid()) { - return Level::RootLevel; + return IndexLevel::Root; } if (!index.internalPointer()) { - return Level::DocsetLevel; + return IndexLevel::Docset; } - if (*static_cast(index.internalPointer()) == Level::DocsetLevel) { - return Level::GroupLevel; + if (*static_cast(index.internalPointer()) == IndexLevel::Docset) { + return IndexLevel::Group; } - return Level::SymbolLevel; + return IndexLevel::Symbol; } ListModel::DocsetItem *ListModel::itemInRow(int row) const diff --git a/src/libs/registry/listmodel.h b/src/libs/registry/listmodel.h index 482ca06..ff8686b 100644 --- a/src/libs/registry/listmodel.h +++ b/src/libs/registry/listmodel.h @@ -54,29 +54,29 @@ private slots: private: friend class DocsetRegistry; - enum Level { - RootLevel, - DocsetLevel, - GroupLevel, - SymbolLevel + enum class IndexLevel { + Root, + Docset, + Group, + Symbol }; explicit ListModel(DocsetRegistry *docsetRegistry); inline static QString pluralize(const QString &s); - inline static Level indexLevel(const QModelIndex &index); + inline static IndexLevel indexLevel(const QModelIndex &index); DocsetRegistry *m_docsetRegistry = nullptr; struct DocsetItem; struct GroupItem { - const Level level = Level::GroupLevel; + const IndexLevel level = IndexLevel::Group; DocsetItem *docsetItem = nullptr; QString symbolType; }; struct DocsetItem { - const Level level = Level::DocsetLevel; + const IndexLevel level = IndexLevel::Docset; Docset *docset = nullptr; QList groups; };