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

« back to all changes in this revision

Viewing changes to khotkeys/libkhotkeysprivate/triggers/triggers.h

  • 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
#ifndef _TRIGGERS_H_
 
21
#define _TRIGGERS_H_
 
22
 
 
23
#include <QtCore/QList>
 
24
#include <QtCore/QMap>
 
25
#include <QtCore/QUuid>
 
26
 
 
27
#include <KDE/KShortcut>
 
28
 
 
29
#include <kdemacros.h>
 
30
#include "khotkeysglobal.h"
 
31
#include "voicesignature.h"
 
32
 
 
33
#include "input.h"
 
34
#include "windows_handler.h"
 
35
#include "triggers/gestures.h"
 
36
 
 
37
class QKeySequence;
 
38
 
 
39
namespace KHotKeys
 
40
{
 
41
 
 
42
class Windowdef_list;
 
43
class ActionData;
 
44
 
 
45
class TriggerVisitor
 
46
    {
 
47
public:
 
48
    virtual ~TriggerVisitor();
 
49
    };
 
50
 
 
51
 
 
52
class KDE_EXPORT Trigger
 
53
    {
 
54
    Q_DISABLE_COPY( Trigger )
 
55
 
 
56
    public:
 
57
 
 
58
        enum TriggerType
 
59
            {
 
60
            GestureTriggerType  = 0x01, //!< @see GestureTrigger
 
61
            ShortcutTriggerType = 0x02, //!< @see ShortcutTrigger
 
62
            WindowTriggerType   = 0x04, //!< @see WindowTrigger
 
63
            TriggerListType     = 0x08, //!< @see Trigger_list
 
64
            AllTypes            = 0xFF  //!< All types. For convenience.
 
65
            };
 
66
 
 
67
        Q_DECLARE_FLAGS(TriggerTypes, TriggerType)
 
68
 
 
69
        Trigger( ActionData* data_P );
 
70
        virtual ~Trigger();
 
71
        virtual void cfg_write( KConfigGroup& cfg_P ) const = 0;
 
72
        virtual Trigger* copy( ActionData* data_P ) const = 0;
 
73
        virtual const QString description() const = 0;
 
74
        virtual void activate( bool activate_P ) = 0;
 
75
 
 
76
        //! Disable the trigger
 
77
        virtual void disable();
 
78
 
 
79
        //! Enable the trigger
 
80
        virtual void enable();
 
81
 
 
82
        /**
 
83
         * The trigger will be erased permanently
 
84
         */
 
85
        virtual void aboutToBeErased();
 
86
 
 
87
        /**
 
88
         * The actual type for this trigger
 
89
         */
 
90
        virtual TriggerType type() const = 0;
 
91
 
 
92
        /**
 
93
         * Acyclic visitor pattern
 
94
         */
 
95
        virtual void accept(TriggerVisitor&) = 0;
 
96
 
 
97
    protected:
 
98
        ActionData* const data;
 
99
    };
 
100
 
 
101
Q_DECLARE_OPERATORS_FOR_FLAGS(Trigger::TriggerTypes)
 
102
 
 
103
 
 
104
class KDE_EXPORT Trigger_list
 
105
    : public QList< Trigger* >
 
106
    {
 
107
    Q_DISABLE_COPY( Trigger_list )
 
108
 
 
109
    public:
 
110
        Trigger_list( const QString& comment = QString() );
 
111
        virtual ~Trigger_list();
 
112
 
 
113
        void activate( bool activate_P );
 
114
        void cfg_write( KConfigGroup& cfg_P ) const;
 
115
        //! Some convenience typedef
 
116
        typedef QList< Trigger* >::Iterator Iterator;
 
117
        typedef QList< Trigger* >::ConstIterator ConstIterator;
 
118
        const QString comment() const;
 
119
        void set_comment(const QString &);
 
120
        Trigger_list* copy( ActionData* data_P ) const;
 
121
 
 
122
        //! Disable the trigger
 
123
        virtual void disable();
 
124
 
 
125
        //! Enable the trigger
 
126
        virtual void enable();
 
127
 
 
128
        /**
 
129
         * @reimp
 
130
         */
 
131
        void aboutToBeErased();
 
132
 
 
133
    private:
 
134
        QString _comment;
 
135
    };
 
136
 
 
137
 
 
138
class ShortcutTrigger;
 
139
class ShortcutTriggerVisitor
 
140
    {
 
141
public:
 
142
    virtual ~ShortcutTriggerVisitor();
 
143
    virtual void visit(ShortcutTrigger&) = 0;
 
144
    };
 
145
 
 
146
 
 
147
class KDE_EXPORT ShortcutTrigger
 
148
    : public QObject, public Trigger
 
149
    {
 
150
    Q_OBJECT
 
151
 
 
152
    typedef Trigger base;
 
153
    public:
 
154
        ShortcutTrigger(
 
155
            ActionData* data,
 
156
            const KShortcut& shortcut = KShortcut(),
 
157
            const QUuid &uuid = QUuid::createUuid() );
 
158
 
 
159
        virtual ~ShortcutTrigger();
 
160
        virtual void cfg_write( KConfigGroup& cfg_P ) const;
 
161
        virtual ShortcutTrigger* copy( ActionData* data_P ) const;
 
162
        virtual const QString description() const;
 
163
        KShortcut shortcut() const;
 
164
        virtual void activate( bool activate_P );
 
165
 
 
166
        void set_key_sequence( const QKeySequence &seq );
 
167
 
 
168
        virtual TriggerType type() const { return ShortcutTriggerType; }
 
169
 
 
170
        /**
 
171
         * @reimp
 
172
         */
 
173
        void aboutToBeErased();
 
174
 
 
175
        //! Disable the trigger
 
176
        virtual void disable();
 
177
 
 
178
        //! Enable the trigger
 
179
        virtual void enable();
 
180
 
 
181
        /**
 
182
         * Acyclic visitor pattern
 
183
         */
 
184
        virtual void accept(TriggerVisitor&);
 
185
 
 
186
    Q_SIGNALS:
 
187
 
 
188
        //! Emitted when the global shortcut is changed from somewhere else
 
189
        //  (Global Shortcuts KCM)
 
190
        void globalShortcutChanged(const QKeySequence&);
 
191
 
 
192
    public Q_SLOTS:
 
193
 
 
194
        void trigger();
 
195
 
 
196
    private:
 
197
 
 
198
        //! A persistent identifier for this shortcut
 
199
        QUuid _uuid;
 
200
 
 
201
        //! Are the conditions met?
 
202
        bool _active;
 
203
 
 
204
        /**
 
205
         * The Key Sequence associated with this Trigger. This is needed
 
206
         * because a inactive trigger doesn't register it's shortcut with
 
207
         * kde's global shortcuts registry so we have to remember the shortcut
 
208
         * ourselves. Beware of synchronizing saved state, global shortcuts
 
209
         * registry state and state of this var :-) .
 
210
         */
 
211
        KShortcut    _shortcut;
 
212
 
 
213
 
 
214
 
 
215
    };
 
216
 
 
217
 
 
218
class WindowTrigger;
 
219
class WindowTriggerVisitor
 
220
    {
 
221
public:
 
222
    virtual ~WindowTriggerVisitor();
 
223
    virtual void visit(WindowTrigger&) = 0;
 
224
    };
 
225
 
 
226
 
 
227
class KDE_EXPORT WindowTrigger : public QObject, public Trigger
 
228
    {
 
229
    Q_OBJECT
 
230
 
 
231
    Q_FLAGS(WindowEvents)
 
232
 
 
233
    typedef Trigger base;
 
234
 
 
235
    public:
 
236
 
 
237
        enum window_action_t
 
238
            {
 
239
            NONE                   = 0,
 
240
            WINDOW_APPEARS         = ( 1 << 0 ),        //!< The window is opened
 
241
            WINDOW_DISAPPEARS      = ( 1 << 1 ),        //!< The window is closed
 
242
            WINDOW_ACTIVATES       = ( 1 << 2 ),        //!< The window gets the focus
 
243
            WINDOW_DEACTIVATES     = ( 1 << 3 )         //!< The window loses the focus
 
244
            };
 
245
 
 
246
        Q_DECLARE_FLAGS(WindowEvents, window_action_t)
 
247
 
 
248
        WindowTrigger(
 
249
                ActionData* data,
 
250
                Windowdef_list* windowslist = NULL,
 
251
                WindowEvents window_actions = 0 );
 
252
 
 
253
        void setOnWindowEvents(WindowEvents events);
 
254
 
 
255
        virtual ~WindowTrigger();
 
256
        virtual void cfg_write( KConfigGroup& cfg_P ) const;
 
257
        virtual WindowTrigger* copy( ActionData* data_P ) const;
 
258
        virtual const QString description() const;
 
259
        const Windowdef_list* windows() const;
 
260
        void set_window_rules(Windowdef_list *list);
 
261
        Windowdef_list* windows();
 
262
        bool triggers_on( window_action_t w_action_P ) const;
 
263
        virtual void activate( bool activate_P );
 
264
 
 
265
        virtual TriggerType type() const { return WindowTriggerType; }
 
266
 
 
267
 
 
268
 
 
269
        /**
 
270
         * Acyclic visitor pattern
 
271
         */
 
272
        virtual void accept(TriggerVisitor&);
 
273
 
 
274
    protected Q_SLOTS:
 
275
        void window_added( WId window_P );
 
276
        void window_removed( WId window_P );
 
277
        void active_window_changed( WId window_P );
 
278
        void window_changed( WId window_P, unsigned int dirty_P );
 
279
 
 
280
    private:
 
281
 
 
282
        //! Useful code for all constructors
 
283
        void init();
 
284
 
 
285
        Windowdef_list* _windows;
 
286
 
 
287
        WindowEvents window_actions;
 
288
 
 
289
        typedef QMap< WId, bool > Windows_map;
 
290
 
 
291
        //! Internal cache. Remembers if a window is a match or not,
 
292
        Windows_map existing_windows;
 
293
 
 
294
        //! The last active window
 
295
        WId last_active_window;
 
296
 
 
297
        //! Is the trigger active?
 
298
        bool active;
 
299
    };
 
300
 
 
301
Q_DECLARE_OPERATORS_FOR_FLAGS(WindowTrigger::WindowEvents)
 
302
 
 
303
/**
 
304
 * This class handles the storage of gesture data; it also matches gestures
 
305
 * and links the gesture to an action.
 
306
 * One object equals one configured gesture.
 
307
 */
 
308
class GestureTrigger;
 
309
class GestureTriggerVisitor
 
310
    {
 
311
public:
 
312
    virtual ~GestureTriggerVisitor();
 
313
    virtual void visit(GestureTrigger&) = 0;
 
314
    };
 
315
 
 
316
class KDE_EXPORT GestureTrigger
 
317
    : public QObject, public Trigger
 
318
    {
 
319
    Q_OBJECT
 
320
    typedef Trigger base;
 
321
    public:
 
322
        GestureTrigger( ActionData* data, const StrokePoints& pointdata_P = StrokePoints() );
 
323
 
 
324
        virtual ~GestureTrigger();
 
325
        virtual void cfg_write( KConfigGroup& cfg_P ) const;
 
326
        virtual Trigger* copy( ActionData* data_P ) const;
 
327
        virtual const QString description() const;
 
328
        const StrokePoints& pointData() const;
 
329
 
 
330
        //! Set the point data of the gesture
 
331
        void setPointData(const StrokePoints &data);
 
332
        void setPointData(const QStringList &strings);
 
333
        void setKDE3Gesture(const QString &gestureCode);
 
334
 
 
335
        virtual void activate( bool activate_P );
 
336
 
 
337
        virtual TriggerType type() const { return GestureTriggerType; }
 
338
 
 
339
        /**
 
340
         * Acyclic visitor pattern
 
341
         */
 
342
        virtual void accept(TriggerVisitor&);
 
343
 
 
344
    protected Q_SLOTS:
 
345
        void handle_gesture( const StrokePoints& gesture_P );
 
346
    Q_SIGNALS:
 
347
        void gotScore( ActionData* const data, const qreal score );
 
348
 
 
349
    private:
 
350
        qreal comparePointData(const StrokePoints &a, const StrokePoints &b) const;
 
351
        inline qreal angleSquareDifference(qreal alpha, qreal beta) const;
 
352
 
 
353
        StrokePoints _pointdata;
 
354
    };
 
355
 
 
356
 
 
357
// FIXME: SOUND
 
358
#if 0
 
359
class KDE_EXPORT Voice_trigger
 
360
    : public QObject, public Trigger
 
361
    {
 
362
    Q_OBJECT
 
363
    typedef Trigger base;
 
364
    public:
 
365
        Voice_trigger( ActionData* data_P, const QString& Voice_P, const VoiceSignature & signature1_P, const VoiceSignature & signature2_P );
 
366
        Voice_trigger( KConfigGroup& cfg_P, ActionData* data_P );
 
367
        virtual ~Voice_trigger();
 
368
        virtual void cfg_write( KConfigGroup& cfg_P ) const;
 
369
        virtual Trigger* copy( ActionData* data_P ) const;
 
370
        virtual const QString description() const;
 
371
        const QString& voicecode() const;
 
372
        virtual void activate( bool activate_P );
 
373
        VoiceSignature voicesignature( int ech ) const;
 
374
 
 
375
        virtual TriggerType type() const { return SoundTrigger; }
 
376
    public slots:
 
377
        void handle_Voice(  );
 
378
    private:
 
379
        QString _voicecode;
 
380
        VoiceSignature _voicesignature[2];
 
381
    };
 
382
#endif
 
383
 
 
384
// FIXME: SOUND
 
385
#if 0
 
386
// Voice_trigger
 
387
inline
 
388
const QString& Voice_trigger::voicecode() const
 
389
    {
 
390
    return _voicecode;
 
391
    }
 
392
 
 
393
inline
 
394
VoiceSignature Voice_trigger::voicesignature(int ech) const
 
395
    {
 
396
    return _voicesignature[ech-1];
 
397
    }
 
398
#endif
 
399
} // namespace KHotKeys
 
400
 
 
401
 
 
402
#endif