File: kshortcutseditoritem.cpp

package info (click to toggle)
kde4libs 4%3A4.8.4-4%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 80,588 kB
  • sloc: cpp: 746,914; xml: 8,370; ansic: 6,295; java: 4,060; perl: 2,930; yacc: 2,462; sh: 1,225; python: 1,081; ruby: 337; lex: 278; makefile: 29
file content (356 lines) | stat: -rw-r--r-- 11,061 bytes parent folder | download | duplicates (7)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* This file is part of the KDE libraries Copyright (C) 1998 Mark Donohoe <donohoe@kde.org>
    Copyright (C) 1997 Nicolas Hadacek <hadacek@kde.org>
    Copyright (C) 1998 Matthias Ettrich <ettrich@kde.org>
    Copyright (C) 2001 Ellis Whitehead <ellis@kde.org>
    Copyright (C) 2006 Hamish Rodda <rodda@kde.org>
    Copyright (C) 2007 Roberto Raggi <roberto@kdevelop.org>
    Copyright (C) 2007 Andreas Hartmetz <ahartmetz@gmail.com>
    Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kshortcutsdialog_p.h"

#include <kaction.h>
#include <kdebug.h>
#include <kstringhandler.h>

#include <QTreeWidgetItem>

KShortcutsEditorItem::KShortcutsEditorItem(QTreeWidgetItem *parent, KAction *action)
    : QTreeWidgetItem(parent, ActionItem)
    , m_action(action)
    , m_isNameBold(false)
    , m_oldLocalShortcut(0)
    , m_oldGlobalShortcut(0)
    , m_oldShapeGesture(0)
    , m_oldRockerGesture(0)
{
    // Filtering message requested by translators (scripting).
    m_id = m_action->objectName();
    m_actionNameInTable = i18nc("@item:intable Action name in shortcuts configuration", "%1", KGlobal::locale()->removeAcceleratorMarker(m_action->text()));
    if (m_actionNameInTable.isEmpty()) {
        kWarning() << "Action without text!" << m_action->objectName();
        m_actionNameInTable = m_id;
    }
}


KShortcutsEditorItem::~KShortcutsEditorItem()
{
    delete m_oldLocalShortcut;
    delete m_oldGlobalShortcut;
    delete m_oldShapeGesture;
    delete m_oldRockerGesture;
}


bool KShortcutsEditorItem::isModified() const
{
    return m_oldLocalShortcut || m_oldGlobalShortcut || m_oldShapeGesture || m_oldRockerGesture;
}


QVariant KShortcutsEditorItem::data(int column, int role) const
{
    switch (role) {
    case Qt::DisplayRole:
        switch(column) {
        case Name:
            return m_actionNameInTable;
        case Id:
            return m_id;
        case LocalPrimary:
        case LocalAlternate:
        case GlobalPrimary:
        case GlobalAlternate:
            return keySequence(column);
        case ShapeGesture:
            return m_action->shapeGesture().shapeName();
        case RockerGesture:
            return m_action->rockerGesture().rockerName();
        default:
            break;
        }
        break;
    case Qt::DecorationRole:
        if (column == Name)
            return m_action->icon();
        else
            return KIcon();
        break;
    case Qt::WhatsThisRole:
        return m_action->whatsThis();
    case Qt::ToolTipRole:
        // There is no such thing as a KAction::description(). So we have
        // nothing to display here.
        return QVariant();
    case Qt::FontRole:
        if (column == Name && m_isNameBold) {
            QFont modifiedFont = treeWidget()->font();
            modifiedFont.setBold(true);
            return modifiedFont;
        }
        break;
    case KExtendableItemDelegate::ShowExtensionIndicatorRole:
        switch (column) {
        case Name:
            return false;
        case LocalPrimary:
        case LocalAlternate:
            if (!m_action->isShortcutConfigurable()) {
                return false;
            }
            return true;
        case GlobalPrimary:
        case GlobalAlternate:
            if (!m_action->isGlobalShortcutEnabled()) {
                return false;
            }
            return true;
        default:
            return false;
        }
    //the following are custom roles, defined in this source file only
    case ShortcutRole:
        switch(column) {
        case LocalPrimary:
        case LocalAlternate:
        case GlobalPrimary:
        case GlobalAlternate:
            return keySequence(column);
        case ShapeGesture: { //scoping for "ret"
            QVariant ret;
            ret.setValue(m_action->shapeGesture());
            return ret; }
        case RockerGesture: {
            QVariant ret;
            ret.setValue(m_action->rockerGesture());
            return ret; }
        default:
            // Column not valid for this role
            Q_ASSERT(false);
            return QVariant();
        }

    case DefaultShortcutRole:
        switch(column) {
        case LocalPrimary:
            return m_action->shortcut(KAction::DefaultShortcut).primary();
        case LocalAlternate:
            return m_action->shortcut(KAction::DefaultShortcut).alternate();
        case GlobalPrimary:
            return m_action->globalShortcut(KAction::DefaultShortcut).primary();
        case GlobalAlternate:
            return m_action->globalShortcut(KAction::DefaultShortcut).alternate();
        case ShapeGesture: {
            QVariant ret;
            ret.setValue(m_action->shapeGesture(KAction::DefaultShortcut));
            return ret; }
        case RockerGesture: {
            QVariant ret;
            ret.setValue(m_action->rockerGesture(KAction::DefaultShortcut));
            return ret; }
        default:
            // Column not valid for this role
            Q_ASSERT(false);
            return QVariant();
        }
    case ObjectRole:
        return qVariantFromValue((QObject*)m_action);

    default:
        break;
    }

    return QVariant();
}


