triggers.h 10.6 KB
Newer Older
Michael Jansen's avatar
Michael Jansen committed
1
2
3
/*
   Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org>
   Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>
4

Michael Jansen's avatar
Michael Jansen committed
5
6
7
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.
8

Michael Jansen's avatar
Michael Jansen committed
9
10
11
12
13
14
15
16
17
18
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/
19
20
21
22

#ifndef _TRIGGERS_H_
#define _TRIGGERS_H_

Yuri Chornoivan's avatar
Yuri Chornoivan committed
23
24
25
#include <QList>
#include <QMap>
#include <QUuid>
26

27
#include <KShortcut>
28

Benjamin Meyer's avatar
Benjamin Meyer committed
29
#include "khotkeysglobal.h"
30
#include "voicesignature.h"
31
32

#include "input.h"
33
#include "windows_handler.h"
34
#include "triggers/gestures.h"
35

36
class QKeySequence;
37
38
39
40
41

namespace KHotKeys
{

class Windowdef_list;
42
class ActionData;
43

44
45
46
47
48
49
50
class TriggerVisitor
    {
public:
    virtual ~TriggerVisitor();
    };


51
class Q_DECL_EXPORT Trigger
52
    {
53
54
    Q_DISABLE_COPY( Trigger )

55
    public:
56
57

        enum TriggerType
58
            {
59
60
61
62
63
            GestureTriggerType  = 0x01, //!< @see GestureTrigger
            ShortcutTriggerType = 0x02, //!< @see ShortcutTrigger
            WindowTriggerType   = 0x04, //!< @see WindowTrigger
            TriggerListType     = 0x08, //!< @see Trigger_list
            AllTypes            = 0xFF  //!< All types. For convenience.
64
65
            };

66
67
        Q_DECLARE_FLAGS(TriggerTypes, TriggerType)

68
        Trigger( ActionData* data_P );
69
        virtual ~Trigger();
70
        virtual void cfg_write( KConfigGroup& cfg_P ) const = 0;
71
        virtual Trigger* copy( ActionData* data_P ) const = 0;
72
73
        virtual const QString description() const = 0;
        virtual void activate( bool activate_P ) = 0;
74

Michael Jansen's avatar
Michael Jansen committed
75
76
77
78
79
80
        //! Disable the trigger
        virtual void disable();

        //! Enable the trigger
        virtual void enable();

81
82
83
84
85
        /**
         * The trigger will be erased permanently
         */
        virtual void aboutToBeErased();

86
87
88
89
90
        /**
         * The actual type for this trigger
         */
        virtual TriggerType type() const = 0;

91
92
93
94
95
        /**
         * Acyclic visitor pattern
         */
        virtual void accept(TriggerVisitor&) = 0;

96
    protected:
97
        ActionData* const data;
98
99
    };

100
101
102
Q_DECLARE_OPERATORS_FOR_FLAGS(Trigger::TriggerTypes)


103
class Q_DECL_EXPORT Trigger_list
104
    : public QList< Trigger* >
105
    {
106
107
    Q_DISABLE_COPY( Trigger_list )

108
    public:
109
        Trigger_list( const QString& comment = QString() );
Michael Jansen's avatar
Michael Jansen committed
110
        virtual ~Trigger_list();
111

112
        void activate( bool activate_P );
113
        void cfg_write( KConfigGroup& cfg_P ) const;
114
115
116
        //! Some convenience typedef
        typedef QList< Trigger* >::Iterator Iterator;
        typedef QList< Trigger* >::ConstIterator ConstIterator;
117
        const QString comment() const;
118
        void set_comment(const QString &);
119
        Trigger_list* copy( ActionData* data_P ) const;
120

Michael Jansen's avatar
Michael Jansen committed
121
122
123
124
125
126
        //! Disable the trigger
        virtual void disable();

        //! Enable the trigger
        virtual void enable();

127
        /**
128
         * @reimp
129
130
131
         */
        void aboutToBeErased();

132
133
134
    private:
        QString _comment;
    };
Michael Jansen's avatar
Michael Jansen committed
135

136
137
138
139
140
141
142
143
144
145

class ShortcutTrigger;
class ShortcutTriggerVisitor
    {
public:
    virtual ~ShortcutTriggerVisitor();
    virtual void visit(ShortcutTrigger&) = 0;
    };


146
class Q_DECL_EXPORT ShortcutTrigger
Michael Jansen's avatar
Michael Jansen committed
147
    : public QObject, public Trigger
