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 #ifndef COMMANDMODEL_H
0007 #define COMMANDMODEL_H
0008 
0009 #include <QAbstractTableModel>
0010 #include <QVector>
0011 
0012 class QAction;
0013 
0014 class CommandModel : public QAbstractTableModel
0015 {
0016     struct Item {
0017         QString component;
0018         QAction *action;
0019         int score;
0020     };
0021 
0022     Q_OBJECT
0023 public:
0024     CommandModel(QObject *parent = nullptr);
0025 
0026     enum Role { Score = Qt::UserRole + 1 };
0027 
0028     void refresh(QVector<QPair<QString, QAction *>> actionList);
0029 
0030     int rowCount(const QModelIndex &parent = QModelIndex()) const override
0031     {
0032         if (parent.isValid()) {
0033             return 0;
0034         }
0035         return m_rows.size();
0036     }
0037 
0038     int columnCount(const QModelIndex &parent = QModelIndex()) const override
0039     {
0040         Q_UNUSED(parent);
0041         return 2;
0042     }
0043 
0044     bool setData(const QModelIndex &index, const QVariant &value, int role) override
0045     {
0046         if (!index.isValid()) {
0047             return false;
0048         }
0049         if (role == Role::Score) {
0050             auto row = index.row();
0051             m_rows[row].score = value.toInt();
0052         }
0053         return QAbstractTableModel::setData(index, value, role);
0054     }
0055 
0056     QVariant data(const QModelIndex &index, int role) const override;
0057 
0058 private:
0059     QVector<Item> m_rows;
0060 };
0061 
0062 #endif // COMMANDMODEL_H