qwt_dial_needle.cpp
Go to the documentation of this file.
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_dial_needle.h"
11 #include "qwt_math.h"
12 
13 #include <qapplication.h>
14 #include <qpainter.h>
15 #include <qpainterpath.h>
16 #include <qmath.h>
17 
18 static void qwtDrawStyle1Needle( QPainter* painter,
19  const QPalette& palette, QPalette::ColorGroup colorGroup, qreal length )
20 {
21  const qreal r[] = { 0.4, 0.3, 1, 0.8, 1, 0.3, 0.4 };
22  const qreal a[] = { -45, -20, -15, 0, 15, 20, 45 };
23 
24  QPainterPath path;
25  for ( int i = 0; i < 7; i++ )
26  {
27  const qreal angle = a[i] / 180.0 * M_PI;
28  const qreal radius = r[i] * length;
29 
30  const qreal x = radius * qFastCos( angle );
31  const qreal y = radius * qFastSin( angle );
32 
33  path.lineTo( x, -y );
34  }
35 
36  painter->setPen( Qt::NoPen );
37  painter->setBrush( palette.brush( colorGroup, QPalette::Light ) );
38  painter->drawPath( path );
39 }
40 
41 static void qwtDrawStyle2Needle( QPainter* painter,
42  const QPalette& palette, QPalette::ColorGroup colorGroup, qreal length )
43 {
44  const qreal ratioX = 0.7;
45  const qreal ratioY = 0.3;
46 
47  QPainterPath path1;
48  path1.lineTo( ratioX * length, 0.0 );
49  path1.lineTo( length, ratioY * length );
50 
51  QPainterPath path2;
52  path2.lineTo( ratioX * length, 0.0 );
53  path2.lineTo( length, -ratioY * length );
54 
55  painter->setPen( Qt::NoPen );
56 
57  painter->setBrush( palette.brush( colorGroup, QPalette::Light ) );
58  painter->drawPath( path1 );
59 
60  painter->setBrush( palette.brush( colorGroup, QPalette::Dark ) );
61  painter->drawPath( path2 );
62 }
63 
64 static void qwtDrawShadedPointer( QPainter* painter,
65  const QColor& lightColor, const QColor& darkColor,
66  qreal length, qreal width )
67 {
68  const qreal peak = qwtMaxF( length / 10.0, 5.0 );
69 
70  const qreal knobWidth = width + 8;
71  QRectF knobRect( 0, 0, knobWidth, knobWidth );
72  knobRect.moveCenter( QPointF(0, 0) );
73 
74  QPainterPath path1;
75  path1.lineTo( 0.0, 0.5 * width );
76  path1.lineTo( length - peak, 0.5 * width );
77  path1.lineTo( length, 0.0 );
78  path1.lineTo( 0.0, 0.0 );
79 
80  QPainterPath arcPath1;
81  arcPath1.arcTo( knobRect, 0.0, -90.0 );
82 
83  path1 = path1.united( arcPath1 );
84 
85  QPainterPath path2;
86  path2.lineTo( 0.0, -0.5 * width );
87  path2.lineTo( length - peak, -0.5 * width );
88  path2.lineTo( length, 0.0 );
89  path2.lineTo( 0.0, 0.0 );
90 
91  QPainterPath arcPath2;
92  arcPath2.arcTo( knobRect, 0.0, 90.0 );
93 
94  path2 = path2.united( arcPath2 );
95 
96  painter->setPen( Qt::NoPen );
97 
98  painter->setBrush( lightColor );
99  painter->drawPath( path1 );
100 
101  painter->setBrush( darkColor );
102  painter->drawPath( path2 );
103 }
104 
105 static void qwtDrawArrowNeedle( QPainter* painter,
106  const QPalette& palette, QPalette::ColorGroup colorGroup,
107  qreal length, qreal width )
108 {
109  if ( width <= 0 )
110  width = qwtMaxF( length * 0.06, 9.0 );
111 
112  const qreal peak = qwtMaxF( 2.0, 0.4 * width );
113 
114  QPainterPath path;
115  path.moveTo( 0.0, 0.5 * width );
116  path.lineTo( length - peak, 0.3 * width );
117  path.lineTo( length, 0.0 );
118  path.lineTo( length - peak, -0.3 * width );
119  path.lineTo( 0.0, -0.5 * width );
120 
121  QRectF br = path.boundingRect();
122 
123  QPalette pal( palette.color( QPalette::Mid ) );
124  QColor c1 = pal.color( QPalette::Light );
125  QColor c2 = pal.color( QPalette::Dark );
126 
127  QLinearGradient gradient( br.topLeft(), br.bottomLeft() );
128  gradient.setColorAt( 0.0, c1 );
129  gradient.setColorAt( 0.5, c1 );
130  gradient.setColorAt( 0.5001, c2 );
131  gradient.setColorAt( 1.0, c2 );
132 
133  QPen pen( gradient, 1 );
134  pen.setJoinStyle( Qt::MiterJoin );
135 
136  painter->setPen( pen );
137  painter->setBrush( palette.brush( colorGroup, QPalette::Mid ) );
138 
139  painter->drawPath( path );
140 }
141 
142 static void qwtDrawTriangleNeedle( QPainter* painter,
143  const QPalette& palette, QPalette::ColorGroup colorGroup, qreal length )
144 {
145  const qreal width = qRound( length / 3.0 );
146 
147  QPainterPath path[4];
148 
149  path[0].lineTo( length, 0.0 );
150  path[0].lineTo( 0.0, width / 2 );
151 
152  path[1].lineTo( length, 0.0 );
153  path[1].lineTo( 0.0, -width / 2 );
154 
155  path[2].lineTo( -length, 0.0 );
156  path[2].lineTo( 0.0, width / 2 );
157 
158  path[3].lineTo( -length, 0.0 );
159  path[3].lineTo( 0.0, -width / 2 );
160 
161 
162  const int colorOffset = 10;
163  const QColor darkColor = palette.color( colorGroup, QPalette::Dark );
164  const QColor lightColor = palette.color( colorGroup, QPalette::Light );
165 
166  QColor color[4];
167  color[0] = darkColor.lighter( 100 + colorOffset );
168  color[1] = darkColor.darker( 100 + colorOffset );
169  color[2] = lightColor.lighter( 100 + colorOffset );
170  color[3] = lightColor.darker( 100 + colorOffset );
171 
172  painter->setPen( Qt::NoPen );
173 
174  for ( int i = 0; i < 4; i++ )
175  {
176  painter->setBrush( color[i] );
177  painter->drawPath( path[i] );
178  }
179 }
180 
183  : m_palette( QApplication::palette() )
184 {
185 }
186 
189 {
190 }
191 
197 void QwtDialNeedle::setPalette( const QPalette& palette )
198 {
199  m_palette = palette;
200 }
201 
205 const QPalette& QwtDialNeedle::palette() const
206 {
207  return m_palette;
208 }
209 
219 void QwtDialNeedle::draw( QPainter* painter,
220  const QPointF& center, double length, double direction,
221  QPalette::ColorGroup colorGroup ) const
222 {
223  painter->save();
224 
225  painter->translate( center );
226  painter->rotate( -direction );
227 
228  drawNeedle( painter, length, colorGroup );
229 
230  painter->restore();
231 }
232 
234 void QwtDialNeedle::drawKnob( QPainter* painter,
235  double width, const QBrush& brush, bool sunken ) const
236 {
237  QPalette palette( brush.color() );
238 
239  QColor c1 = palette.color( QPalette::Light );
240  QColor c2 = palette.color( QPalette::Dark );
241 
242  if ( sunken )
243  qSwap( c1, c2 );
244 
245  QRectF rect( 0.0, 0.0, width, width );
246  rect.moveCenter( painter->transform().map( QPointF() ) );
247 
248  QLinearGradient gradient( rect.topLeft(), rect.bottomRight() );
249  gradient.setColorAt( 0.0, c1 );
250  gradient.setColorAt( 0.3, c1 );
251  gradient.setColorAt( 0.7, c2 );
252  gradient.setColorAt( 1.0, c2 );
253 
254  painter->save();
255 
256  painter->resetTransform();
257 
258  painter->setPen( QPen( gradient, 1 ) );
259  painter->setBrush( brush );
260  painter->drawEllipse( rect );
261 
262  painter->restore();
263 }
264 
274  const QColor& mid, const QColor& base ):
275  m_style( style ),
276  m_hasKnob( hasKnob ),
277  m_width( -1 )
278 {
279  QPalette palette;
280  palette.setColor( QPalette::Mid, mid );
281  palette.setColor( QPalette::Base, base );
282 
283  setPalette( palette );
284 }
285 
291 void QwtDialSimpleNeedle::setWidth( double width )
292 {
293  m_width = width;
294 }
295 
301 {
302  return m_width;
303 }
304 
312 void QwtDialSimpleNeedle::drawNeedle( QPainter* painter,
313  double length, QPalette::ColorGroup colorGroup ) const
314 {
315  qreal knobWidth = 0.0;
316  qreal width = m_width;
317 
318  if ( m_style == Arrow )
319  {
320  if ( width <= 0.0 )
321  width = qwtMaxF( length * 0.06, 6.0 );
322 
323  qwtDrawArrowNeedle( painter,
324  palette(), colorGroup, length, width );
325 
326  knobWidth = qwtMinF( width * 2.0, 0.2 * length );
327  }
328  else
329  {
330  if ( width <= 0.0 )
331  width = 5.0;
332 
333  QPen pen ( palette().brush( colorGroup, QPalette::Mid ), width );
334  pen.setCapStyle( Qt::FlatCap );
335 
336  painter->setPen( pen );
337  painter->drawLine( QPointF( 0.0, 0.0 ), QPointF( length, 0.0 ) );
338 
339  knobWidth = qwtMaxF( width * 3.0, 5.0 );
340  }
341 
342  if ( m_hasKnob && knobWidth > 0.0 )
343  {
344  drawKnob( painter, knobWidth,
345  palette().brush( colorGroup, QPalette::Base ), false );
346  }
347 }
348 
351  const QColor& light, const QColor& dark ):
352  m_style( style )
353 {
354  QPalette palette;
355  palette.setColor( QPalette::Light, light );
356  palette.setColor( QPalette::Dark, dark );
357  palette.setColor( QPalette::Base, Qt::gray );
358 
359  setPalette( palette );
360 }
361 
369 void QwtCompassMagnetNeedle::drawNeedle( QPainter* painter,
370  double length, QPalette::ColorGroup colorGroup ) const
371 {
372  if ( m_style == ThinStyle )
373  {
374  const qreal width = qwtMaxF( length / 6.0, 3.0 );
375 
376  const int colorOffset = 10;
377 
378  const QColor light = palette().color( colorGroup, QPalette::Light );
379  const QColor dark = palette().color( colorGroup, QPalette::Dark );
380 
381  qwtDrawShadedPointer( painter,
382  dark.lighter( 100 + colorOffset ),
383  dark.darker( 100 + colorOffset ),
384  length, width );
385 
386  painter->rotate( 180.0 );
387 
388  qwtDrawShadedPointer( painter,
389  light.lighter( 100 + colorOffset ),
390  light.darker( 100 + colorOffset ),
391  length, width );
392 
393  const QBrush baseBrush = palette().brush( colorGroup, QPalette::Base );
394  drawKnob( painter, width, baseBrush, true );
395  }
396  else
397  {
398  qwtDrawTriangleNeedle( painter, palette(), colorGroup, length );
399  }
400 }
401 
410  const QColor& light, const QColor& dark ):
411  m_style( style )
412 {
413  QPalette palette;
414  palette.setColor( QPalette::Light, light );
415  palette.setColor( QPalette::Dark, dark );
416 
417  setPalette( palette );
418 }
419 
427 void QwtCompassWindArrow::drawNeedle( QPainter* painter,
428  double length, QPalette::ColorGroup colorGroup ) const
429 {
430  if ( m_style == Style1 )
431  qwtDrawStyle1Needle( painter, palette(), colorGroup, length );
432  else
433  qwtDrawStyle2Needle( painter, palette(), colorGroup, length );
434 }
color
color
Definition: color.h:16
QwtCompassWindArrow::drawNeedle
virtual void drawNeedle(QPainter *, double length, QPalette::ColorGroup) const QWT_OVERRIDE
Definition: qwt_dial_needle.cpp:427
QwtDialNeedle::~QwtDialNeedle
virtual ~QwtDialNeedle()
Destructor.
Definition: qwt_dial_needle.cpp:188
QwtDialSimpleNeedle::drawNeedle
virtual void drawNeedle(QPainter *, double length, QPalette::ColorGroup) const QWT_OVERRIDE
Definition: qwt_dial_needle.cpp:312
QwtDialNeedle::drawNeedle
virtual void drawNeedle(QPainter *painter, double length, QPalette::ColorGroup colorGroup) const =0
Draw the needle.
qwtDrawTriangleNeedle
static void qwtDrawTriangleNeedle(QPainter *painter, const QPalette &palette, QPalette::ColorGroup colorGroup, qreal length)
Definition: qwt_dial_needle.cpp:142
QwtCompassMagnetNeedle::drawNeedle
virtual void drawNeedle(QPainter *, double length, QPalette::ColorGroup) const QWT_OVERRIDE
Definition: qwt_dial_needle.cpp:369
QwtDialSimpleNeedle::Arrow
@ Arrow
Arrow.
Definition: qwt_dial_needle.h:88
QwtCompassMagnetNeedle::QwtCompassMagnetNeedle
QwtCompassMagnetNeedle(Style=TriangleStyle, const QColor &light=Qt::white, const QColor &dark=Qt::red)
Constructor.
Definition: qwt_dial_needle.cpp:350
QwtDialSimpleNeedle::width
double width() const
Definition: qwt_dial_needle.cpp:300
mqtt_test_proto.x
x
Definition: mqtt_test_proto.py:34
qwt_math.h
QwtDialSimpleNeedle::setWidth
void setWidth(double width)
Definition: qwt_dial_needle.cpp:291
QwtDialNeedle::palette
const QPalette & palette() const
Definition: qwt_dial_needle.cpp:205
QwtDialSimpleNeedle::Style
Style
Style of the needle.
Definition: qwt_dial_needle.h:85
mqtt_test_proto.y
y
Definition: mqtt_test_proto.py:35
QwtCompassMagnetNeedle::Style
Style
Style of the needle.
Definition: qwt_dial_needle.h:131
M_PI
#define M_PI
Definition: qwt_math.h:56
qwtMinF
QWT_CONSTEXPR float qwtMinF(float a, float b)
Definition: qwt_math.h:103
QwtCompassMagnetNeedle::ThinStyle
@ ThinStyle
A thin needle.
Definition: qwt_dial_needle.h:137
QwtDialNeedle::draw
virtual void draw(QPainter *, const QPointF &center, double length, double direction, QPalette::ColorGroup=QPalette::Active) const
Definition: qwt_dial_needle.cpp:219
qwtDrawStyle1Needle
static void qwtDrawStyle1Needle(QPainter *painter, const QPalette &palette, QPalette::ColorGroup colorGroup, qreal length)
Definition: qwt_dial_needle.cpp:18
qwtMaxF
QWT_CONSTEXPR float qwtMaxF(float a, float b)
Definition: qwt_math.h:127
QwtDialSimpleNeedle::m_hasKnob
bool m_hasKnob
Definition: qwt_dial_needle.h:106
QwtCompassMagnetNeedle::m_style
Style m_style
Definition: qwt_dial_needle.h:148
QwtCompassWindArrow::m_style
Style m_style
Definition: qwt_dial_needle.h:185
qwtDrawStyle2Needle
static void qwtDrawStyle2Needle(QPainter *painter, const QPalette &palette, QPalette::ColorGroup colorGroup, qreal length)
Definition: qwt_dial_needle.cpp:41
QwtDialNeedle::m_palette
QPalette m_palette
Definition: qwt_dial_needle.h:65
QwtDialSimpleNeedle::QwtDialSimpleNeedle
QwtDialSimpleNeedle(Style, bool hasKnob=true, const QColor &mid=Qt::gray, const QColor &base=Qt::darkGray)
Definition: qwt_dial_needle.cpp:273
QwtDialNeedle::setPalette
virtual void setPalette(const QPalette &)
Definition: qwt_dial_needle.cpp:197
qwtDrawArrowNeedle
static void qwtDrawArrowNeedle(QPainter *painter, const QPalette &palette, QPalette::ColorGroup colorGroup, qreal length, qreal width)
Definition: qwt_dial_needle.cpp:105
qwt_dial_needle.h
QwtCompassWindArrow::QwtCompassWindArrow
QwtCompassWindArrow(Style, const QColor &light=Qt::white, const QColor &dark=Qt::gray)
Definition: qwt_dial_needle.cpp:409
QwtDialNeedle::drawKnob
virtual void drawKnob(QPainter *, double width, const QBrush &, bool sunken) const
Draw the knob.
Definition: qwt_dial_needle.cpp:234
QwtCompassWindArrow::Style
Style
Style of the arrow.
Definition: qwt_dial_needle.h:168
QwtDialNeedle::QwtDialNeedle
QwtDialNeedle()
Constructor.
Definition: qwt_dial_needle.cpp:182
qwtDrawShadedPointer
static void qwtDrawShadedPointer(QPainter *painter, const QColor &lightColor, const QColor &darkColor, qreal length, qreal width)
Definition: qwt_dial_needle.cpp:64
QwtCompassWindArrow::Style1
@ Style1
A needle pointing to the center.
Definition: qwt_dial_needle.h:171
QwtDialSimpleNeedle::m_width
double m_width
Definition: qwt_dial_needle.h:107
QwtDialSimpleNeedle::m_style
Style m_style
Definition: qwt_dial_needle.h:105


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:24