148
    {
Michael Jansen's avatar
Michael Jansen committed
149
150
    Q_OBJECT

151
152
    typedef Trigger base;
    public:
153
        ShortcutTrigger(
154
155
            ActionData* data,
            const KShortcut& shortcut = KShortcut(),
Michael Jansen's avatar
Michael Jansen committed
156
157
            const QUuid &uuid = QUuid::createUuid() );

158
        virtual ~ShortcutTrigger();
Laurent Montel's avatar
Laurent Montel committed
159
160
161
        void cfg_write( KConfigGroup& cfg_P ) const Q_DECL_OVERRIDE;
        ShortcutTrigger* copy( ActionData* data_P ) const Q_DECL_OVERRIDE;
        const QString description() const Q_DECL_OVERRIDE;
162
        QList<QKeySequence> shortcut() const;
Laurent Montel's avatar
Laurent Montel committed
163
        void activate( bool activate_P ) Q_DECL_OVERRIDE;
Michael Jansen's avatar
Michael Jansen committed
164

165
166
        void set_key_sequence( const QKeySequence &seq );

Laurent Montel's avatar
Laurent Montel committed
167
        TriggerType type() const Q_DECL_OVERRIDE { return ShortcutTriggerType; }
168
169
170
171

        /**
         * @reimp
         */
Laurent Montel's avatar
Laurent Montel committed
172
        void aboutToBeErased() Q_DECL_OVERRIDE;
173

Michael Jansen's avatar
Michael Jansen committed
174
        //! Disable the trigger
Laurent Montel's avatar
Laurent Montel committed
175
        void disable() Q_DECL_OVERRIDE;
Michael Jansen's avatar
Michael Jansen committed
176
177

        //! Enable the trigger
Laurent Montel's avatar
Laurent Montel committed
178
        void enable() Q_DECL_OVERRIDE;
Michael Jansen's avatar
Michael Jansen committed
179

180
181
182
        /**
         * Acyclic visitor pattern
         */
Laurent Montel's avatar
Laurent Montel committed
183
        void accept(TriggerVisitor&) Q_DECL_OVERRIDE;
184

185
186
        QString primaryShortcut() const;

Michael Jansen's avatar
Michael Jansen committed
187
188
189
190
    public Q_SLOTS:

        void trigger();

191
    private:
192
        QString shortcuts() const;
Michael Jansen's avatar
Michael Jansen committed
193
194
195

        //! A persistent identifier for this shortcut
        QUuid _uuid;
196

Michael Jansen's avatar
Michael Jansen committed
197
        //! Are the conditions met?
198
        bool _active;
Michael Jansen's avatar
Michael Jansen committed
199
200
201
202
203
204
205
206
207
208
209
210

        /**
         * The Key Sequence associated with this Trigger. This is needed
         * because a inactive trigger doesn't register it's shortcut with
         * kde's global shortcuts registry so we have to remember the shortcut
         * ourselves. Beware of synchronizing saved state, global shortcuts
         * registry state and state of this var :-) .
         */
        KShortcut    _shortcut;



211
212
    };

213

214
215
216
217
218
219
220
221
222
class WindowTrigger;
class WindowTriggerVisitor
    {
public:
    virtual ~WindowTriggerVisitor();
    virtual void visit(WindowTrigger&) = 0;
    };


223
class Q_DECL_EXPORT WindowTrigger : public QObject, public Trigger
224
225
    {
    Q_OBJECT
226
227
228

    Q_FLAGS(WindowEvents)

229
    typedef Trigger base;
230

231
    public:
232

233
234
        enum window_action_t
            {
235
236
237
238
239
            NONE                   = 0,
            WINDOW_APPEARS         = ( 1 << 0 ),        //!< The window is opened
            WINDOW_DISAPPEARS      = ( 1 << 1 ),        //!< The window is closed
            WINDOW_ACTIVATES       = ( 1 << 2 ),        //!< The window gets the focus
            WINDOW_DEACTIVATES     = ( 1 << 3 )         //!< The window loses the focus
240
            };
241
242
243
244

        Q_DECLARE_FLAGS(WindowEvents, window_action_t)

        WindowTrigger(
245
                ActionData* data,
Laurent Montel's avatar
Laurent Montel committed
246
                Windowdef_list* windowslist = nullptr,
Laurent Montel's avatar
Laurent Montel committed
247
                WindowEvents window_actions = NONE );
248
249
250

        void setOnWindowEvents(WindowEvents events);

251
        virtual ~WindowTrigger();
Laurent Montel's avatar
Laurent Montel committed
252
253
254
        void cfg_write( KConfigGroup& cfg_P ) const Q_DECL_OVERRIDE;
        WindowTrigger* copy( ActionData* data_P ) const Q_DECL_OVERRIDE;
        const QString description() const Q_DECL_OVERRIDE;
255
        const Windowdef_list* windows() const;
256
        void set_window_rules(Windowdef_list *list);
257
        Windowdef_list* windows();
258
        bool triggers_on( window_action_t w_action_P ) const;
Laurent Montel's avatar
Laurent Montel committed
259
        void activate( bool activate_P ) Q_DECL_OVERRIDE;
260

Laurent Montel's avatar
Laurent Montel committed
261
        TriggerType type() const Q_DECL_OVERRIDE { return WindowTriggerType; }
Michael Jansen's avatar
Michael Jansen committed
262
263


264
265
266
267

        /**
         * Acyclic visitor pattern
         */
Laurent Montel's avatar
Laurent Montel committed
268
        void accept(TriggerVisitor&) Q_DECL_OVERRIDE;
269

Laurent Montel's avatar
Laurent Montel committed
270
    protected Q_SLOTS:
271
272
273
274
        void window_added( WId window_P );
        void window_removed( WId window_P );
        void active_window_changed( WId window_P );
        void window_changed( WId window_P, unsigned int dirty_P );
Michael Jansen's avatar
Michael Jansen committed
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

    private:

        //! Useful code for all constructors
        void init();

        Windowdef_list* _windows;

        WindowEvents window_actions;

        typedef QMap< WId, bool > Windows_map;

        //! Internal cache. Remembers if a window is a match or not,
        Windows_map existing_windows;

        //! The last active window
        WId last_active_window;

        //! Is the trigger active?
294
295
296
        bool active;
    };

