not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / khotkeys / libkhotkeysprivate / triggers / shortcut_trigger.cpp
blob9041f8ee03f9e993064cacd37c15d3dd3a6cbd7a
1 /*
2 Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org>
3 Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
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 "triggers/triggers.h"
21 #include "action_data/action_data.h"
22 #include "windows.h"
24 #include <KDE/KAction>
25 #include <KDE/KConfigGroup>
26 #include <KDE/KDebug>
28 #include "shortcuts_handler.h"
30 namespace KHotKeys {
32 ShortcutTrigger::ShortcutTrigger(
33 ActionData* data_P,
34 const KShortcut& shortcut,
35 const QUuid &uuid )
36 : Trigger( data_P ), _uuid(uuid)
38 QString name;
39 if (data_P)
41 name = data_P->name();
43 else
45 name = "TODO";
47 KAction *act = keyboard_handler->addAction( _uuid, name, shortcut );
48 connect(
49 act, SIGNAL(triggered(bool)),
50 this, SLOT(trigger()) );
52 connect(
53 act, SIGNAL(globalShortcutChanged(const QKeySequence&)),
54 this, SIGNAL(globalShortcutChanged(const QKeySequence&)));
58 ShortcutTrigger::ShortcutTrigger(
59 KConfigGroup& cfg_P
60 ,ActionData* data_P )
61 : Trigger( cfg_P, data_P )
62 ,_uuid( cfg_P.readEntry( "Uuid", QUuid::createUuid().toString()))
64 QString shortcutString = cfg_P.readEntry( "Key" );
66 // TODO: Check if this is still necessary
67 shortcutString.replace("Win+", "Meta+"); // Qt4 doesn't parse Win+, avoid a shortcut without modifier
69 KAction *act = keyboard_handler->addAction(
70 _uuid,
71 data_P->name(),
72 KShortcut(shortcutString));
74 connect(
75 act, SIGNAL(triggered(bool)),
76 this, SLOT(trigger()) );
78 connect(
79 act, SIGNAL(globalShortcutChanged(const QKeySequence&)),
80 this, SIGNAL(globalShortcutChanged(const QKeySequence&)));
84 ShortcutTrigger::~ShortcutTrigger()
86 keyboard_handler->removeAction( _uuid );
90 void ShortcutTrigger::aboutToBeErased()
92 kDebug();
93 KAction *action = qobject_cast<KAction*>(keyboard_handler->getAction( _uuid ));
94 if(action)
96 kDebug() << "removing the global shortcut";
97 action->forgetGlobalShortcut();
102 void ShortcutTrigger::activate( bool activate_P )
104 kDebug() << activate_P << " and " << khotkeys_active();
105 if( activate_P && khotkeys_active())
107 kDebug() << "TODO implement activate";
109 else
111 kDebug() << "TODO implement activate";
116 void ShortcutTrigger::cfg_write( KConfigGroup& cfg_P ) const
118 base::cfg_write( cfg_P );
119 cfg_P.writeEntry( "Key", shortcut().toString());
120 cfg_P.writeEntry( "Type", "SHORTCUT" ); // overwrites value set in base::cfg_write()
121 cfg_P.writeEntry( "Uuid", _uuid.toString() );
125 ShortcutTrigger* ShortcutTrigger::copy( ActionData* data_P ) const
127 kDebug() << "Shortcut_trigger::copy()";
128 return new ShortcutTrigger( data_P ? data_P : data, shortcut(), QUuid::createUuid());
132 const QString ShortcutTrigger::description() const
134 // CHECKME vice mods
135 return i18n( "Shortcut trigger: " ) + shortcut().toString();
136 // CHECKME i18n pro toString() ?
140 void ShortcutTrigger::set_key_sequence( const QKeySequence &seq )
142 // Get the action from the keyboard handler
143 KAction *action = qobject_cast<KAction*>(keyboard_handler->getAction( _uuid ));
144 Q_ASSERT(action);
145 if (!action) return;
147 // Set our key sequence
148 action->setGlobalShortcut(
149 KShortcut(seq),
150 KAction::ActiveShortcut,
151 KAction::NoAutoloading );
155 KShortcut ShortcutTrigger::shortcut() const
157 // Get the action from the keyboard handler
158 KAction *action = qobject_cast<KAction*>(keyboard_handler->getAction( _uuid ));
159 Q_ASSERT(action);
160 if (!action)
161 return KShortcut();
163 return action->globalShortcut();
167 void ShortcutTrigger::trigger()
169 kDebug() << data->name() << " was triggered";
170 windows_handler->set_action_window( 0 ); // use active window
171 data->execute();
175 } // namespace KHotKeys