Qwt User's Guide: qwt_picker_machine.cpp Source File

Qwt User's Guide 6.2.0

qwt_picker_machine.cpp
1 /******************************************************************************
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 "qwt_picker_machine.h"
11 #include "qwt_event_pattern.h"
12
13 #include <qevent.h>
14
16 QwtPickerMachine::QwtPickerMachine( SelectionType type )
17 : m_selectionType( type )
18 , m_state( 0 )
19 {
20 }
21
23 QwtPickerMachine::~QwtPickerMachine()
24 {
25 }
26
28 QwtPickerMachine::SelectionType QwtPickerMachine::selectionType() const
29 {
30 return m_selectionType;
31 }
32
34 int QwtPickerMachine::state() const
35 {
36 return m_state;
37 }
38
40 void QwtPickerMachine::setState( int state )
41 {
42 m_state = state;
43 }
44
46 void QwtPickerMachine::reset()
47 {
48 setState( 0 );
49 }
50
52 QwtPickerTrackerMachine::QwtPickerTrackerMachine():
53 QwtPickerMachine( NoSelection )
54 {
55 }
56
58 QList< QwtPickerMachine::Command > QwtPickerTrackerMachine::transition(
59 const QwtEventPattern&, const QEvent* e )
60 {
61 QList< QwtPickerMachine::Command > cmdList;
62
63 switch ( e->type() )
64 {
65 case QEvent::Enter:
66 case QEvent::MouseMove:
67 {
68 if ( state() == 0 )
69 {
70 cmdList += Begin;
71 cmdList += Append;
72 setState( 1 );
73 }
74 else
75 {
76 cmdList += Move;
77 }
78 break;
79 }
80 case QEvent::Leave:
81 {
82 cmdList += Remove;
83 cmdList += End;
84 setState( 0 );
85 }
86 default:
87 break;
88 }
89
90 return cmdList;
91 }
92
94 QwtPickerClickPointMachine::QwtPickerClickPointMachine():
95 QwtPickerMachine( PointSelection )
96 {
97 }
98
100 QList< QwtPickerMachine::Command > QwtPickerClickPointMachine::transition(
101 const QwtEventPattern& eventPattern, const QEvent* event )
102 {
103 QList< QwtPickerMachine::Command > cmdList;
104
105 switch ( event->type() )
106 {
107 case QEvent::MouseButtonPress:
108 {
109 if ( eventPattern.mouseMatch( QwtEventPattern::MouseSelect1,
110 static_cast< const QMouseEvent* >( event ) ) )
111 {
112 cmdList += Begin;
113 cmdList += Append;
114 cmdList += End;
115 }
116 break;
117 }
118 case QEvent::KeyPress:
119 {
120 const QKeyEvent* keyEvent = static_cast< const QKeyEvent* > ( event );
121 if ( eventPattern.keyMatch( QwtEventPattern::KeySelect1, keyEvent ) )
122 {
123 if ( !keyEvent->isAutoRepeat() )
124 {
125 cmdList += Begin;
126 cmdList += Append;
127 cmdList += End;
128 }
129 }
130 break;
131 }
132 default:
133 break;
134 }
135
136 return cmdList;
137 }
138
140 QwtPickerDragPointMachine::QwtPickerDragPointMachine():
141 QwtPickerMachine( PointSelection )
142 {
143 }
144
146 QList< QwtPickerMachine::Command > QwtPickerDragPointMachine::transition(
147 const QwtEventPattern& eventPattern, const QEvent* event )
148 {
149 QList< QwtPickerMachine::Command > cmdList;
150
151 switch ( event->type() )
152 {
153 case QEvent::MouseButtonPress:
154 {
155 if ( eventPattern.mouseMatch( QwtEventPattern::MouseSelect1,
156 static_cast< const QMouseEvent* >( event ) ) )
157 {
158 if ( state() == 0 )
159 {
160 cmdList += Begin;
161 cmdList += Append;
162 setState( 1 );
163 }
164 }
165 break;
166 }
167 case QEvent::MouseMove:
168 case QEvent::Wheel:
169 {
170 if ( state() != 0 )
171 cmdList += Move;
172 break;
173 }
174 case QEvent::MouseButtonRelease:
175 {
176 if ( state() != 0 )
177 {
178 cmdList += End;
179 setState( 0 );
180 }
181 break;
182 }
183 case QEvent::KeyPress:
184 {
185 const QKeyEvent* keyEvent = static_cast< const QKeyEvent* > ( event );
186 if ( eventPattern.keyMatch( QwtEventPattern::KeySelect1, keyEvent ) )
187 {
188 if ( !keyEvent->isAutoRepeat() )
189 {
190 if ( state() == 0 )
191 {
192 cmdList += Begin;
193 cmdList += Append;
194 setState( 1 );
195 }
196 else
197 {
198 cmdList += End;
199 setState( 0 );
200 }
201 }
202 }
203 break;
204 }
205 default:
206 break;
207 }
208
209 return cmdList;
210 }
211
213 QwtPickerClickRectMachine::QwtPickerClickRectMachine():
214 QwtPickerMachine( RectSelection )
215 {
216 }
217
219 QList< QwtPickerMachine::Command > QwtPickerClickRectMachine::transition(
220 const QwtEventPattern& eventPattern, const QEvent* event )
221 {
222 QList< QwtPickerMachine::Command > cmdList;
223
224 switch ( event->type() )
225 {
226 case QEvent::MouseButtonPress:
227 {
228 if ( eventPattern.mouseMatch( QwtEventPattern::MouseSelect1,
229 static_cast< const QMouseEvent* >( event ) ) )
230 {
231 switch ( state() )
232 {
233 case 0:
234 {
235 cmdList += Begin;
236 cmdList += Append;
237 setState( 1 );
238 break;
239 }
240 case 1:
241 {
242 // Uh, strange we missed the MouseButtonRelease
243 break;
244 }
245 default:
246 {
247 cmdList += End;
248 setState( 0 );
249 }
250 }
251 }
252 break;
253 }
254 case QEvent::MouseMove:
255 case QEvent::Wheel:
256 {
257 if ( state() != 0 )
258 cmdList += Move;
259 break;
260 }
261 case QEvent::MouseButtonRelease:
262 {
263 if ( eventPattern.mouseMatch( QwtEventPattern::MouseSelect1,
264 static_cast< const QMouseEvent* >( event ) ) )
265 {
266 if ( state() == 1 )
267 {
268 cmdList += Append;
269 setState( 2 );
270 }
271 }
272 break;
273 }
274 case QEvent::KeyPress:
275 {
276 const QKeyEvent* keyEvent = static_cast< const QKeyEvent* > ( event );
277 if ( eventPattern.keyMatch( QwtEventPattern::KeySelect1, keyEvent ) )
278 {
279 if ( !keyEvent->isAutoRepeat() )
280 {
281 if ( state() == 0 )
282 {
283 cmdList += Begin;
284 cmdList += Append;
285 setState( 1 );
286 }
287 else
288 {
289 if ( state() == 1 )
290 {
291 cmdList += Append;
292 setState( 2 );
293 }
294 else if ( state() == 2 )
295 {
296 cmdList += End;
297 setState( 0 );
298 }
299 }
300 }
301 }
302 break;
303 }
304 default:
305 break;
306 }
307
308 return cmdList;
309 }
310
312 QwtPickerDragRectMachine::QwtPickerDragRectMachine():
313 QwtPickerMachine( RectSelection )
314 {
315 }
316
318 QList< QwtPickerMachine::Command > QwtPickerDragRectMachine::transition(
319 const QwtEventPattern& eventPattern, const QEvent* event )
320 {
321 QList< QwtPickerMachine::Command > cmdList;
322
323 switch ( event->type() )
324 {
325 case QEvent::MouseButtonPress:
326 {
327 if ( eventPattern.mouseMatch( QwtEventPattern::MouseSelect1,
328 static_cast< const QMouseEvent* >( event ) ) )
329 {
330 if ( state() == 0 )
331 {
332 cmdList += Begin;
333 cmdList += Append;
334 cmdList += Append;
335 setState( 2 );
336 }
337 }
338 break;
339 }
340 case QEvent::MouseMove:
341 case QEvent::Wheel:
342 {
343 if ( state() != 0 )
344 cmdList += Move;
345 break;
346 }
347 case QEvent::MouseButtonRelease:
348 {
349 if ( state() == 2 )
350 {
351 cmdList += End;
352 setState( 0 );
353 }
354 break;
355 }
356 case QEvent::KeyPress:
357 {
358 if ( eventPattern.keyMatch( QwtEventPattern::KeySelect1,
359 static_cast< const QKeyEvent* > ( event ) ) )
360 {
361 if ( state() == 0 )
362 {
363 cmdList += Begin;
364 cmdList += Append;
365 cmdList += Append;
366 setState( 2 );
367 }
368 else
369 {
370 cmdList += End;
371 setState( 0 );
372 }
373 }
374 break;
375 }
376 default:
377 break;
378 }
379
380 return cmdList;
381 }
382
384 QwtPickerPolygonMachine::QwtPickerPolygonMachine():
385 QwtPickerMachine( PolygonSelection )
386 {
387 }
388
390 QList< QwtPickerMachine::Command > QwtPickerPolygonMachine::transition(
391 const QwtEventPattern& eventPattern, const QEvent* event )
392 {
393 QList< QwtPickerMachine::Command > cmdList;
394
395 switch ( event->type() )
396 {
397 case QEvent::MouseButtonPress:
398 {
399 if ( eventPattern.mouseMatch( QwtEventPattern::MouseSelect1,
400 static_cast< const QMouseEvent* >( event ) ) )
401 {
402 if ( state() == 0 )
403 {
404 cmdList += Begin;
405 cmdList += Append;
406 cmdList += Append;
407 setState( 1 );
408 }
409 else
410 {
411 cmdList += Append;
412 }
413 }
414 if ( eventPattern.mouseMatch( QwtEventPattern::MouseSelect2,
415 static_cast< const QMouseEvent* >( event ) ) )
416 {
417 if ( state() == 1 )
418 {
419 cmdList += End;
420 setState( 0 );
421 }
422 }
423 break;
424 }
425 case QEvent::MouseMove:
426 case QEvent::Wheel:
427 {
428 if ( state() != 0 )
429 cmdList += Move;
430 break;
431 }
432 case QEvent::KeyPress:
433 {
434 const QKeyEvent* keyEvent = static_cast< const QKeyEvent* > ( event );
435 if ( eventPattern.keyMatch( QwtEventPattern::KeySelect1, keyEvent ) )
436 {
437 if ( !keyEvent->isAutoRepeat() )
438 {
439 if ( state() == 0 )
440 {
441 cmdList += Begin;
442 cmdList += Append;
443 cmdList += Append;
444 setState( 1 );
445 }
446 else
447 {
448 cmdList += Append;
449 }
450 }
451 }
452 else if ( eventPattern.keyMatch( QwtEventPattern::KeySelect2, keyEvent ) )
453 {
454 if ( !keyEvent->isAutoRepeat() )
455 {
456 if ( state() == 1 )
457 {
458 cmdList += End;
459 setState( 0 );
460 }
461 }
462 }
463 break;
464 }
465 default:
466 break;
467 }
468
469 return cmdList;
470 }
471
473 QwtPickerDragLineMachine::QwtPickerDragLineMachine():
474 QwtPickerMachine( PolygonSelection )
475 {
476 }
477
479 QList< QwtPickerMachine::Command > QwtPickerDragLineMachine::transition(
480 const QwtEventPattern& eventPattern, const QEvent* event )
481 {
482 QList< QwtPickerMachine::Command > cmdList;
483
484 switch( event->type() )
485 {
486 case QEvent::MouseButtonPress:
487 {
488 if ( eventPattern.mouseMatch( QwtEventPattern::MouseSelect1,
489 static_cast< const QMouseEvent* >( event ) ) )
490 {
491 if ( state() == 0 )
492 {
493 cmdList += Begin;
494 cmdList += Append;
495 cmdList += Append;
496 setState( 1 );
497 }
498 }
499 break;
500 }
501 case QEvent::KeyPress:
502 {
503 if ( eventPattern.keyMatch( QwtEventPattern::KeySelect1,
504 static_cast< const QKeyEvent* > ( event ) ) )
505 {
506 if ( state() == 0 )
507 {
508 cmdList += Begin;
509 cmdList += Append;
510 cmdList += Append;
511 setState( 1 );
512 }
513 else
514 {
515 cmdList += End;
516 setState( 0 );
517 }
518 }
519 break;
520 }
521 case QEvent::MouseMove:
522 case QEvent::Wheel:
523 {
524 if ( state() != 0 )
525 cmdList += Move;
526
527 break;
528 }
529 case QEvent::MouseButtonRelease:
530 {
531 if ( state() != 0 )
532 {
533 cmdList += End;
534 setState( 0 );
535 }
536 }
537 default:
538 break;
539 }
540
541 return cmdList;
542 }
QList
Definition: qwt_abstract_legend.h:17
QwtEventPattern
A collection of event patterns.
Definition: qwt_event_pattern.h:31
QwtEventPattern::mouseMatch
bool mouseMatch(MousePatternCode, const QMouseEvent *) const
Compare a mouse event with an event pattern.
Definition: qwt_event_pattern.cpp:188
QwtEventPattern::keyMatch
bool keyMatch(KeyPatternCode, const QKeyEvent *) const
Compare a key event with an event pattern.
Definition: qwt_event_pattern.cpp:234
QwtEventPattern::KeySelect1
@ KeySelect1
Qt::Key_Return.
Definition: qwt_event_pattern.h:115
QwtEventPattern::KeySelect2
@ KeySelect2
Qt::Key_Space.
Definition: qwt_event_pattern.h:118
QwtEventPattern::MouseSelect1
@ MouseSelect1
Definition: qwt_event_pattern.h:54
QwtEventPattern::MouseSelect2
@ MouseSelect2
Definition: qwt_event_pattern.h:63
QwtPickerClickPointMachine::transition
virtual QList< Command > transition(const QwtEventPattern &, const QEvent *) override
Transition.
Definition: qwt_picker_machine.cpp:100
QwtPickerClickPointMachine::QwtPickerClickPointMachine
QwtPickerClickPointMachine()
Constructor.
Definition: qwt_picker_machine.cpp:94
QwtPickerClickRectMachine::QwtPickerClickRectMachine
QwtPickerClickRectMachine()
Constructor.
Definition: qwt_picker_machine.cpp:213
QwtPickerClickRectMachine::transition
virtual QList< Command > transition(const QwtEventPattern &, const QEvent *) override
Transition.
Definition: qwt_picker_machine.cpp:219
QwtPickerDragLineMachine::transition
virtual QList< Command > transition(const QwtEventPattern &, const QEvent *) override
Transition.
Definition: qwt_picker_machine.cpp:479
QwtPickerDragLineMachine::QwtPickerDragLineMachine
QwtPickerDragLineMachine()
Constructor.
Definition: qwt_picker_machine.cpp:473
QwtPickerDragPointMachine::transition
virtual QList< Command > transition(const QwtEventPattern &, const QEvent *) override
Transition.
Definition: qwt_picker_machine.cpp:146
QwtPickerDragPointMachine::QwtPickerDragPointMachine
QwtPickerDragPointMachine()
Constructor.
Definition: qwt_picker_machine.cpp:140
QwtPickerDragRectMachine::QwtPickerDragRectMachine
QwtPickerDragRectMachine()
Constructor.
Definition: qwt_picker_machine.cpp:312
QwtPickerDragRectMachine::transition
virtual QList< Command > transition(const QwtEventPattern &, const QEvent *) override
Transition.
Definition: qwt_picker_machine.cpp:318
QwtPickerMachine
A state machine for QwtPicker selections.
Definition: qwt_picker_machine.h:29
QwtPickerMachine::SelectionType
SelectionType
Definition: qwt_picker_machine.h:36
QwtPickerMachine::state
int state() const
Return the current state.
Definition: qwt_picker_machine.cpp:34
QwtPickerMachine::setState
void setState(int)
Change the current state.
Definition: qwt_picker_machine.cpp:40
QwtPickerMachine::selectionType
SelectionType selectionType() const
Return the selection type.
Definition: qwt_picker_machine.cpp:28
QwtPickerMachine::~QwtPickerMachine
virtual ~QwtPickerMachine()
Destructor.
Definition: qwt_picker_machine.cpp:23
QwtPickerMachine::QwtPickerMachine
QwtPickerMachine(SelectionType)
Constructor.
Definition: qwt_picker_machine.cpp:16
QwtPickerMachine::reset
void reset()
Set the current state to 0.
Definition: qwt_picker_machine.cpp:46
QwtPickerPolygonMachine::QwtPickerPolygonMachine
QwtPickerPolygonMachine()
Constructor.
Definition: qwt_picker_machine.cpp:384
QwtPickerPolygonMachine::transition
virtual QList< Command > transition(const QwtEventPattern &, const QEvent *) override
Transition.
Definition: qwt_picker_machine.cpp:390
QwtPickerTrackerMachine::QwtPickerTrackerMachine
QwtPickerTrackerMachine()
Constructor.
Definition: qwt_picker_machine.cpp:52
QwtPickerTrackerMachine::transition
virtual QList< Command > transition(const QwtEventPattern &, const QEvent *) override
Transition.
Definition: qwt_picker_machine.cpp:58