Krita Source Code Documentation
Loading...
Searching...
No Matches
KisShortcutsEditorItem.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries SPDX-FileCopyrightText: 1998 Mark Donohoe <donohoe@kde.org>
2 SPDX-FileCopyrightText: 1997 Nicolas Hadacek <hadacek@kde.org>
3 SPDX-FileCopyrightText: 1998 Matthias Ettrich <ettrich@kde.org>
4 SPDX-FileCopyrightText: 2001 Ellis Whitehead <ellis@kde.org>
5 SPDX-FileCopyrightText: 2006 Hamish Rodda <rodda@kde.org>
6 SPDX-FileCopyrightText: 2007 Roberto Raggi <roberto@kdevelop.org>
7 SPDX-FileCopyrightText: 2007 Andreas Hartmetz <ahartmetz@gmail.com>
8 SPDX-FileCopyrightText: 2008 Michael Jansen <kde@michael-jansen.biz>
9
10 SPDX-License-Identifier: LGPL-2.0-or-later
11*/
12
15
16#include <QAction>
17#include <QTreeWidgetItem>
18#include <kis_debug.h>
19
20
21KisShortcutsEditorItem::KisShortcutsEditorItem(QTreeWidgetItem *parent, QAction *action)
22 : QTreeWidgetItem(parent, ActionItem)
23 , m_action(action)
24{
25 // Filtering message requested by translators (scripting).
26 m_id = m_action->objectName();
27 m_actionNameInTable = i18nc("@item:intable Action name in shortcuts configuration",
28 "%1", KLocalizedString::removeAcceleratorMarker(m_action->text()));
29 if (m_actionNameInTable.isEmpty()) {
30 warnKrita << "Action without text!" << m_action->objectName();
32 }
33
34 m_collator.setNumericMode(true);
35 m_collator.setCaseSensitivity(Qt::CaseSensitive);
36
37 // qDebug() << "Adding new action" << m_id << "with shortcut" << keySequence(LocalPrimary).toString();
38}
39
44
49
50QVariant KisShortcutsEditorItem::data(int column, int role) const
51{
52 switch (role) {
53 case Qt::DisplayRole:
54 switch (column) {
55 case Name:
57 case Id:
58 return m_id;
59 case LocalPrimary:
60 case LocalAlternate:
61 return QVariant::fromValue(keySequence(column));
62 default:
63 break;
64 }
65 break;
66 case Qt::DecorationRole:
67 if (column == Name) {
68 return m_action->icon();
69 } else {
70 return QIcon();
71 }
72 break;
73 case Qt::WhatsThisRole:
74 return m_action->whatsThis();
75 case Qt::ToolTipRole:
76 // TODO: show command descriptions/tooltips in the shortcut editor
77 return QVariant();
78 case Qt::FontRole:
79 if (column == Name && m_isNameBold) {
80 QFont modifiedFont = treeWidget()->font();
81 modifiedFont.setBold(true);
82 return modifiedFont;
83 }
84 break;
85 case KExtendableItemDelegate::ShowExtensionIndicatorRole:
86 switch (column) {
87 case Name:
88 return false;
89 case LocalPrimary:
90 case LocalAlternate:
91 return !m_action->property("isShortcutConfigurable").isValid()
92 || m_action->property("isShortcutConfigurable").toBool();
93 default:
94 return false;
95 }
96 //the following are custom roles, defined in this source file only
97 case ShortcutRole:
98 switch (column) {
99 case LocalPrimary:
100 case LocalAlternate:
101 return QVariant::fromValue(keySequence(column));
102 default:
103 // Column not valid for this role
104 Q_ASSERT(false);
105 return QVariant();
106 }
107
108 case DefaultShortcutRole: {
109
110 // Note: we are using the QMetaObject system to store this property.
111 QList<QKeySequence> defaultShortcuts = m_action->property("defaultShortcuts").value<QList<QKeySequence> >();
112
113 switch (column) {
114 case LocalPrimary:
115 return QVariant::fromValue(primarySequence(defaultShortcuts));
116 case LocalAlternate:
117 return QVariant::fromValue(alternateSequence(defaultShortcuts));
118 default:
119 // Column not valid for this role
120 Q_ASSERT(false);
121 return QVariant();
122 }
123 }
124 case ObjectRole:
125 return QVariant::fromValue((QObject *)m_action);
126
127 default:
128 break;
129 }
130
131 return QVariant();
132}
133
134bool KisShortcutsEditorItem::operator<(const QTreeWidgetItem &other) const
135{
136 const int column = treeWidget() ? treeWidget()->sortColumn() : 0;
137 return m_collator.compare(text(column), other.text(column)) < 0;
138}
139
141{
142 QList<QKeySequence> shortcuts = m_action->shortcuts();
143
144 switch (column) {
145 case LocalPrimary:
146 return primarySequence(shortcuts);
147 case LocalAlternate:
148 return alternateSequence(shortcuts);
149 default:
150 return QKeySequence();
151 }
152}
153
154void KisShortcutsEditorItem::setKeySequence(uint column, const QKeySequence &seq)
155{
157 ks = m_action->shortcuts();
158 if (!m_oldLocalShortcut) {
160 }
161
162 if (column == LocalAlternate) {
163 if (ks.isEmpty()) {
164 ks << QKeySequence();
165 }
166
167 if (ks.size() <= 1) {
168 ks << seq;
169 } else {
170 ks[1] = seq;
171 }
172 } else {
173 if (ks.isEmpty()) {
174 ks << seq;
175 } else {
176 ks[0] = seq;
177 }
178 }
179
180 m_action->setShortcuts(ks);
181
183}
184
185//our definition of modified is "modified since the chooser was shown".
193
195{
196 switch (column) {
197 case Name:
198 return false;
199 case LocalPrimary:
200 case LocalAlternate:
201 if (!m_oldLocalShortcut) {
202 return false;
203 }
204 if (column == LocalPrimary) {
206 } else {
208 }
209 default:
210 return false;
211 }
212}
213
215{
216 //dbgKrita << "Undoing changes for " << data(Name, Qt::DisplayRole).toString();
217
218 if (m_oldLocalShortcut) {
219 // We only ever reset the active Shortcut
220 m_action->setShortcuts(*m_oldLocalShortcut);
221 }
222
224}
225
227{
228 if (m_oldLocalShortcut) { // || m_oldShapeGesture || m_oldRockerGesture) {
229 dbgUI << "Committing changes for " << data(Name, Qt::DisplayRole).toString();
230 }
231
232 delete m_oldLocalShortcut;
234}
QKeySequence alternateSequence(const QList< QKeySequence > &sequences)
QKeySequence primarySequence(const QList< QKeySequence > &sequences)
@ ShortcutRole
@ DefaultShortcutRole
@ LocalAlternate
@ LocalPrimary
unsigned int uint
QAction * m_action
The action this item is responsible for.
void commit()
Commit the changes.
void undo()
Undo the changes since the last commit.
QList< QKeySequence > * m_oldLocalShortcut
The original shortcuts before user changes. 0 means no change.
~KisShortcutsEditorItem() override
Destructor will erase unsaved changes.
bool operator<(const QTreeWidgetItem &other) const override
bool m_isNameBold
Should the Name column be painted in bold?
QKeySequence keySequence(uint column) const
QString m_id
The action id. Needed for exporting and importing.
KisShortcutsEditorItem(QTreeWidgetItem *parent, QAction *action)
QCollator m_collator
The collator, for sorting.
void updateModified()
Recheck modified status - could have changed back to initial value.
QVariant data(int column, int role=Qt::DisplayRole) const override
QString m_actionNameInTable
The localized action name.
void setKeySequence(uint column, const QKeySequence &seq)
#define warnKrita
Definition kis_debug.h:87
#define dbgUI
Definition kis_debug.h:52