File indexing completed on 2024-05-12 16:02:26

0001 /*
0002     SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "commandmodel.h"
0007 
0008 #include <KLocalizedString>
0009 #include <QAction>
0010 #include <QDebug>
0011 
0012 CommandModel::CommandModel(QObject *parent)
0013     : QAbstractTableModel(parent)
0014 {
0015 }
0016 
0017 void CommandModel::refresh(QVector<QPair<QString, QAction *>> actionList)
0018 {
0019     QVector<Item> temp;
0020     temp.reserve(actionList.size());
0021     for (auto action : actionList) {
0022         temp.push_back({action.first, action.second, 0});
0023     }
0024 
0025     beginResetModel();
0026     m_rows = std::move(temp);
0027     endResetModel();
0028 }
0029 
0030 QVariant CommandModel::data(const QModelIndex &index, int role) const
0031 {
0032     if (!index.isValid()) {
0033         return {};
0034     };
0035 
0036     auto entry = m_rows[index.row()];
0037     int col = index.column();
0038 
0039     switch (role) {
0040     case Qt::DisplayRole:
0041         if (col == 0) {
0042             return QString(entry.component + QStringLiteral(": ") + KLocalizedString::removeAcceleratorMarker(entry.action->text()));
0043         } else {
0044             return entry.action->shortcut().toString();
0045         }
0046     case Qt::DecorationRole:
0047         if (col == 0) {
0048             return entry.action->icon();
0049         }
0050         break;
0051     case Qt::TextAlignmentRole:
0052         if (col == 0) {
0053             return Qt::AlignLeft;
0054         } else {
0055             return Qt::AlignRight;
0056         }
0057     case Qt::UserRole: {
0058         QVariant v;
0059         v.setValue(entry.action);
0060         return v;
0061     }
0062     case Role::Score:
0063         return entry.score;
0064     }
0065 
0066     return {};
0067 }