MauiKit Calendar

actionsmodel.h
1// SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com>
2// SPDX-License-Identifier: LGPL-2.0-or-later
3
4#pragma once
5
6#include <QAbstractTableModel>
7#include <QVector>
8
9class QAction;
10
11class KalCommandBarModel final : public QAbstractTableModel
12{
14public:
15 struct Item {
16 QString groupName;
17 QAction *action = nullptr;
18 int score;
19 };
20
21 /**
22 * Represents a list of action that belong to the same group.
23 * For example:
24 * - All actions under the menu "File" or "Tool"
25 */
26 struct ActionGroup {
27 QString name;
28 QList<QAction *> actions;
29 };
30
31 explicit KalCommandBarModel(QObject *parent = nullptr);
32
33 enum Role {
34 Score = Qt::UserRole + 1,
35 ShortcutRole,
36 };
37
38 /**
39 * Resets the model
40 *
41 * If you are using last Used actions functionality, make sure
42 * to set the last used actions before calling this function
43 */
44 void refresh(const QVector<ActionGroup> &actionGroups);
45
46 int rowCount(const QModelIndex &parent = QModelIndex()) const override
47 {
48 if (parent.isValid()) {
49 return 0;
50 }
51 return m_rows.size();
52 }
53
54 int columnCount(const QModelIndex &parent = QModelIndex()) const override
55 {
56 Q_UNUSED(parent);
57 return 2;
58 }
59
60 /**
61 * reimplemented function to update score that is calculated by KFuzzyMatcher
62 */
63 bool setData(const QModelIndex &index, const QVariant &value, int role) override
64 {
65 if (!index.isValid())
66 return false;
67 if (role == Role::Score) {
68 auto row = index.row();
69 m_rows[row].score = value.toInt();
70 }
71 return QAbstractTableModel::setData(index, value, role);
72 }
73
74 QVariant data(const QModelIndex &index, int role) const override;
75
76 /**
77 * action with name @p name was triggered, store it in m_lastTriggered
78 */
79 void actionTriggered(const QString &name);
80
81 /**
82 * last used actions
83 * max = 6;
84 */
85 QStringList lastUsedActions() const;
86
87 /**
88 * incoming lastUsedActions
89 *
90 * should be set before calling refresh()
91 */
92 void setLastUsedActions(const QStringList &actionNames);
93
94 QHash<int, QByteArray> roleNames() const override;
95
96private:
97 QVector<Item> m_rows;
98
99 /**
100 * Last triggered actions by user
101 *
102 * Ordered descending i.e., least recently invoked
103 * action is at the end
104 */
105 QStringList m_lastTriggered;
106};
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
bool isValid() const const
int row() const const
Q_OBJECTQ_OBJECT
QObject * parent() const const
UserRole
int toInt(bool *ok) const const
Represents a list of action that belong to the same group.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:35:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.