1/*
2 SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6#ifndef KCOMMANDBARMODEL_H
7#define KCOMMANDBARMODEL_H
8
9#include "kcommandbar.h"
10
11#include <QAbstractTableModel>
12#include <QList>
13
14class QAction;
15
16class KCommandBarModel final : public QAbstractTableModel
17{
18 Q_OBJECT
19public:
20 struct Item {
21 QString displayName() const;
22
23 QString groupName;
24 QAction *action;
25 int score;
26 };
27
28 KCommandBarModel(QObject *parent = nullptr);
29
30 enum Role { Score = Qt::UserRole + 1 };
31
32 enum Column { Column_Command, Column_Shortcut, Column_Count };
33
34 /**
35 * Resets the model
36 *
37 * If you are using last Used actions functionality, make sure
38 * to set the last used actions before calling this function
39 */
40 void refresh(const QList<KCommandBar::ActionGroup> &actionGroups);
41
42 int rowCount(const QModelIndex &parent = QModelIndex()) const override
43 {
44 if (parent.isValid()) {
45 return 0;
46 }
47 return m_rows.size();
48 }
49
50 int columnCount(const QModelIndex &parent = QModelIndex()) const override
51 {
52 Q_UNUSED(parent);
53 return Column_Count;
54 }
55
56 /**
57 * reimplemented function to update score that is calculated by KFuzzyMatcher
58 */
59 bool setData(const QModelIndex &index, const QVariant &value, int role) override
60 {
61 if (!index.isValid())
62 return false;
63 if (role == Role::Score) {
64 auto row = index.row();
65 m_rows[row].score = value.toInt();
66 }
67 return QAbstractTableModel::setData(index, value, role);
68 }
69
70 QVariant data(const QModelIndex &index, int role) const override;
71
72 /**
73 * action with name @p name was triggered, store it in m_lastTriggered
74 */
75 void actionTriggered(const QString &name);
76
77 /**
78 * last used actions
79 * max = 6;
80 */
81 QStringList lastUsedActions() const;
82
83 /**
84 * incoming lastUsedActions
85 *
86 * should be set before calling refresh()
87 */
88 void setLastUsedActions(const QStringList &actionNames);
89
90private:
91 QList<Item> m_rows;
92
93 /**
94 * Last triggered actions by user
95 *
96 * Ordered descending i.e., least recently invoked
97 * action is at the end
98 */
99 QStringList m_lastTriggered;
100
101 QAction *m_clearHistoryAction;
102};
103
104Q_DECLARE_TYPEINFO(KCommandBarModel::Item, Q_RELOCATABLE_TYPE);
105
106#endif // KCOMMANDBARMODEL_H
107

source code of kconfigwidgets/src/kcommandbarmodel_p.h