qa: Remove dead code
[quassel.git] / src / qtui / settingspages / shortcutsmodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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         Item *item = new Item();
34         item->row = r;
35         item->collection = coll;
36         for (int i = 0; i < coll->actions().count(); i++) {
37             Action *action = qobject_cast<Action *>(coll->actions().at(i));
38             if (!action)
39                 continue;
40             Item *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
53 ShortcutsModel::~ShortcutsModel()
54 {
55     qDeleteAll(_categoryItems);
56 }
57
58
59 QModelIndex ShortcutsModel::parent(const QModelIndex &child) const
60 {
61     if (!child.isValid())
62         return QModelIndex();
63
64     Item *item = static_cast<Item *>(child.internalPointer());
65     Q_ASSERT(item);
66
67     if (!item->parentItem)
68         return QModelIndex();
69
70     return createIndex(item->parentItem->row, 0, item->parentItem);
71 }
72
73
74 QModelIndex ShortcutsModel::index(int row, int column, const QModelIndex &parent) const
75 {
76     if (parent.isValid())
77         return createIndex(row, column, static_cast<Item *>(parent.internalPointer())->actionItems.at(row));
78
79     // top level category item
80     return createIndex(row, column, _categoryItems.at(row));
81 }
82
83
84 int ShortcutsModel::columnCount(const QModelIndex &) const
85 {
86     return 2;
87 }
88
89
90 int ShortcutsModel::rowCount(const QModelIndex &parent) const
91 {
92     if (!parent.isValid())
93         return _categoryItems.count();
94
95     Item *item = static_cast<Item *>(parent.internalPointer());
96     Q_ASSERT(item);
97
98     if (!item->parentItem)
99         return item->actionItems.count();
100
101     return 0;
102 }
103
104
105 QVariant ShortcutsModel::headerData(int section, Qt::Orientation orientation, int role) const
106 {
107     if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
108         return QVariant();
109     switch (section) {
110     case 0:
111         return tr("Action");
112     case 1:
113         return tr("Shortcut");
114     default:
115         return QVariant();
116     }
117 }
118
119
120 QVariant ShortcutsModel::data(const QModelIndex &index, int role) const
121 {
122     if (!index.isValid())
123         return QVariant();
124
125     Item *item = static_cast<Item *>(index.internalPointer());
126     Q_ASSERT(item);
127
128     if (!item->parentItem) {
129         if (index.column() != 0)
130             return QVariant();
131         switch (role) {
132         case Qt::DisplayRole:
133             return item->collection->property("Category");
134         default:
135             return QVariant();
136         }
137     }
138
139     Action *action = qobject_cast<Action *>(item->action);
140     Q_ASSERT(action);
141
142     switch (role) {
143     case Qt::DisplayRole:
144         switch (index.column()) {
145         case 0:
146             return stripAcceleratorMarkers(action->text());
147         case 1:
148             return item->shortcut.toString(QKeySequence::NativeText);
149         default:
150             return QVariant();
151         }
152
153     case Qt::DecorationRole:
154         if (index.column() == 0)
155             return action->icon();
156         return QVariant();
157
158     case ActionRole:
159         return QVariant::fromValue<QObject *>(action);
160
161     case DefaultShortcutRole:
162         return action->shortcut(Action::DefaultShortcut);
163     case ActiveShortcutRole:
164         return item->shortcut;
165
166     case IsConfigurableRole:
167         return action->isShortcutConfigurable();
168
169     default:
170         return QVariant();
171     }
172 }
173
174
175 bool ShortcutsModel::setData(const QModelIndex &index, const QVariant &value, int role)
176 {
177     if (role != ActiveShortcutRole)
178         return false;
179
180     if (!index.parent().isValid())
181         return false;
182
183     Item *item = static_cast<Item *>(index.internalPointer());
184     Q_ASSERT(item);
185
186     QKeySequence newSeq = value.value<QKeySequence>();
187     QKeySequence oldSeq = item->shortcut;
188     QKeySequence storedSeq = item->action->shortcut(Action::ActiveShortcut);
189
190     item->shortcut = newSeq;
191     emit dataChanged(index, index.sibling(index.row(), 1));
192
193     if (oldSeq == storedSeq && newSeq != storedSeq) {
194         if (++_changedCount == 1)
195             emit hasChanged(true);
196     }
197     else if (oldSeq != storedSeq && newSeq == storedSeq) {
198         if (--_changedCount == 0)
199             emit hasChanged(false);
200     }
201
202     return true;
203 }
204
205
206 void ShortcutsModel::load()
207 {
208     foreach(Item *catItem, _categoryItems) {
209         foreach(Item *actItem, catItem->actionItems) {
210             actItem->shortcut = actItem->action->shortcut(Action::ActiveShortcut);
211         }
212     }
213     emit dataChanged(index(0, 1), index(rowCount()-1, 1));
214     if (_changedCount != 0) {
215         _changedCount = 0;
216         emit hasChanged(false);
217     }
218 }
219
220
221 void ShortcutsModel::commit()
222 {
223     foreach(Item *catItem, _categoryItems) {
224         foreach(Item *actItem, catItem->actionItems) {
225             actItem->action->setShortcut(actItem->shortcut, Action::ActiveShortcut);
226         }
227     }
228     if (_changedCount != 0) {
229         _changedCount = 0;
230         emit hasChanged(false);
231     }
232 }
233
234
235 void ShortcutsModel::defaults()
236 {
237     for (int cat = 0; cat < rowCount(); cat++) {
238         QModelIndex catidx = index(cat, 0);
239         for (int act = 0; act < rowCount(catidx); act++) {
240             QModelIndex actidx = index(act, 1, catidx);
241             setData(actidx, actidx.data(DefaultShortcutRole), ActiveShortcutRole);
242         }
243     }
244 }