Skip to content

Commit

Permalink
refactor(registry): use class enum for index level
Browse files Browse the repository at this point in the history
  • Loading branch information
trollixx committed Jun 16, 2024
1 parent 3d40ec7 commit 4bcc896
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
55 changes: 29 additions & 26 deletions src/libs/registry/listmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocsetItem *>(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<GroupItem *>(index.internalPointer());
return groupItem->docsetItem->docset->symbolTypeIcon(groupItem->symbolType);
}
Expand All @@ -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<DocsetItem *>(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<GroupItem *>(index.internalPointer());
auto it = groupItem->docsetItem->docset->symbols(groupItem->symbolType).cbegin();
std::advance(it, index.row());
Expand All @@ -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());
}
Expand All @@ -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<GroupItem *>(index.internalPointer());
auto it = groupItem->docsetItem->docset->symbols(groupItem->symbolType).cbegin();
std::advance(it, index.row());
Expand All @@ -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<void *>(itemInRow(parent.row())));
case Level::GroupLevel: {
case IndexLevel::Group: {
auto docsetItem = static_cast<DocsetItem *>(parent.internalPointer());
return createIndex(row, column, docsetItem->groups.at(parent.row()));
}
Expand All @@ -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<DocsetItem *>(child.internalPointer());

auto it = std::find_if(m_docsetItems.cbegin(), m_docsetItems.cend(), [item](const auto &pair) {
Expand All @@ -160,7 +162,7 @@ QModelIndex ListModel::parent(const QModelIndex &child) const
const int row = static_cast<int>(std::distance(m_docsetItems.begin(), it));
return createIndex(row, 0);
}
case SymbolLevel: {
case IndexLevel::Symbol: {
auto item = static_cast<GroupItem *>(child.internalPointer());
return createIndex(item->docsetItem->groups.indexOf(item), 0, item->docsetItem);
}
Expand All @@ -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<int>(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<DocsetItem *>(parent.internalPointer());
return docsetItem->docset->symbolCount(docsetItem->groups.at(parent.row())->symbolType);
}
Expand Down Expand Up @@ -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<Level *>(index.internalPointer()) == Level::DocsetLevel) {
return Level::GroupLevel;
if (*static_cast<IndexLevel *>(index.internalPointer()) == IndexLevel::Docset) {
return IndexLevel::Group;
}

return Level::SymbolLevel;
return IndexLevel::Symbol;
}

ListModel::DocsetItem *ListModel::itemInRow(int row) const
Expand Down
16 changes: 8 additions & 8 deletions src/libs/registry/listmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<GroupItem *> groups;
};
Expand Down

0 comments on commit 4bcc896

Please sign in to comment.