Wed, 26 Dec 2018 23:01:45 +0200
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 | #pragma once |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
20 | #include <QAbstractItemModel> |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
21 | #include <QAction> |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
22 | #include <QStyledItemDelegate> |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
23 | #include "main.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 | * Implements a delegate for editing key sequence cells. |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
27 | */ |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
28 | class KeySequenceDelegate : public QStyledItemDelegate |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
29 | { |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
30 | public: |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
31 | KeySequenceDelegate(QObject* parent = nullptr); |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
32 | QWidget* createEditor( |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
33 | QWidget *parent, |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
34 | const QStyleOptionViewItem &option, |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
35 | const QModelIndex &index) const override; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
36 | void setEditorData(QWidget *widget, const QModelIndex &index) const override; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
37 | void setModelData( |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
38 | QWidget *widget, |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
39 | QAbstractItemModel *model, |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
40 | const QModelIndex &index) const override; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
41 | void updateEditorGeometry( |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
42 | QWidget *editor, |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
43 | const QStyleOptionViewItem &option, |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
44 | const QModelIndex &index) const override; |
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 | /* |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
48 | * Models a table of shortcuts. Each action in the provided main window is given |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
49 | * a row, which contains editable shortcuts. |
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 | * Calling saveChanges updates the actions and updates the settings object. |
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 | class ShortcutsModel : public QAbstractItemModel |
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 | public: |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
56 | enum Column |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
57 | { |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
58 | ActionColumn, |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
59 | KeySequenceColumn |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
60 | }; |
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 | enum |
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 | DefaultKeySequenceRole = Qt::UserRole |
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 | |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
67 | ShortcutsModel(class MainWindow* parent); |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
68 | int rowCount(QModelIndex const&) const override; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
69 | int columnCount(QModelIndex const&) const override; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
70 | QVariant data(const QModelIndex &index, int role) const override; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
71 | Qt::ItemFlags flags(const QModelIndex& index) const override; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
72 | bool isValidRow(int row) const; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
73 | bool isValidIndex(const QModelIndex &index) const; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
74 | bool setData(const QModelIndex &index, const QVariant &value, int role) override; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
75 | void saveChanges(); |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
76 | QModelIndex index(int row, int column, const QModelIndex& parent) const override; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
77 | QModelIndex parent(const QModelIndex& child) const override; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
78 | QVariant headerData(int section, Qt::Orientation orientation, int role) const override; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
79 | |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
80 | private: |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
81 | struct Item |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
82 | { |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
83 | QAction* action; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
84 | QKeySequence sequence; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
85 | const QKeySequence defaultSequence; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
86 | }; |
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 | QVector<Item> shortcuts; |
bd3a9e237ef5
reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
89 | }; |