00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "qwt_dial_needle.h"
00011 #include "qwt_global.h"
00012 #include "qwt_math.h"
00013 #include "qwt_painter.h"
00014 #include <qapplication.h>
00015 #include <qpainter.h>
00016
00017 #if QT_VERSION < 0x040601
00018 #define qFastSin(x) qSin(x)
00019 #define qFastCos(x) qCos(x)
00020 #endif
00021
00022 static void qwtDrawStyle1Needle( QPainter *painter,
00023 const QPalette &palette, QPalette::ColorGroup colorGroup,
00024 double length )
00025 {
00026 const double r[] = { 0.4, 0.3, 1, 0.8, 1, 0.3, 0.4 };
00027 const double a[] = { -45, -20, -15, 0, 15, 20, 45 };
00028
00029 QPainterPath path;
00030 for ( int i = 0; i < 7; i++ )
00031 {
00032 const double angle = a[i] / 180.0 * M_PI;
00033 const double radius = r[i] * length;
00034
00035 const double x = radius * qFastCos( angle );
00036 const double y = radius * qFastSin( angle );
00037
00038 path.lineTo( x, -y );
00039 }
00040
00041 painter->setPen( Qt::NoPen );
00042 painter->setBrush( palette.brush( colorGroup, QPalette::Light ) );
00043 painter->drawPath( path );
00044 }
00045
00046 static void qwtDrawStyle2Needle( QPainter *painter,
00047 const QPalette &palette, QPalette::ColorGroup colorGroup, double length )
00048 {
00049 const double ratioX = 0.7;
00050 const double ratioY = 0.3;
00051
00052 QPainterPath path1;
00053 path1.lineTo( ratioX * length, 0.0 );
00054 path1.lineTo( length, ratioY * length );
00055
00056 QPainterPath path2;
00057 path2.lineTo( ratioX * length, 0.0 );
00058 path2.lineTo( length, -ratioY * length );
00059
00060 painter->setPen( Qt::NoPen );
00061
00062 painter->setBrush( palette.brush( colorGroup, QPalette::Light ) );
00063 painter->drawPath( path1 );
00064
00065 painter->setBrush( palette.brush( colorGroup, QPalette::Dark ) );
00066 painter->drawPath( path2 );
00067 }
00068
00069 static void qwtDrawShadedPointer( QPainter *painter,
00070 const QColor &lightColor, const QColor &darkColor,
00071 double length, double width )
00072 {
00073 const double peak = qMax( length / 10.0, 5.0 );
00074
00075 const double knobWidth = width + 8;
00076 QRectF knobRect( 0, 0, knobWidth, knobWidth );
00077 knobRect.moveCenter( QPointF(0, 0) );
00078
00079 QPainterPath path1;
00080 path1.lineTo( 0.0, 0.5 * width );
00081 path1.lineTo( length - peak, 0.5 * width );
00082 path1.lineTo( length, 0.0 );
00083 path1.lineTo( 0.0, 0.0 );
00084
00085 QPainterPath arcPath1;
00086 arcPath1.arcTo( knobRect, 0.0, -90.0 );
00087
00088 path1 = path1.united( arcPath1 );
00089
00090 QPainterPath path2;
00091 path2.lineTo( 0.0, -0.5 * width );
00092 path2.lineTo( length - peak, -0.5 * width );
00093 path2.lineTo( length, 0.0 );
00094 path2.lineTo( 0.0, 0.0 );
00095
00096 QPainterPath arcPath2;
00097 arcPath2.arcTo( knobRect, 0.0, 90.0 );
00098
00099 path2 = path2.united( arcPath2 );
00100
00101 painter->setPen( Qt::NoPen );
00102
00103 painter->setBrush( lightColor );
00104 painter->drawPath( path1 );
00105
00106 painter->setBrush( darkColor );
00107 painter->drawPath( path2 );
00108 }
00109
00110 static void qwtDrawArrowNeedle( QPainter *painter,
00111 const QPalette &palette, QPalette::ColorGroup colorGroup,
00112 double length, double width )
00113 {
00114 if ( width <= 0 )
00115 width = qMax( length * 0.06, 9.0 );
00116
00117 const double peak = qMax( 2.0, 0.4 * width );
00118
00119 QPainterPath path;
00120 path.moveTo( 0.0, 0.5 * width );
00121 path.lineTo( length - peak, 0.3 * width );
00122 path.lineTo( length, 0.0 );
00123 path.lineTo( length - peak, -0.3 * width );
00124 path.lineTo( 0.0, -0.5 * width );
00125
00126 QRectF br = path.boundingRect();
00127
00128 QPalette pal( palette.color( QPalette::Mid ) );
00129 QColor c1 = pal.color( QPalette::Light );
00130 QColor c2 = pal.color( QPalette::Dark );
00131
00132 QLinearGradient gradient( br.topLeft(), br.bottomLeft() );
00133 gradient.setColorAt( 0.0, c1 );
00134 gradient.setColorAt( 0.5, c1 );
00135 gradient.setColorAt( 0.5001, c2 );
00136 gradient.setColorAt( 1.0, c2 );
00137
00138 QPen pen( gradient, 1 );
00139 pen.setJoinStyle( Qt::MiterJoin );
00140
00141 painter->setPen( pen );
00142 painter->setBrush( palette.brush( colorGroup, QPalette::Mid ) );
00143
00144 painter->drawPath( path );
00145 }
00146
00147 static void qwtDrawTriangleNeedle( QPainter *painter,
00148 const QPalette &palette, QPalette::ColorGroup colorGroup,
00149 double length )
00150 {
00151 const double width = qRound( length / 3.0 );
00152
00153 QPainterPath path[4];
00154
00155 path[0].lineTo( length, 0.0 );
00156 path[0].lineTo( 0.0, width / 2 );
00157
00158 path[1].lineTo( length, 0.0 );
00159 path[1].lineTo( 0.0, -width / 2 );
00160
00161 path[2].lineTo( -length, 0.0 );
00162 path[2].lineTo( 0.0, width / 2 );
00163
00164 path[3].lineTo( -length, 0.0 );
00165 path[3].lineTo( 0.0, -width / 2 );
00166
00167
00168 const int colorOffset = 10;
00169 const QColor darkColor = palette.color( colorGroup, QPalette::Dark );
00170 const QColor lightColor = palette.color( colorGroup, QPalette::Light );
00171
00172 QColor color[4];
00173 color[0] = darkColor.light( 100 + colorOffset );
00174 color[1] = darkColor.dark( 100 + colorOffset );
00175 color[2] = lightColor.light( 100 + colorOffset );
00176 color[3] = lightColor.dark( 100 + colorOffset );
00177
00178 painter->setPen( Qt::NoPen );
00179
00180 for ( int i = 0; i < 4; i++ )
00181 {
00182 painter->setBrush( color[i] );
00183 painter->drawPath( path[i] );
00184 }
00185 }
00186
00188 QwtDialNeedle::QwtDialNeedle():
00189 d_palette( QApplication::palette() )
00190 {
00191 }
00192
00194 QwtDialNeedle::~QwtDialNeedle()
00195 {
00196 }
00197
00203 void QwtDialNeedle::setPalette( const QPalette &palette )
00204 {
00205 d_palette = palette;
00206 }
00207
00211 const QPalette &QwtDialNeedle::palette() const
00212 {
00213 return d_palette;
00214 }
00215
00225 void QwtDialNeedle::draw( QPainter *painter,
00226 const QPointF ¢er, double length, double direction,
00227 QPalette::ColorGroup colorGroup ) const
00228 {
00229 painter->save();
00230
00231 painter->translate( center );
00232 painter->rotate( -direction );
00233
00234 drawNeedle( painter, length, colorGroup );
00235
00236 painter->restore();
00237 }
00238
00240 void QwtDialNeedle::drawKnob( QPainter *painter,
00241 double width, const QBrush &brush, bool sunken ) const
00242 {
00243 QPalette palette( brush.color() );
00244
00245 QColor c1 = palette.color( QPalette::Light );
00246 QColor c2 = palette.color( QPalette::Dark );
00247
00248 if ( sunken )
00249 qSwap( c1, c2 );
00250
00251 QRectF rect( 0.0, 0.0, width, width );
00252 rect.moveCenter( painter->combinedTransform().map( QPointF() ) );
00253
00254 QLinearGradient gradient( rect.topLeft(), rect.bottomRight() );
00255 gradient.setColorAt( 0.0, c1 );
00256 gradient.setColorAt( 0.3, c1 );
00257 gradient.setColorAt( 0.7, c2 );
00258 gradient.setColorAt( 1.0, c2 );
00259
00260 painter->save();
00261
00262 painter->resetTransform();
00263
00264 painter->setPen( QPen( gradient, 1 ) );
00265 painter->setBrush( brush );
00266 painter->drawEllipse( rect );
00267
00268 painter->restore();
00269 }
00270
00279 QwtDialSimpleNeedle::QwtDialSimpleNeedle( Style style, bool hasKnob,
00280 const QColor &mid, const QColor &base ):
00281 d_style( style ),
00282 d_hasKnob( hasKnob ),
00283 d_width( -1 )
00284 {
00285 QPalette palette;
00286 palette.setColor( QPalette::Mid, mid );
00287 palette.setColor( QPalette::Base, base );
00288
00289 setPalette( palette );
00290 }
00291
00297 void QwtDialSimpleNeedle::setWidth( double width )
00298 {
00299 d_width = width;
00300 }
00301
00306 double QwtDialSimpleNeedle::width() const
00307 {
00308 return d_width;
00309 }
00310
00318 void QwtDialSimpleNeedle::drawNeedle( QPainter *painter,
00319 double length, QPalette::ColorGroup colorGroup ) const
00320 {
00321 double knobWidth = 0.0;
00322 double width = d_width;
00323
00324 if ( d_style == Arrow )
00325 {
00326 if ( width <= 0.0 )
00327 width = qMax(length * 0.06, 6.0);
00328
00329 qwtDrawArrowNeedle( painter,
00330 palette(), colorGroup, length, width );
00331
00332 knobWidth = qMin( width * 2.0, 0.2 * length );
00333 }
00334 else
00335 {
00336 if ( width <= 0.0 )
00337 width = 5.0;
00338
00339 QPen pen ( palette().brush( colorGroup, QPalette::Mid ), width );
00340 pen.setCapStyle( Qt::FlatCap );
00341
00342 painter->setPen( pen );
00343 painter->drawLine( QPointF( 0.0, 0.0 ), QPointF( length, 0.0 ) );
00344
00345 knobWidth = qMax( width * 3.0, 5.0 );
00346 }
00347
00348 if ( d_hasKnob && knobWidth > 0.0 )
00349 {
00350 drawKnob( painter, knobWidth,
00351 palette().brush( colorGroup, QPalette::Base ), false );
00352 }
00353 }
00354
00356 QwtCompassMagnetNeedle::QwtCompassMagnetNeedle( Style style,
00357 const QColor &light, const QColor &dark ):
00358 d_style( style )
00359 {
00360 QPalette palette;
00361 palette.setColor( QPalette::Light, light );
00362 palette.setColor( QPalette::Dark, dark );
00363 palette.setColor( QPalette::Base, Qt::gray );
00364
00365 setPalette( palette );
00366 }
00367
00375 void QwtCompassMagnetNeedle::drawNeedle( QPainter *painter,
00376 double length, QPalette::ColorGroup colorGroup ) const
00377 {
00378 if ( d_style == ThinStyle )
00379 {
00380 const double width = qMax( length / 6.0, 3.0 );
00381
00382 const int colorOffset = 10;
00383
00384 const QColor light = palette().color( colorGroup, QPalette::Light );
00385 const QColor dark = palette().color( colorGroup, QPalette::Dark );
00386
00387 qwtDrawShadedPointer( painter,
00388 dark.light( 100 + colorOffset ),
00389 dark.dark( 100 + colorOffset ),
00390 length, width );
00391
00392 painter->rotate( 180.0 );
00393
00394 qwtDrawShadedPointer( painter,
00395 light.light( 100 + colorOffset ),
00396 light.dark( 100 + colorOffset ),
00397 length, width );
00398
00399 const QBrush baseBrush = palette().brush( colorGroup, QPalette::Base );
00400 drawKnob( painter, width, baseBrush, true );
00401 }
00402 else
00403 {
00404 qwtDrawTriangleNeedle( painter, palette(), colorGroup, length );
00405 }
00406 }
00407
00415 QwtCompassWindArrow::QwtCompassWindArrow( Style style,
00416 const QColor &light, const QColor &dark ):
00417 d_style( style )
00418 {
00419 QPalette palette;
00420 palette.setColor( QPalette::Light, light );
00421 palette.setColor( QPalette::Dark, dark );
00422
00423 setPalette( palette );
00424 }
00425
00433 void QwtCompassWindArrow::drawNeedle( QPainter *painter,
00434 double length, QPalette::ColorGroup colorGroup ) const
00435 {
00436 if ( d_style == Style1 )
00437 qwtDrawStyle1Needle( painter, palette(), colorGroup, length );
00438 else
00439 qwtDrawStyle2Needle( painter, palette(), colorGroup, length );
00440 }