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
/* -*- 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_dial.h"
#include "qwt_dial_needle.h"
#include "qwt_math.h"
#include "qwt_scale_engine.h"
#include "qwt_scale_map.h"
#include "qwt_round_scale_draw.h"
#include "qwt_painter.h"
#include <qpainter.h>
#include <qpalette.h>
#include <qpixmap.h>
#include <qevent.h>
#include <qalgorithms.h>
#include <qmath.h>
#include <qstyle.h>
#include <qstyleoption.h>
#include <qapplication.h>
static inline double qwtAngleDist( double a1, double a2 )
{
double dist = qAbs( a2 - a1 );
if ( dist > 360.0 )
dist -= 360.0;
return dist;
}
static inline bool qwtIsOnArc( double angle, double min, double max )
{
if ( min < max )
{
return ( angle >= min ) && ( angle <= max );
}
else
{
return ( angle >= min ) || ( angle <= max );
}
}
static inline double qwtBoundedAngle( double min, double angle, double max )
{
double from = qwtNormalizeDegrees( min );
double to = qwtNormalizeDegrees( max );
double a;
if ( qwtIsOnArc( angle, from, to ) )
{
a = angle;
if ( a < min )
a += 360.0;
}
else
{
if ( qwtAngleDist( angle, from ) <
qwtAngleDist( angle, to ) )
{
a = min;
}
else
{
a = max;
}
}
return a;
}
class QwtDial::PrivateData
{
public:
PrivateData():
frameShadow( Sunken ),
lineWidth( 0 ),
mode( RotateNeedle ),
origin( 90.0 ),
minScaleArc( 0.0 ),
maxScaleArc( 0.0 ),
needle( NULL ),
arcOffset( 0.0 ),
mouseOffset( 0.0 )
{
}
~PrivateData()
{
delete needle;
}
Shadow frameShadow;
int lineWidth;
QwtDial::Mode mode;
double origin;
double minScaleArc;
double maxScaleArc;
double scalePenWidth;
QwtDialNeedle *needle;
double arcOffset;
double mouseOffset;
QPixmap pixmapCache;
};
/*!
\brief Constructor
\param parent Parent widget
Create a dial widget with no needle. The scale is initialized
to [ 0.0, 360.0 ] and 360 steps ( QwtAbstractSlider::setTotalSteps() ).
The origin of the scale is at 90°,
The value is set to 0.0.
The default mode is QwtDial::RotateNeedle.
*/
QwtDial::QwtDial( QWidget* parent ):
QwtAbstractSlider( parent )
{
d_data = new PrivateData;
setFocusPolicy( Qt::TabFocus );
QPalette p = palette();
for ( int i = 0; i < QPalette::NColorGroups; i++ )
{
const QPalette::ColorGroup colorGroup =
static_cast<QPalette::ColorGroup>( i );
// Base: background color of the circle inside the frame.
// WindowText: background color of the circle inside the scale
p.setColor( colorGroup, QPalette::WindowText,
p.color( colorGroup, QPalette::Base ) );
}
setPalette( p );
QwtRoundScaleDraw* scaleDraw = new QwtRoundScaleDraw();
scaleDraw->setRadius( 0 );
setScaleDraw( scaleDraw );
setScaleArc( 0.0, 360.0 ); // scale as a full circle
setScaleMaxMajor( 10 );
setScaleMaxMinor( 5 );
setValue( 0.0 );
}
//! Destructor
QwtDial::~QwtDial()
{
delete d_data;
}
/*!
Sets the frame shadow value from the frame style.
\param shadow Frame shadow
\sa setLineWidth(), QFrame::setFrameShadow()
*/
void QwtDial::setFrameShadow( Shadow shadow )
{
if ( shadow != d_data->frameShadow )
{
invalidateCache();
d_data->frameShadow = shadow;
if ( lineWidth() > 0 )
update();
}
}
/*!
\return Frame shadow
/sa setFrameShadow(), lineWidth(), QFrame::frameShadow()
*/
QwtDial::Shadow QwtDial::frameShadow() const
{
return d_data->frameShadow;
}
/*!
Sets the line width of the frame
\param lineWidth Line width
\sa setFrameShadow()
*/
void QwtDial::setLineWidth( int lineWidth )
{
if ( lineWidth < 0 )
lineWidth = 0;
if ( d_data->lineWidth != lineWidth )
{
invalidateCache();
d_data->lineWidth = lineWidth;
update();
}
}
/*!
\return Line width of the frame
\sa setLineWidth(), frameShadow(), lineWidth()
*/
int QwtDial::lineWidth() const
{
return d_data->lineWidth;
}
/*!
\return bounding rectangle of the circle inside the frame
\sa setLineWidth(), scaleInnerRect(), boundingRect()
*/
QRect QwtDial::innerRect() const
{
const int lw = lineWidth();
return boundingRect().adjusted( lw, lw, -lw, -lw );
}
/*!
\return bounding rectangle of the dial including the frame
\sa setLineWidth(), scaleInnerRect(), innerRect()
*/
QRect QwtDial::boundingRect() const
{
const QRect cr = contentsRect();
const double dim = qMin( cr.width(), cr.height() );
QRect inner( 0, 0, dim, dim );
inner.moveCenter( cr.center() );
return inner;
}
/*!
\return rectangle inside the scale
\sa setLineWidth(), boundingRect(), innerRect()
*/
QRect QwtDial::scaleInnerRect() const
{
QRect rect = innerRect();
const QwtAbstractScaleDraw *sd = scaleDraw();
if ( sd )
{
int scaleDist = qCeil( sd->extent( font() ) );
scaleDist++; // margin
rect.adjust( scaleDist, scaleDist, -scaleDist, -scaleDist );
}
return rect;
}
/*!
\brief Change the mode of the dial.
\param mode New mode
In case of QwtDial::RotateNeedle the needle is rotating, in case of
QwtDial::RotateScale, the needle points to origin()
and the scale is rotating.
The default mode is QwtDial::RotateNeedle.
\sa mode(), setValue(), setOrigin()
*/
void QwtDial::setMode( Mode mode )
{
if ( mode != d_data->mode )
{
invalidateCache();
d_data->mode = mode;
sliderChange();
}
}
/*!
\return Mode of the dial.
\sa setMode(), origin(), setScaleArc(), value()
*/
QwtDial::Mode QwtDial::mode() const
{
return d_data->mode;
}
/*!
Invalidate the internal caches used to speed up repainting
*/
void QwtDial::invalidateCache()
{
d_data->pixmapCache = QPixmap();
}
/*!
Paint the dial
\param event Paint event
*/
void QwtDial::paintEvent( QPaintEvent *event )
{
QPainter painter( this );
painter.setClipRegion( event->region() );
QStyleOption opt;
opt.init(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
if ( d_data->mode == QwtDial::RotateScale )
{
painter.save();
painter.setRenderHint( QPainter::Antialiasing, true );
drawContents( &painter );
painter.restore();
}
const QRect r = contentsRect();
if ( r.size() != d_data->pixmapCache.size() )
{
d_data->pixmapCache = QwtPainter::backingStore( this, r.size() );
d_data->pixmapCache.fill( Qt::transparent );
QPainter p( &d_data->pixmapCache );
p.setRenderHint( QPainter::Antialiasing, true );
p.translate( -r.topLeft() );
if ( d_data->mode != QwtDial::RotateScale )
drawContents( &p );
if ( lineWidth() > 0 )
drawFrame( &p );
if ( d_data->mode != QwtDial::RotateNeedle )
drawNeedle( &p );
}
painter.drawPixmap( r.topLeft(), d_data->pixmapCache );
if ( d_data->mode == QwtDial::RotateNeedle )
drawNeedle( &painter );
if ( hasFocus() )
drawFocusIndicator( &painter );
}
/*!
Draw the focus indicator
\param painter Painter
*/
void QwtDial::drawFocusIndicator( QPainter *painter ) const
{
QwtPainter::drawFocusRect( painter, this, boundingRect() );
}
/*!
Draw the frame around the dial
\param painter Painter
\sa lineWidth(), frameShadow()
*/
void QwtDial::drawFrame( QPainter *painter )
{
QwtPainter::drawRoundFrame( painter, boundingRect(),
palette(), lineWidth(), d_data->frameShadow );
}
/*!
\brief Draw the contents inside the frame
QPalette::Window is the background color outside of the frame.
QPalette::Base is the background color inside the frame.
QPalette::WindowText is the background color inside the scale.
\param painter Painter
\sa boundingRect(), innerRect(),
scaleInnerRect(), QWidget::setPalette()
*/
void QwtDial::drawContents( QPainter *painter ) const
{
if ( testAttribute( Qt::WA_NoSystemBackground ) ||
palette().brush( QPalette::Base ) !=
palette().brush( QPalette::Window ) )
{
const QRectF br = boundingRect();
painter->save();
painter->setPen( Qt::NoPen );
painter->setBrush( palette().brush( QPalette::Base ) );
painter->drawEllipse( br );
painter->restore();
}
const QRectF insideScaleRect = scaleInnerRect();
if ( palette().brush( QPalette::WindowText ) !=
palette().brush( QPalette::Base ) )
{
painter->save();
painter->setPen( Qt::NoPen );
painter->setBrush( palette().brush( QPalette::WindowText ) );
painter->drawEllipse( insideScaleRect );
painter->restore();
}
const QPointF center = insideScaleRect.center();
const double radius = 0.5 * insideScaleRect.width();
painter->save();
drawScale( painter, center, radius );
painter->restore();
painter->save();
drawScaleContents( painter, center, radius );
painter->restore();
}
/*!
Draw the needle
\param painter Painter
\param center Center of the dial
\param radius Length for the needle
\param direction Direction of the needle in degrees, counter clockwise
\param colorGroup ColorGroup
*/
void QwtDial::drawNeedle( QPainter *painter, const QPointF ¢er,
double radius, double direction, QPalette::ColorGroup colorGroup ) const
{
if ( d_data->needle )
{
direction = 360.0 - direction; // counter clockwise
d_data->needle->draw( painter, center, radius, direction, colorGroup );
}
}
void QwtDial::drawNeedle( QPainter *painter ) const
{
if ( !isValid() )
return;
QPalette::ColorGroup colorGroup;
if ( isEnabled() )
colorGroup = hasFocus() ? QPalette::Active : QPalette::Inactive;
else
colorGroup = QPalette::Disabled;
const QRectF sr = scaleInnerRect();
painter->save();
painter->setRenderHint( QPainter::Antialiasing, true );
drawNeedle( painter, sr.center(), 0.5 * sr.width(),
transform( value() ) + 270.0, colorGroup );
painter->restore();
}
/*!
Draw the scale
\param painter Painter
\param center Center of the dial
\param radius Radius of the scale
*/
void QwtDial::drawScale( QPainter *painter,
const QPointF ¢er, double radius ) const
{
QwtRoundScaleDraw *sd = const_cast<QwtRoundScaleDraw *>( scaleDraw() );
if ( sd == NULL )
return;
sd->setRadius( radius );
sd->moveCenter( center );
QPalette pal = palette();
const QColor textColor = pal.color( QPalette::Text );
pal.setColor( QPalette::WindowText, textColor ); // ticks, backbone
painter->setFont( font() );
painter->setPen( QPen( textColor, sd->penWidth() ) );
painter->setBrush( Qt::red );
sd->draw( painter, pal );
}
/*!
Draw the contents inside the scale
Paints nothing.
\param painter Painter
\param center Center of the contents circle
\param radius Radius of the contents circle
*/
void QwtDial::drawScaleContents( QPainter *painter,
const QPointF ¢er, double radius ) const
{
Q_UNUSED(painter);
Q_UNUSED(center);
Q_UNUSED(radius);
}
/*!
Set a needle for the dial
\param needle Needle
\warning The needle will be deleted, when a different needle is
set or in ~QwtDial()
*/
void QwtDial::setNeedle( QwtDialNeedle *needle )
{
if ( needle != d_data->needle )
{
if ( d_data->needle )
delete d_data->needle;
d_data->needle = needle;
update();
}
}
/*!
\return needle
\sa setNeedle()
*/
const QwtDialNeedle *QwtDial::needle() const
{
return d_data->needle;
}
/*!
\return needle
\sa setNeedle()
*/
QwtDialNeedle *QwtDial::needle()
{
return d_data->needle;
}
//! \return the scale draw
QwtRoundScaleDraw *QwtDial::scaleDraw()
{
return static_cast<QwtRoundScaleDraw *>( abstractScaleDraw() );
}
//! \return the scale draw
const QwtRoundScaleDraw *QwtDial::scaleDraw() const
{
return static_cast<const QwtRoundScaleDraw *>( abstractScaleDraw() );
}
/*!
Set an individual scale draw
The motivation for setting a scale draw is often
to overload QwtRoundScaleDraw::label() to return
individual tick labels.
\param scaleDraw Scale draw
\warning The previous scale draw is deleted
*/
void QwtDial::setScaleDraw( QwtRoundScaleDraw *scaleDraw )
{
setAbstractScaleDraw( scaleDraw );
sliderChange();
}
/*!
Change the arc of the scale
\param minArc Lower limit
\param maxArc Upper limit
\sa minScaleArc(), maxScaleArc()
*/
void QwtDial::setScaleArc( double minArc, double maxArc )
{
if ( minArc != 360.0 && minArc != -360.0 )
minArc = ::fmod( minArc, 360.0 );
if ( maxArc != 360.0 && maxArc != -360.0 )
maxArc = ::fmod( maxArc, 360.0 );
double minScaleArc = qMin( minArc, maxArc );
double maxScaleArc = qMax( minArc, maxArc );
if ( maxScaleArc - minScaleArc > 360.0 )
maxScaleArc = minScaleArc + 360.0;
if ( ( minScaleArc != d_data->minScaleArc ) ||
( maxScaleArc != d_data->maxScaleArc ) )
{
d_data->minScaleArc = minScaleArc;
d_data->maxScaleArc = maxScaleArc;
invalidateCache();
sliderChange();
}
}
/*!
Set the lower limit for the scale arc
\param min Lower limit of the scale arc
\sa setScaleArc(), setMaxScaleArc()
*/
void QwtDial::setMinScaleArc( double min )
{
setScaleArc( min, d_data->maxScaleArc );
}
/*!
\return Lower limit of the scale arc
\sa setScaleArc()
*/
double QwtDial::minScaleArc() const
{
return d_data->minScaleArc;
}
/*!
Set the upper limit for the scale arc
\param max Upper limit of the scale arc
\sa setScaleArc(), setMinScaleArc()
*/
void QwtDial::setMaxScaleArc( double max )
{
setScaleArc( d_data->minScaleArc, max );
}
/*!
\return Upper limit of the scale arc
\sa setScaleArc()
*/
double QwtDial::maxScaleArc() const
{
return d_data->maxScaleArc;
}
/*!
\brief Change the origin
The origin is the angle where scale and needle is relative to.
\param origin New origin
\sa origin()
*/
void QwtDial::setOrigin( double origin )
{
invalidateCache();
d_data->origin = origin;
sliderChange();
}
/*!
The origin is the angle where scale and needle is relative to.
\return Origin of the dial
\sa setOrigin()
*/
double QwtDial::origin() const
{
return d_data->origin;
}
/*!
\return Size hint
\sa minimumSizeHint()
*/
QSize QwtDial::sizeHint() const
{
int sh = 0;
if ( scaleDraw() )
sh = qCeil( scaleDraw()->extent( font() ) );
const int d = 6 * sh + 2 * lineWidth();
QSize hint( d, d );
if ( !isReadOnly() )
hint = hint.expandedTo( QApplication::globalStrut() );
return hint;
}
/*!
\return Minimum size hint
\sa sizeHint()
*/
QSize QwtDial::minimumSizeHint() const
{
int sh = 0;
if ( scaleDraw() )
sh = qCeil( scaleDraw()->extent( font() ) );
const int d = 3 * sh + 2 * lineWidth();
return QSize( d, d );
}
/*!
\brief Determine what to do when the user presses a mouse button.
\param pos Mouse position
\retval True, when the inner circle contains pos
\sa scrolledTo()
*/
bool QwtDial::isScrollPosition( const QPoint &pos ) const
{
const QRegion region( innerRect(), QRegion::Ellipse );
if ( region.contains( pos ) && ( pos != innerRect().center() ) )
{
double angle = QLineF( rect().center(), pos ).angle();
if ( d_data->mode == QwtDial::RotateScale )
angle = 360.0 - angle;
double valueAngle =
qwtNormalizeDegrees( 90.0 - transform( value() ) );
d_data->mouseOffset = qwtNormalizeDegrees( angle - valueAngle );
d_data->arcOffset = scaleMap().p1();
return true;
}
return false;
}
/*!
\brief Determine the value for a new position of the
slider handle.
\param pos Mouse position
\return Value for the mouse position
\sa isScrollPosition()
*/
double QwtDial::scrolledTo( const QPoint &pos ) const
{
double angle = QLineF( rect().center(), pos ).angle();
if ( d_data->mode == QwtDial::RotateScale )
{
angle += scaleMap().p1() - d_data->arcOffset;
angle = 360.0 - angle;
}
angle = qwtNormalizeDegrees( angle - d_data->mouseOffset );
angle = qwtNormalizeDegrees( 90.0 - angle );
if ( scaleMap().pDist() >= 360.0 )
{
if ( angle < scaleMap().p1() )
angle += 360.0;
if ( !wrapping() )
{
double boundedAngle = angle;
const double arc = angle - transform( value() );
if ( qAbs( arc ) > 180.0 )
{
boundedAngle = ( arc > 0 )
? scaleMap().p1() : scaleMap().p2();
}
d_data->mouseOffset += ( boundedAngle - angle );
angle = boundedAngle;
}
}
else
{
const double boundedAngle =
qwtBoundedAngle( scaleMap().p1(), angle, scaleMap().p2() );
if ( !wrapping() )
d_data->mouseOffset += ( boundedAngle - angle );
angle = boundedAngle;
}
return invTransform( angle );
}
/*!
Change Event handler
\param event Change event
Invalidates internal paint caches if necessary
*/
void QwtDial::changeEvent( QEvent *event )
{
switch( event->type() )
{
case QEvent::EnabledChange:
case QEvent::FontChange:
case QEvent::StyleChange:
case QEvent::PaletteChange:
case QEvent::LanguageChange:
case QEvent::LocaleChange:
{
invalidateCache();
break;
}
default:
break;
}
QwtAbstractSlider::changeEvent( event );
}
/*!
Wheel Event handler
\param event Wheel event
*/
void QwtDial::wheelEvent( QWheelEvent *event )
{
const QRegion region( innerRect(), QRegion::Ellipse );
if ( region.contains( event->pos() ) )
QwtAbstractSlider::wheelEvent( event );
}
void QwtDial::setAngleRange( double angle, double span )
{
QwtRoundScaleDraw *sd = const_cast<QwtRoundScaleDraw *>( scaleDraw() );
if ( sd )
{
angle = qwtNormalizeDegrees( angle - 270.0 );
sd->setAngleRange( angle, angle + span );
}
}
/*!
Invalidate the internal caches and call
QwtAbstractSlider::scaleChange()
*/
void QwtDial::scaleChange()
{
invalidateCache();
QwtAbstractSlider::scaleChange();
}
void QwtDial::sliderChange()
{
setAngleRange( d_data->origin + d_data->minScaleArc,
d_data->maxScaleArc - d_data->minScaleArc );
if ( mode() == RotateScale )
{
const double arc = transform( value() ) - scaleMap().p1();
setAngleRange( d_data->origin - arc,
d_data->maxScaleArc - d_data->minScaleArc );
}
QwtAbstractSlider::sliderChange();
}