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

« back to all changes in this revision

Viewing changes to khotkeys/libkhotkeysprivate/triggers/shortcut_trigger.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) 1999-2001 Lubos Lunak <l.lunak@kde.org>
 
3
   Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>
 
4
 
 
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.
 
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 "triggers/triggers.h"
 
21
#include "action_data/action_data.h"
 
22
#include "windows_handler.h"
 
23
 
 
24
#include <KDE/KAction>
 
25
#include <KDE/KConfigGroup>
 
26
#include <KDE/KDebug>
 
27
 
 
28
#include "shortcuts_handler.h"
 
29
 
 
30
namespace KHotKeys {
 
31
 
 
32
ShortcutTriggerVisitor::~ShortcutTriggerVisitor()
 
33
    {}
 
34
 
 
35
 
 
36
ShortcutTrigger::ShortcutTrigger(
 
37
        ActionData* data_P,
 
38
        const KShortcut& shortcut,
 
39
        const QUuid &uuid )
 
40
    :   Trigger( data_P ),
 
41
        _uuid(uuid),
 
42
        _active(false),
 
43
        _shortcut(shortcut)
 
44
    {
 
45
    }
 
46
 
 
47
 
 
48
ShortcutTrigger::~ShortcutTrigger()
 
49
    {
 
50
    keyboard_handler->removeAction( _uuid );
 
51
    }
 
52
 
 
53
 
 
54
void ShortcutTrigger::accept(TriggerVisitor& visitor)
 
55
    {
 
56
    if (ShortcutTriggerVisitor *v = dynamic_cast<ShortcutTriggerVisitor*>(&visitor))
 
57
        {
 
58
        v->visit(*this);
 
59
        }
 
60
    else
 
61
        {
 
62
        kDebug() << "Visitor error";
 
63
        }
 
64
    }
 
65
 
 
66
 
 
67
void ShortcutTrigger::aboutToBeErased()
 
68
    {
 
69
    disable();
 
70
    }
 
71
 
 
72
 
 
73
void ShortcutTrigger::activate( bool newState )
 
74
    {
 
75
#ifdef KHOTKEYS_TRACE
 
76
    kDebug() << "new:" << newState << "old:" << _active;
 
77
#endif
 
78
    // If there is no change in state just return.
 
79
    if (newState == _active)
 
80
        return;
 
81
 
 
82
    _active = newState;
 
83
 
 
84
    if (_active)
 
85
        {
 
86
        QString name = data
 
87
            ? data->name()
 
88
            : "TODO";
 
89
 
 
90
        // FIXME: The following workaround tries to prevent having two actions with
 
91
        // the same uuid. That happens wile exporting/importing actions. The uuid
 
92
        // is exported too.
 
93
        KAction *act = keyboard_handler->addAction( _uuid, name, _shortcut );
 
94
        // addAction can change the uuid. That's why we store the uuid from the
 
95
        // action
 
96
        _uuid = act->objectName();
 
97
 
 
98
        connect(
 
99
            act, SIGNAL(triggered(bool)),
 
100
            this, SLOT(trigger()) );
 
101
 
 
102
        connect(
 
103
            act, SIGNAL(globalShortcutChanged(const QKeySequence&)),
 
104
            this, SIGNAL(globalShortcutChanged(const QKeySequence&)));
 
105
        }
 
106
    else
 
107
        {
 
108
        // Disable the trigger. Delete the action.
 
109
        KAction *action = qobject_cast<KAction*>(keyboard_handler->getAction( _uuid ));
 
110
        if(action)
 
111
            {
 
112
            // In case the shortcut was changed from the kcm.
 
113
            _shortcut = action->globalShortcut();
 
114
            keyboard_handler->removeAction(_uuid);
 
115
            }
 
116
        }
 
117
    }
 
118
 
 
119
 
 
120
void ShortcutTrigger::cfg_write( KConfigGroup& cfg_P ) const
 
121
    {
 
122
    base::cfg_write( cfg_P );
 
123
    cfg_P.writeEntry( "Key", shortcut().toString());
 
124
    cfg_P.writeEntry( "Type", "SHORTCUT" ); // overwrites value set in base::cfg_write()
 
125
    cfg_P.writeEntry( "Uuid", _uuid.toString() );
 
126
    }
 
127
 
 
128
 
 
129
ShortcutTrigger* ShortcutTrigger::copy( ActionData* data_P ) const
 
130
    {
 
131
    return new ShortcutTrigger( data_P ? data_P : data, shortcut(), QUuid::createUuid());
 
132
    }
 
133
 
 
134
 
 
135
const QString ShortcutTrigger::description() const
 
136
    {
 
137
    return i18n( "Shortcut trigger: " ) + shortcut().toString();
 
138
    }
 
139
 
 
140
 
 
141
void ShortcutTrigger::disable()
 
142
    {
 
143
    activate(false);
 
144
 
 
145
    // Unregister the shortcut with kglobalaccel
 
146
    KAction *action = qobject_cast<KAction*>(keyboard_handler->getAction( _uuid ));
 
147
    if(action)
 
148
        {
 
149
        // In case the shortcut was changed from the kcm.
 
150
        _shortcut = action->globalShortcut();
 
151
 
 
152
 
 
153
        // Unregister the global shortcut.
 
154
        action->forgetGlobalShortcut();
 
155
        keyboard_handler->removeAction(_uuid);
 
156
        }
 
157
    }
 
158
 
 
159
 
 
160
void ShortcutTrigger::enable()
 
161
    {
 
162
    // To enable the shortcut we have to just register it once with
 
163
    // kglobalaccel and deactivate it immediately
 
164
    activate(true);
 
165
    activate(false);
 
166
    }
 
167
 
 
168
 
 
169
void ShortcutTrigger::set_key_sequence( const QKeySequence &seq )
 
170
    {
 
171
    // Get the action from the keyboard handler
 
172
    KAction *action = qobject_cast<KAction*>(keyboard_handler->getAction( _uuid ));
 
173
    if (!action)
 
174
        {
 
175
        _shortcut.setPrimary(seq);
 
176
        }
 
177
    else
 
178
        {
 
179
        action->setGlobalShortcut(
 
180
            KShortcut(seq),
 
181
            KAction::ActiveShortcut,
 
182
            KAction::NoAutoloading );
 
183
        }
 
184
    }
 
185
 
 
186
 
 
187
KShortcut ShortcutTrigger::shortcut() const
 
188
    {
 
189
    // Get the action from the keyboard handler
 
190
    KAction *action = qobject_cast<KAction*>(keyboard_handler->getAction( _uuid ));
 
191
    if (!action)
 
192
        {
 
193
        // Not active!
 
194
        return _shortcut;
 
195
        }
 
196
 
 
197
    return action->globalShortcut();
 
198
    }
 
199
 
 
200
 
 
201
void ShortcutTrigger::trigger()
 
202
    {
 
203
    if (khotkeys_active())
 
204
        {
 
205
        windows_handler->set_action_window( 0 ); // use active window
 
206
        data->execute();
 
207
        }
 
208
    }
 
209
 
 
210
 
 
211
} // namespace KHotKeys
 
212