#include "ApplicationModel.hpp" #include #include ApplicationModel::ApplicationModel(QObject *parent) : QAbstractListModel(parent) { } int ApplicationModel::rowCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; return m_entries.size(); } int ApplicationModel::count() const { return m_entries.size(); } QVariant ApplicationModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || index.row() < 0 || index.row() >= m_entries.size()) return {}; const DesktopEntry &entry = m_entries.at(index.row()); switch (role) { case DesktopIdRole: case Qt::UserRole: return entry.desktopId; case NameRole: case Qt::DisplayRole: return entry.name; case GenericNameRole: return entry.genericName; case CommentRole: return entry.comment; case IconNameRole: return entry.icon; case CategoriesRole: return entry.categories; case TerminalRole: return entry.terminal; default: return {}; } } QHash ApplicationModel::roleNames() const { return { {DesktopIdRole, QByteArrayLiteral("desktopId")}, {NameRole, QByteArrayLiteral("name")}, {GenericNameRole, QByteArrayLiteral("genericName")}, {CommentRole, QByteArrayLiteral("comment")}, {IconNameRole, QByteArrayLiteral("iconName")}, {CategoriesRole, QByteArrayLiteral("categories")}, {TerminalRole, QByteArrayLiteral("terminal")}, }; } void ApplicationModel::setEntries(QVector entries) { std::sort(entries.begin(), entries.end(), [](const DesktopEntry &left, const DesktopEntry &right) { const int order = QString::localeAwareCompare(left.name, right.name); if (order != 0) return order < 0; return left.desktopId < right.desktopId; }); beginResetModel(); m_entries = std::move(entries); endResetModel(); emit countChanged(); } const DesktopEntry *ApplicationModel::entryAt(int row) const { if (row < 0 || row >= m_entries.size()) return nullptr; return &m_entries.at(row); } const DesktopEntry *ApplicationModel::entryById(const QString &desktopId) const { for (const DesktopEntry &entry : m_entries) { if (entry.desktopId == desktopId) return &entry; } return nullptr; }