~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to khotkeys/kcm_hotkeys/hotkeys_context_menu.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>
 
3
 
 
4
   This library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Library General Public
 
6
   License as published by the Free Software Foundation; either
 
7
   version 2 of the License, or (at your option) any later version.
 
8
 
 
9
   This library is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   Library General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Library General Public License
 
15
   along with this library; see the file COPYING.LIB.  If not, write to
 
16
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
   Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "hotkeys_tree_view.h"
 
21
#include "hotkeys_context_menu.h"
 
22
#include "hotkeys_export_widget.h"
 
23
 
 
24
#include "hotkeys_model.h"
 
25
 
 
26
#include "actions/actions.h"
 
27
#include "action_data/action_data_group.h"
 
28
#include "action_data/simple_action_data.h"
 
29
 
 
30
#include <KDE/KDebug>
 
31
#include <KDE/KFileDialog>
 
32
#include <KDE/KLocale>
 
33
#include <KDE/KUrl>
 
34
 
 
35
#include <QtCore/QSignalMapper>
 
36
#include <QtGui/QContextMenuEvent>
 
37
 
 
38
 
 
39
HotkeysTreeViewContextMenu::HotkeysTreeViewContextMenu( const QModelIndex &index, HotkeysTreeView *parent )
 
40
        : QMenu(parent)
 
41
         ,_index(index)
 
42
         ,_view(parent)
 
43
    {
 
44
    setTitle( i18n("Test") );
 
45
 
 
46
    connect(this, SIGNAL(aboutToShow()),
 
47
            SLOT(slotAboutToShow()));
 
48
    }
 
49
 
 
50
 
 
51
HotkeysTreeViewContextMenu::HotkeysTreeViewContextMenu( HotkeysTreeView *parent )
 
52
        : QMenu(parent)
 
53
         ,_index()
 
54
         ,_view(parent)
 
55
    {
 
56
    setTitle( i18n("Test") );
 
57
 
 
58
    connect(this, SIGNAL(aboutToShow()),
 
59
            SLOT(slotAboutToShowForCurrent()));
 
60
    }
 
61
 
 
62
 
 
63
HotkeysTreeViewContextMenu::~HotkeysTreeViewContextMenu()
 
64
    {}
 
65
 
 
66
 
 
67
KHotKeys::Action *
 
68
HotkeysTreeViewContextMenu::createActionFromType(
 
69
        int actionType,
 
70
        KHotKeys::SimpleActionData* data
 
71
        ) const
 
72
    {
 
73
    KHotKeys::Action *action = NULL;
 
74
    switch (actionType)
 
75
        {
 
76
        case KHotKeys::Action::CommandUrlActionType:
 
77
            action = new KHotKeys::CommandUrlAction( data );
 
78
            break;
 
79
 
 
80
        case KHotKeys::Action::DBusActionType:
 
81
            action = new KHotKeys::DBusAction( data );
 
82
            break;
 
83
 
 
84
        case KHotKeys::Action::KeyboardInputActionType:
 
85
            action = new KHotKeys::KeyboardInputAction( data );
 
86
            break;
 
87
 
 
88
        case KHotKeys::Action::MenuEntryActionType:
 
89
            action = new KHotKeys::MenuEntryAction( data );
 
90
            break;
 
91
 
 
92
        default:
 
93
            Q_ASSERT(false);
 
94
            return NULL;
 
95
        }
 
96
 
 
97
    data->set_action(action);
 
98
    return action;
 
99
    }
 
100
 
 
101
 
 
102
void HotkeysTreeViewContextMenu::slotAboutToShowForCurrent()
 
103
    {
 
104
    _index = _view->currentIndex();
 
105
 
 
106
    slotAboutToShow();
 
107
    }
 
108
 
 
109
 
 
110
void HotkeysTreeViewContextMenu::slotAboutToShow()
 
