Krita Source Code Documentation
Loading...
Searching...
No Matches
KisActionShortcutsModel Class Reference

A QAbstractListModel subclass that lists shortcuts associated with an action from a profile. More...

#include <kis_action_shortcuts_model.h>

+ Inheritance diagram for KisActionShortcutsModel:

Classes

class  Private
 

Public Slots

void setAction (KisAbstractInputAction *action)
 
void setProfile (KisInputProfile *profile)
 

Public Member Functions

KisAbstractInputActionaction () const
 
bool canRemoveRow (int row) const
 
int columnCount (const QModelIndex &) const override
 
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const override
 
Qt::ItemFlags flags (const QModelIndex &index) const override
 
QVariant headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
 
 KisActionShortcutsModel (QObject *parent=0)
 
KisInputProfileprofile () const
 
bool removeRows (int row, int count, const QModelIndex &parent=QModelIndex()) override
 
int rowCount (const QModelIndex &parent=QModelIndex()) const override
 
bool setData (const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
 
 ~KisActionShortcutsModel () override
 

Private Slots

void currentProfileChanged ()
 

Private Attributes

Private *const d
 

Detailed Description

A QAbstractListModel subclass that lists shortcuts associated with an action from a profile.

This class lists all shortcuts from the set profile that correspond to a specific action. This is used to allow editing of shortcuts from the ui.

It defines the following columns:

  • Type: The type of shortcut, can be one of Key Combination, Mouse Button, Mouse Wheel, Gesture
  • Input: What input is used to activate the shortcut. Depends on the type.
  • Action: What mode of the action will be activated by the shortcut.
Note
Before this model will provide any data you should call setAction and setProfile.
This model is editable and provides an implementation of removeRows.

Definition at line 30 of file kis_action_shortcuts_model.h.

Constructor & Destructor Documentation

◆ KisActionShortcutsModel()

KisActionShortcutsModel::KisActionShortcutsModel ( QObject * parent = 0)
explicit

Constructor.

Definition at line 40 of file kis_action_shortcuts_model.cpp.

41 : QAbstractListModel(parent), d(new Private)
42{
44}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
static KisInputProfileManager * instance()

References connect(), currentProfileChanged(), and KisInputProfileManager::instance().

◆ ~KisActionShortcutsModel()

KisActionShortcutsModel::~KisActionShortcutsModel ( )
override

Destructor.

Definition at line 46 of file kis_action_shortcuts_model.cpp.

47{
48}

Member Function Documentation

◆ action()

KisAbstractInputAction * KisActionShortcutsModel::action ( ) const
Returns
The action used as data constraint for this model.

Definition at line 296 of file kis_action_shortcuts_model.cpp.

297{
298 return d->action;
299}

References KisActionShortcutsModel::Private::action, and d.

◆ canRemoveRow()

bool KisActionShortcutsModel::canRemoveRow ( int row) const

◆ columnCount()

int KisActionShortcutsModel::columnCount ( const QModelIndex & ) const
override

Reimplemented from QAbstractItemModel::columnCount()

Definition at line 168 of file kis_action_shortcuts_model.cpp.

169{
170 return 3;
171}

◆ currentProfileChanged

void KisActionShortcutsModel::currentProfileChanged ( )
privateslot

Definition at line 342 of file kis_action_shortcuts_model.cpp.

343{
344 setProfile(KisInputProfileManager::instance()->currentProfile());
345}
void setProfile(KisInputProfile *profile)

References KisInputProfileManager::instance(), and setProfile().

◆ data()

QVariant KisActionShortcutsModel::data ( const QModelIndex & index,
int role = Qt::DisplayRole ) const
override

Reimplemented from QAbstractItemModel::data()

Definition at line 50 of file kis_action_shortcuts_model.cpp.

51{
52 if (!index.isValid()) {
53 return QVariant();
54 }
55
56 if (index.row() == d->shortcuts.count() && role == Qt::DisplayRole) {
57 if (index.column() == 0) {
58 return i18n("Add shortcut...");
59 }
60 else {
61 return QVariant();
62 }
63 }
64
65 if (role == Qt::DisplayRole) {
66 switch (index.column()) {
67 case 0:
68 switch (d->shortcuts.at(index.row())->type()) {
70 return i18nc("Shortcut type", "Key Combination");
71
73 return i18nc("Shortcut type", "Mouse Button");
74
76 return i18nc("Shortcut type", "Mouse Wheel");
77
79 return i18nc("Shortcut type", "Gesture");
80
81 default:
82 return i18n("Unknown Input");
83 }
84
85 break;
86
87 case 1: {
88 KisShortcutConfiguration *s = d->shortcuts.at(index.row());
89 QString output;
90
91 switch (s->type()) {
94 break;
95
98 s->keys(), s->buttons());
99 break;
100
103 s->keys(), s->wheel());
104 break;
105
108 break;
109
110 default:
111 break;
112 }
113
114 return output;
115 }
116
117 case 2:
118 return d->action->shortcutIndexes().key(d->shortcuts.at(index.row())->mode());
119
120 case 3:
121 return KisIconUtils::loadIcon("edit-delete");
122
123 default:
124 break;
125 }
126 }
127 else if (role == Qt::EditRole) {
129
130 if (index.row() == d->shortcuts.count()) {
131 if (!d->temporaryShortcut) {
133 }
134
135 s = d->temporaryShortcut;
136 }
137 else {
138 s = d->shortcuts.at(index.row());
139 }
140
141 switch (index.column()) {
142 case 0:
143 return s->type();
144
145 case 1:
146 return QVariant::fromValue(s);
147
148 case 2:
149 return s->mode();
150
151 default:
152 break;
153 }
154 }
155
156 return QVariant();
157}
virtual QHash< QString, int > shortcutIndexes() const
static QString keysToText(const QList< Qt::Key > &keys)
@ MouseButtonType
A mouse button, possibly with key modifiers.
@ MouseWheelType
Mouse wheel movement, possibly with key modifiers.
@ KeyCombinationType
A list of keys that should be pressed.
static QString wheelInputToText(const QList< Qt::Key > &keys, MouseWheelMovement wheel)
static QString gestureToText(GestureAction action)
static QString buttonsInputToText(const QList< Qt::Key > &keys, Qt::MouseButtons buttons)
QIcon loadIcon(const QString &name)

