1 /***************************************************************************
2 * Copyright (C) 2005-2014 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "shortcutsmodel.h"
24 #include "actioncollection.h"
27 ShortcutsModel::ShortcutsModel(const QHash<QString, ActionCollection *> &actionCollections, QObject *parent)
28 : QAbstractItemModel(parent),
31 for (int r = 0; r < actionCollections.values().count(); r++) {
32 ActionCollection *coll = actionCollections.values().at(r);
33 Item *item = new Item();
35 item->collection = coll;
36 for (int i = 0; i < coll->actions().count(); i++) {
37 Action *action = qobject_cast<Action *>(coll->actions().at(i));
40 Item *actionItem = new Item();
41 actionItem->parentItem = item;
43 actionItem->collection = coll;
44 actionItem->action = action;
45 actionItem->shortcut = action->shortcut();
46 item->actionItems.append(actionItem);
48 _categoryItems.append(item);
53 ShortcutsModel::~ShortcutsModel()
55 qDeleteAll(_categoryItems);
59 QModelIndex ShortcutsModel::parent(const QModelIndex &child) const
64 Item *item = static_cast<Item *>(child.internalPointer());
67 if (!item->parentItem)
70 return createIndex(item->parentItem->row, 0, item->parentItem);
74 QModelIndex ShortcutsModel::index(int row, int column, const QModelIndex &parent) const
77 return createIndex(row, column, static_cast<Item *>(parent.internalPointer())->actionItems.at(row));
79 // top level category item
80 return createIndex(row, column, _categoryItems.at(row));
84 int ShortcutsModel::columnCount(const QModelIndex &parent) const
87 if (!parent.isValid())
90 Item *item = static_cast<Item *>(parent.internalPointer());
93 if (!item->parentItem)
100 int ShortcutsModel::rowCount(const QModelIndex &parent) const
102 if (!parent.isValid())
103 return _categoryItems.count();
105 Item *item = static_cast<Item *>(parent.internalPointer());
108 if (!item->parentItem)
109 return item->actionItems.count();
115 QVariant ShortcutsModel::headerData(int section, Qt::Orientation orientation, int role) const
117 if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
123 return tr("Shortcut");
130 QVariant ShortcutsModel::data(const QModelIndex &index, int role) const
132 if (!index.isValid())
135 Item *item = static_cast<Item *>(index.internalPointer());
138 if (!item->parentItem) {
139 if (index.column() != 0)
142 case Qt::DisplayRole:
143 return item->collection->property("Category");
149 Action *action = qobject_cast<Action *>(item->action);
153 case Qt::DisplayRole:
154 switch (index.column()) {
156 return stripAcceleratorMarkers(action->text());
158 return item->shortcut.toString(QKeySequence::NativeText);
163 case Qt::DecorationRole:
164 if (index.column() == 0)
165 return action->icon();
169 return QVariant::fromValue<QObject *>(action);
171 case DefaultShortcutRole:
172 return action->shortcut(Action::DefaultShortcut);
173 case ActiveShortcutRole:
174 return item->shortcut;
176 case IsConfigurableRole:
177 return action->isShortcutConfigurable();
185 bool ShortcutsModel::setData(const QModelIndex &index, const QVariant &value, int role)
187 if (role != ActiveShortcutRole)
190 if (!index.parent().isValid())
193 Item *item = static_cast<Item *>(index.internalPointer());
196 QKeySequence newSeq = value.value<QKeySequence>();
197 QKeySequence oldSeq = item->shortcut;
198 QKeySequence storedSeq = item->action->shortcut(Action::ActiveShortcut);
200 item->shortcut = newSeq;
201 emit dataChanged(index, index.sibling(index.row(), 1));
203 if (oldSeq == storedSeq && newSeq != storedSeq) {
204 if (++_changedCount == 1)
205 emit hasChanged(true);
207 else if (oldSeq != storedSeq && newSeq == storedSeq) {
208 if (--_changedCount == 0)
209 emit hasChanged(false);
216 void ShortcutsModel::load()
218 foreach(Item *catItem, _categoryItems) {
219 foreach(Item *actItem, catItem->actionItems) {
220 actItem->shortcut = actItem->action->shortcut(Action::ActiveShortcut);
223 emit dataChanged(index(0, 1), index(rowCount()-1, 1));
224 if (_changedCount != 0) {
226 emit hasChanged(false);
231 void ShortcutsModel::commit()
233 foreach(Item *catItem, _categoryItems) {
234 foreach(Item *actItem, catItem->actionItems) {
235 actItem->action->setShortcut(actItem->shortcut, Action::ActiveShortcut);
238 if (_changedCount != 0) {
240 emit hasChanged(false);
245 void ShortcutsModel::defaults()
247 for (int cat = 0; cat < rowCount(); cat++) {
248 QModelIndex catidx = index(cat, 0);
249 for (int act = 0; act < rowCount(catidx); act++) {
250 QModelIndex actidx = index(act, 1, catidx);
251 setData(actidx, actidx.data(DefaultShortcutRole), ActiveShortcutRole);