111
    {
 
112
    clear();
 
113
 
 
114
    if (_index.isValid())
 
115
        {
 
116
        KHotKeys::ActionDataBase *element = _view->model()->indexToActionDataBase(_index);
 
117
        KHotKeys::ActionDataGroup *group =  _view->model()->indexToActionDataGroup(_index);
 
118
        bool isGroup = group;   // Is the current element a group
 
119
        if (!isGroup)
 
120
            {
 
121
            group = element->parent();
 
122
            }
 
123
 
 
124
        // Create the create actions
 
125
        createTriggerMenus(group->allowedTriggerTypes(), group->allowedActionTypes());
 
126
 
 
127
        // It is not allowed to create a subgroup for a system group.
 
128
        if (!group->is_system_group())
 
129
            {
 
130
            addAction( i18n("New Group") , this, SLOT(newGroupAction()) );
 
131
            }
 
132
 
 
133
        // It is not allowed to delete a system group
 
134
        if (!(isGroup && group->is_system_group()))
 
135
            {
 
136
            // Item related actions
 
137
            addAction( i18n("Delete"), this, SLOT(deleteAction()) );
 
138
            }
 
139
        }
 
140
    else
 
141
        {
 
142
        createTriggerMenus(KHotKeys::Trigger::AllTypes, KHotKeys::Action::AllTypes);
 
143
        addAction( i18n("New Group") , this, SLOT(newGroupAction()) );
 
144
        }
 
145
 
 
146
    addSeparator();
 
147
    addAction( i18n("Export Group..."), this, SLOT(exportAction()) );
 
148
    addAction( i18n("Import..."), this, SLOT(importAction()) );
 
149
    }
 
150
 
 
151
 
 
152
void HotkeysTreeViewContextMenu::createTriggerMenus(
 
153
        KHotKeys::Trigger::TriggerTypes triggerTypes,
 
154
        KHotKeys::Action::ActionTypes actionTypes)
 
155
    {
 
156
    QMenu *newMenu = new QMenu(i18n("New"));
 
157
 
 
158
    if (triggerTypes & KHotKeys::Trigger::ShortcutTriggerType)
 
159
        {
 
160
        QSignalMapper *mapper = new QSignalMapper(this);
 
161
 
 
162
        QMenu *menu = new QMenu( i18n("Global Shortcut") );
 
163
        populateTriggerMenu(menu, mapper, actionTypes);
 
164
        newMenu->addMenu(menu);
 
165
 
 
166
        connect(
 
167
            mapper, SIGNAL(mapped(int)),
 
168
            this, SLOT(newGlobalShortcutActionAction(int)) );
 
169
        }
 
170
 
 
171
    if (triggerTypes & KHotKeys::Trigger::WindowTriggerType)
 
172
        {
 
173
        QSignalMapper *mapper = new QSignalMapper(this);
 
174
 
 
175
        QMenu *menu = new QMenu( i18n("Window Action") );
 
176
        populateTriggerMenu(menu, mapper, actionTypes);
 
177
        newMenu->addMenu(menu);
 
178
 
 
179
        connect(
 
180
            mapper, SIGNAL(mapped(int)),
 
181
            this, SLOT(newWindowTriggerActionAction(int)) );
 
182
        }
 
183
 
 
184
    if (triggerTypes & KHotKeys::Trigger::GestureTriggerType)
 
185
        {
 
186
        QSignalMapper *mapper = new QSignalMapper(this);
 
187
 
 
188
        QMenu *menu = new QMenu( i18n("Mouse Gesture Action") );
 
189
        populateTriggerMenu(menu, mapper, actionTypes);
 
190
        newMenu->addMenu(menu);
 
191
 
 
192
        connect(
 
193
            mapper, SIGNAL(mapped(int)),
 
194
            this, SLOT(newMouseGestureTriggerActionAction(int)) );
 
195
        }
 
196
 
 
197
    addMenu(newMenu);
 
198
    }
 
199
 
 
200
 
 
201
void HotkeysTreeViewContextMenu::importAction()
 
202
    {
 
203
    KUrl url = KFileDialog::getOpenFileName(KUrl(), "*.khotkeys", this);
 
204
    if (!url.isEmpty())
 
205
        {
 
206
        KConfig config(url.path(), KConfig::SimpleConfig);
 
207
        _view->model()->importInputActions(_index, config);
 
208
        }
 
209
    }
 
210
 
 
211
 
 
212
void HotkeysTreeViewContextMenu::exportAction()
 