bool KShortcutsEditorItem::operator<(const QTreeWidgetItem &other) const
{
    const int column = treeWidget() ? treeWidget()->sortColumn() : 0;
    return KStringHandler::naturalCompare(text(column), other.text(column)) < 0;
}


QKeySequence KShortcutsEditorItem::keySequence(uint column) const
{
    switch (column) {
    case LocalPrimary:
        return m_action->shortcut().primary();
    case LocalAlternate:
        return m_action->shortcut().alternate();
    case GlobalPrimary:
        return m_action->globalShortcut().primary();
    case GlobalAlternate:
        return m_action->globalShortcut().alternate();
    default:
        return QKeySequence();
    }
}


void KShortcutsEditorItem::setKeySequence(uint column, const QKeySequence &seq)
{
    KShortcut ks;
    if (column == GlobalPrimary || column == GlobalAlternate) {
        ks = m_action->globalShortcut();
        if (!m_oldGlobalShortcut)
            m_oldGlobalShortcut = new KShortcut(ks);
     } else {
        ks = m_action->shortcut();
        if (!m_oldLocalShortcut)
            m_oldLocalShortcut = new KShortcut(ks);
    }

    if (column == LocalAlternate || column == GlobalAlternate)
        ks.setAlternate(seq);
    else
        ks.setPrimary(seq);

    //avoid also setting the default shortcut - what we are setting here is custom by definition
    if (column == GlobalPrimary || column == GlobalAlternate) {
        m_action->setGlobalShortcut(ks, KAction::ActiveShortcut, KAction::NoAutoloading);
    } else {
        m_action->setShortcut(ks, KAction::ActiveShortcut);
    }

    updateModified();
}


void KShortcutsEditorItem::setShapeGesture(const KShapeGesture &gst)
{
    if (!m_oldShapeGesture) {
        m_oldShapeGesture = new KShapeGesture(gst);
    }
    m_action->setShapeGesture(gst);
    updateModified();
}


void KShortcutsEditorItem::setRockerGesture(const KRockerGesture &gst)
{
    if (!m_oldRockerGesture) {
        m_oldRockerGesture = new KRockerGesture(gst);
    }
    m_action->setRockerGesture(gst);
    updateModified();
}


//our definition of modified is "modified since the chooser was shown".
void KShortcutsEditorItem::updateModified()
{
    if (m_oldLocalShortcut && *m_oldLocalShortcut == m_action->shortcut()) {
        delete m_oldLocalShortcut;
        m_oldLocalShortcut = 0;
    }
    if (m_oldGlobalShortcut && *m_oldGlobalShortcut == m_action->globalShortcut()) {
        delete m_oldGlobalShortcut;
        m_oldGlobalShortcut = 0;
    }
    if (m_oldShapeGesture && *m_oldShapeGesture == m_action->shapeGesture()) {
        delete m_oldShapeGesture;
        m_oldShapeGesture = 0;
    }
    if (m_oldRockerGesture && *m_oldRockerGesture == m_action->rockerGesture()) {
        delete m_oldRockerGesture;
        m_oldRockerGesture = 0;
    }
}


bool KShortcutsEditorItem::isModified(uint column) const
{
    switch (column) {
    case Name:
        return false;
    case LocalPrimary:
    case LocalAlternate:
        if (!m_oldLocalShortcut)
            return false;
        if (column == LocalPrimary)
            return m_oldLocalShortcut->primary() != m_action->shortcut().primary();
        else
            return m_oldLocalShortcut->alternate() != m_action->shortcut().alternate();
    case GlobalPrimary:
    case GlobalAlternate:
        if (!m_oldGlobalShortcut)
            return false;
        if (column == GlobalPrimary)
            return m_oldGlobalShortcut->primary() != m_action->globalShortcut().primary();
        else
            return m_oldGlobalShortcut->alternate() != m_action->globalShortcut().alternate();
    case ShapeGesture:
        return static_cast<bool>(m_oldShapeGesture);
    case RockerGesture:
        return static_cast<bool>(m_oldRockerGesture);
    default:
        return false;
    }
}



void KShortcutsEditorItem::undo()
{
#ifndef NDEBUG
    if (m_oldLocalShortcut || m_oldGlobalShortcut || m_oldShapeGesture || m_oldRockerGesture ) {
        kDebug(125) << "Undoing changes for " << data(Name, Qt::DisplayRole).toString();
    }
#endif
    if (m_oldLocalShortcut) {
        // We only ever reset the active Shortcut
        m_action->setShortcut(*m_oldLocalShortcut, KAction::ActiveShortcut);
    }

    if (m_oldGlobalShortcut) {
        m_action->setGlobalShortcut(*m_oldGlobalShortcut, KAction::ActiveShortcut,
                                    KAction::NoAutoloading);
    }

    if (m_oldShapeGesture) {
        m_action->setShapeGesture(*m_oldShapeGesture);
    }

    if (m_oldRockerGesture) {
        m_action->setRockerGesture(*m_oldRockerGesture);
    }

    updateModified();
}


void KShortcutsEditorItem::commit()
{
#ifndef NDEBUG
    if (m_oldLocalShortcut || m_oldGlobalShortcut || m_oldShapeGesture || m_oldRockerGesture ) {
        kDebug(125) << "Committing changes for " << data(Name, Qt::DisplayRole).toString();
    }
#endif

    delete m_oldLocalShortcut; 
    m_oldLocalShortcut = 0;
    delete m_oldGlobalShortcut;
    m_oldGlobalShortcut = 0;
    delete m_oldShapeGesture;
    m_oldShapeGesture = 0;
    delete m_oldRockerGesture;
    m_oldRockerGesture = 0;
}