not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / khotkeys / kcm_hotkeys / hotkeys_tree_view.cpp
blobe169696044589869328fdc0f3b8880381e7cee91
1 /*
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"
22 #include "hotkeys_model.h"
24 #include "actions/actions.h"
25 #include "action_data/action_data_group.h"
26 #include "action_data/simple_action_data.h"
28 #include "KDE/KLocale"
29 #include "KDE/KDebug"
31 #include <QtCore/QSignalMapper>
32 #include <QtGui/QContextMenuEvent>
35 HotkeysTreeViewContextMenu::HotkeysTreeViewContextMenu( const QModelIndex &index, HotkeysTreeView *parent )
36 : QMenu(parent)
37 ,_index(index)
38 ,_view(parent)
40 setTitle( i18n("Test") );
42 if (index.isValid())
45 KHotKeys::ActionDataBase *element = parent->model()->indexToActionDataBase(index);
46 KHotKeys::ActionDataGroup *group = parent->model()->indexToActionDataGroup(index);
47 bool isGroup = group; // Is the current element a group
48 if (!isGroup)
50 group = element->parent();
53 // Create the create actions
54 createTriggerMenus(group->allowedTriggerTypes(), group->allowedActionTypes());
56 // It is not allowed to create a subgroup for a system group.
57 if (!group->is_system_group())
59 addAction( i18n("New Group") , this, SLOT(newGroupAction()) );
62 // It is not allowed to delete a system group
63 if (!(isGroup && group->is_system_group()))
65 // Global actions
66 addSeparator();
68 // Item related actions
69 addAction( i18n("Delete"), this, SLOT(deleteAction()) );
72 else
74 createTriggerMenus(KHotKeys::Trigger::AllTypes, KHotKeys::Action::AllTypes);
75 addAction( i18n("New Group") , this, SLOT(newGroupAction()) );
80 HotkeysTreeViewContextMenu::~HotkeysTreeViewContextMenu()
83 void HotkeysTreeViewContextMenu::createTriggerMenus(
84 KHotKeys::Trigger::TriggerTypes triggerTypes,
85 KHotKeys::Action::ActionTypes actionTypes)
87 if (triggerTypes & KHotKeys::Trigger::ShortcutTriggerType)
89 QSignalMapper *mapper = new QSignalMapper(this);
91 QMenu *menu = new QMenu( i18n("New Global Shortcut") );
92 populateTriggerMenu(menu, mapper, actionTypes);
93 addMenu(menu);
95 connect(
96 mapper, SIGNAL(mapped(int)),
97 this, SLOT(newGlobalShortcutActionAction(int)) );
102 void HotkeysTreeViewContextMenu::populateTriggerMenu(
103 QMenu *menu,
104 QSignalMapper *mapper,
105 KHotKeys::Action::ActionTypes types)
107 if (types & KHotKeys::Action::CommandUrlActionType)
109 mapper->setMapping(
110 menu->addAction( i18n("Command/URL"), mapper, SLOT(map()) ),
111 KHotKeys::Action::CommandUrlActionType );
114 if (types & KHotKeys::Action::DBusActionType)
116 mapper->setMapping(
117 menu->addAction( i18n("D-Bus Command"), mapper, SLOT(map()) ),
118 KHotKeys::Action::DBusActionType );
121 if (types & KHotKeys::Action::MenuEntryActionType)
123 mapper->setMapping(
124 menu->addAction( i18n("K-Menu Entry"), mapper, SLOT(map()) ),
125 KHotKeys::Action::MenuEntryActionType );
129 void HotkeysTreeViewContextMenu::newGlobalShortcutActionAction( int actionType )
131 QModelIndex parent; // == root element
132 if (!_index.isValid()
133 || _view->model()->data( _index.sibling( _index.row(), KHotkeysModel::IsGroupColumn)).toBool())
135 // if the index is invalid (root index) or represents an group use it.
136 parent = _index;
138 else
140 // It is an action. Take the parent.
141 parent = _index.parent();
144 KHotKeys::SimpleActionData *data =
145 new KHotKeys::SimpleActionData( 0, i18n("New Group"), i18n("Comment"));
146 data->set_trigger( new KHotKeys::ShortcutTrigger( data, KShortcut() ) );
148 switch (actionType)
150 case KHotKeys::Action::MenuEntryActionType:
151 data->set_action( new KHotKeys::MenuEntryAction( data ));
152 break;
154 case KHotKeys::Action::CommandUrlActionType:
155 data->set_action( new KHotKeys::CommandUrlAction( data ));
156 break;
158 case KHotKeys::Action::DBusActionType:
159 data->set_action( new KHotKeys::DBusAction( data ));
160 break;
162 default:
163 Q_ASSERT(false);
164 return;
167 QModelIndex newAct = _view->model()->insertActionData(data, parent);
168 _view->setCurrentIndex(newAct);
169 _view->edit(newAct);
170 _view->resizeColumnToContents(0);
174 void HotkeysTreeViewContextMenu::newGroupAction()
176 QModelIndex parent; // == root element
177 if (!_index.isValid()
178 || _view->model()->data( _index.sibling( _index.row(), KHotkeysModel::IsGroupColumn)).toBool())
180 // if the index is invalid (root index) or represents an group use it.
181 parent = _index;
183 else
185 // It is an action. Take the parent.
186 parent = _index.parent();
189 QModelIndex newGroup = _view->model()->addGroup(parent);
190 _view->setCurrentIndex(newGroup);
191 _view->edit(newGroup);
192 _view->resizeColumnToContents(0);
196 void HotkeysTreeViewContextMenu::deleteAction()
198 if (!_index.isValid())
200 Q_ASSERT( _index.isValid() );
201 return;
203 Q_ASSERT( _view->model()->removeRow(_index.row(), _index.parent()) );
205 _view->setCurrentIndex(QModelIndex());
209 HotkeysTreeView::HotkeysTreeView( QWidget *parent )
210 : QTreeView(parent)
212 setObjectName("khotkeys treeview");
213 setAllColumnsShowFocus(true);
214 setDragEnabled(true);
215 setAcceptDrops(true);
216 setDropIndicatorShown(true);
220 HotkeysTreeView::~HotkeysTreeView()
224 void
225 HotkeysTreeView::contextMenuEvent( QContextMenuEvent *event )
227 QModelIndex index = indexAt(event->pos());
228 // KHotKeys::ActionDataBase *item = model()->indexToActionDataBase(index);
229 HotkeysTreeViewContextMenu menu( index, this );
230 menu.exec(event->globalPos());
234 void
235 HotkeysTreeView::modelReset()
237 kDebug();
238 resizeColumnToContents(0);
242 KHotkeysModel *HotkeysTreeView::model()
244 return dynamic_cast<KHotkeysModel*>( QTreeView::model() );
248 void
249 HotkeysTreeView::setModel( QAbstractItemModel *model )
251 if (!dynamic_cast<KHotkeysModel*>( model ))
253 Q_ASSERT(dynamic_cast<KHotkeysModel*>( model ));
254 return;
256 QTreeView::setModel(model);
258 setAllColumnsShowFocus(true);
259 setAlternatingRowColors(true);
261 setSelectionBehavior( QAbstractItemView::SelectRows );
262 setSelectionMode( QAbstractItemView::SingleSelection );
264 connect(
265 model, SIGNAL(modelReset()),
266 this, SLOT(modelReset()));
269 #include "moc_hotkeys_tree_view.cpp"