src: Yearly copyright bump
[quassel.git] / src / qtui / settingspages / shortcutsmodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "shortcutsmodel.h"
22
23 #include "action.h"
24 #include "actioncollection.h"
25 #include "util.h"
26
27 ShortcutsModel::ShortcutsModel(const QHash<QString, ActionCollection*>& actionCollections, QObject* parent)
28     : QAbstractItemModel(parent)
29     , _changedCount(0)
30 {
31     for (int r = 0; r < actionCollections.values().count(); r++) {
32         ActionCollection* coll = actionCollections.values().at(r);
33         auto* item = new Item();
34         item->row = r;
35         item->collection = coll;
36         for (int i = 0; i < coll->actions().count(); i++) {
37             auto* action = qobject_cast<Action*>(coll->actions().at(i));
38             if (!action)
39                 continue;
40             auto* actionItem = new Item();
41             actionItem->parentItem = item;
42             actionItem->row = i;
43             actionItem->collection = coll;
44             actionItem->action = action;
45             actionItem->shortcut = action->shortcut();
46             item->actionItems.append(actionItem);
47         }
48         _categoryItems.append(item);
49     }
50 }
51
52 ShortcutsModel::~ShortcutsModel()
53 {
54     qDeleteAll(_categoryItems);
55 }
56
57 QModelIndex ShortcutsModel::parent(const QModelIndex& child) const
58 {
59     if (!child.isValid())
60         return {};
61
62     auto* item = static_cast<Item*>(child.internalPointer());
63     Q_ASSERT(item);
64
65     if (!item->parentItem)
66         return {};
67
68     return createIndex(item->parentItem->row, 0, item->parentItem);
69 }
70
71 QModelIndex ShortcutsModel::index(int row, int column, const QModelIndex& parent) const
72 {
73     if (parent.isValid())
74         return createIndex(row, column, static_cast<Item*>(parent.internalPointer())->actionItems.at(row));
75
76     // top level category item
77     return createIndex(row, column, _categoryItems.at(row));
78 }
79
80 int ShortcutsModel::columnCount(const QModelIndex&) const
81 {
82     return 2;
83 }
84
85 int ShortcutsModel::rowCount(const QModelIndex& parent) const
86 {
87     if (!parent.isValid())
88         return _categoryItems.count();
89
90     auto* item = static_cast<Item*>(parent.internalPointer());
91     Q_ASSERT(item);
92
93     if (!item->parentItem)
94         return item->actionItems.count();
95
96     return 0;
97 }
98
99 QVariant ShortcutsModel::headerData(int section, Qt::Orientation orientation, int role) const
100 {
101     if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
102         return QVariant();
103     switch (section) {
104     case 0:
105         return tr("Action");
106     case 1:
107         return tr("Shortcut");
108     default:
109         return QVariant();
110     }
111 }
112
113 QVariant ShortcutsModel::data(const QModelIndex& index, int role) const
114 {
115     if (!index.isValid())
116         return QVariant();
117
118     auto* item = static_cast<Item*>(index.internalPointer());
119     Q_ASSERT(item);
120
121     if (!item->parentItem) {
122         if (index.column() != 0)
123             return QVariant();
124         switch (role) {
125         case Qt::DisplayRole:
126             return item->collection->property("Category");
127         default:
128             return QVariant();
129         }
130     }
131
132     auto* action = qobject_cast<Action*>(item->action);
133     Q_ASSERT(action);
134
135     switch (role) {
136     case Qt::DisplayRole:
137         switch (index.column()) {
138         case 0:
139             return stripAcceleratorMarkers(action->text());
140         case 1:
141             return item->shortcut.toString(QKeySequence::NativeText);
142         default:
143             return QVariant();
144         }
145
146     case Qt::DecorationRole:
147         if (index.column() == 0)
148             return action->icon();
149         return QVariant();
150
151     case ActionRole:
152         return QVariant::fromValue<QObject*>(action);
153
154     case DefaultShortcutRole:
155         return action->shortcut(Action::DefaultShortcut);
156     case ActiveShortcutRole:
157         return item->shortcut;
158
159     case IsConfigurableRole:
160         return action->isShortcutConfigurable();
161
162     default:
163         return QVariant();
164     }
165 }
166
167 bool ShortcutsModel::setData(const QModelIndex& index, const QVariant& value, int role)
168 {
169     if (role != ActiveShortcutRole)
170         return false;
171
172     if (!index.parent().isValid())
173         return false;
174
175     auto* item = static_cast<Item*>(index.internalPointer());
176     Q_ASSERT(item);
177
178     QKeySequence newSeq = value.value<QKeySequence>();
179     QKeySequence oldSeq = item->shortcut;
180     QKeySequence storedSeq = item->action->shortcut(Action::ActiveShortcut);
181
182     item->shortcut = newSeq;
183     emit dataChanged(index, index.sibling(index.row(), 1));
184
185     if (oldSeq == storedSeq && newSeq != storedSeq) {
186         if (++_changedCount == 1)
187             emit changed(true);
188     }
189     else if (oldSeq != storedSeq && newSeq == storedSeq) {
190         if (--_changedCount == 0)
191             emit changed(false);
192     }
193
194     return true;
195 }
196
197 void ShortcutsModel::load()
198 {
199     foreach (Item* catItem, _categoryItems) {
200         foreach (Item* actItem, catItem->actionItems) {
201             actItem->shortcut = actItem->action->shortcut(Action::ActiveShortcut);
202         }
203     }
204     emit dataChanged(index(0, 1), index(rowCount() - 1, 1));
205     if (_changedCount != 0) {
206         _changedCount = 0;
207         emit changed(false);
208     }
209 }
210
211 void ShortcutsModel::commit()
212 {
213     foreach (Item* catItem, _categoryItems) {
214         foreach (Item* actItem, catItem->actionItems) {
215             actItem->action->setShortcut(actItem->shortcut, Action::ActiveShortcut);
216         }
217     }
218     if (_changedCount != 0) {
219         _changedCount = 0;
220         emit changed(false);
221     }
222 }
223
224 void ShortcutsModel::defaults()
225 {
226     for (int cat = 0; cat < rowCount(); cat++) {
227         QModelIndex catidx = index(cat, 0);
228         for (int act = 0; act < rowCount(catidx); act++) {
229             QModelIndex actidx = index(act, 1, catidx);
230             setData(actidx, actidx.data(DefaultShortcutRole), ActiveShortcutRole);
231         }
232     }
233 }