References KisActionShortcutsModel::Private::action, KisShortcutConfiguration::buttons(), KisShortcutConfiguration::buttonsInputToText(), d, KisShortcutConfiguration::gesture(), KisShortcutConfiguration::gestureToText(), KisShortcutConfiguration::GestureType, KisShortcutConfiguration::KeyCombinationType, KisShortcutConfiguration::keys(), KisShortcutConfiguration::keysToText(), KisIconUtils::loadIcon(), KisShortcutConfiguration::mode(), KisShortcutConfiguration::MouseButtonType, KisShortcutConfiguration::MouseWheelType, KisAbstractInputAction::shortcutIndexes(), KisActionShortcutsModel::Private::shortcuts, KisActionShortcutsModel::Private::temporaryShortcut, KisShortcutConfiguration::type(), KisShortcutConfiguration::wheel(), and KisShortcutConfiguration::wheelInputToText().

◆ flags()

Qt::ItemFlags KisActionShortcutsModel::flags ( const QModelIndex & index) const
override

Reimplemented from QAbstractItemModel::flags()

Definition at line 196 of file kis_action_shortcuts_model.cpp.

197{
198 if (!index.isValid()) {
199 return Qt::ItemIsEnabled;
200 }
201
202 if (index.row() == d->shortcuts.count() && index.column() != 0) {
203 return Qt::ItemIsEnabled;
204 }
205
206 if (index.row() >= d->shortcuts.count()) {
207 return Qt::ItemIsEnabled | Qt::ItemIsEditable;
208 }
209
210 KisShortcutConfiguration* config = d->shortcuts.at(index.row());
211 if (index.column() == 2 && d->action->isShortcutRequired(config->mode()) && d->shortcutModeCount(config->mode()) < 2) {
212 return Qt::ItemIsSelectable;
213 }
214
215 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
216}

