File indexing completed on 2024-04-14 03:51:13

0001 /*
0002     SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #ifndef KCOMMANDBARMODEL_H
0007 #define KCOMMANDBARMODEL_H
0008 
0009 #include "kcommandbar.h"
0010 
0011 #include <QAbstractTableModel>
0012 #include <QList>
0013 
0014 class QAction;
0015 
0016 class KCommandBarModel final : public QAbstractTableModel
0017 {
0018     Q_OBJECT
0019 public:
0020     struct Item {
0021         QString displayName() const;
0022 
0023         QString groupName;
0024         QAction *action;
0025         int score;
0026     };
0027 
0028     KCommandBarModel(QObject *parent = nullptr);
0029 
0030     enum Role { Score = Qt::UserRole + 1 };
0031 
0032     enum Column { Column_Command, Column_Shortcut, Column_Count };
0033 
0034     /**
0035      * Resets the model
0036      *
0037      * If you are using last Used actions functionality, make sure
0038      * to set the last used actions before calling this function
0039      */
0040     void refresh(const QList<KCommandBar::ActionGroup> &actionGroups);
0041 
0042     int rowCount(const QModelIndex &parent = QModelIndex()) const override
0043     {
0044         if (parent.isValid()) {
0045             return 0;
0046         }
0047         return m_rows.size();
0048     }
0049 
0050     int columnCount(const QModelIndex &parent = QModelIndex()) const override
0051     {
0052         Q_UNUSED(parent);
0053         return Column_Count;
0054     }
0055 
0056     /**
0057      * reimplemented function to update score that is calculated by KFuzzyMatcher
0058      */
0059     bool setData(const QModelIndex &index, const QVariant &value, int role) override
0060     {
0061         if (!index.isValid())
0062             return false;
0063         if (role == Role::Score) {
0064             auto row = index.row();
0065             m_rows[row].score = value.toInt();
0066         }
0067         return QAbstractTableModel::setData(index, value, role);
0068     }
0069 
0070     QVariant data(const QModelIndex &index, int role) const override;
0071 
0072     /**
0073      * action with name @p name was triggered, store it in m_lastTriggered
0074      */
0075     void actionTriggered(const QString &name);
0076 
0077     /**
0078      * last used actions
0079      * max = 6;
0080      */
0081     QStringList lastUsedActions() const;
0082 
0083     /**
0084      * incoming lastUsedActions
0085      *
0086      * should be set before calling refresh()
0087      */
0088     void setLastUsedActions(const QStringList &actionNames);
0089 
0090 private:
0091     QList<Item> m_rows;
0092 
0093     /**
0094      * Last triggered actions by user
0095      *
0096      * Ordered descending i.e., least recently invoked
0097      * action is at the end
0098      */
0099     QStringList m_lastTriggered;
0100 
0101     QAction *m_clearHistoryAction;
0102 };
0103 
0104 Q_DECLARE_TYPEINFO(KCommandBarModel::Item, Q_RELOCATABLE_TYPE);
0105 
0106 #endif // KCOMMANDBARMODEL_H