source: ntrip/branches/BNC_2.13.beta/qwt/qwt_dial_needle.cpp@ 10132

Last change on this file since 10132 was 9383, checked in by stoecker, 4 years ago

update to qwt verion 6.1.1 to fix build with newer Qt5

File size: 11.0 KB
Line 
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#include <qpainterpath.h>
17
18#if QT_VERSION < 0x040601
19#define qFastSin(x) qSin(x)
20#define qFastCos(x) qCos(x)
21#endif
22
23static void qwtDrawStyle1Needle( QPainter *painter,
24 const QPalette &palette, QPalette::ColorGroup colorGroup,
25 double length )
26{
27 const double r[] = { 0.4, 0.3, 1, 0.8, 1, 0.3, 0.4 };
28 const double a[] = { -45, -20, -15, 0, 15, 20, 45 };
29
30 QPainterPath path;
31 for ( int i = 0; i < 7; i++ )
32 {
33 const double angle = a[i] / 180.0 * M_PI;
34 const double radius = r[i] * length;
35
36 const double x = radius * qFastCos( angle );
37 const double y = radius * qFastSin( angle );
38
39 path.lineTo( x, -y );
40 }
41
42 painter->setPen( Qt::NoPen );
43 painter->setBrush( palette.brush( colorGroup, QPalette::Light ) );
44 painter->drawPath( path );
45}
46
47static void qwtDrawStyle2Needle( QPainter *painter,
48 const QPalette &palette, QPalette::ColorGroup colorGroup, double length )
49{
50 const double ratioX = 0.7;
51 const double ratioY = 0.3;
52
53 QPainterPath path1;
54 path1.lineTo( ratioX * length, 0.0 );
55 path1.lineTo( length, ratioY * length );
56
57 QPainterPath path2;
58 path2.lineTo( ratioX * length, 0.0 );
59 path2.lineTo( length, -ratioY * length );
60
61 painter->setPen( Qt::NoPen );
62
63 painter->setBrush( palette.brush( colorGroup, QPalette::Light ) );
64 painter->drawPath( path1 );
65
66 painter->setBrush( palette.brush( colorGroup, QPalette::Dark ) );
67 painter->drawPath( path2 );
68}
69
70static void qwtDrawShadedPointer( QPainter *painter,
71 const QColor &lightColor, const QColor &darkColor,
72 double length, double width )
73{
74 const double peak = qMax( length / 10.0, 5.0 );
75
76 const double knobWidth = width + 8;
77 QRectF knobRect( 0, 0, knobWidth, knobWidth );
78 knobRect.moveCenter( QPointF(0, 0) );
79
80 QPainterPath path1;
81 path1.lineTo( 0.0, 0.5 * width );
82 path1.lineTo( length - peak, 0.5 * width );
83 path1.lineTo( length, 0.0 );
84 path1.lineTo( 0.0, 0.0 );
85
86 QPainterPath arcPath1;
87 arcPath1.arcTo( knobRect, 0.0, -90.0 );
88
89 path1 = path1.united( arcPath1 );
90
91 QPainterPath path2;
92 path2.lineTo( 0.0, -0.5 * width );
93 path2.lineTo( length - peak, -0.5 * width );
94 path2.lineTo( length, 0.0 );
95 path2.lineTo( 0.0, 0.0 );
96
97 QPainterPath arcPath2;
98 arcPath2.arcTo( knobRect, 0.0, 90.0 );
99
100 path2 = path2.united( arcPath2 );
101
102 painter->setPen( Qt::NoPen );
103
104 painter->setBrush( lightColor );
105 painter->drawPath( path1 );
106
107 painter->setBrush( darkColor );
108 painter->drawPath( path2 );
109}
110
111static void qwtDrawArrowNeedle( QPainter *painter,
112 const QPalette &palette, QPalette::ColorGroup colorGroup,
113 double length, double width )
114{
115 if ( width <= 0 )
116 width = qMax( length * 0.06, 9.0 );
117
118 const double peak = qMax( 2.0, 0.4 * width );
119
120 QPainterPath path;
121 path.moveTo( 0.0, 0.5 * width );
122 path.lineTo( length - peak, 0.3 * width );
123 path.lineTo( length, 0.0 );
124 path.lineTo( length - peak, -0.3 * width );
125 path.lineTo( 0.0, -0.5 * width );
126
127 QRectF br = path.boundingRect();
128
129 QPalette pal( palette.color( QPalette::Mid ) );
130 QColor c1 = pal.color( QPalette::Light );
131 QColor c2 = pal.color( QPalette::Dark );
132
133 QLinearGradient gradient( br.topLeft(), br.bottomLeft() );
134 gradient.setColorAt( 0.0, c1 );
135 gradient.setColorAt( 0.5, c1 );
136 gradient.setColorAt( 0.5001, c2 );
137 gradient.setColorAt( 1.0, c2 );
138
139 QPen pen( gradient, 1 );
140 pen.setJoinStyle( Qt::MiterJoin );
141
142 painter->setPen( pen );
143 painter->setBrush( palette.brush( colorGroup, QPalette::Mid ) );
144
145 painter->drawPath( path );
146}
147
148static void qwtDrawTriangleNeedle( QPainter *painter,
149 const QPalette &palette, QPalette::ColorGroup colorGroup,
150 double length )
151{
152 const double width = qRound( length / 3.0 );
153
154 QPainterPath path[4];
155
156 path[0].lineTo( length, 0.0 );
157 path[0].lineTo( 0.0, width / 2 );
158
159 path[1].lineTo( length, 0.0 );
160 path[1].lineTo( 0.0, -width / 2 );
161
162 path[2].lineTo( -length, 0.0 );
163 path[2].lineTo( 0.0, width / 2 );
164
165 path[3].lineTo( -length, 0.0 );
166 path[3].lineTo( 0.0, -width / 2 );
167
168
169 const int colorOffset = 10;
170 const QColor darkColor = palette.color( colorGroup, QPalette::Dark );
171 const QColor lightColor = palette.color( colorGroup, QPalette::Light );
172
173 QColor color[4];
174 color[0] = darkColor.lighter( 100 + colorOffset );
175 color[1] = darkColor.darker( 100 + colorOffset );
176 color[2] = lightColor.lighter( 100 + colorOffset );
177 color[3] = lightColor.darker( 100 + colorOffset );
178
179 painter->setPen( Qt::NoPen );
180
181 for ( int i = 0; i < 4; i++ )
182 {
183 painter->setBrush( color[i] );
184 painter->drawPath( path[i] );
185 }
186}
187
188//! Constructor
189QwtDialNeedle::QwtDialNeedle():
190 d_palette( QApplication::palette() )
191{
192}
193
194//! Destructor
195QwtDialNeedle::~QwtDialNeedle()
196{
197}
198
199/*!
200 Sets the palette for the needle.
201
202 \param palette New Palette
203*/
204void QwtDialNeedle::setPalette( const QPalette &palette )
205{
206 d_palette = palette;
207}
208
209/*!
210 \return the palette of the needle.
211*/
212const QPalette &QwtDialNeedle::palette() const
213{
214 return d_palette;
215}
216
217/*!
218 Draw the needle
219
220 \param painter Painter
221 \param center Center of the dial, start position for the needle
222 \param length Length of the needle
223 \param direction Direction of the needle, in degrees counter clockwise
224 \param colorGroup Color group, used for painting
225*/
226void QwtDialNeedle::draw( QPainter *painter,
227 const QPointF &center, double length, double direction,
228 QPalette::ColorGroup colorGroup ) const
229{
230 painter->save();
231
232 painter->translate( center );
233 painter->rotate( -direction );
234
235 drawNeedle( painter, length, colorGroup );
236
237 painter->restore();
238}
239
240//! Draw the knob
241void QwtDialNeedle::drawKnob( QPainter *painter,
242 double width, const QBrush &brush, bool sunken ) const
243{
244 QPalette palette( brush.color() );
245
246 QColor c1 = palette.color( QPalette::Light );
247 QColor c2 = palette.color( QPalette::Dark );
248
249 if ( sunken )
250 qSwap( c1, c2 );
251
252 QRectF rect( 0.0, 0.0, width, width );
253 rect.moveCenter( painter->combinedTransform().map( QPointF() ) );
254
255 QLinearGradient gradient( rect.topLeft(), rect.bottomRight() );
256 gradient.setColorAt( 0.0, c1 );
257 gradient.setColorAt( 0.3, c1 );
258 gradient.setColorAt( 0.7, c2 );
259 gradient.setColorAt( 1.0, c2 );
260
261 painter->save();
262
263 painter->resetTransform();
264
265 painter->setPen( QPen( gradient, 1 ) );
266 painter->setBrush( brush );
267 painter->drawEllipse( rect );
268
269 painter->restore();
270}
271
272/*!
273 Constructor
274
275 \param style Style
276 \param hasKnob With/Without knob
277 \param mid Middle color
278 \param base Base color
279*/
280QwtDialSimpleNeedle::QwtDialSimpleNeedle( Style style, bool hasKnob,
281 const QColor &mid, const QColor &base ):
282 d_style( style ),
283 d_hasKnob( hasKnob ),
284 d_width( -1 )
285{
286 QPalette palette;
287 palette.setColor( QPalette::Mid, mid );
288 palette.setColor( QPalette::Base, base );
289
290 setPalette( palette );
291}
292
293/*!
294 Set the width of the needle
295 \param width Width
296 \sa width()
297*/
298void QwtDialSimpleNeedle::setWidth( double width )
299{
300 d_width = width;
301}
302
303/*!
304 \return the width of the needle
305 \sa setWidth()
306*/
307double QwtDialSimpleNeedle::width() const
308{
309 return d_width;
310}
311
312/*!
313 Draw the needle
314
315 \param painter Painter
316 \param length Length of the needle
317 \param colorGroup Color group, used for painting
318*/
319void QwtDialSimpleNeedle::drawNeedle( QPainter *painter,
320 double length, QPalette::ColorGroup colorGroup ) const
321{
322 double knobWidth = 0.0;
323 double width = d_width;
324
325 if ( d_style == Arrow )
326 {
327 if ( width <= 0.0 )
328 width = qMax(length * 0.06, 6.0);
329
330 qwtDrawArrowNeedle( painter,
331 palette(), colorGroup, length, width );
332
333 knobWidth = qMin( width * 2.0, 0.2 * length );
334 }
335 else
336 {
337 if ( width <= 0.0 )
338 width = 5.0;
339
340 QPen pen ( palette().brush( colorGroup, QPalette::Mid ), width );
341 pen.setCapStyle( Qt::FlatCap );
342
343 painter->setPen( pen );
344 painter->drawLine( QPointF( 0.0, 0.0 ), QPointF( length, 0.0 ) );
345
346 knobWidth = qMax( width * 3.0, 5.0 );
347 }
348
349 if ( d_hasKnob && knobWidth > 0.0 )
350 {
351 drawKnob( painter, knobWidth,
352 palette().brush( colorGroup, QPalette::Base ), false );
353 }
354}
355
356//! Constructor
357QwtCompassMagnetNeedle::QwtCompassMagnetNeedle( Style style,
358 const QColor &light, const QColor &dark ):
359 d_style( style )
360{
361 QPalette palette;
362 palette.setColor( QPalette::Light, light );
363 palette.setColor( QPalette::Dark, dark );
364 palette.setColor( QPalette::Base, Qt::gray );
365
366 setPalette( palette );
367}
368
369/*!
370 Draw the needle
371
372 \param painter Painter
373 \param length Length of the needle
374 \param colorGroup Color group, used for painting
375*/
376void QwtCompassMagnetNeedle::drawNeedle( QPainter *painter,
377 double length, QPalette::ColorGroup colorGroup ) const
378{
379 if ( d_style == ThinStyle )
380 {
381 const double width = qMax( length / 6.0, 3.0 );
382
383 const int colorOffset = 10;
384
385 const QColor light = palette().color( colorGroup, QPalette::Light );
386 const QColor dark = palette().color( colorGroup, QPalette::Dark );
387
388 qwtDrawShadedPointer( painter,
389 dark.lighter( 100 + colorOffset ),
390 dark.darker( 100 + colorOffset ),
391 length, width );
392
393 painter->rotate( 180.0 );
394
395 qwtDrawShadedPointer( painter,
396 light.lighter( 100 + colorOffset ),
397 light.darker( 100 + colorOffset ),
398 length, width );
399
400 const QBrush baseBrush = palette().brush( colorGroup, QPalette::Base );
401 drawKnob( painter, width, baseBrush, true );
402 }
403 else
404 {
405 qwtDrawTriangleNeedle( painter, palette(), colorGroup, length );
406 }
407}
408
409/*!
410 Constructor
411
412 \param style Arrow style
413 \param light Light color
414 \param dark Dark color
415*/
416QwtCompassWindArrow::QwtCompassWindArrow( Style style,
417 const QColor &light, const QColor &dark ):
418 d_style( style )
419{
420 QPalette palette;
421 palette.setColor( QPalette::Light, light );
422 palette.setColor( QPalette::Dark, dark );
423
424 setPalette( palette );
425}
426
427/*!
428 Draw the needle
429
430 \param painter Painter
431 \param length Length of the needle
432 \param colorGroup Color group, used for painting
433*/
434void QwtCompassWindArrow::drawNeedle( QPainter *painter,
435 double length, QPalette::ColorGroup colorGroup ) const
436{
437 if ( d_style == Style1 )
438 qwtDrawStyle1Needle( painter, palette(), colorGroup, length );
439 else
440 qwtDrawStyle2Needle( painter, palette(), colorGroup, length );
441}
Note: See TracBrowser for help on using the repository browser.