References KisActionShortcutsModel::Private::action, d, KisAbstractInputAction::isShortcutRequired(), KisShortcutConfiguration::mode(), KisActionShortcutsModel::Private::shortcutModeCount(), and KisActionShortcutsModel::Private::shortcuts.

◆ headerData()

QVariant KisActionShortcutsModel::headerData ( int section,
Qt::Orientation orientation,
int role = Qt::DisplayRole ) const
override

Reimplemented from QAbstractItemModel::headerData()

Definition at line 173 of file kis_action_shortcuts_model.cpp.

174{
175 if (orientation != Qt::Horizontal || role != Qt::DisplayRole) {
176 return QVariant();
177 }
178
179 switch (section) {
180 case 0:
181 return i18nc("Type of shortcut", "Type");
182
183 case 1:
184 return i18nc("Input for shortcut", "Input");
185
186 case 2:
187 return i18nc("Action to trigger with shortcut", "Action");
188
189 default:
190 break;
191 }
192
193 return QVariant();
194}

◆ profile()

KisInputProfile * KisActionShortcutsModel::profile ( ) const
Returns
The profile used as data source for this model.

Definition at line 319 of file kis_action_shortcuts_model.cpp.

320{
321 return d->profile;
322}

References d, and KisActionShortcutsModel::Private::profile.

◆ removeRows()

bool KisActionShortcutsModel::removeRows ( int row,
int count,
const QModelIndex & parent = QModelIndex() )
override

Reimplemented from QAbstractItemModel::removeRows.

Removes count rows starting at row.

Note
The associated shortcuts will also be removed from the profile and completely deleted.

Definition at line 347 of file kis_action_shortcuts_model.cpp.

348{
349 if (row < 0 || row >= d->shortcuts.count() || count == 0) {
350 return false;
351 }
352
353 beginRemoveRows(parent, row, row + count - 1);
354
355 for (int i = row; i < d->shortcuts.count() && count > 0; ++i, count--) {
357
359 d->shortcuts.removeOne(s);
360 delete s;
361 }
362
363 endRemoveRows();
364
365 return true;
366}
void removeShortcut(KisShortcutConfiguration *shortcut)

References d, KisActionShortcutsModel::Private::profile, KisInputProfile::removeShortcut(), and KisActionShortcutsModel::Private::shortcuts.

◆ rowCount()

int KisActionShortcutsModel::rowCount ( const QModelIndex & parent = QModelIndex()) const
override

Reimplemented from QAbstractItemModel::rowCount()

Definition at line 159 of file kis_action_shortcuts_model.cpp.

160{
161 if (parent.isValid()) {
162 return 0;
163 }
164
165 return d->shortcuts.count() + 1;
166}
ChildIterator< value_type, is_const > parent(const ChildIterator< value_type, is_const > &it)
Definition KisForest.h:327

References d, and KisActionShortcutsModel::Private::shortcuts.

◆ setAction

void KisActionShortcutsModel::setAction ( KisAbstractInputAction * action)
slot

Set the action used as data constraint for this model.

Parameters
actionThe action to use.

Definition at line 301 of file kis_action_shortcuts_model.cpp.

302{
303 if (action != d->action) {
304 if (d->action) {
305 beginRemoveRows(QModelIndex(), 0, d->shortcuts.count() - 1);
306 endRemoveRows();
307 }
308
309 d->action = action;
310
311 if (d->action && d->profile) {
313 beginInsertRows(QModelIndex(), 0, d->shortcuts.count() - 1);
314 endInsertRows();
315 }
316 }
317}
KisAbstractInputAction * action() const
QList< KisShortcutConfiguration * > shortcutsForAction(KisAbstractInputAction *action) const

References KisActionShortcutsModel::Private::action, action(), d, KisActionShortcutsModel::Private::profile, KisActionShortcutsModel::Private::shortcuts, and KisInputProfile::shortcutsForAction().

◆ setData()

bool KisActionShortcutsModel::setData ( const QModelIndex & index,
const QVariant & value,
int role = Qt::EditRole )
override

