refactor(registry): use class enum for index level

This commit is contained in:
Oleg Shparber 2024-06-16 14:43:38 -04:00
parent 3d40ec7d75
commit 4bcc896eaa
2 changed files with 37 additions and 34 deletions

View File

@ -49,20 +49,21 @@ ListModel::~ListModel()
QVariant ListModel::data(const QModelIndex &index, int role) const QVariant ListModel::data(const QModelIndex &index, int role) const
{ {
if (!index.isValid()) if (!index.isValid()) {
return QVariant(); return QVariant();
}
switch (role) { switch (role) {
case Qt::DecorationRole: case Qt::DecorationRole:
switch (indexLevel(index)) { switch (indexLevel(index)) {
case Level::DocsetLevel: case IndexLevel::Docset:
return itemInRow(index.row())->docset->icon(); return itemInRow(index.row())->docset->icon();
case Level::GroupLevel: { case IndexLevel::Group: {
auto docsetItem = static_cast<DocsetItem *>(index.internalPointer()); auto docsetItem = static_cast<DocsetItem *>(index.internalPointer());
const QString symbolType = docsetItem->groups.at(index.row())->symbolType; const QString symbolType = docsetItem->groups.at(index.row())->symbolType;
return docsetItem->docset->symbolTypeIcon(symbolType); return docsetItem->docset->symbolTypeIcon(symbolType);
} }
case Level::SymbolLevel: { case IndexLevel::Symbol: {
auto groupItem = static_cast<GroupItem *>(index.internalPointer()); auto groupItem = static_cast<GroupItem *>(index.internalPointer());
return groupItem->docsetItem->docset->symbolTypeIcon(groupItem->symbolType); return groupItem->docsetItem->docset->symbolTypeIcon(groupItem->symbolType);
} }
@ -71,15 +72,15 @@ QVariant ListModel::data(const QModelIndex &index, int role) const
} }
case Qt::DisplayRole: case Qt::DisplayRole:
switch (indexLevel(index)) { switch (indexLevel(index)) {
case Level::DocsetLevel: case IndexLevel::Docset:
return itemInRow(index.row())->docset->title(); return itemInRow(index.row())->docset->title();
case Level::GroupLevel: { case IndexLevel::Group: {
auto docsetItem = static_cast<DocsetItem *>(index.internalPointer()); auto docsetItem = static_cast<DocsetItem *>(index.internalPointer());
const QString symbolType = docsetItem->groups.at(index.row())->symbolType; const QString symbolType = docsetItem->groups.at(index.row())->symbolType;
return QStringLiteral("%1 (%2)").arg(pluralize(symbolType), return QStringLiteral("%1 (%2)").arg(pluralize(symbolType),
QString::number(docsetItem->docset->symbolCount(symbolType))); QString::number(docsetItem->docset->symbolCount(symbolType)));
} }
case Level::SymbolLevel: { case IndexLevel::Symbol: {
auto groupItem = static_cast<GroupItem *>(index.internalPointer()); auto groupItem = static_cast<GroupItem *>(index.internalPointer());
auto it = groupItem->docsetItem->docset->symbols(groupItem->symbolType).cbegin(); auto it = groupItem->docsetItem->docset->symbols(groupItem->symbolType).cbegin();
std::advance(it, index.row()); std::advance(it, index.row());
@ -90,7 +91,7 @@ QVariant ListModel::data(const QModelIndex &index, int role) const
} }
case Qt::ToolTipRole: case Qt::ToolTipRole:
switch (indexLevel(index)) { switch (indexLevel(index)) {
case Level::DocsetLevel: { case IndexLevel::Docset: {
const auto docset = itemInRow(index.row())->docset; const auto docset = itemInRow(index.row())->docset;
return tr("Version: %1r%2").arg(docset->version()).arg(docset->revision()); 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: case ItemDataRole::UrlRole:
switch (indexLevel(index)) { switch (indexLevel(index)) {
case Level::DocsetLevel: case IndexLevel::Docset:
return itemInRow(index.row())->docset->indexFileUrl(); return itemInRow(index.row())->docset->indexFileUrl();
case Level::SymbolLevel: { case IndexLevel::Symbol: {
auto groupItem = static_cast<GroupItem *>(index.internalPointer()); auto groupItem = static_cast<GroupItem *>(index.internalPointer());
auto it = groupItem->docsetItem->docset->symbols(groupItem->symbolType).cbegin(); auto it = groupItem->docsetItem->docset->symbols(groupItem->symbolType).cbegin();
std::advance(it, index.row()); 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 QModelIndex ListModel::index(int row, int column, const QModelIndex &parent) const
{ {
if (!hasIndex(row, column, parent)) if (!hasIndex(row, column, parent)) {
return {}; return {};
}
switch (indexLevel(parent)) { switch (indexLevel(parent)) {
case Level::RootLevel: case IndexLevel::Root:
return createIndex(row, column); return createIndex(row, column);
case Level::DocsetLevel: case IndexLevel::Docset:
return createIndex(row, column, static_cast<void *>(itemInRow(parent.row()))); return createIndex(row, column, static_cast<void *>(itemInRow(parent.row())));
case Level::GroupLevel: { case IndexLevel::Group: {
auto docsetItem = static_cast<DocsetItem *>(parent.internalPointer()); auto docsetItem = static_cast<DocsetItem *>(parent.internalPointer());
return createIndex(row, column, docsetItem->groups.at(parent.row())); 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 QModelIndex ListModel::parent(const QModelIndex &child) const
{ {
switch (indexLevel(child)) { switch (indexLevel(child)) {
case Level::GroupLevel: { case IndexLevel::Group: {
auto item = static_cast<DocsetItem *>(child.internalPointer()); auto item = static_cast<DocsetItem *>(child.internalPointer());
auto it = std::find_if(m_docsetItems.cbegin(), m_docsetItems.cend(), [item](const auto &pair) { 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<int>(std::distance(m_docsetItems.begin(), it)); const int row = static_cast<int>(std::distance(m_docsetItems.begin(), it));
return createIndex(row, 0); return createIndex(row, 0);
} }
case SymbolLevel: { case IndexLevel::Symbol: {
auto item = static_cast<GroupItem *>(child.internalPointer()); auto item = static_cast<GroupItem *>(child.internalPointer());
return createIndex(item->docsetItem->groups.indexOf(item), 0, item->docsetItem); 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 int ListModel::rowCount(const QModelIndex &parent) const
{ {
if (parent.column() > 0) if (parent.column() > 0) {
return 0; return 0;
}
switch (indexLevel(parent)) { switch (indexLevel(parent)) {
case Level::RootLevel: case IndexLevel::Root:
return static_cast<int>(m_docsetItems.size()); return static_cast<int>(m_docsetItems.size());
case Level::DocsetLevel: case IndexLevel::Docset:
return itemInRow(parent.row())->docset->symbolCounts().count(); return itemInRow(parent.row())->docset->symbolCounts().count();
case Level::GroupLevel: { case IndexLevel::Group: {
auto docsetItem = static_cast<DocsetItem *>(parent.internalPointer()); auto docsetItem = static_cast<DocsetItem *>(parent.internalPointer());
return docsetItem->docset->symbolCount(docsetItem->groups.at(parent.row())->symbolType); 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")); 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()) { if (!index.isValid()) {
return Level::RootLevel; return IndexLevel::Root;
} }
if (!index.internalPointer()) { if (!index.internalPointer()) {
return Level::DocsetLevel; return IndexLevel::Docset;
} }
if (*static_cast<Level *>(index.internalPointer()) == Level::DocsetLevel) { if (*static_cast<IndexLevel *>(index.internalPointer()) == IndexLevel::Docset) {
return Level::GroupLevel; return IndexLevel::Group;
} }
return Level::SymbolLevel; return IndexLevel::Symbol;
} }
ListModel::DocsetItem *ListModel::itemInRow(int row) const ListModel::DocsetItem *ListModel::itemInRow(int row) const

View File

@ -54,29 +54,29 @@ private slots:
private: private:
friend class DocsetRegistry; friend class DocsetRegistry;
enum Level { enum class IndexLevel {
RootLevel, Root,
DocsetLevel, Docset,
GroupLevel, Group,
SymbolLevel Symbol
}; };
explicit ListModel(DocsetRegistry *docsetRegistry); explicit ListModel(DocsetRegistry *docsetRegistry);
inline static QString pluralize(const QString &s); 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; DocsetRegistry *m_docsetRegistry = nullptr;
struct DocsetItem; struct DocsetItem;
struct GroupItem { struct GroupItem {
const Level level = Level::GroupLevel; const IndexLevel level = IndexLevel::Group;
DocsetItem *docsetItem = nullptr; DocsetItem *docsetItem = nullptr;
QString symbolType; QString symbolType;
}; };
struct DocsetItem { struct DocsetItem {
const Level level = Level::DocsetLevel; const IndexLevel level = IndexLevel::Docset;
Docset *docset = nullptr; Docset *docset = nullptr;
QList<GroupItem *> groups; QList<GroupItem *> groups;
}; };