4#include "shortcutsmodel_p.h"
7#include <unordered_set>
8#include <KLocalizedString>
10#include "kirigamiactioncollection.h"
12ShortcutsModel::ShortcutsModel(
QObject *parent)
17int ShortcutsModel::rowCount(
const QModelIndex &parent)
const
19 return parent.
isValid() ? 0 : m_items.count();
24 Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid));
26 const auto &item = m_items[index.
row()];
30 return item.action->text();
32 return item.action->icon().name();
34 return item.action->shortcut();
35 case ShortcutDisplayRole: {
37 const auto shortcuts = item.action->shortcuts();
38 for (
const auto &shortcut : shortcuts) {
41 return displayText.
join(
i18nc(
"List separator",
", "));
43 case DefaultShortcutRole:
45 case AlternateShortcutsRole:
46 if (item.action->shortcuts().size() <= 1) {
51 case CollectionNameRole:
52 return item.collection->componentDisplayName();
62 { IconNameRole,
"iconName" },
63 { DefaultShortcutRole,
"defaultShortcut" },
64 { ShortcutRole,
"shortcut" },
65 { ShortcutDisplayRole,
"shortcutDisplay" },
66 { AlternateShortcutsRole,
"alternateShortcuts" },
67 { CollectionNameRole,
"collectionName" },
73 m_collections = actionCollections;
75 return a + collection->actions().count();
79 std::unordered_set<QAction *> uniqueActions;
80 temp_rows.
reserve(totalActions);
81 for (
const auto &collection : actionCollections) {
82 for (
const auto action : collection->actions()) {
83 if (collection->isShortcutsConfigurable(action)) {
84 temp_rows.
push_back(ShortcutsModel::Item{collection, action});
90 m_items = std::move(temp_rows);
96 Q_ASSERT(row >= 0 && row < rowCount());
98 const auto &item = m_items[row];
99 auto oldShortcuts = item.action->shortcuts();
101 if (shortcutIndex != oldShortcuts.count()) {
102 oldShortcuts.remove(shortcutIndex);
105 if (shortcutIndex == oldShortcuts.count()) {
112 item.action->setShortcuts(oldShortcuts);
114 Q_EMIT dataChanged(index(row), index(row));
116 if (item.action->shortcuts().size() <= 1) {
119 return item.action->shortcuts().mid(1);
123void ShortcutsModel::resetAll()
125 for (
int i = 0, count = rowCount(); i < count; i++) {
130void ShortcutsModel::save()
132 for (
const auto *collection : std::as_const(m_collections)) {
133 collection->writeSettings(
nullptr);
139 Q_ASSERT(row >= 0 && row < rowCount());
141 const auto &item = m_items[row];
145 if (item.action->shortcuts() != defaultShortcuts) {
146 item.action->setShortcuts(defaultShortcuts);
149 Q_EMIT dataChanged(index(row), index(row));
151 if (item.action->shortcuts().size() <= 1) {
154 return item.action->shortcuts().mid(1);
A container for a set of QAction objects.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
const QList< QKeySequence > & shortcut(StandardShortcut id)
void push_back(parameter_type value)
void reserve(qsizetype size)
T value(qsizetype i) const const
bool isValid() const const
QString join(QChar separator) const const
void keySequence(QWidget *widget, const QKeySequence &keySequence)
QVariant fromValue(T &&value)