213
    {
 
214
    KHotkeysExportDialog *widget = new KHotkeysExportDialog(this);
 
215
 
 
216
    KHotKeys::ActionDataGroup *group =  _view->model()->indexToActionDataGroup(_index);
 
217
    if (!group)
 
218
        group = _view->model()->indexToActionDataBase(_index)->parent();
 
219
 
 
220
    widget->setImportId(group->importId());
 
221
    widget->setAllowMerging(group->allowMerging());
 
222
 
 
223
    if (widget->exec() == QDialog::Accepted)
 
224
        {
 
225
        KHotKeys::ActionState state;
 
226
        switch (widget->state())
 
227
            {
 
228
            case 0:
 
229
                state = KHotKeys::Retain;
 
230
                break;
 
231
 
 
232
            case 1:
 
233
                state = KHotKeys::Enabled;
 
234
                break;
 
235
 
 
236
            case 2:
 
237
                state = KHotKeys::Disabled;
 
238
                break;
 
239
 
 
240
            default:
 
241
                // Unknown value alled to our ui file. Use disabled as a
 
242
                // default.
 
243
                Q_ASSERT(false);
 
244
                state = KHotKeys::Disabled;
 
245
                break;
 
246
            }
 
247
 
 
248
        QString id = widget->importId();
 
249
        KUrl url   = widget->url();
 
250
        bool allowMerging = widget->allowMerging();
 
251
        if (!url.isEmpty())
 
252
            {
 
253
            KConfig config(url.path(), KConfig::SimpleConfig);
 
254
            _view->model()->exportInputActions(_index, config, id, state, allowMerging);
 
255
            }
 
256
        }
 
257
    delete widget;
 
258
    }
 
259
 
 
260
 
 
261
void HotkeysTreeViewContextMenu::populateTriggerMenu(
 
262
        QMenu *menu,
 
263
        QSignalMapper *mapper,
 
264
        KHotKeys::Action::ActionTypes types)
 
265
    {
 
266
    if (types & KHotKeys::Action::CommandUrlActionType)
 
267
        {
 
268
        mapper->setMapping(
 
269
            menu->addAction( i18n("Command/URL"), mapper, SLOT(map()) ),
 
270
            KHotKeys::Action::CommandUrlActionType );
 
271
        }
 
272
 
 
273
    if (types & KHotKeys::Action::DBusActionType)
 
274
        {
 
275
        mapper->setMapping(
 
276
            menu->addAction( i18n("D-Bus Command"), mapper, SLOT(map()) ),
 
277
            KHotKeys::Action::DBusActionType );
 
278
        }
 
279
 
 
280
    if (types & KHotKeys::Action::MenuEntryActionType)
 
281
        {
 
282
        mapper->setMapping(
 
283
            menu->addAction( i18n("K-Menu Entry"), mapper, SLOT(map()) ),
 
284
            KHotKeys::Action::MenuEntryActionType );
 
285
        }
 
286
 
 
287
    if (types & KHotKeys::Action::KeyboardInputActionType)
 
288
        {
 
289
        mapper->setMapping(
 
290
            menu->addAction( i18n("Send Keyboard Input"), mapper, SLOT(map()) ),
 
291
            KHotKeys::Action::KeyboardInputActionType );
 
292
        }
 
293
    }
 
294
 
 
295
 
 
296
void HotkeysTreeViewContextMenu::newGlobalShortcutActionAction( int actionType )
 
297
    {
 
298
    QModelIndex parent;      // == root element
 
299
    if (!_index.isValid()
 
300
        || _view->model()->data( _index.sibling( _index.row(), KHotkeysModel::IsGroupColumn)).toBool())
 
301
        {
 
302
        // if the index is invalid (root index) or represents an group use it.
 
303
        parent = _index;
 
304
        }
 
305
    else
 
306
        {
 
307
        // It is an action. Take the parent.
 
308
        parent = _index.parent();
 
309
        }
 
310
 
 
311
    KHotKeys::SimpleActionData *data =
 
312
        new KHotKeys::SimpleActionData( 0, i18n("New Action"), i18n("Comment"));
 
313
    data->set_trigger( new KHotKeys::ShortcutTrigger( data, KShortcut() ) );
 
314
    data->enable();
 
315
 
 
316
    createActionFromType(actionType, data);
 
317
 
 
318
    QModelIndex newAct = _view->model()->insertActionData(data, parent);
 
319
    _view->setCurrentIndex(newAct);
 
320
    _view->edit(newAct);
 
321
    _view->resizeColumnToContents(KHotkeysModel::NameColumn);
 
322
    }
 
323
 
 
324
 
 
325
void HotkeysTreeViewContextMenu::newMouseGestureTriggerActionAction( int actionType )
 
326
    {
 
327
    QModelIndex parent;      // == root element
 
328
    if (!_index.isValid()
 
329
        || _view->model()->data( _index.sibling( _index.row(), KHotkeysModel::IsGroupColumn)).toBool())
 
330
        {
 
331
        // if the index is invalid (root index) or represents an group use it.
 
332
        parent = _index;
 
333
        }
 
334
    else
 
335
        {
 
336
        // It is an action. Take the parent.
 
337
        parent = _index.parent();
 
338
        }
 
339
 
 
340
    KHotKeys::SimpleActionData *data =
 
341
        new KHotKeys::SimpleActionData( 0, i18n("New Action"), i18n("Comment"));
 
342
    data->set_trigger( new KHotKeys::GestureTrigger(data) );
 
343
    data->enable();
 
344
 
 
345
    createActionFromType(actionType, data);
 
346
 
 
347
    QModelIndex newAct = _view->model()->insertActionData(data, parent);
 
348
    _view->setCurrentIndex(newAct);
 
349
    _view->edit(newAct);
 
350
    _view->resizeColumnToContents(KHotkeysModel::NameColumn);
 
351
    }
 
352
 
 
353
 
 
354
void HotkeysTreeViewContextMenu::newWindowTriggerActionAction( int actionType )
 
355
    {
 
356
    QModelIndex parent;      // == root element
 
357
    if (!_index.isValid()
 
358
        || _view->model()->data( _index.sibling( _index.row(), KHotkeysModel::IsGroupColumn)).toBool())
 
359
        {
 
360
        // if the index is invalid (root index) or represents an group use it.
 
361
        parent = _index;
 
362
        }
 
363
    else
 
364
        {
 
365
        // It is an action. Take the parent.
 
366
        parent = _index.parent();
 
367
        }
 
368
 
 
369
    KHotKeys::SimpleActionData *data =
 
370
        new KHotKeys::SimpleActionData( 0, i18n("New Action"), i18n("Comment"));
 
371
    data->set_trigger( new KHotKeys::WindowTrigger(data) );
 
372
    data->enable();
 
373
 
 
374
    createActionFromType(actionType, data);
 
375
 
 
376
    QModelIndex newAct = _view->model()->insertActionData(data, parent);
 
377
    _view->setCurrentIndex(newAct);
 
378
    _view->edit(newAct);
 
379
    _view->resizeColumnToContents(KHotkeysModel::NameColumn);
 
380
    }
 
381
 
 
382
 
 
383
void HotkeysTreeViewContextMenu::newGroupAction()
 
384
    {
 
385
    QModelIndex parent;      // == root element
 
386
    if (!_index.isValid()
 
387
        || _view->model()->data( _index.sibling( _index.row(), KHotkeysModel::IsGroupColumn)).toBool())
 
388
        {
 
389
        // if the index is invalid (root index) or represents an group use it.
 
390
        parent = _index;
 
391
        }
 
392
    else
 
393
        {
 
394
        // It is an action. Take the parent.
 
395
        parent = _index.parent();
 
396
        }
 
397
 
 
398
    QModelIndex newGroup = _view->model()->addGroup(parent);
 
399
    _view->setCurrentIndex(newGroup);
 
400
    _view->edit(newGroup);
 
401
    _view->resizeColumnToContents(KHotkeysModel::NameColumn);
 
402
    }
 
403
 
 
404
 
 
405
void HotkeysTreeViewContextMenu::deleteAction()
 
406
    {
 
407
    if (!_index.isValid())
 
408
        {
 
409
        Q_ASSERT( _index.isValid() );
 
410
        return;
 
411
        }
 
412
 
 
413
    bool deletionSuccess;
 
414
    deletionSuccess = _view->model()->removeRow(_index.row(), _index.parent());
 
415
    Q_ASSERT(deletionSuccess == true);
 
416
 
 
417
    _view->setCurrentIndex(QModelIndex());
 
418
    }
 
419
 
 
420
 
 
421
#include "moc_hotkeys_context_menu.cpp"
 
422