297
298
Q_DECLARE_OPERATORS_FOR_FLAGS(WindowTrigger::WindowEvents)

299
300
301
302
303
/**
 * This class handles the storage of gesture data; it also matches gestures
 * and links the gesture to an action.
 * One object equals one configured gesture.
 */
304
305
306
307
308
309
310
class GestureTrigger;
class GestureTriggerVisitor
    {
public:
    virtual ~GestureTriggerVisitor();
    virtual void visit(GestureTrigger&) = 0;
    };
311

312
class Q_DECL_EXPORT GestureTrigger
313
314
315
316
317
    : public QObject, public Trigger
    {
    Q_OBJECT
    typedef Trigger base;
    public:
318
319
        GestureTrigger( ActionData* data, const StrokePoints& pointdata_P = StrokePoints() );

320
        virtual ~GestureTrigger();
Laurent Montel's avatar
Laurent Montel committed
321
322
323
        void cfg_write( KConfigGroup& cfg_P ) const Q_DECL_OVERRIDE;
        Trigger* copy( ActionData* data_P ) const Q_DECL_OVERRIDE;
        const QString description() const Q_DECL_OVERRIDE;
324
        const StrokePoints& pointData() const;
325

326
327
        //! Set the point data of the gesture
        void setPointData(const StrokePoints &data);
328
329
        void setPointData(const QStringList &strings);
        void setKDE3Gesture(const QString &gestureCode);
330

Laurent Montel's avatar
Laurent Montel committed
331
        void activate( bool activate_P ) Q_DECL_OVERRIDE;
332

Laurent Montel's avatar
Laurent Montel committed
333
        TriggerType type() const Q_DECL_OVERRIDE { return GestureTriggerType; }
334
335
336
337

        /**
         * Acyclic visitor pattern
         */
Laurent Montel's avatar
Laurent Montel committed
338
        void accept(TriggerVisitor&) Q_DECL_OVERRIDE;
339

Laurent Montel's avatar
Laurent Montel committed
340
    protected Q_SLOTS:
341
342
343
        void handle_gesture( const StrokePoints& gesture_P );
    Q_SIGNALS:
        void gotScore( ActionData* const data, const qreal score );
344

345
    private:
346
347
348
349
        qreal comparePointData(const StrokePoints &a, const StrokePoints &b) const;
        inline qreal angleSquareDifference(qreal alpha, qreal beta) const;

        StrokePoints _pointdata;
350
351
    };

352

353
354
// FIXME: SOUND
#if 0
355
class Q_DECL_EXPORT Voice_trigger
356
357
358
359
360
    : public QObject, public Trigger
    {
    Q_OBJECT
    typedef Trigger base;
    public:
361
362
        Voice_trigger( ActionData* data_P, const QString& Voice_P, const VoiceSignature & signature1_P, const VoiceSignature & signature2_P );
        Voice_trigger( KConfigGroup& cfg_P, ActionData* data_P );
363
        virtual ~Voice_trigger();
364
        virtual void cfg_write( KConfigGroup& cfg_P ) const;
365
        virtual Trigger* copy( ActionData* data_P ) const;
366
367
368
        virtual const QString description() const;
        const QString& voicecode() const;
        virtual void activate( bool activate_P );
Michael Jansen's avatar
Michael Jansen committed
369
        VoiceSignature voicesignature( int ech ) const;
370

371
        virtual TriggerType type() const { return SoundTrigger; }
372
373
374
375
    public slots:
        void handle_Voice(  );
    private:
        QString _voicecode;
Michael Jansen's avatar
Michael Jansen committed
376
        VoiceSignature _voicesignature[2];
377
    };
378
#endif
379

380
381
// FIXME: SOUND
#if 0
382
383
384
// Voice_trigger
inline
const QString& Voice_trigger::voicecode() const
Michael Jansen's avatar
Michael Jansen committed
385
386
387
    {
    return _voicecode;
    }
388
389
390

inline
VoiceSignature Voice_trigger::voicesignature(int ech) const
Michael Jansen's avatar
Michael Jansen committed
391
392
393
    {
    return _voicesignature[ech-1];
    }
394
#endif
395
} // namespace KHotKeys
Michael Jansen's avatar
Michael Jansen committed
396

397

398
#endif