source: ntrip/trunk/BNC/qwt/qwt_dial_needle.cpp@ 8938

Last change on this file since 8938 was 8127, checked in by stoecker, 8 years ago

update qwt and qwtpolar, many QT5 fixes (unfinished)

File size: 11.0 KB
RevLine 
[8127]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
22static 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
46static 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
69static 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
110static 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
147static 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
187//! Constructor
188QwtDialNeedle::QwtDialNeedle():
189 d_palette( QApplication::palette() )
190{
191}
192
193//! Destructor
194QwtDialNeedle::~QwtDialNeedle()
195{
196}
197
198/*!
199 Sets the palette for the needle.
200
201 \param palette New Palette
202*/
203void QwtDialNeedle::setPalette( const QPalette &palette )
204{
205 d_palette = palette;
206}
207
208/*!
209 \return the palette of the needle.
210*/
211const QPalette &QwtDialNeedle::palette() const
212{
213 return d_palette;
214}
215
216/*!
217 Draw the needle
218
219 \param painter Painter
220 \param center Center of the dial, start position for the needle
221 \param length Length of the needle
222 \param direction Direction of the needle, in degrees counter clockwise
223 \param colorGroup Color group, used for painting
224*/
225void 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
239//! Draw the knob
240void 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
271/*!
272 Constructor
273
274 \param style Style
275 \param hasKnob With/Without knob
276 \param mid Middle color
277 \param base Base color
278*/
279QwtDialSimpleNeedle::QwtDialSimpleNeedle( Style style, bool hasKnob,
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
292/*!
293 Set the width of the needle
294 \param width Width
295 \sa width()
296*/
297void QwtDialSimpleNeedle::setWidth( double width )
298{
299 d_width = width;
300}
301
302/*!
303 \return the width of the needle
304 \sa setWidth()
305*/
306double QwtDialSimpleNeedle::width() const
307{
308 return d_width;
309}
310
311/*!
312 Draw the needle
313
314 \param painter Painter
315 \param length Length of the needle
316 \param colorGroup Color group, used for painting
317*/
318void 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
355//! Constructor
356QwtCompassMagnetNeedle::QwtCompassMagnetNeedle( Style style,
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
368/*!
369 Draw the needle
370
371 \param painter Painter
372 \param length Length of the needle
373 \param colorGroup Color group, used for painting
374*/
375void 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
408/*!
409 Constructor
410
411 \param style Arrow style
412 \param light Light color
413 \param dark Dark color
414*/
415QwtCompassWindArrow::QwtCompassWindArrow( Style style,
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
426/*!
427 Draw the needle
428
429 \param painter Painter
430 \param length Length of the needle
431 \param colorGroup Color group, used for painting
432*/
433void 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}
Note: See TracBrowser for help on using the repository browser.