~ubuntu-branches/ubuntu/oneiric/qwt/oneiric-proposed

« back to all changes in this revision

Viewing changes to qwt-5.1.1/src/qwt_event_pattern.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2008-05-26 10:26:31 UTC
  • mfrom: (1.1.3 upstream) (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080526102631-bp95mfccnrb957nx
Tags: 5.1.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 
2
 * Qwt Widget Library
 
3
 * Copyright (C) 1997   Josef Wilgen
 
4
 * Copyright (C) 2002   Uwe Rathmann
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the Qwt License, Version 1.0
 
8
 *****************************************************************************/
 
9
 
 
10
#include <qevent.h>
 
11
#include "qwt_event_pattern.h"
 
12
 
 
13
/*! 
 
14
  Constructor
 
15
 
 
16
  \sa MousePatternCode, KeyPatternCode
 
17
*/
 
18
 
 
19
QwtEventPattern::QwtEventPattern():
 
20
    d_mousePattern(MousePatternCount),
 
21
    d_keyPattern(KeyPatternCount)
 
22
{
 
23
    initKeyPattern();
 
24
    initMousePattern(3);
 
25
}
 
26
 
 
27
//! Destructor
 
28
QwtEventPattern::~QwtEventPattern()
 
29
{
 
30
}
 
31
 
 
32
/*!
 
33
  Set default mouse patterns, depending on the number of mouse buttons
 
34
 
 
35
  \param numButtons Number of mouse buttons ( <= 3 )
 
36
  \sa MousePatternCode
 
37
*/
 
38
void QwtEventPattern::initMousePattern(int numButtons)
 
39
{
 
40
#if QT_VERSION < 0x040000
 
41
    const int altButton = Qt::AltButton;
 
42
    const int controlButton = Qt::ControlButton;
 
43
    const int shiftButton = Qt::ShiftButton;
 
44
#else
 
45
    const int altButton = Qt::AltModifier;
 
46
    const int controlButton = Qt::ControlModifier;
 
47
    const int shiftButton = Qt::ShiftModifier;
 
48
#endif
 
49
 
 
50
    d_mousePattern.resize(MousePatternCount);
 
51
 
 
52
    switch(numButtons)
 
53
    {
 
54
        case 1:
 
55
        {
 
56
            setMousePattern(MouseSelect1, Qt::LeftButton);
 
57
            setMousePattern(MouseSelect2, Qt::LeftButton, controlButton);
 
58
            setMousePattern(MouseSelect3, Qt::LeftButton, altButton);
 
59
            break;
 
60
        }
 
61
        case 2:
 
62
        {
 
63
            setMousePattern(MouseSelect1, Qt::LeftButton);
 
64
            setMousePattern(MouseSelect2, Qt::RightButton);
 
65
            setMousePattern(MouseSelect3, Qt::LeftButton, altButton);
 
66
            break;
 
67
        }
 
68
        default:
 
69
        {
 
70
            setMousePattern(MouseSelect1, Qt::LeftButton);
 
71
            setMousePattern(MouseSelect2, Qt::RightButton);
 
72
            setMousePattern(MouseSelect3, Qt::MidButton);
 
73
        }
 
74
    }
 
75
    for ( int i = 0; i < 3; i++ )
 
76
    {
 
77
        setMousePattern(MouseSelect4 + i, 
 
78
            d_mousePattern[MouseSelect1 + i].button,
 
79
            d_mousePattern[MouseSelect1 + i].state | shiftButton);
 
80
    }
 
81
}
 
82
 
 
83
/*!
 
84
  Set default mouse patterns.
 
85
 
 
86
  \sa KeyPatternCode
 
87
*/
 
88
void QwtEventPattern::initKeyPattern()
 
89
{
 
90
    d_keyPattern.resize(KeyPatternCount);
 
91
 
 
92
    setKeyPattern(KeySelect1, Qt::Key_Return);
 
93
    setKeyPattern(KeySelect2, Qt::Key_Space);
 
94
    setKeyPattern(KeyAbort, Qt::Key_Escape);
 
95
 
 
96
    setKeyPattern(KeyLeft, Qt::Key_Left);
 
97
    setKeyPattern(KeyRight, Qt::Key_Right);
 
98
    setKeyPattern(KeyUp, Qt::Key_Up);
 
99
    setKeyPattern(KeyDown, Qt::Key_Down);
 
100
 
 
101
    setKeyPattern(KeyRedo, Qt::Key_Plus);
 
102
    setKeyPattern(KeyUndo, Qt::Key_Minus);
 
103
    setKeyPattern(KeyHome, Qt::Key_Escape);
 
104
}
 
105
 
 
106
/*!
 
107
  Change one mouse pattern
 
108
 
 
109
  \param pattern Index of the pattern
 
110
  \param button Button
 
111
  \param state State
 
112
 
 
113
  \sa QMouseEvent
 
114
*/
 
115
void QwtEventPattern::setMousePattern(uint pattern, int button, int state)
 
116
{
 
117
    if ( pattern < (uint)d_mousePattern.count() )
 
118
    {
 
119
        d_mousePattern[int(pattern)].button = button;
 
120
        d_mousePattern[int(pattern)].state = state;
 
121
    }
 
122
}
 
123
 
 
124
/*!
 
125
  Change one key pattern
 
126
 
 
127
  \param pattern Index of the pattern
 
128
  \param key Key
 
129
  \param state State
 
130
 
 
131
  \sa QKeyEvent
 
132
*/
 
133
void QwtEventPattern::setKeyPattern(uint pattern, int key, int state)
 
134
{
 
135
    if ( pattern < (uint)d_keyPattern.count() )
 
136
    {
 
137
        d_keyPattern[int(pattern)].key = key;
 
138
        d_keyPattern[int(pattern)].state = state;
 
139
    }
 
140
}
 
141
 
 
142
//! Change the mouse event patterns
 
143
void QwtEventPattern::setMousePattern(const QwtArray<MousePattern> &pattern)
 
144
{
 
145
    d_mousePattern = pattern;
 
146
}
 
147
 
 
148
//! Change the key event patterns
 
149
void QwtEventPattern::setKeyPattern(const QwtArray<KeyPattern> &pattern)
 
150
{
 
151
    d_keyPattern = pattern;
 
152
}
 
153
 
 
154
//! Return mouse patterns
 
155
const QwtArray<QwtEventPattern::MousePattern> &
 
156
QwtEventPattern::mousePattern() const
 
157
{
 
158
    return d_mousePattern;
 
159
}
 
160
 
 
161
//! Return key patterns
 
162
const QwtArray<QwtEventPattern::KeyPattern> &
 
163
QwtEventPattern::keyPattern() const
 
164
{
 
165
    return d_keyPattern;
 
166
}
 
167
 
 
168
//! Return ,ouse patterns
 
169
QwtArray<QwtEventPattern::MousePattern> &QwtEventPattern::mousePattern() 
 
170
{
 
171
    return d_mousePattern;
 
172
}
 
173
 
 
174
//! Return Key patterns
 
175
QwtArray<QwtEventPattern::KeyPattern> &QwtEventPattern::keyPattern() 
 
176
{
 
177
    return d_keyPattern;
 
178
}
 
179
 
 
180
/*!
 
181
  \brief Compare a mouse event with an event pattern. 
 
182
 
 
183
  A mouse event matches the pattern when both have the same button
 
184
  value and in the state value the same key flags(Qt::KeyButtonMask)
 
185
  are set.
 
186
  
 
187
  \param pattern Index of the event pattern
 
188
  \param e Mouse event
 
189
  \return true if matches
 
190
 
 
191
  \sa keyMatch()
 
192
*/
 
193
bool QwtEventPattern::mouseMatch(uint pattern, const QMouseEvent *e) const
 
194
{
 
195
    bool ok = false;
 
196
 
 
197
    if ( e && pattern < (uint)d_mousePattern.count() )
 
198
        ok = mouseMatch(d_mousePattern[int(pattern)], e);
 
199
 
 
200
    return ok;
 
201
}
 
202
 
 
203
/*!
 
204
  \brief Compare a mouse event with an event pattern. 
 
205
 
 
206
  A mouse event matches the pattern when both have the same button
 
207
  value and in the state value the same key flags(Qt::KeyButtonMask)
 
208
  are set.
 
209
  
 
210
  \param pattern Mouse event pattern
 
211
  \param e Mouse event
 
212
  \return true if matches
 
213
 
 
214
  \sa keyMatch()
 
215
*/
 
216
 
 
217
bool QwtEventPattern::mouseMatch(const MousePattern &pattern,
 
218
    const QMouseEvent *e) const
 
219
{
 
220
    if ( e->button() != pattern.button )
 
221
        return false;
 
222
 
 
223
    const bool matched =
 
224
#if QT_VERSION < 0x040000
 
225
        (e->state() & Qt::KeyButtonMask) == 
 
226
            (pattern.state & Qt::KeyButtonMask);
 
227
#else
 
228
        (e->modifiers() & Qt::KeyboardModifierMask) == 
 
229
            (int)(pattern.state & Qt::KeyboardModifierMask);
 
230
#endif
 
231
 
 
232
    return matched;
 
233
}
 
234
 
 
235
/*!
 
236
  \brief Compare a key event with an event pattern. 
 
237
 
 
238
  A key event matches the pattern when both have the same key
 
239
  value and in the state value the same key flags (Qt::KeyButtonMask)
 
240
  are set.
 
241
  
 
242
  \param pattern Index of the event pattern
 
243
  \param e Key event
 
244
  \return true if matches
 
245
 
 
246
  \sa mouseMatch()
 
247
*/
 
248
bool QwtEventPattern::keyMatch(uint pattern, const QKeyEvent *e) const
 
249
{
 
250
    bool ok = false;
 
251
 
 
252
    if ( e && pattern < (uint)d_keyPattern.count() )
 
253
        ok = keyMatch(d_keyPattern[int(pattern)], e);
 
254
 
 
255
    return ok;
 
256
}
 
257
 
 
258
/*!
 
259
  \brief Compare a key event with an event pattern. 
 
260
 
 
261
  A key event matches the pattern when both have the same key
 
262
  value and in the state value the same key flags (Qt::KeyButtonMask)
 
263
  are set.
 
264
  
 
265
  \param pattern Key event pattern
 
266
  \param e Key event
 
267
  \return true if matches
 
268
 
 
269
  \sa mouseMatch()
 
270
*/
 
271
 
 
272
bool QwtEventPattern::keyMatch(
 
273
    const KeyPattern &pattern, const QKeyEvent *e) const
 
274
{
 
275
    if ( e->key() != pattern.key)
 
276
        return false;
 
277
 
 
278
    const bool matched =
 
279
#if QT_VERSION < 0x040000
 
280
        (e->state() & Qt::KeyButtonMask) == 
 
281
            (pattern.state & Qt::KeyButtonMask);
 
282
#else
 
283
        (e->modifiers() & Qt::KeyboardModifierMask) == 
 
284
            (int)(pattern.state & Qt::KeyboardModifierMask);
 
285
#endif
 
286
 
 
287
    return matched;
 
288
}