Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
qwt_thermo.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_thermo.h"
11#include "qwt_scale_draw.h"
12#include "qwt_scale_map.h"
13#include "qwt_color_map.h"
14#include "qwt_math.h"
15
16#include <qpainter.h>
17#include <qevent.h>
18#include <qdrawutil.h>
19#include <qstyle.h>
20#include <qstyleoption.h>
21#include <qmargins.h>
22
23#include <algorithm>
24#include <functional>
25
26static inline void qwtDrawLine( QPainter* painter, int pos,
27 const QColor& color, const QRect& pipeRect, const QRect& liquidRect,
28 Qt::Orientation orientation )
29{
30 painter->setPen( color );
31 if ( orientation == Qt::Horizontal )
32 {
33 if ( pos >= liquidRect.left() && pos < liquidRect.right() )
34 painter->drawLine( pos, pipeRect.top(), pos, pipeRect.bottom() );
35 }
36 else
37 {
38 if ( pos >= liquidRect.top() && pos < liquidRect.bottom() )
39 painter->drawLine( pipeRect.left(), pos, pipeRect.right(), pos );
40 }
41}
42
43static QVector< double > qwtTickList( const QwtScaleDiv& scaleDiv )
44{
45 QVector< double > values;
46
47 double lowerLimit = scaleDiv.interval().minValue();
48 double upperLimit = scaleDiv.interval().maxValue();
49
50 if ( upperLimit < lowerLimit )
51 qSwap( lowerLimit, upperLimit );
52
53 values += lowerLimit;
54
55 for ( int tickType = QwtScaleDiv::MinorTick;
56 tickType < QwtScaleDiv::NTickTypes; tickType++ )
57 {
58 const QList< double > ticks = scaleDiv.ticks( tickType );
59
60 for ( int i = 0; i < ticks.count(); i++ )
61 {
62 const double v = ticks[i];
63 if ( v > lowerLimit && v < upperLimit )
64 values += v;
65 }
66 }
67
68 values += upperLimit;
69
70 return values;
71}
72
73class QwtThermo::PrivateData
74{
75 public:
76 PrivateData()
77 : orientation( Qt::Vertical )
78 , scalePosition( QwtThermo::TrailingScale )
79 , spacing( 3 )
80 , borderWidth( 2 )
81 , pipeWidth( 10 )
82 , alarmLevel( 0.0 )
83 , alarmEnabled( false )
84 , autoFillPipe( true )
85 , originMode( QwtThermo::OriginMinimum )
86 , origin( 0.0 )
87 , colorMap( NULL )
88 , value( 0.0 )
89 {
90 rangeFlags = QwtInterval::IncludeBorders;
91 }
92
93 ~PrivateData()
94 {
95 delete colorMap;
96 }
97
98 Qt::Orientation orientation;
99 QwtThermo::ScalePosition scalePosition;
100
101 int spacing;
102 int borderWidth;
103 int pipeWidth;
104
105 QwtInterval::BorderFlags rangeFlags;
106 double alarmLevel;
107 bool alarmEnabled;
108 bool autoFillPipe;
109 QwtThermo::OriginMode originMode;
110 double origin;
111
112 QwtColorMap* colorMap;
113
114 double value;
115};
116
121QwtThermo::QwtThermo( QWidget* parent )
122 : QwtAbstractScale( parent )
123{
124 m_data = new PrivateData;
125
126 QSizePolicy policy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
127 if ( m_data->orientation == Qt::Vertical )
128 policy.transpose();
129
130 setSizePolicy( policy );
131
132 setAttribute( Qt::WA_WState_OwnSizePolicy, false );
133 layoutThermo( true );
134}
135
138{
139 delete m_data;
140}
141
158{
159 if ( m_data->rangeFlags != flags )
160 {
161 m_data->rangeFlags = flags;
162 update();
163 }
164}
165
171{
172 return m_data->rangeFlags;
173}
174
181void QwtThermo::setValue( double value )
182{
183 if ( m_data->value != value )
184 {
185 m_data->value = value;
186 update();
187 }
188}
189
191double QwtThermo::value() const
192{
193 return m_data->value;
194}
195
208{
210 layoutThermo( true );
211}
212
218{
219 return static_cast< const QwtScaleDraw* >( abstractScaleDraw() );
220}
221
227{
228 return static_cast< QwtScaleDraw* >( abstractScaleDraw() );
229}
230
235void QwtThermo::paintEvent( QPaintEvent* event )
236{
237 QPainter painter( this );
238 painter.setClipRegion( event->region() );
239
240 QStyleOption opt;
241 opt.initFrom(this);
242 style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
243
244 const QRect tRect = pipeRect();
245
246 if ( !tRect.contains( event->rect() ) )
247 {
248 if ( m_data->scalePosition != QwtThermo::NoScale )
249 scaleDraw()->draw( &painter, palette() );
250 }
251
252 const int bw = m_data->borderWidth;
253
254 const QBrush brush = palette().brush( QPalette::Base );
255 qDrawShadePanel( &painter,
256 tRect.adjusted( -bw, -bw, bw, bw ),
257 palette(), true, bw,
258 m_data->autoFillPipe ? &brush : NULL );
259
260 drawLiquid( &painter, tRect );
261}
262
267void QwtThermo::resizeEvent( QResizeEvent* event )
268{
269 Q_UNUSED( event );
270 layoutThermo( false );
271}
272
277void QwtThermo::changeEvent( QEvent* event )
278{
279 switch( event->type() )
280 {
281 case QEvent::StyleChange:
282 case QEvent::FontChange:
283 {
284 layoutThermo( true );
285 break;
286 }
287 default:
288 break;
289 }
290}
291
299void QwtThermo::layoutThermo( bool update_geometry )
300{
301 const QRect tRect = pipeRect();
302 const int bw = m_data->borderWidth + m_data->spacing;
303 const bool inverted = ( upperBound() < lowerBound() );
304
305 int from, to;
306
307 if ( m_data->orientation == Qt::Horizontal )
308 {
309 from = tRect.left();
310 to = tRect.right();
311
312 if ( m_data->rangeFlags & QwtInterval::ExcludeMinimum )
313 {
314 if ( inverted )
315 to++;
316 else
317 from--;
318 }
319 if ( m_data->rangeFlags & QwtInterval::ExcludeMaximum )
320 {
321 if ( inverted )
322 from--;
323 else
324 to++;
325 }
326
327 if ( m_data->scalePosition == QwtThermo::TrailingScale )
328 {
330 scaleDraw()->move( from, tRect.top() - bw );
331 }
332 else
333 {
335 scaleDraw()->move( from, tRect.bottom() + bw );
336 }
337
338 scaleDraw()->setLength( qMax( to - from, 0 ) );
339 }
340 else // Qt::Vertical
341 {
342 from = tRect.top();
343 to = tRect.bottom();
344
345 if ( m_data->rangeFlags & QwtInterval::ExcludeMinimum )
346 {
347 if ( inverted )
348 from--;
349 else
350 to++;
351 }
352 if ( m_data->rangeFlags & QwtInterval::ExcludeMaximum )
353 {
354 if ( inverted )
355 to++;
356 else
357 from--;
358 }
359
360 if ( m_data->scalePosition == QwtThermo::LeadingScale )
361 {
363 scaleDraw()->move( tRect.right() + bw, from );
364 }
365 else
366 {
368 scaleDraw()->move( tRect.left() - bw, from );
369 }
370
371 scaleDraw()->setLength( qMax( to - from, 0 ) );
372 }
373
374 if ( update_geometry )
375 {
376 updateGeometry();
377 update();
378 }
379}
380
386{
387 int mbd = 0;
388 if ( m_data->scalePosition != QwtThermo::NoScale )
389 {
390 int d1, d2;
391 scaleDraw()->getBorderDistHint( font(), d1, d2 );
392 mbd = qMax( d1, d2 );
393 }
394 const int bw = m_data->borderWidth;
395 const int scaleOff = bw + mbd;
396
397 const QRect cr = contentsRect();
398
399 QRect pipeRect = cr;
400 if ( m_data->orientation == Qt::Horizontal )
401 {
402 pipeRect.adjust( scaleOff, 0, -scaleOff, 0 );
403
404 if ( m_data->scalePosition == QwtThermo::TrailingScale )
405 pipeRect.setTop( cr.top() + cr.height() - bw - m_data->pipeWidth );
406 else
407 pipeRect.setTop( bw );
408
409 pipeRect.setHeight( m_data->pipeWidth );
410 }
411 else // Qt::Vertical
412 {
413 pipeRect.adjust( 0, scaleOff, 0, -scaleOff );
414
415 if ( m_data->scalePosition == QwtThermo::LeadingScale )
416 pipeRect.setLeft( bw );
417 else
418 pipeRect.setLeft( cr.left() + cr.width() - bw - m_data->pipeWidth );
419
420 pipeRect.setWidth( m_data->pipeWidth );
421 }
422
423 return pipeRect;
424}
425
432void QwtThermo::setOrientation( Qt::Orientation orientation )
433{
434 if ( orientation == m_data->orientation )
435 return;
436
437 m_data->orientation = orientation;
438
439 if ( !testAttribute( Qt::WA_WState_OwnSizePolicy ) )
440 {
441 QSizePolicy sp = sizePolicy();
442 sp.transpose();
443 setSizePolicy( sp );
444
445 setAttribute( Qt::WA_WState_OwnSizePolicy, false );
446 }
447
448 layoutThermo( true );
449}
450
455Qt::Orientation QwtThermo::orientation() const
456{
457 return m_data->orientation;
458}
459
465{
466 if ( m == m_data->originMode )
467 return;
468
469 m_data->originMode = m;
470 update();
471}
472
478{
479 return m_data->originMode;
480}
481
491void QwtThermo::setOrigin( double origin )
492{
493 if ( origin == m_data->origin )
494 return;
495
496 m_data->origin = origin;
497 update();
498}
499
504double QwtThermo::origin() const
505{
506 return m_data->origin;
507}
508
516{
517 if ( m_data->scalePosition == scalePosition )
518 return;
519
520 m_data->scalePosition = scalePosition;
521
522 if ( testAttribute( Qt::WA_WState_Polished ) )
523 layoutThermo( true );
524}
525
531{
532 return m_data->scalePosition;
533}
534
537{
538 layoutThermo( true );
539}
540
547 QPainter* painter, const QRect& pipeRect ) const
548{
549 painter->save();
550 painter->setClipRect( pipeRect, Qt::IntersectClip );
551 painter->setPen( Qt::NoPen );
552
554
555 QRect liquidRect = fillRect( pipeRect );
556
557 if ( m_data->colorMap != NULL )
558 {
559 const QwtInterval interval = scaleDiv().interval().normalized();
560
561 // Because the positions of the ticks are rounded
562 // we calculate the colors for the rounded tick values
563
564 QVector< double > values = qwtTickList( scaleDraw()->scaleDiv() );
565
566 if ( scaleMap.isInverting() )
567 std::sort( values.begin(), values.end(), std::greater< double >() );
568 else
569 std::sort( values.begin(), values.end(), std::less< double >() );
570
571 int from;
572 if ( !values.isEmpty() )
573 {
574 from = qRound( scaleMap.transform( values[0] ) );
575 qwtDrawLine( painter, from,
576 m_data->colorMap->color( interval, values[0] ),
577 pipeRect, liquidRect, m_data->orientation );
578 }
579
580 for ( int i = 1; i < values.size(); i++ )
581 {
582 const int to = qRound( scaleMap.transform( values[i] ) );
583
584 for ( int pos = from + 1; pos < to; pos++ )
585 {
586 const double v = scaleMap.invTransform( pos );
587
588 qwtDrawLine( painter, pos,
589 m_data->colorMap->color( interval, v ),
590 pipeRect, liquidRect, m_data->orientation );
591 }
592
593 qwtDrawLine( painter, to,
594 m_data->colorMap->color( interval, values[i] ),
595 pipeRect, liquidRect, m_data->orientation );
596
597 from = to;
598 }
599 }
600 else
601 {
602 if ( !liquidRect.isEmpty() && m_data->alarmEnabled )
603 {
604 const QRect r = alarmRect( liquidRect );
605 if ( !r.isEmpty() )
606 {
607 painter->fillRect( r, palette().brush( QPalette::Highlight ) );
608 liquidRect = QRegion( liquidRect ).subtracted( r ).boundingRect();
609 }
610 }
611
612 painter->fillRect( liquidRect, palette().brush( QPalette::ButtonText ) );
613 }
614
615 painter->restore();
616}
617
629void QwtThermo::setSpacing( int spacing )
630{
631 if ( spacing <= 0 )
632 spacing = 0;
633
634 if ( spacing != m_data->spacing )
635 {
636 m_data->spacing = spacing;
637 layoutThermo( true );
638 }
639}
640
646{
647 return m_data->spacing;
648}
649
656{
657 if ( width <= 0 )
658 width = 0;
659
660 if ( width != m_data->borderWidth )
661 {
662 m_data->borderWidth = width;
663 layoutThermo( true );
664 }
665}
666
672{
673 return m_data->borderWidth;
674}
675
684{
685 if ( colorMap != m_data->colorMap )
686 {
687 delete m_data->colorMap;
688 m_data->colorMap = colorMap;
689 }
690}
691
698{
699 return m_data->colorMap;
700}
701
708{
709 return m_data->colorMap;
710}
711
720void QwtThermo::setFillBrush( const QBrush& brush )
721{
722 QPalette pal = palette();
723 pal.setBrush( QPalette::ButtonText, brush );
724 setPalette( pal );
725}
726
732{
733 return palette().brush( QPalette::ButtonText );
734}
735
747void QwtThermo::setAlarmBrush( const QBrush& brush )
748{
749 QPalette pal = palette();
750 pal.setBrush( QPalette::Highlight, brush );
751 setPalette( pal );
752}
753
762{
763 return palette().brush( QPalette::Highlight );
764}
765
775void QwtThermo::setAlarmLevel( double level )
776{
777 m_data->alarmLevel = level;
778 m_data->alarmEnabled = 1;
779 update();
780}
781
790{
791 return m_data->alarmLevel;
792}
793
800void QwtThermo::setPipeWidth( int width )
801{
802 if ( width > 0 )
803 {
804 m_data->pipeWidth = width;
805 layoutThermo( true );
806 }
807}
808
814{
815 return m_data->pipeWidth;
816}
817
826{
827 m_data->alarmEnabled = on;
828 update();
829}
830
838{
839 return m_data->alarmEnabled;
840}
841
847{
848 return minimumSizeHint();
849}
850
857{
858 int w = 0, h = 0;
859
860 if ( m_data->scalePosition != NoScale )
861 {
862 const int sdExtent = qwtCeil( scaleDraw()->extent( font() ) );
863 const int sdLength = scaleDraw()->minLength( font() );
864
865 w = sdLength;
866 h = m_data->pipeWidth + sdExtent + m_data->spacing;
867
868 }
869 else // no scale
870 {
871 w = 200;
872 h = m_data->pipeWidth;
873 }
874
875 if ( m_data->orientation == Qt::Vertical )
876 qSwap( w, h );
877
878 w += 2 * m_data->borderWidth;
879 h += 2 * m_data->borderWidth;
880
881 // finally add the margins
882 const QMargins m = contentsMargins();
883 w += m.left() + m.right();
884 h += m.top() + m.bottom();
885
886 return QSize( w, h );
887}
888
897QRect QwtThermo::fillRect( const QRect& pipeRect ) const
898{
899 double origin;
900 if ( m_data->originMode == OriginMinimum )
901 {
902 origin = qMin( lowerBound(), upperBound() );
903 }
904 else if ( m_data->originMode == OriginMaximum )
905 {
906 origin = qMax( lowerBound(), upperBound() );
907 }
908 else // OriginCustom
909 {
910 origin = m_data->origin;
911 }
912
914
915 int from = qRound( scaleMap.transform( m_data->value ) );
916 int to = qRound( scaleMap.transform( origin ) );
917
918 if ( to < from )
919 qSwap( from, to );
920
921 QRect fillRect = pipeRect;
922 if ( m_data->orientation == Qt::Horizontal )
923 {
924 fillRect.setLeft( from );
925 fillRect.setRight( to );
926 }
927 else // Qt::Vertical
928 {
929 fillRect.setTop( from );
930 fillRect.setBottom( to );
931 }
932
933 return fillRect.normalized();
934}
935
944QRect QwtThermo::alarmRect( const QRect& fillRect ) const
945{
946 QRect alarmRect( 0, 0, -1, -1); // something invalid
947
948 if ( !m_data->alarmEnabled )
949 return alarmRect;
950
951 const bool inverted = ( upperBound() < lowerBound() );
952
953 bool increasing;
954 if ( m_data->originMode == OriginCustom )
955 {
956 increasing = m_data->value > m_data->origin;
957 }
958 else
959 {
960 increasing = m_data->originMode == OriginMinimum;
961 }
962
963 const QwtScaleMap map = scaleDraw()->scaleMap();
964 const int alarmPos = qRound( map.transform( m_data->alarmLevel ) );
965 const int valuePos = qRound( map.transform( m_data->value ) );
966
967 if ( m_data->orientation == Qt::Horizontal )
968 {
969 int v1, v2;
970 if ( inverted )
971 {
972 v1 = fillRect.left();
973
974 v2 = alarmPos - 1;
975 v2 = qMin( v2, increasing ? fillRect.right() : valuePos );
976 }
977 else
978 {
979 v1 = alarmPos + 1;
980 v1 = qMax( v1, increasing ? fillRect.left() : valuePos );
981
982 v2 = fillRect.right();
983
984 }
985 alarmRect.setRect( v1, fillRect.top(), v2 - v1 + 1, fillRect.height() );
986 }
987 else
988 {
989 int v1, v2;
990 if ( inverted )
991 {
992 v1 = alarmPos + 1;
993 v1 = qMax( v1, increasing ? fillRect.top() : valuePos );
994
995 v2 = fillRect.bottom();
996 }
997 else
998 {
999 v1 = fillRect.top();
1000
1001 v2 = alarmPos - 1;
1002 v2 = qMin( v2, increasing ? fillRect.bottom() : valuePos );
1003 }
1004 alarmRect.setRect( fillRect.left(), v1, fillRect.width(), v2 - v1 + 1 );
1005 }
1006
1007 return alarmRect;
1008}
1009
1010#include "moc_qwt_thermo.cpp"
const QwtScaleMap & scaleMap() const
virtual void draw(QPainter *, const QPalette &) const
Draw the scale.
An abstract base class for widgets having a scale.
const QwtScaleMap & scaleMap() const
double lowerBound() const
const QwtAbstractScaleDraw * abstractScaleDraw() const
const QwtScaleDiv & scaleDiv() const
void setAbstractScaleDraw(QwtAbstractScaleDraw *)
Set a scale draw.
double upperBound() const
QwtColorMap is used to map values into colors.
QColor color(const QwtInterval &, double value) const
A class representing an interval.
double minValue() const
QwtInterval normalized() const
Normalize the limits of the interval.
@ ExcludeMaximum
Max value is not included in the interval.
@ ExcludeMinimum
Min value is not included in the interval.
@ IncludeBorders
Min/Max values are inside the interval.
double maxValue() const
QFlags< BorderFlag > BorderFlags
Border flags.
A class representing a scale division.
QwtInterval interval() const
QList< double > ticks(int tickType) const
@ MinorTick
Minor ticks.
@ NTickTypes
Number of valid tick types.
A class for drawing scales.
void setLength(double length)
void move(double x, double y)
void getBorderDistHint(const QFont &, int &start, int &end) const
Determine the minimum border distance.
int minLength(const QFont &) const
void setAlignment(Alignment)
@ BottomScale
The scale is below.
@ TopScale
The scale is above.
@ RightScale
The scale is right.
@ LeftScale
The scale is left.
A scale map.
bool isInverting() const
double transform(double s) const
double invTransform(double p) const
The Thermometer Widget.
Definition qwt_thermo.h:47
void setOrigin(double)
Specifies the custom origin.
void setScalePosition(ScalePosition)
Change the position of the scale.
void setAlarmEnabled(bool)
Enable or disable the alarm threshold.
void setAlarmLevel(double)
virtual void resizeEvent(QResizeEvent *) override
QRect pipeRect() const
int spacing() const
QwtThermo(QWidget *parent=NULL)
virtual void setValue(double)
void setPipeWidth(int)
const QwtScaleDraw * scaleDraw() const
void setAlarmBrush(const QBrush &)
Specify the liquid brush above the alarm threshold.
@ NoScale
The slider has no scale.
Definition qwt_thermo.h:76
@ TrailingScale
The scale is left of a vertical or above of a horizontal slider.
Definition qwt_thermo.h:82
@ LeadingScale
The scale is right of a vertical or below of a horizontal slider.
Definition qwt_thermo.h:79
int pipeWidth() const
double origin() const
virtual void paintEvent(QPaintEvent *) override
void setSpacing(int)
Change the spacing between pipe and scale.
QBrush fillBrush() const
void setBorderWidth(int)
void setScaleDraw(QwtScaleDraw *)
Set a scale draw.
QwtColorMap * colorMap()
@ OriginMaximum
The origin is the maximum of the scale.
Definition qwt_thermo.h:97
@ OriginCustom
The origin is specified using the origin() property.
Definition qwt_thermo.h:100
@ OriginMinimum
The origin is the minimum of the scale.
Definition qwt_thermo.h:94
void setOriginMode(OriginMode)
Change how the origin is determined.
QwtInterval::BorderFlags rangeFlags() const
double value() const
Return the value.
QBrush alarmBrush() const
void setRangeFlags(QwtInterval::BorderFlags)
Exclude/Include min/max values.
virtual void changeEvent(QEvent *) override
QRect fillRect(const QRect &) const
Calculate the filled rectangle of the pipe.
virtual QSize minimumSizeHint() const override
virtual void drawLiquid(QPainter *, const QRect &) const
void setFillBrush(const QBrush &)
Change the brush of the liquid.
virtual ~QwtThermo()
Destructor.
void setColorMap(QwtColorMap *)
Assign a color map for the fill color.
int borderWidth() const
bool alarmEnabled() const
Qt::Orientation orientation() const
OriginMode originMode() const
void setOrientation(Qt::Orientation)
Set the orientation.
ScalePosition scalePosition() const
QRect alarmRect(const QRect &) const
Calculate the alarm rectangle of the pipe.
virtual void scaleChange() override
Notify a scale change.
double alarmLevel() const
virtual QSize sizeHint() const override