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_global.h"
12 #include "qwt_math.h"
13 #include "qwt_painter.h"
14 #include <qapplication.h>
15 #include <qpainter.h>
16 
17 #if QT_VERSION < 0x040601
18 #define qFastSin(x) qSin(x)
19 #define qFastCos(x) qCos(x)
20 #endif
21 
22 static void qwtDrawStyle1Needle( QPainter *painter,
23  const QPalette &palette, QPalette::ColorGroup colorGroup,
24  double length )
25 {
26  const double r[] = { 0.4, 0.3, 1, 0.8, 1, 0.3, 0.4 };
27  const double a[] = { -45, -20, -15, 0, 15, 20, 45 };
28 
29  QPainterPath path;
30  for ( int i = 0; i < 7; i++ )
31  {
32  const double angle = a[i] / 180.0 * M_PI;
33  const double radius = r[i] * length;
34 
35  const double x = radius * qFastCos( angle );
36  const double y = radius * qFastSin( angle );
37 
38  path.lineTo( x, -y );
39  }
40 
41  painter->setPen( Qt::NoPen );
42  painter->setBrush( palette.brush( colorGroup, QPalette::Light ) );
43  painter->drawPath( path );
44 }
45 
46 static void qwtDrawStyle2Needle( QPainter *painter,
47  const QPalette &palette, QPalette::ColorGroup colorGroup, double length )
48 {
49  const double ratioX = 0.7;
50  const double ratioY = 0.3;
51 
52  QPainterPath path1;
53  path1.lineTo( ratioX * length, 0.0 );
54  path1.lineTo( length, ratioY * length );
55 
56  QPainterPath path2;
57  path2.lineTo( ratioX * length, 0.0 );
58  path2.lineTo( length, -ratioY * length );
59 
60  painter->setPen( Qt::NoPen );
61 
62  painter->setBrush( palette.brush( colorGroup, QPalette::Light ) );
63  painter->drawPath( path1 );
64 
65  painter->setBrush( palette.brush( colorGroup, QPalette::Dark ) );
66  painter->drawPath( path2 );
67 }
68 
69 static void qwtDrawShadedPointer( QPainter *painter,
70  const QColor &lightColor, const QColor &darkColor,
71  double length, double width )
72 {
73  const double peak = qMax( length / 10.0, 5.0 );
74 
75  const double knobWidth = width + 8;
76  QRectF knobRect( 0, 0, knobWidth, knobWidth );
77  knobRect.moveCenter( QPointF(0, 0) );
78 
79  QPainterPath path1;
80  path1.lineTo( 0.0, 0.5 * width );
81  path1.lineTo( length - peak, 0.5 * width );
82  path1.lineTo( length, 0.0 );
83  path1.lineTo( 0.0, 0.0 );
84 
85  QPainterPath arcPath1;
86  arcPath1.arcTo( knobRect, 0.0, -90.0 );
87 
88  path1 = path1.united( arcPath1 );
89 
90  QPainterPath path2;
91  path2.lineTo( 0.0, -0.5 * width );
92  path2.lineTo( length - peak, -0.5 * width );
93  path2.lineTo( length, 0.0 );
94  path2.lineTo( 0.0, 0.0 );
95 
96  QPainterPath arcPath2;
97  arcPath2.arcTo( knobRect, 0.0, 90.0 );
98 
99  path2 = path2.united( arcPath2 );
100 
101  painter->setPen( Qt::NoPen );
102 
103  painter->setBrush( lightColor );
104  painter->drawPath( path1 );
105 
106  painter->setBrush( darkColor );
107  painter->drawPath( path2 );
108 }
109 
110 static void qwtDrawArrowNeedle( QPainter *painter,
111  const QPalette &palette, QPalette::ColorGroup colorGroup,
112  double length, double width )
113 {
114  if ( width <= 0 )
115  width = qMax( length * 0.06, 9.0 );
116 
117  const double peak = qMax( 2.0, 0.4 * width );
118 
119  QPainterPath path;
120  path.moveTo( 0.0, 0.5 * width );
121  path.lineTo( length - peak, 0.3 * width );
122  path.lineTo( length, 0.0 );
123  path.lineTo( length - peak, -0.3 * width );
124  path.lineTo( 0.0, -0.5 * width );
125 
126  QRectF br = path.boundingRect();
127 
128  QPalette pal( palette.color( QPalette::Mid ) );
129  QColor c1 = pal.color( QPalette::Light );
130  QColor c2 = pal.color( QPalette::Dark );
131 
132  QLinearGradient gradient( br.topLeft(), br.bottomLeft() );
133  gradient.setColorAt( 0.0, c1 );
134  gradient.setColorAt( 0.5, c1 );
135  gradient.setColorAt( 0.5001, c2 );
136  gradient.setColorAt( 1.0, c2 );
137 
138  QPen pen( gradient, 1 );
139  pen.setJoinStyle( Qt::MiterJoin );
140 
141  painter->setPen( pen );
142  painter->setBrush( palette.brush( colorGroup, QPalette::Mid ) );
143 
144  painter->drawPath( path );
145 }
146 
147 static void qwtDrawTriangleNeedle( QPainter *painter,
148  const QPalette &palette, QPalette::ColorGroup colorGroup,
149  double length )
150 {
151  const double width = qRound( length / 3.0 );
152 
153  QPainterPath path[4];
154 
155  path[0].lineTo( length, 0.0 );
156  path[0].lineTo( 0.0, width / 2 );
157 
158  path[1].lineTo( length, 0.0 );
159  path[1].lineTo( 0.0, -width / 2 );
160 
161  path[2].lineTo( -length, 0.0 );
162  path[2].lineTo( 0.0, width / 2 );
163 
164  path[3].lineTo( -length, 0.0 );
165  path[3].lineTo( 0.0, -width / 2 );
166 
167 
168  const int colorOffset = 10;
169  const QColor darkColor = palette.color( colorGroup, QPalette::Dark );
170  const QColor lightColor = palette.color( colorGroup, QPalette::Light );
171 
172  QColor color[4];
173  color[0] = darkColor.light( 100 + colorOffset );
174  color[1] = darkColor.dark( 100 + colorOffset );
175  color[2] = lightColor.light( 100 + colorOffset );
176  color[3] = lightColor.dark( 100 + colorOffset );
177 
178  painter->setPen( Qt::NoPen );
179 
180  for ( int i = 0; i < 4; i++ )
181  {
182  painter->setBrush( color[i] );
183  painter->drawPath( path[i] );
184  }
185 }
186 
189  d_palette( QApplication::palette() )
190 {
191 }
192 
195 {
196 }
197 
203 void QwtDialNeedle::setPalette( const QPalette &palette )
204 {
205  d_palette = palette;
206 }
207 
211 const QPalette &QwtDialNeedle::palette() const
212 {
213  return d_palette;
214 }
215 
225 void QwtDialNeedle::draw( QPainter *painter,
226  const QPointF &center, double length, double direction,
227  QPalette::ColorGroup colorGroup ) const
228 {
229  painter->save();
230 
231  painter->translate( center );
232  painter->rotate( -direction );
233 
234  drawNeedle( painter, length, colorGroup );
235 
236  painter->restore();
237 }
238 
240 void QwtDialNeedle::drawKnob( QPainter *painter,
241  double width, const QBrush &brush, bool sunken ) const
242 {
243  QPalette palette( brush.color() );
244 
245  QColor c1 = palette.color( QPalette::Light );
246  QColor c2 = palette.color( QPalette::Dark );
247 
248  if ( sunken )
249  qSwap( c1, c2 );
250 
251  QRectF rect( 0.0, 0.0, width, width );
252  rect.moveCenter( painter->combinedTransform().map( QPointF() ) );
253 
254  QLinearGradient gradient( rect.topLeft(), rect.bottomRight() );
255  gradient.setColorAt( 0.0, c1 );
256  gradient.setColorAt( 0.3, c1 );
257  gradient.setColorAt( 0.7, c2 );
258  gradient.setColorAt( 1.0, c2 );
259 
260  painter->save();
261 
262  painter->resetTransform();
263 
264  painter->setPen( QPen( gradient, 1 ) );
265  painter->setBrush( brush );
266  painter->drawEllipse( rect );
267 
268  painter->restore();
269 }
270 
280  const QColor &mid, const QColor &base ):
281  d_style( style ),
282  d_hasKnob( hasKnob ),
283  d_width( -1 )
284 {
285  QPalette palette;
286  palette.setColor( QPalette::Mid, mid );
287  palette.setColor( QPalette::Base, base );
288 
289  setPalette( palette );
290 }
291 
298 {
299  d_width = width;
300 }
301 
307 {
308  return d_width;
309 }
310 
318 void QwtDialSimpleNeedle::drawNeedle( QPainter *painter,
319  double length, QPalette::ColorGroup colorGroup ) const
320 {
321  double knobWidth = 0.0;
322  double width = d_width;
323 
324  if ( d_style == Arrow )
325  {
326  if ( width <= 0.0 )
327  width = qMax(length * 0.06, 6.0);
328 
329  qwtDrawArrowNeedle( painter,
330  palette(), colorGroup, length, width );
331 
332  knobWidth = qMin( width * 2.0, 0.2 * length );
333  }
334  else
335  {
336  if ( width <= 0.0 )
337  width = 5.0;
338 
339  QPen pen ( palette().brush( colorGroup, QPalette::Mid ), width );
340  pen.setCapStyle( Qt::FlatCap );
341 
342  painter->setPen( pen );
343  painter->drawLine( QPointF( 0.0, 0.0 ), QPointF( length, 0.0 ) );
344 
345  knobWidth = qMax( width * 3.0, 5.0 );
346  }
347 
348  if ( d_hasKnob && knobWidth > 0.0 )
349  {
350  drawKnob( painter, knobWidth,
351  palette().brush( colorGroup, QPalette::Base ), false );
352  }
353 }
354 
357  const QColor &light, const QColor &dark ):
358  d_style( style )
359 {
360  QPalette palette;
361  palette.setColor( QPalette::Light, light );
362  palette.setColor( QPalette::Dark, dark );
363  palette.setColor( QPalette::Base, Qt::gray );
364 
365  setPalette( palette );
366 }
367 
375 void QwtCompassMagnetNeedle::drawNeedle( QPainter *painter,
376  double length, QPalette::ColorGroup colorGroup ) const
377 {
378  if ( d_style == ThinStyle )
379  {
380  const double width = qMax( length / 6.0, 3.0 );
381 
382  const int colorOffset = 10;
383 
384  const QColor light = palette().color( colorGroup, QPalette::Light );
385  const QColor dark = palette().color( colorGroup, QPalette::Dark );
386 
387  qwtDrawShadedPointer( painter,
388  dark.light( 100 + colorOffset ),
389  dark.dark( 100 + colorOffset ),
390  length, width );
391 
392  painter->rotate( 180.0 );
393 
394  qwtDrawShadedPointer( painter,
395  light.light( 100 + colorOffset ),
396  light.dark( 100 + colorOffset ),
397  length, width );
398 
399  const QBrush baseBrush = palette().brush( colorGroup, QPalette::Base );
400  drawKnob( painter, width, baseBrush, true );
401  }
402  else
403  {
404  qwtDrawTriangleNeedle( painter, palette(), colorGroup, length );
405  }
406 }
407 
416  const QColor &light, const QColor &dark ):
417  d_style( style )
418 {
419  QPalette palette;
420  palette.setColor( QPalette::Light, light );
421  palette.setColor( QPalette::Dark, dark );
422 
423  setPalette( palette );
424 }
425 
433 void QwtCompassWindArrow::drawNeedle( QPainter *painter,
434  double length, QPalette::ColorGroup colorGroup ) const
435 {
436  if ( d_style == Style1 )
437  qwtDrawStyle1Needle( painter, palette(), colorGroup, length );
438  else
439  qwtDrawStyle2Needle( painter, palette(), colorGroup, length );
440 }
static void qwtDrawArrowNeedle(QPainter *painter, const QPalette &palette, QPalette::ColorGroup colorGroup, double length, double width)
static void qwtDrawShadedPointer(QPainter *painter, const QColor &lightColor, const QColor &darkColor, double length, double width)
virtual void setPalette(const QPalette &)
double width() const
virtual ~QwtDialNeedle()
Destructor.
virtual void drawNeedle(QPainter *, double length, QPalette::ColorGroup) const
TFSIMD_FORCE_INLINE const tfScalar & y() const
QPalette d_palette
QwtDialSimpleNeedle(Style, bool hasKnob=true, const QColor &mid=Qt::gray, const QColor &base=Qt::darkGray)
Style
Style of the needle.
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
static void qwtDrawStyle1Needle(QPainter *painter, const QPalette &palette, QPalette::ColorGroup colorGroup, double length)
Style
Style of the needle.
#define qFastSin(x)
virtual void drawKnob(QPainter *, double width, const QBrush &, bool sunken) const
Draw the knob.
#define qFastCos(x)
virtual void drawNeedle(QPainter *, double length, QPalette::ColorGroup) const
virtual void drawNeedle(QPainter *painter, double length, QPalette::ColorGroup colorGroup) const =0
Draw the needle.
GraphId path[kMaxDeadlockPathLen]
virtual void drawNeedle(QPainter *, double length, QPalette::ColorGroup) const
Style
Style of the arrow.
const QPalette & palette() const
A needle pointing to the center.
TFSIMD_FORCE_INLINE const tfScalar & x() const
static void qwtDrawTriangleNeedle(QPainter *painter, const QPalette &palette, QPalette::ColorGroup colorGroup, double length)
void setWidth(double width)
QwtDialNeedle()
Constructor.
virtual void draw(QPainter *painter, const QPointF &center, double length, double direction, QPalette::ColorGroup=QPalette::Active) const
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.
int i
int a
static void qwtDrawStyle2Needle(QPainter *painter, const QPalette &palette, QPalette::ColorGroup colorGroup, double length)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17