src/dialogs/shortcutsmodel.cpp

Wed, 26 Dec 2018 23:01:45 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 26 Dec 2018 23:01:45 +0200
changeset 1433
bd3a9e237ef5
permissions
-rw-r--r--

reimplented shortcuts in the config dialog using model/view programming

1433
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 * Copyright (C) 2013 - 2018 Teemu Piippo
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #include <QKeySequenceEdit>
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 #include <QSettings>
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 #include "mainwindow.h"
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 #include "widgets/extendedkeysequenceeditor.h"
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 #include "shortcutsmodel.h"
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 * Constructs a new shortcuts model.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 * Actions are acquired from the provided mainwindow.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 ShortcutsModel::ShortcutsModel(MainWindow* parent)
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 for (QAction* action : parent->findChildren<QAction*>())
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 if (not action->objectName().isEmpty())
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 shortcuts.append({action, action->shortcut(), parent->defaultShortcut(action)});
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 * Returns the amount of shortcuts.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 int ShortcutsModel::rowCount(const QModelIndex &) const
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 return shortcuts.size();
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 * Returns the amount of columns.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 int ShortcutsModel::columnCount(const QModelIndex &) const
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 return 2;
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 * Returns various shortcut data.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 QVariant ShortcutsModel::data(const QModelIndex &index, int role) const
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 if (not isValidIndex(index))
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 return {};
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 Item const& entry = shortcuts[index.row()];
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 switch (role)
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 case Qt::DisplayRole:
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 switch (index.column())
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 case ActionColumn:
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 return entry.action->text().replace("&", "");
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 case KeySequenceColumn:
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 return entry.sequence.toString(QKeySequence::NativeText);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 default:
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 return "";
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 case Qt::DecorationRole:
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 if (index.column() == ActionColumn)
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 return entry.action->icon();
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 else
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 return {};
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 case Qt::EditRole:
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 switch (index.column())
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 case KeySequenceColumn:
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 return QVariant::fromValue(entry.sequence);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 default:
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 return {};
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 case DefaultKeySequenceRole:
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 return entry.defaultSequence;
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 default:
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 return {};
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 * Reimplementation of QAbstractItemModel::flags that supplies the editable flag
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105 * for the key sequence cells.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 Qt::ItemFlags ShortcutsModel::flags(const QModelIndex& index) const
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
108 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 Qt::ItemFlags flags = QAbstractItemModel::flags(index);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
110
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
111 if (index.column() == KeySequenceColumn)
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
112 flags |= Qt::ItemIsEditable;
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
114 return flags;
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
115 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
116
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
117 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
118 * Returns whether or not the specified row is valid.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
119 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
120 bool ShortcutsModel::isValidRow(int row) const
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
121 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
122 return row >= 0 and row < shortcuts.size();
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
123 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
124
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
125 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
126 * Returns whether or not the specified model index is valid.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
127 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
128 bool ShortcutsModel::isValidIndex(const QModelIndex &index) const
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
129 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
130 return index.column() >= 0 and index.column() < 2 and isValidRow(index.row());
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
131 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
132
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
133 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
134 * Provides an interface for changing the key sequence.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
135 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
136 bool ShortcutsModel::setData(const QModelIndex &index, const QVariant &value, int role)
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
137 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
138 if (isValidIndex(index) and index.column() == KeySequenceColumn and role == Qt::EditRole)
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
139 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
140 shortcuts[index.row()].sequence = value.value<QKeySequence>();
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
141 return true;
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
142 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
143 else
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
144 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
145 return false;
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
146 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
147 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
148
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
149 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
150 * Saves shortcuts to the settings object and updates the actions.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
151 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
152 void ShortcutsModel::saveChanges()
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
153 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
154 for (Item& shortcut : shortcuts)
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
155 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
156 shortcut.action->setShortcut(shortcut.sequence);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
157 QString const key = "shortcut_" + shortcut.action->objectName();
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
158
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
159 if (shortcut.defaultSequence != shortcut.sequence)
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
160 settingsObject().setValue(key, shortcut.sequence);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
161 else
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
162 settingsObject().remove(key);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
163 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
164 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
165
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
166 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
167 * Returns an index.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
168 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
169 QModelIndex ShortcutsModel::index(int row, int column, const QModelIndex& parent) const
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
170 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
171 ignore(parent);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
172 return createIndex(row, column);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
173 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
174
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
175 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
176 * Returns nothing, because the shortcuts model does not use parents.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
177 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
178 QModelIndex ShortcutsModel::parent(const QModelIndex &child) const
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
179 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
180 ignore(child);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
181 return {};
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
182 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
183
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
184 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
185 * Returns headers.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
186 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
187 QVariant ShortcutsModel::headerData(int section, Qt::Orientation orientation, int role) const
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
188 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
189 if (role == Qt::DisplayRole and orientation == Qt::Horizontal)
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
190 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
191 switch (section)
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
192 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
193 case ActionColumn:
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
194 return "Action";
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
195
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
196 case KeySequenceColumn:
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
197 return "Shortcut";
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
198
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
199 default:
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
200 return "";
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
201 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
202 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
203 else
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
204 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
205 return {};
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
206 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
207 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
208
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
209 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
210 * Constructs a new key sequence delegate.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
211 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
212 KeySequenceDelegate::KeySequenceDelegate(QObject *parent) :
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
213 QStyledItemDelegate {parent} {}
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
214
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
215 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
216 * Creates a key sequence editor.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
217 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
218 QWidget *KeySequenceDelegate::createEditor(
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
219 QWidget *parent,
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
220 const QStyleOptionViewItem &option,
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
221 const QModelIndex &index) const
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
222 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
223 ignore(option, index);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
224 const QVariant variant = index.model()->data(index, ShortcutsModel::DefaultKeySequenceRole);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
225 const QKeySequence defaultSequence = variant.value<QKeySequence>();
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
226 ExtendedKeySequenceEditor* editor = new ExtendedKeySequenceEditor {{}, defaultSequence, parent};
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
227 return editor;
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
228 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
229
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
230 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
231 * Sets the initial key sequence used in the key sequence editor.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
232 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
233 void KeySequenceDelegate::setEditorData(QWidget *widget, const QModelIndex &index) const
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
234 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
235 QKeySequence sequence = index.model()->data(index, Qt::EditRole).value<QKeySequence>();
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
236 ExtendedKeySequenceEditor* editor = static_cast<ExtendedKeySequenceEditor*>(widget);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
237 editor->setKeySequence(sequence);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
238 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
239
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
240 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
241 * Updates the shortcuts model when the key sequence has been accepted by the user.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
242 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
243 void KeySequenceDelegate::setModelData(
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
244 QWidget *widget,
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
245 QAbstractItemModel *model,
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
246 const QModelIndex &index) const
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
247 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
248 ExtendedKeySequenceEditor* editor = static_cast<ExtendedKeySequenceEditor*>(widget);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
249 model->setData(index, editor->keySequence(), Qt::EditRole);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
250 }
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
251
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
252 /*
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
253 * Updates editor geometry.
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
254 */
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
255 void KeySequenceDelegate::updateEditorGeometry(
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
256 QWidget *editor,
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
257 const QStyleOptionViewItem &option,
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
258 const QModelIndex &index) const
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
259 {
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
260 ignore(index);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
261 editor->setGeometry(option.rect);
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
262 }

mercurial