qwt_dial_needle.cpp
Go to the documentation of this file.
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
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  d_palette( QApplication::palette() )
184 {
185 }
186 
189 {
190 }
191 
197 void QwtDialNeedle::setPalette( const QPalette &palette )
198 {
199  d_palette = palette;
200 }
201 
205 const QPalette &QwtDialNeedle::palette() const
206 {
207  return d_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->combinedTransform().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  d_style( style ),
276  d_hasKnob( hasKnob ),
277  d_width( -1 )
278 {
279  QPalette palette;
280  palette.setColor( QPalette::Mid, mid );
281  palette.setColor( QPalette::Base, base );
282 
283  setPalette( palette );
284 }
285 
292 {
293  d_width = width;
294 }
295 
301 {
302  return d_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 = d_width;
317 
318  if ( d_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 ( d_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  d_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 ( d_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  d_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 ( d_style == Style1 )
431  qwtDrawStyle1Needle( painter, palette(), colorGroup, length );
432  else
433  qwtDrawStyle2Needle( painter, palette(), colorGroup, length );
434 }
virtual void draw(QPainter *, const QPointF &center, double length, double direction, QPalette::ColorGroup=QPalette::Active) const
QWT_CONSTEXPR float qwtMaxF(float a, float b)
Definition: qwt_math.h:123
static void qwtDrawTriangleNeedle(QPainter *painter, const QPalette &palette, QPalette::ColorGroup colorGroup, qreal length)
virtual void drawNeedle(QPainter *, double length, QPalette::ColorGroup) const QWT_OVERRIDE
virtual void setPalette(const QPalette &)
double width() const
virtual void drawNeedle(QPainter *, double length, QPalette::ColorGroup) const QWT_OVERRIDE
virtual ~QwtDialNeedle()
Destructor.
QPalette d_palette
QWT_CONSTEXPR float qwtMinF(float a, float b)
Definition: qwt_math.h:99
QwtDialSimpleNeedle(Style, bool hasKnob=true, const QColor &mid=Qt::gray, const QColor &base=Qt::darkGray)
Style
Style of the needle.
Style
Style of the needle.
virtual void drawKnob(QPainter *, double width, const QBrush &, bool sunken) const
Draw the knob.
virtual void drawNeedle(QPainter *painter, double length, QPalette::ColorGroup colorGroup) const =0
Draw the needle.
static void qwtDrawStyle1Needle(QPainter *painter, const QPalette &palette, QPalette::ColorGroup colorGroup, qreal length)
Style
Style of the arrow.
const QPalette & palette() const
A needle pointing to the center.
static void qwtDrawStyle2Needle(QPainter *painter, const QPalette &palette, QPalette::ColorGroup colorGroup, qreal length)
static void qwtDrawArrowNeedle(QPainter *painter, const QPalette &palette, QPalette::ColorGroup colorGroup, qreal length, qreal width)
void setWidth(double width)
#define M_PI
Definition: qwt_math.h:56
QwtDialNeedle()
Constructor.
virtual void drawNeedle(QPainter *, double length, QPalette::ColorGroup) const QWT_OVERRIDE
QwtCompassWindArrow(Style, const QColor &light=Qt::white, const QColor &dark=Qt::gray)
QwtCompassMagnetNeedle(Style=TriangleStyle, const QColor &light=Qt::white, const QColor &dark=Qt::red)
Constructor.
static void qwtDrawShadedPointer(QPainter *painter, const QColor &lightColor, const QColor &darkColor, qreal length, qreal width)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10