Reimplemented from QAbstractItemModel::setData()

Definition at line 227 of file kis_action_shortcuts_model.cpp.

228{
229 if (!index.isValid() || role != Qt::EditRole) {
230 return false;
231 }
232
233 if (index.row() == d->shortcuts.count()) {
234 if (!d->temporaryShortcut || (index.column() == 0 && value.toUInt() == 0)) {
235 return false;
236 }
237
238 beginInsertRows(QModelIndex(), d->shortcuts.count(), d->shortcuts.count());
241 d->shortcuts.append(d->temporaryShortcut);
242 d->temporaryShortcut = 0;
243 endInsertRows();
244 }
245
246 switch (index.column()) {
247 case 0:
248 d->shortcuts.at(index.row())->setType(static_cast<KisShortcutConfiguration::ShortcutType>(value.toUInt()));
249 break;
250
251 case 1: {
253 KisShortcutConfiguration *oldData = d->shortcuts.at(index.row());
254
255 if (newData == oldData) {
256 const QList<KisShortcutConfiguration *> conflictingShortcuts =
258 KisInputProfileManager::instance()->currentProfile());
259
260 // check if this shortcut resulted in a conflict
261 const bool isConflictingShortcut = std::find_if(conflictingShortcuts.begin(),
262 conflictingShortcuts.end(),
263 [&oldData](KisShortcutConfiguration *shortcut) {
264 return *oldData == *shortcut;
265 })
266 != conflictingShortcuts.end();
267
268 if (isConflictingShortcut) {
269 QMessageBox::warning(qApp->activeWindow(),
270 "Warning",
271 i18n("A conflict exists between two or more shortcuts."));
272 }
273 // change was done on the object
274 Q_EMIT dataChanged(index, index);
275 return true;
276 }
277
278 oldData->setKeys(newData->keys());
279 oldData->setButtons(newData->buttons());
280 oldData->setWheel(newData->wheel());
281 oldData->setGesture(newData->gesture());
282
283 break;
284 }
285
286 case 2:
287 d->shortcuts.at(index.row())->setMode(value.toUInt());
288 break;
289 }
290
291 Q_EMIT dataChanged(index, index);
292
293 return true;
294}
float value(const T *src, size_t ch)
QList< KisShortcutConfiguration * > getConflictingShortcuts(KisInputProfile *profile)
void addShortcut(KisShortcutConfiguration *shortcut)
void setAction(KisAbstractInputAction *newAction)

References KisActionShortcutsModel::Private::action, KisInputProfile::addShortcut(), KisShortcutConfiguration::buttons(), d, KisShortcutConfiguration::gesture(), KisInputProfileManager::getConflictingShortcuts(), KisInputProfileManager::instance(), KisShortcutConfiguration::keys(), KisActionShortcutsModel::Private::profile, KisShortcutConfiguration::setAction(), KisActionShortcutsModel::Private::shortcuts, KisActionShortcutsModel::Private::temporaryShortcut, value(), and KisShortcutConfiguration::wheel().

◆ setProfile

void KisActionShortcutsModel::setProfile ( KisInputProfile * profile)
slot

Set the profile used as data source for this model.

Parameters
profileThe profile to get the data from.

Definition at line 324 of file kis_action_shortcuts_model.cpp.

325{
326 if (profile != d->profile) {
327 if (d->profile) {
328 beginRemoveRows(QModelIndex(), 0, d->shortcuts.count() - 1);
329 endRemoveRows();
330 }
331
332 d->profile = profile;
333
334 if (d->action && d->profile) {
336 beginInsertRows(QModelIndex(), 0, d->shortcuts.count() - 1);
337 endInsertRows();
338 }
339 }
340}
KisInputProfile * profile() const

References KisActionShortcutsModel::Private::action, d, KisActionShortcutsModel::Private::profile, profile(), KisActionShortcutsModel::Private::shortcuts, and KisInputProfile::shortcutsForAction().

Member Data Documentation

◆ d

Private* const KisActionShortcutsModel::d
private

Definition at line 109 of file kis_action_shortcuts_model.h.


The documentation for this class was generated from the following files: