2
Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>
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.
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.
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.
20
#include "hotkeys_tree_view.h"
21
#include "hotkeys_context_menu.h"
22
#include "hotkeys_export_widget.h"
24
#include "hotkeys_model.h"
26
#include "actions/actions.h"
27
#include "action_data/action_data_group.h"
28
#include "action_data/simple_action_data.h"
31
#include <KDE/KFileDialog>
32
#include <KDE/KLocale>
35
#include <QtCore/QSignalMapper>
36
#include <QtGui/QContextMenuEvent>
39
HotkeysTreeViewContextMenu::HotkeysTreeViewContextMenu( const QModelIndex &index, HotkeysTreeView *parent )
44
setTitle( i18n("Test") );
46
connect(this, SIGNAL(aboutToShow()),
47
SLOT(slotAboutToShow()));
51
HotkeysTreeViewContextMenu::HotkeysTreeViewContextMenu( HotkeysTreeView *parent )
56
setTitle( i18n("Test") );
58
connect(this, SIGNAL(aboutToShow()),
59
SLOT(slotAboutToShowForCurrent()));
63
HotkeysTreeViewContextMenu::~HotkeysTreeViewContextMenu()
68
HotkeysTreeViewContextMenu::createActionFromType(
70
KHotKeys::SimpleActionData* data
73
KHotKeys::Action *action = NULL;
76
case KHotKeys::Action::CommandUrlActionType:
77
action = new KHotKeys::CommandUrlAction( data );
80
case KHotKeys::Action::DBusActionType:
81
action = new KHotKeys::DBusAction( data );
84
case KHotKeys::Action::KeyboardInputActionType:
85
action = new KHotKeys::KeyboardInputAction( data );
88
case KHotKeys::Action::MenuEntryActionType:
89
action = new KHotKeys::MenuEntryAction( data );
97
data->set_action(action);
102
void HotkeysTreeViewContextMenu::slotAboutToShowForCurrent()
104
_index = _view->currentIndex();
110
void HotkeysTreeViewContextMenu::slotAboutToShow()
114
if (_index.isValid())
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
121
group = element->parent();
124
// Create the create actions
125
createTriggerMenus(group->allowedTriggerTypes(), group->allowedActionTypes());
127
// It is not allowed to create a subgroup for a system group.
128
if (!group->is_system_group())
130
addAction( i18n("New Group") , this, SLOT(newGroupAction()) );
133
// It is not allowed to delete a system group
134
if (!(isGroup && group->is_system_group()))
136
// Item related actions
137
addAction( i18n("Delete"), this, SLOT(deleteAction()) );
142
createTriggerMenus(KHotKeys::Trigger::AllTypes, KHotKeys::Action::AllTypes);
143
addAction( i18n("New Group") , this, SLOT(newGroupAction()) );
147
addAction( i18n("Export Group..."), this, SLOT(exportAction()) );
148
addAction( i18n("Import..."), this, SLOT(importAction()) );
152
void HotkeysTreeViewContextMenu::createTriggerMenus(
153
KHotKeys::Trigger::TriggerTypes triggerTypes,
154
KHotKeys::Action::ActionTypes actionTypes)
156
QMenu *newMenu = new QMenu(i18n("New"));
158
if (triggerTypes & KHotKeys::Trigger::ShortcutTriggerType)
160
QSignalMapper *mapper = new QSignalMapper(this);
162
QMenu *menu = new QMenu( i18n("Global Shortcut") );
163
populateTriggerMenu(menu, mapper, actionTypes);
164
newMenu->addMenu(menu);
167
mapper, SIGNAL(mapped(int)),
168
this, SLOT(newGlobalShortcutActionAction(int)) );
171
if (triggerTypes & KHotKeys::Trigger::WindowTriggerType)
173
QSignalMapper *mapper = new QSignalMapper(this);
175
QMenu *menu = new QMenu( i18n("Window Action") );
176
populateTriggerMenu(menu, mapper, actionTypes);
177
newMenu->addMenu(menu);
180
mapper, SIGNAL(mapped(int)),
181
this, SLOT(newWindowTriggerActionAction(int)) );
184
if (triggerTypes & KHotKeys::Trigger::GestureTriggerType)
186
QSignalMapper *mapper = new QSignalMapper(this);
188
QMenu *menu = new QMenu( i18n("Mouse Gesture Action") );
189
populateTriggerMenu(menu, mapper, actionTypes);
190
newMenu->addMenu(menu);
193
mapper, SIGNAL(mapped(int)),
194
this, SLOT(newMouseGestureTriggerActionAction(int)) );
201
void HotkeysTreeViewContextMenu::importAction()
203
KUrl url = KFileDialog::getOpenFileName(KUrl(), "*.khotkeys", this);
206
KConfig config(url.path(), KConfig::SimpleConfig);
207
_view->model()->importInputActions(_index, config);
212
void HotkeysTreeViewContextMenu::exportAction()
214
KHotkeysExportDialog *widget = new KHotkeysExportDialog(this);
216
KHotKeys::ActionDataGroup *group = _view->model()->indexToActionDataGroup(_index);
218
group = _view->model()->indexToActionDataBase(_index)->parent();
220
widget->setImportId(group->importId());
221
widget->setAllowMerging(group->allowMerging());
223
if (widget->exec() == QDialog::Accepted)
225
KHotKeys::ActionState state;
226
switch (widget->state())
229
state = KHotKeys::Retain;
233
state = KHotKeys::Enabled;
237
state = KHotKeys::Disabled;
241
// Unknown value alled to our ui file. Use disabled as a
244
state = KHotKeys::Disabled;
248
QString id = widget->importId();
249
KUrl url = widget->url();
250
bool allowMerging = widget->allowMerging();
253
KConfig config(url.path(), KConfig::SimpleConfig);
254
_view->model()->exportInputActions(_index, config, id, state, allowMerging);
261
void HotkeysTreeViewContextMenu::populateTriggerMenu(
263
QSignalMapper *mapper,
264
KHotKeys::Action::ActionTypes types)
266
if (types & KHotKeys::Action::CommandUrlActionType)
269
menu->addAction( i18n("Command/URL"), mapper, SLOT(map()) ),
270
KHotKeys::Action::CommandUrlActionType );
273
if (types & KHotKeys::Action::DBusActionType)
276
menu->addAction( i18n("D-Bus Command"), mapper, SLOT(map()) ),
277
KHotKeys::Action::DBusActionType );
280
if (types & KHotKeys::Action::MenuEntryActionType)
283
menu->addAction( i18n("K-Menu Entry"), mapper, SLOT(map()) ),
284
KHotKeys::Action::MenuEntryActionType );
287
if (types & KHotKeys::Action::KeyboardInputActionType)
290
menu->addAction( i18n("Send Keyboard Input"), mapper, SLOT(map()) ),
291
KHotKeys::Action::KeyboardInputActionType );
296
void HotkeysTreeViewContextMenu::newGlobalShortcutActionAction( int actionType )
298
QModelIndex parent; // == root element
299
if (!_index.isValid()
300
|| _view->model()->data( _index.sibling( _index.row(), KHotkeysModel::IsGroupColumn)).toBool())
302
// if the index is invalid (root index) or represents an group use it.
307
// It is an action. Take the parent.
308
parent = _index.parent();
311
KHotKeys::SimpleActionData *data =
312
new KHotKeys::SimpleActionData( 0, i18n("New Action"), i18n("Comment"));
313
data->set_trigger( new KHotKeys::ShortcutTrigger( data, KShortcut() ) );
316
createActionFromType(actionType, data);
318
QModelIndex newAct = _view->model()->insertActionData(data, parent);
319
_view->setCurrentIndex(newAct);
321
_view->resizeColumnToContents(KHotkeysModel::NameColumn);
325
void HotkeysTreeViewContextMenu::newMouseGestureTriggerActionAction( int actionType )
327
QModelIndex parent; // == root element
328
if (!_index.isValid()
329
|| _view->model()->data( _index.sibling( _index.row(), KHotkeysModel::IsGroupColumn)).toBool())
331
// if the index is invalid (root index) or represents an group use it.
336
// It is an action. Take the parent.
337
parent = _index.parent();
340
KHotKeys::SimpleActionData *data =
341
new KHotKeys::SimpleActionData( 0, i18n("New Action"), i18n("Comment"));
342
data->set_trigger( new KHotKeys::GestureTrigger(data) );
345
createActionFromType(actionType, data);
347
QModelIndex newAct = _view->model()->insertActionData(data, parent);
348
_view->setCurrentIndex(newAct);
350
_view->resizeColumnToContents(KHotkeysModel::NameColumn);
354
void HotkeysTreeViewContextMenu::newWindowTriggerActionAction( int actionType )
356
QModelIndex parent; // == root element
357
if (!_index.isValid()
358
|| _view->model()->data( _index.sibling( _index.row(), KHotkeysModel::IsGroupColumn)).toBool())
360
// if the index is invalid (root index) or represents an group use it.
365
// It is an action. Take the parent.
366
parent = _index.parent();
369
KHotKeys::SimpleActionData *data =
370
new KHotKeys::SimpleActionData( 0, i18n("New Action"), i18n("Comment"));
371
data->set_trigger( new KHotKeys::WindowTrigger(data) );
374
createActionFromType(actionType, data);
376
QModelIndex newAct = _view->model()->insertActionData(data, parent);
377
_view->setCurrentIndex(newAct);
379
_view->resizeColumnToContents(KHotkeysModel::NameColumn);
383
void HotkeysTreeViewContextMenu::newGroupAction()
385
QModelIndex parent; // == root element
386
if (!_index.isValid()
387
|| _view->model()->data( _index.sibling( _index.row(), KHotkeysModel::IsGroupColumn)).toBool())
389
// if the index is invalid (root index) or represents an group use it.
394
// It is an action. Take the parent.
395
parent = _index.parent();
398
QModelIndex newGroup = _view->model()->addGroup(parent);
399
_view->setCurrentIndex(newGroup);
400
_view->edit(newGroup);
401
_view->resizeColumnToContents(KHotkeysModel::NameColumn);
405
void HotkeysTreeViewContextMenu::deleteAction()
407
if (!_index.isValid())
409
Q_ASSERT( _index.isValid() );
413
bool deletionSuccess;
414
deletionSuccess = _view->model()->removeRow(_index.row(), _index.parent());
415
Q_ASSERT(deletionSuccess == true);
417
_view->setCurrentIndex(QModelIndex());
421
#include "moc_hotkeys_context_menu.cpp"