-
Notifications
You must be signed in to change notification settings - Fork 0
/
qwt_picker2.cpp
1588 lines (1321 loc) · 39 KB
/
qwt_picker2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_picker2.h"
#include "qwt_picker_machine2.h"
#include "qwt_painter.h"
#include "qwt_math.h"
#include "qwt_widget_overlay.h"
#include <qapplication.h>
#include <qevent.h>
#include <qpainter.h>
#include <qframe.h>
#include <qcursor.h>
#include <qbitmap.h>
#include <qpointer.h>
#include <qpaintengine.h>
#include <qmath.h>
static inline QRegion qwtMaskRegion( const QRect &r, int penWidth )
{
const int pw = qMax( penWidth, 1 );
const int pw2 = penWidth / 2;
int x1 = r.left() - pw2;
int x2 = r.right() + 1 + pw2 + ( pw % 2 );
int y1 = r.top() - pw2;
int y2 = r.bottom() + 1 + pw2 + ( pw % 2 );
QRegion region;
region += QRect( x1, y1, x2 - x1, pw );
region += QRect( x1, y1, pw, y2 - y1 );
region += QRect( x1, y2 - pw, x2 - x1, pw );
region += QRect( x2 - pw, y1, pw, y2 - y1 );
return region;
}
static inline QRegion qwtMaskRegion( const QLine &l, int penWidth )
{
const int pw = qMax( penWidth, 1 );
const int pw2 = penWidth / 2;
QRegion region;
if ( l.x1() == l.x2() )
{
region += QRect( l.x1() - pw2, l.y1(),
pw, l.y2() ).normalized();
}
else if ( l.y1() == l.y2() )
{
region += QRect( l.x1(), l.y1() - pw2,
l.x2(), pw ).normalized();
}
return region;
}
class QwtPicker2Rubberband: public QwtWidgetOverlay
{
public:
QwtPicker2Rubberband( QwtPicker2 *, QWidget * );
protected:
virtual void drawOverlay( QPainter * ) const;
virtual QRegion maskHint() const;
QwtPicker2 *d_picker;
};
class QwtPicker2Tracker: public QwtWidgetOverlay
{
public:
QwtPicker2Tracker( QwtPicker2 *, QWidget * );
protected:
virtual void drawOverlay( QPainter * ) const;
virtual QRegion maskHint() const;
QwtPicker2 *d_picker;
};
class QwtPicker2::PrivateData
{
public:
PrivateData():
enabled( false ),
stateMachine( NULL ),
resizeMode( QwtPicker2::Stretch ),
rubberBand( QwtPicker2::NoRubberBand ),
trackerMode( QwtPicker2::AlwaysOff ),
isActive( false ),
trackerPosition( -1, -1 ),
mouseTracking( false ),
openGL( false )
{
}
bool enabled;
QwtPicker2Machine *stateMachine;
QwtPicker2::ResizeMode resizeMode;
QwtPicker2::RubberBand rubberBand;
QPen rubberBandPen;
QwtPicker2::DisplayMode trackerMode;
QPen trackerPen;
QFont trackerFont;
QPolygon pickedPoints;
bool isActive;
QPoint trackerPosition;
bool mouseTracking; // used to save previous value
QPointer< QwtPicker2Rubberband > rubberBandOverlay;
QPointer< QwtPicker2Tracker> trackerOverlay;
bool openGL;
};
QwtPicker2Rubberband::QwtPicker2Rubberband(
QwtPicker2 *picker, QWidget *parent ):
QwtWidgetOverlay( parent ),
d_picker( picker )
{
setMaskMode( QwtWidgetOverlay::MaskHint );
}
QRegion QwtPicker2Rubberband::maskHint() const
{
return d_picker->rubberBandMask();
}
void QwtPicker2Rubberband::drawOverlay( QPainter *painter ) const
{
painter->setPen( d_picker->rubberBandPen() );
d_picker->drawRubberBand( painter );
}
QwtPicker2Tracker::QwtPicker2Tracker(
QwtPicker2 *picker, QWidget *parent ):
QwtWidgetOverlay( parent ),
d_picker( picker )
{
setMaskMode( QwtWidgetOverlay::MaskHint );
}
QRegion QwtPicker2Tracker::maskHint() const
{
return d_picker->trackerRect( font() );
}
void QwtPicker2Tracker::drawOverlay( QPainter *painter ) const
{
painter->setPen( d_picker->trackerPen() );
d_picker->drawTracker( painter );
}
/*!
Constructor
Creates an picker that is enabled, but without a state machine.
rubber band and tracker are disabled.
\param parent Parent widget, that will be observed
*/
QwtPicker2::QwtPicker2( QWidget *parent ):
QObject( parent )
{
init( parent, NoRubberBand, AlwaysOff );
}
/*!
Constructor
\param rubberBand Rubber band style
\param trackerMode Tracker mode
\param parent Parent widget, that will be observed
*/
QwtPicker2::QwtPicker2( RubberBand rubberBand,
DisplayMode trackerMode, QWidget *parent ):
QObject( parent )
{
init( parent, rubberBand, trackerMode );
}
//! Destructor
QwtPicker2::~QwtPicker2()
{
setMouseTracking( false );
delete d_data->stateMachine;
delete d_data->rubberBandOverlay;
delete d_data->trackerOverlay;
delete d_data;
}
//! Initialize the picker - used by the constructors
void QwtPicker2::init( QWidget *parent,
RubberBand rubberBand, DisplayMode trackerMode )
{
d_data = new PrivateData;
d_data->rubberBand = rubberBand;
if ( parent )
{
if ( parent->focusPolicy() == Qt::NoFocus )
parent->setFocusPolicy( Qt::WheelFocus );
d_data->openGL = parent->inherits( "QGLWidget" );
d_data->trackerFont = parent->font();
d_data->mouseTracking = parent->hasMouseTracking();
setEnabled( true );
}
setTrackerMode( trackerMode );
}
/*!
Set a state machine and delete the previous one
\param stateMachine State machine
\sa stateMachine()
*/
void QwtPicker2::setStateMachine( QwtPicker2Machine *stateMachine )
{
if ( d_data->stateMachine != stateMachine )
{
reset();
delete d_data->stateMachine;
d_data->stateMachine = stateMachine;
if ( d_data->stateMachine )
d_data->stateMachine->reset();
}
}
/*!
\return Assigned state machine
\sa setStateMachine()
*/
QwtPicker2Machine *QwtPicker2::stateMachine()
{
return d_data->stateMachine;
}
/*!
\return Assigned state machine
\sa setStateMachine()
*/
const QwtPicker2Machine *QwtPicker2::stateMachine() const
{
return d_data->stateMachine;
}
//! Return the parent widget, where the selection happens
QWidget *QwtPicker2::parentWidget()
{
QObject *obj = parent();
if ( obj && obj->isWidgetType() )
return static_cast<QWidget *>( obj );
return NULL;
}
//! Return the parent widget, where the selection happens
const QWidget *QwtPicker2::parentWidget() const
{
QObject *obj = parent();
if ( obj && obj->isWidgetType() )
return static_cast< const QWidget *>( obj );
return NULL;
}
/*!
Set the rubber band style
\param rubberBand Rubber band style
The default value is NoRubberBand.
\sa rubberBand(), RubberBand, setRubberBandPen()
*/
void QwtPicker2::setRubberBand( RubberBand rubberBand )
{
d_data->rubberBand = rubberBand;
}
/*!
\return Rubber band style
\sa setRubberBand(), RubberBand, rubberBandPen()
*/
QwtPicker2::RubberBand QwtPicker2::rubberBand() const
{
return d_data->rubberBand;
}
/*!
\brief Set the display mode of the tracker.
A tracker displays information about current position of
the cursor as a string. The display mode controls
if the tracker has to be displayed whenever the observed
widget has focus and cursor (AlwaysOn), never (AlwaysOff), or
only when the selection is active (ActiveOnly).
\param mode Tracker display mode
\warning In case of AlwaysOn, mouseTracking will be enabled
for the observed widget.
\sa trackerMode(), DisplayMode
*/
void QwtPicker2::setTrackerMode( DisplayMode mode )
{
if ( d_data->trackerMode != mode )
{
d_data->trackerMode = mode;
setMouseTracking( d_data->trackerMode == AlwaysOn );
}
}
/*!
\return Tracker display mode
\sa setTrackerMode(), DisplayMode
*/
QwtPicker2::DisplayMode QwtPicker2::trackerMode() const
{
return d_data->trackerMode;
}
/*!
\brief Set the resize mode.
The resize mode controls what to do with the selected points of an active
selection when the observed widget is resized.
Stretch means the points are scaled according to the new
size, KeepSize means the points remain unchanged.
The default mode is Stretch.
\param mode Resize mode
\sa resizeMode(), ResizeMode
*/
void QwtPicker2::setResizeMode( ResizeMode mode )
{
d_data->resizeMode = mode;
}
/*!
\return Resize mode
\sa setResizeMode(), ResizeMode
*/
QwtPicker2::ResizeMode QwtPicker2::resizeMode() const
{
return d_data->resizeMode;
}
/*!
\brief En/disable the picker
When enabled is true an event filter is installed for
the observed widget, otherwise the event filter is removed.
\param enabled true or false
\sa isEnabled(), eventFilter()
*/
void QwtPicker2::setEnabled( bool enabled )
{
if ( d_data->enabled != enabled )
{
d_data->enabled = enabled;
QWidget *w = parentWidget();
if ( w )
{
if ( enabled )
w->installEventFilter( this );
else
w->removeEventFilter( this );
}
updateDisplay();
}
}
/*!
\return true when enabled, false otherwise
\sa setEnabled(), eventFilter()
*/
bool QwtPicker2::isEnabled() const
{
return d_data->enabled;
}
/*!
Set the font for the tracker
\param font Tracker font
\sa trackerFont(), setTrackerMode(), setTrackerPen()
*/
void QwtPicker2::setTrackerFont( const QFont &font )
{
if ( font != d_data->trackerFont )
{
d_data->trackerFont = font;
updateDisplay();
}
}
/*!
\return Tracker font
\sa setTrackerFont(), trackerMode(), trackerPen()
*/
QFont QwtPicker2::trackerFont() const
{
return d_data->trackerFont;
}
/*!
Set the pen for the tracker
\param pen Tracker pen
\sa trackerPen(), setTrackerMode(), setTrackerFont()
*/
void QwtPicker2::setTrackerPen( const QPen &pen )
{
if ( pen != d_data->trackerPen )
{
d_data->trackerPen = pen;
updateDisplay();
}
}
/*!
\return Tracker pen
\sa setTrackerPen(), trackerMode(), trackerFont()
*/
QPen QwtPicker2::trackerPen() const
{
return d_data->trackerPen;
}
/*!
Set the pen for the rubberband
\param pen Rubber band pen
\sa rubberBandPen(), setRubberBand()
*/
void QwtPicker2::setRubberBandPen( const QPen &pen )
{
if ( pen != d_data->rubberBandPen )
{
d_data->rubberBandPen = pen;
updateDisplay();
}
}
/*!
\return Rubber band pen
\sa setRubberBandPen(), rubberBand()
*/
QPen QwtPicker2::rubberBandPen() const
{
return d_data->rubberBandPen;
}
/*!
\brief Return the label for a position
In case of HLineRubberBand the label is the value of the
y position, in case of VLineRubberBand the value of the x position.
Otherwise the label contains x and y position separated by a ',' .
The format for the string conversion is "%d".
\param pos Position
\return Converted position as string
*/
QwtText QwtPicker2::trackerText( const QPoint &pos ) const
{
QString label;
switch ( rubberBand() )
{
case HLineRubberBand:
label.sprintf( "%d", pos.y() );
break;
case VLineRubberBand:
label.sprintf( "%d", pos.x() );
break;
default:
label.sprintf( "%d, %d", pos.x(), pos.y() );
}
return label;
}
/*!
Calculate the mask for the rubber band overlay
\return Region for the mask
\sa QWidget::setMask()
*/
QRegion QwtPicker2::rubberBandMask() const
{
QRegion mask;
if ( !isActive() || rubberBand() == NoRubberBand ||
rubberBandPen().style() == Qt::NoPen )
{
return mask;
}
const QPolygon pa = adjustedPoints( d_data->pickedPoints );
QwtPicker2Machine::SelectionType selectionType =
QwtPicker2Machine::NoSelection;
if ( d_data->stateMachine )
selectionType = d_data->stateMachine->selectionType();
switch ( selectionType )
{
case QwtPicker2Machine::NoSelection:
case QwtPicker2Machine::PointSelection:
{
if ( pa.count() < 1 )
return mask;
const QPoint pos = pa[0];
const int pw = rubberBandPen().width();
const QRect pRect = pickArea().boundingRect().toRect();
switch ( rubberBand() )
{
case VLineRubberBand:
{
mask += qwtMaskRegion( QLine( pos.x(), pRect.top(),
pos.x(), pRect.bottom() ), pw );
break;
}
case HLineRubberBand:
{
mask += qwtMaskRegion( QLine( pRect.left(), pos.y(),
pRect.right(), pos.y() ), pw );
break;
}
case CrossRubberBand:
{
mask += qwtMaskRegion( QLine( pos.x(), pRect.top(),
pos.x(), pRect.bottom() ), pw );
mask += qwtMaskRegion( QLine( pRect.left(), pos.y(),
pRect.right(), pos.y() ), pw );
break;
}
default:
break;
}
break;
}
case QwtPicker2Machine::RectSelection:
{
if ( pa.count() < 2 )
return mask;
const int pw = rubberBandPen().width();
switch ( rubberBand() )
{
case RectRubberBand:
{
const QRect r = QRect( pa.first(), pa.last() );
mask = qwtMaskRegion( r.normalized(), pw );
break;
}
case EllipseRubberBand:
{
const QRect r = QRect( pa.first(), pa.last() );
mask += r.adjusted( -pw, -pw, pw, pw );
break;
}
default:
break;
}
break;
}
case QwtPicker2Machine::PolygonSelection:
{
const int pw = rubberBandPen().width();
if ( pw <= 1 )
{
// because of the join style we better
// return a mask for a pen width <= 1 only
const int off = 2 * pw;
const QRect r = pa.boundingRect();
mask += r.adjusted( -off, -off, off, off );
}
break;
}
default:
break;
}
return mask;
}
/*!
Draw a rubber band, depending on rubberBand()
\param painter Painter, initialized with a clip region
\sa rubberBand(), RubberBand
*/
void QwtPicker2::drawRubberBand( QPainter *painter ) const
{
if ( !isActive() || rubberBand() == NoRubberBand ||
rubberBandPen().style() == Qt::NoPen )
{
return;
}
const QPolygon pa = adjustedPoints( d_data->pickedPoints );
QwtPicker2Machine::SelectionType selectionType =
QwtPicker2Machine::NoSelection;
if ( d_data->stateMachine )
selectionType = d_data->stateMachine->selectionType();
switch ( selectionType )
{
case QwtPicker2Machine::NoSelection:
case QwtPicker2Machine::PointSelection:
{
if ( pa.count() < 1 )
return;
const QPoint pos = pa[0];
const QRect pRect = pickArea().boundingRect().toRect();
switch ( rubberBand() )
{
case VLineRubberBand:
{
QwtPainter::drawLine( painter, pos.x(),
pRect.top(), pos.x(), pRect.bottom() );
break;
}
case HLineRubberBand:
{
QwtPainter::drawLine( painter, pRect.left(),
pos.y(), pRect.right(), pos.y() );
break;
}
case CrossRubberBand:
{
QwtPainter::drawLine( painter, pos.x(),
pRect.top(), pos.x(), pRect.bottom() );
QwtPainter::drawLine( painter, pRect.left(),
pos.y(), pRect.right(), pos.y() );
break;
}
default:
break;
}
break;
}
case QwtPicker2Machine::RectSelection:
{
if ( pa.count() < 2 )
return;
const QRect rect = QRect( pa.first(), pa.last() ).normalized();
switch ( rubberBand() )
{
case EllipseRubberBand:
{
QwtPainter::drawEllipse( painter, rect );
break;
}
case RectRubberBand:
{
QwtPainter::drawRect( painter, rect );
break;
}
default:
break;
}
break;
}
case QwtPicker2Machine::PolygonSelection:
{
if ( rubberBand() == PolygonRubberBand )
painter->drawPolyline( pa );
break;
}
default:
break;
}
}
/*!
Draw the tracker
\param painter Painter
\sa trackerRect(), trackerText()
*/
void QwtPicker2::drawTracker( QPainter *painter ) const
{
const QRect textRect = trackerRect( painter->font() );
if ( !textRect.isEmpty() )
{
const QwtText label = trackerText( d_data->trackerPosition );
if ( !label.isEmpty() )
label.draw( painter, textRect );
}
}
/*!
\brief Map the pickedPoints() into a selection()
adjustedPoints() maps the points, that have been collected on
the parentWidget() into a selection(). The default implementation
simply returns the points unmodified.
The reason, why a selection() differs from the picked points
depends on the application requirements. F.e. :
- A rectangular selection might need to have a specific aspect ratio only.
- A selection could accept non intersecting polygons only.
- ...
The example below is for a rectangular selection, where the first
point is the center of the selected rectangle.
\par Example
\code
QPolygon MyPicker::adjustedPoints( const QPolygon &points ) const
{
QPolygon adjusted;
if ( points.size() == 2 )
{
const int width = qAbs( points[1].x() - points[0].x() );
const int height = qAbs( points[1].y() - points[0].y() );
QRect rect( 0, 0, 2 * width, 2 * height );
rect.moveCenter( points[0] );
adjusted += rect.topLeft();
adjusted += rect.bottomRight();
}
return adjusted;
}
\endcode
\endpar
\param points Selected points
\return Selected points unmodified
*/
QPolygon QwtPicker2::adjustedPoints( const QPolygon &points ) const
{
return points;
}
/*!
\return Selected points
\sa pickedPoints(), adjustedPoints()
*/
QPolygon QwtPicker2::selection() const
{
return adjustedPoints( d_data->pickedPoints );
}
//! \return Current position of the tracker
QPoint QwtPicker2::trackerPosition() const
{
return d_data->trackerPosition;
}
/*!
Calculate the bounding rectangle for the tracker text
from the current position of the tracker
\param font Font of the tracker text
\return Bounding rectangle of the tracker text
\sa trackerPosition()
*/
QRect QwtPicker2::trackerRect( const QFont &font ) const
{
if ( trackerMode() == AlwaysOff ||
( trackerMode() == ActiveOnly && !isActive() ) )
{
return QRect();
}
if ( d_data->trackerPosition.x() < 0 || d_data->trackerPosition.y() < 0 )
return QRect();
QwtText text = trackerText( d_data->trackerPosition );
if ( text.isEmpty() )
return QRect();
const QSizeF textSize = text.textSize( font );
QRect textRect( 0, 0, qCeil( textSize.width() ), qCeil( textSize.height() ) );
const QPoint &pos = d_data->trackerPosition;
int alignment = 0;
if ( isActive() && d_data->pickedPoints.count() > 1
&& rubberBand() != NoRubberBand )
{
const QPoint last =
d_data->pickedPoints[ d_data->pickedPoints.count() - 2 ];
alignment |= ( pos.x() >= last.x() ) ? Qt::AlignRight : Qt::AlignLeft;
alignment |= ( pos.y() > last.y() ) ? Qt::AlignBottom : Qt::AlignTop;
}
else
alignment = Qt::AlignTop | Qt::AlignRight;
const int margin = 5;
int x = pos.x();
if ( alignment & Qt::AlignLeft )
x -= textRect.width() + margin;
else if ( alignment & Qt::AlignRight )
x += margin;
int y = pos.y();
if ( alignment & Qt::AlignBottom )
y += margin;
else if ( alignment & Qt::AlignTop )
y -= textRect.height() + margin;
textRect.moveTopLeft( QPoint( x, y ) );
const QRect pickRect = pickArea().boundingRect().toRect();
int right = qMin( textRect.right(), pickRect.right() - margin );
int bottom = qMin( textRect.bottom(), pickRect.bottom() - margin );
textRect.moveBottomRight( QPoint( right, bottom ) );
int left = qMax( textRect.left(), pickRect.left() + margin );
int top = qMax( textRect.top(), pickRect.top() + margin );
textRect.moveTopLeft( QPoint( left, top ) );
return textRect;
}
/*!
\brief Event filter
When isEnabled() is true all events of the observed widget are filtered.
Mouse and keyboard events are translated into widgetMouse- and widgetKey-
and widgetWheel-events. Paint and Resize events are handled to keep
rubber band and tracker up to date.
\param object Object to be filtered
\param event Event
\return Always false.
\sa widgetEnterEvent(), widgetLeaveEvent(),
widgetMousePressEvent(), widgetMouseReleaseEvent(),
widgetMouseDoubleClickEvent(), widgetMouseMoveEvent(),
widgetWheelEvent(), widgetKeyPressEvent(), widgetKeyReleaseEvent(),
QObject::installEventFilter(), QObject::event()
*/
bool QwtPicker2::eventFilter( QObject *object, QEvent *event )
{
if ( object && object == parentWidget() )
{
switch ( event->type() )
{
case QEvent::Resize:
{
const QResizeEvent *re = static_cast<QResizeEvent *>( event );
/*
Adding/deleting additional event filters inside of an event filter
is not safe dues to the implementation in Qt ( changing alist while iterating ).
So we create the overlays in a way, that they don't install en event filter
( parent set to NULL ) and do the resizing here.
*/
if ( d_data->trackerOverlay )
d_data->trackerOverlay->resize( re->size() );
if ( d_data->rubberBandOverlay )
d_data->rubberBandOverlay->resize( re->size() );
if ( d_data->resizeMode == Stretch )
stretchSelection( re->oldSize(), re->size() );
updateDisplay();
break;
}
case QEvent::Enter:
{
widgetEnterEvent( event );
break;
}
case QEvent::Leave:
{
widgetLeaveEvent( event );
break;
}
case QEvent::MouseButtonPress:
{
widgetMousePressEvent( static_cast<QMouseEvent *>( event ) );
break;
}
case QEvent::MouseButtonRelease:
{
widgetMouseReleaseEvent( static_cast<QMouseEvent *>( event ) );
break;
}
case QEvent::MouseButtonDblClick:
{
widgetMouseDoubleClickEvent( static_cast<QMouseEvent *>( event ) );
break;
}
case QEvent::MouseMove:
{
widgetMouseMoveEvent( static_cast<QMouseEvent *>( event ) );
break;
}
case QEvent::KeyPress:
{
widgetKeyPressEvent( static_cast<QKeyEvent *>( event ) );
break;
}
case QEvent::KeyRelease:
{
widgetKeyReleaseEvent( static_cast<QKeyEvent *>( event ) );
break;
}
case QEvent::Wheel:
{
widgetWheelEvent( static_cast<QWheelEvent *>( event ) );
break;
}
default:
break;
}
}
return false;
}
/*!
Handle a mouse press event for the observed widget.
\param mouseEvent Mouse event
\sa eventFilter(), widgetMouseReleaseEvent(),
widgetMouseDoubleClickEvent(), widgetMouseMoveEvent(),
widgetWheelEvent(), widgetKeyPressEvent(), widgetKeyReleaseEvent()
*/
void QwtPicker2::widgetMousePressEvent( QMouseEvent *mouseEvent )
{
transition( mouseEvent );
}
/*!
Handle a mouse move event for the observed widget.
\param mouseEvent Mouse event
\sa eventFilter(), widgetMousePressEvent(), widgetMouseReleaseEvent(),
widgetMouseDoubleClickEvent(),
widgetWheelEvent(), widgetKeyPressEvent(), widgetKeyReleaseEvent()
*/
void QwtPicker2::widgetMouseMoveEvent( QMouseEvent *mouseEvent )
{
if ( pickArea().contains( mouseEvent->pos() ) )