Kirigami-addons

actionsmodel.cpp
1// SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com>
2// SPDX-License-Identifier: LGPL-2.0-or-later
3
4#include "actionsmodel_p.h"
5
6#include <KLocalizedString>
7
8#include <QAction>
9
10#include <unordered_set>
11
12KCommandBarModel::KCommandBarModel(QObject *parent)
13 : QAbstractTableModel(parent)
14{
15}
16
17void fillRows(QList<KCommandBarModel::Item> &rows, const QString &title, const QList<QAction *> &actions, std::unordered_set<QAction *> &uniqueActions)
18{
19 for (const auto &action : actions) {
20 // We don't want disabled actions
21 if (!action->isEnabled()) {
22 continue;
23 }
24
25 if (uniqueActions.insert(action).second) {
26 rows.push_back(KCommandBarModel::Item{title, action, -1});
27 }
28 }
29}
30
31void KCommandBarModel::refresh(const QList<ActionGroup> &actionGroups)
32{
33 int totalActions = std::accumulate(actionGroups.begin(), actionGroups.end(), 0, [](int a, const ActionGroup &ag) {
34 return a + ag.actions.count();
35 });
36
37 QList<Item> temp_rows;
38 std::unordered_set<QAction *> uniqueActions;
39 temp_rows.reserve(totalActions);
40 int actionGroupIdx = 0;
41 for (const auto &ag : actionGroups) {
42 const auto &agActions = ag.actions;
43 fillRows(temp_rows, ag.name, agActions, uniqueActions);
44
45 actionGroupIdx++;
46 }
47
48 /**
49 * For each action in last triggered actions,
50 * - Find it in the actions
51 * - Use the score variable to set its score
52 *
53 * Items in m_lastTriggered are stored in descending order
54 * by their usage i.e., the first item in the vector is the most
55 * recently invoked action.
56 *
57 * Here we traverse them in reverse order, i.e., from least recent to
58 * most recent and then assign a score to them in a way that most recent
59 * ends up having the highest score. Thus when proxy model does the sorting
60 * later, most recent item will end up on the top
61 */
62 int score = 0;
63 std::for_each(m_lastTriggered.crbegin(), m_lastTriggered.crend(), [&score, &temp_rows](const QString &act) {
64 auto it = std::find_if(temp_rows.begin(), temp_rows.end(), [act](const KCommandBarModel::Item &i) {
65 return i.action->text() == act;
66 });
67 if (it != temp_rows.end()) {
68 it->score = score++;
69 }
70 });
71
72 beginResetModel();
73 m_rows = std::move(temp_rows);
74 endResetModel();
75}
76
77QVariant KCommandBarModel::data(const QModelIndex &index, int role) const
78{
79 if (!index.isValid()) {
80 return {};
81 }
82
83 const auto &entry = m_rows[index.row()];
84 const int col = index.column();
85
86 switch (role) {
87 case Qt::DisplayRole:
88 case DisplayNameRole:
89 if (col == 0) {
90 QString groupName = KLocalizedString::removeAcceleratorMarker(entry.groupName);
91 QString actionText = KLocalizedString::removeAcceleratorMarker(entry.action->text());
92 return QString(groupName + QStringLiteral(": ") + actionText);
93 } else {
94 return entry.action->shortcut().toString(QKeySequence::NativeText);
95 }
96 case ShortcutRole:
97 return entry.action->shortcut().toString(QKeySequence::NativeText);
99 if (col == 0) {
100 return entry.action->icon().name();
101 }
102 break;
104 if (col == 0) {
105 return Qt::AlignLeft;
106 } else {
107 return Qt::AlignRight;
108 }
109 case Qt::UserRole: {
110 return QVariant::fromValue(entry.action);
111 }
112 case Role::Score:
113 return entry.score;
114 }
115
116 return {};
117}
118
119void KCommandBarModel::actionTriggered(const QString &name)
120{
121 if (m_lastTriggered.size() == 6) {
122 m_lastTriggered.pop_back();
123 }
124 m_lastTriggered.push_front(name);
125}
126
127QStringList KCommandBarModel::lastUsedActions() const
128{
129 return m_lastTriggered;
130}
131
132void KCommandBarModel::setLastUsedActions(const QStringList &actionNames)
133{
134 m_lastTriggered = actionNames;
135
136 while (m_lastTriggered.size() > 6) {
137 m_lastTriggered.pop_back();
138 }
139}
140
141QHash<int, QByteArray> KCommandBarModel::roleNames() const
142{
143 auto roles = QAbstractTableModel::roleNames();
144 roles[Qt::UserRole] = QByteArrayLiteral("qaction");
145 roles[Score] = QByteArrayLiteral("score");
146 roles[ShortcutRole] = QByteArrayLiteral("shortcut");
147 roles[DisplayNameRole] = QByteArrayLiteral("displayName");
148 return roles;
149}
150
151#include "moc_actionsmodel_p.cpp"
static QString removeAcceleratorMarker(const QString &label)
virtual QHash< int, QByteArray > roleNames() const const
iterator begin()
iterator end()
void pop_back()
void push_back(parameter_type value)
void reserve(qsizetype size)
int column() const const
bool isValid() const const
int row() const const
AlignLeft
DisplayRole
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:54:39 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.