File: shortcutsmodel.cpp

package info (click to toggle)
quassel 1:0.13.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 25,640 kB
  • sloc: cpp: 66,953; perl: 15,837; ansic: 4,418; sql: 1,225; sh: 328; xml: 263; python: 224; makefile: 24
file content (244 lines) | stat: -rw-r--r-- 7,206 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/***************************************************************************
 *   Copyright (C) 2005-2019 by the Quassel Project                        *
 *   devel@quassel-irc.org                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) version 3.                                           *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include "shortcutsmodel.h"

#include "action.h"
#include "actioncollection.h"
#include "util.h"

ShortcutsModel::ShortcutsModel(const QHash<QString, ActionCollection *> &actionCollections, QObject *parent)
    : QAbstractItemModel(parent),
    _changedCount(0)
{
    for (int r = 0; r < actionCollections.values().count(); r++) {
        ActionCollection *coll = actionCollections.values().at(r);
        Item *item = new Item();
        item->row = r;
        item->collection = coll;
        for (int i = 0; i < coll->actions().count(); i++) {
            Action *action = qobject_cast<Action *>(coll->actions().at(i));
            if (!action)
                continue;
            Item *actionItem = new Item();
            actionItem->parentItem = item;
            actionItem->row = i;
            actionItem->collection = coll;
            actionItem->action = action;
            actionItem->shortcut = action->shortcut();
            item->actionItems.append(actionItem);
        }
        _categoryItems.append(item);
    }
}


ShortcutsModel::~ShortcutsModel()
{
    qDeleteAll(_categoryItems);
}


QModelIndex ShortcutsModel::parent(const QModelIndex &child) const
{
    if (!child.isValid())
        return QModelIndex();

    Item *item = static_cast<Item *>(child.internalPointer());
    Q_ASSERT(item);

    if (!item->parentItem)
        return QModelIndex();

    return createIndex(item->parentItem->row, 0, item->parentItem);
}


QModelIndex ShortcutsModel::index(int row, int column, const QModelIndex &parent) const
{
    if (parent.isValid())
        return createIndex(row, column, static_cast<Item *>(parent.internalPointer())->actionItems.at(row));

    // top level category item
    return createIndex(row, column, _categoryItems.at(row));
}


int ShortcutsModel::columnCount(const QModelIndex &) const
{
    return 2;
}


int ShortcutsModel::rowCount(const QModelIndex &parent) const
{
    if (!parent.isValid())
        return _categoryItems.count();

    Item *item = static_cast<Item *>(parent.internalPointer());
    Q_ASSERT(item);

    if (!item->parentItem)
        return item->actionItems.count();

    return 0;
}


QVariant ShortcutsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
        return QVariant();
    switch (section) {
    case 0:
        return tr("Action");
    case 1:
        return tr("Shortcut");
    default:
        return QVariant();
    }
}


QVariant ShortcutsModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    Item *item = static_cast<Item *>(index.internalPointer());
    Q_ASSERT(item);

    if (!item->parentItem) {
        if (index.column() != 0)
            return QVariant();
        switch (role) {
        case Qt::DisplayRole:
            return item->collection->property("Category");
        default:
            return QVariant();
        }
    }

    Action *action = qobject_cast<Action *>(item->action);
    Q_ASSERT(action);

    switch (role) {
    case Qt::DisplayRole:
        switch (index.column()) {
        case 0:
            return stripAcceleratorMarkers(action->text());
        case 1:
            return item->shortcut.toString(QKeySequence::NativeText);
        default:
            return QVariant();
        }

    case Qt::DecorationRole:
        if (index.column() == 0)
            return action->icon();
        return QVariant();

    case ActionRole:
        return QVariant::fromValue(action);

    case DefaultShortcutRole:
        return action->shortcut(Action::DefaultShortcut);
    case ActiveShortcutRole:
        return item->shortcut;

    case IsConfigurableRole:
        return action->isShortcutConfigurable();

    default:
        return QVariant();
    }
}


bool ShortcutsModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (role != ActiveShortcutRole)
        return false;

    if (!index.parent().isValid())
        return false;

    Item *item = static_cast<Item *>(index.internalPointer());
    Q_ASSERT(item);

    QKeySequence newSeq = value.value<QKeySequence>();
    QKeySequence oldSeq = item->shortcut;
    QKeySequence storedSeq = item->action->shortcut(Action::ActiveShortcut);

    item->shortcut = newSeq;
    emit dataChanged(index, index.sibling(index.row(), 1));

    if (oldSeq == storedSeq && newSeq != storedSeq) {
        if (++_changedCount == 1)
            emit hasChanged(true);
    }
    else if (oldSeq != storedSeq && newSeq == storedSeq) {
        if (--_changedCount == 0)
            emit hasChanged(false);
    }

    return true;
}


void ShortcutsModel::load()
{
    foreach(Item *catItem, _categoryItems) {
        foreach(Item *actItem, catItem->actionItems) {
            actItem->shortcut = actItem->action->shortcut(Action::ActiveShortcut);
        }
    }
    emit dataChanged(index(0, 1), index(rowCount()-1, 1));
    if (_changedCount != 0) {
        _changedCount = 0;
        emit hasChanged(false);
    }
}


void ShortcutsModel::commit()
{
    foreach(Item *catItem, _categoryItems) {
        foreach(Item *actItem, catItem->actionItems) {
            actItem->action->setShortcut(actItem->shortcut, Action::ActiveShortcut);
        }
    }
    if (_changedCount != 0) {
        _changedCount = 0;
        emit hasChanged(false);
    }
}


void ShortcutsModel::defaults()
{
    for (int cat = 0; cat < rowCount(); cat++) {
        QModelIndex catidx = index(cat, 0);
        for (int act = 0; act < rowCount(catidx); act++) {
            QModelIndex actidx = index(act, 1, catidx);
            setData(actidx, actidx.data(DefaultShortcutRole), ActiveShortcutRole);
        }
    }
}