123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606 |
- /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
- * Qwt Widget Library
- * Copyright (C) 1997 Josef Wilgen
- * Copyright (C) 2002 Uwe Rathmann
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the Qwt License, Version 1.0
- *****************************************************************************/
- #include "qwt_dial_needle.h"
- #include "qwt_global.h"
- #include "qwt_math.h"
- #include "qwt_painter.h"
- #include <qapplication.h>
- #include <qpainter.h>
- //! Constructor
- QwtDialNeedle::QwtDialNeedle():
- d_palette( QApplication::palette() )
- {
- }
- //! Destructor
- QwtDialNeedle::~QwtDialNeedle()
- {
- }
- /*!
- Sets the palette for the needle.
- \param palette New Palette
- */
- void QwtDialNeedle::setPalette( const QPalette &palette )
- {
- d_palette = palette;
- }
- /*!
- \return the palette of the needle.
- */
- const QPalette &QwtDialNeedle::palette() const
- {
- return d_palette;
- }
- //! Draw the knob
- void QwtDialNeedle::drawKnob( QPainter *painter,
- const QPoint &pos, int width, const QBrush &brush, bool sunken )
- {
- painter->save();
- QRect rect( 0, 0, width, width );
- rect.moveCenter( pos );
- painter->setPen( Qt::NoPen );
- painter->setBrush( brush );
- painter->drawEllipse( rect );
- painter->setBrush( Qt::NoBrush );
- const int colorOffset = 20;
- int startAngle = 45;
- if ( sunken )
- startAngle += 180;
- QPen pen;
- pen.setWidth( 1 );
- pen.setColor( brush.color().dark( 100 - colorOffset ) );
- painter->setPen( pen );
- painter->drawArc( rect, startAngle * 16, 180 * 16 );
- pen.setColor( brush.color().dark( 100 + colorOffset ) );
- painter->setPen( pen );
- painter->drawArc( rect, ( startAngle + 180 ) * 16, 180 * 16 );
- painter->restore();
- }
- /*!
- Constructor
- \param style Style
- \param hasKnob With/Without knob
- \param mid Middle color
- \param base Base color
- */
- QwtDialSimpleNeedle::QwtDialSimpleNeedle( Style style, bool hasKnob,
- const QColor &mid, const QColor &base ):
- d_style( style ),
- d_hasKnob( hasKnob ),
- d_width( -1 )
- {
- QPalette palette;
- for ( int i = 0; i < QPalette::NColorGroups; i++ )
- {
- palette.setColor( ( QPalette::ColorGroup )i,
- QPalette::Mid, mid );
- palette.setColor( ( QPalette::ColorGroup )i,
- QPalette::Base, base );
- }
- setPalette( palette );
- }
- /*!
- Set the width of the needle
- \param width Width
- \sa width()
- */
- void QwtDialSimpleNeedle::setWidth( int width )
- {
- d_width = width;
- }
- /*!
- \return the width of the needle
- \sa setWidth()
- */
- int QwtDialSimpleNeedle::width() const
- {
- return d_width;
- }
- /*!
- Draw the needle
- \param painter Painter
- \param center Center of the dial, start position for the needle
- \param length Length of the needle
- \param direction Direction of the needle, in degrees counter clockwise
- \param colorGroup Color group, used for painting
- */
- void QwtDialSimpleNeedle::draw( QPainter *painter, const QPoint ¢er,
- int length, double direction, QPalette::ColorGroup colorGroup ) const
- {
- if ( d_style == Arrow )
- {
- drawArrowNeedle( painter, palette(), colorGroup,
- center, length, d_width, direction, d_hasKnob );
- }
- else
- {
- drawRayNeedle( painter, palette(), colorGroup,
- center, length, d_width, direction, d_hasKnob );
- }
- }
- /*!
- Draw a needle looking like a ray
- \param painter Painter
- \param palette Palette
- \param colorGroup Color group
- \param center center of the needle
- \param length Length of the needle
- \param width Width of the needle
- \param direction Current Direction
- \param hasKnob With/Without knob
- */
- void QwtDialSimpleNeedle::drawRayNeedle( QPainter *painter,
- const QPalette &palette, QPalette::ColorGroup colorGroup,
- const QPoint ¢er, int length, int width, double direction,
- bool hasKnob )
- {
- if ( width <= 0 )
- width = 5;
- direction *= M_PI / 180.0;
- painter->save();
- const QPoint p1( center.x() + 1, center.y() + 2 );
- const QPoint p2 = qwtPolar2Pos( p1, length, direction );
- if ( width == 1 )
- {
- const QColor midColor =
- palette.color( colorGroup, QPalette::Mid );
- painter->setPen( QPen( midColor, 1 ) );
- painter->drawLine( p1, p2 );
- }
- else
- {
- QPolygon pa( 4 );
- pa.setPoint( 0, qwtPolar2Pos( p1, width / 2, direction + M_PI_2 ) );
- pa.setPoint( 1, qwtPolar2Pos( p2, width / 2, direction + M_PI_2 ) );
- pa.setPoint( 2, qwtPolar2Pos( p2, width / 2, direction - M_PI_2 ) );
- pa.setPoint( 3, qwtPolar2Pos( p1, width / 2, direction - M_PI_2 ) );
- painter->setPen( Qt::NoPen );
- painter->setBrush( palette.brush( colorGroup, QPalette::Mid ) );
- painter->drawPolygon( pa );
- }
- if ( hasKnob )
- {
- int knobWidth = qMax( qRound( width * 0.7 ), 5 );
- if ( knobWidth % 2 == 0 )
- knobWidth++;
- drawKnob( painter, center, knobWidth,
- palette.brush( colorGroup, QPalette::Base ),
- false );
- }
- painter->restore();
- }
- /*!
- Draw a needle looking like an arrow
- \param painter Painter
- \param palette Palette
- \param colorGroup Color group
- \param center center of the needle
- \param length Length of the needle
- \param width Width of the needle
- \param direction Current Direction
- \param hasKnob With/Without knob
- */
- void QwtDialSimpleNeedle::drawArrowNeedle( QPainter *painter,
- const QPalette &palette, QPalette::ColorGroup colorGroup,
- const QPoint ¢er, int length, int width,
- double direction, bool hasKnob )
- {
- direction *= M_PI / 180.0;
- painter->save();
- if ( width <= 0 )
- {
- width = ( int )qMax( length * 0.06, 9.0 );
- if ( width % 2 == 0 )
- width++;
- }
- const int peak = 3;
- const QPoint p1( center.x() + 1, center.y() + 1 );
- const QPoint p2 = qwtPolar2Pos( p1, length - peak, direction );
- const QPoint p3 = qwtPolar2Pos( p1, length, direction );
- QPolygon pa( 5 );
- pa.setPoint( 0, qwtPolar2Pos( p1, width / 2, direction - M_PI_2 ) );
- pa.setPoint( 1, qwtPolar2Pos( p2, 1, direction - M_PI_2 ) );
- pa.setPoint( 2, p3 );
- pa.setPoint( 3, qwtPolar2Pos( p2, 1, direction + M_PI_2 ) );
- pa.setPoint( 4, qwtPolar2Pos( p1, width / 2, direction + M_PI_2 ) );
- painter->setPen( Qt::NoPen );
- painter->setBrush( palette.brush( colorGroup, QPalette::Mid ) );
- painter->drawPolygon( pa );
- QPolygon shadowPa( 3 );
- const int colorOffset = 10;
- int i;
- for ( i = 0; i < 3; i++ )
- shadowPa.setPoint( i, pa[i] );
- const QColor midColor = palette.color( colorGroup, QPalette::Mid );
- painter->setPen( midColor.dark( 100 + colorOffset ) );
- painter->drawPolyline( shadowPa );
- for ( i = 0; i < 3; i++ )
- shadowPa.setPoint( i, pa[i + 2] );
- painter->setPen( midColor.dark( 100 - colorOffset ) );
- painter->drawPolyline( shadowPa );
- if ( hasKnob )
- {
- drawKnob( painter, center, qRound( width * 1.3 ),
- palette.brush( colorGroup, QPalette::Base ),
- false );
- }
- painter->restore();
- }
- //! Constructor
- QwtCompassMagnetNeedle::QwtCompassMagnetNeedle( Style style,
- const QColor &light, const QColor &dark ):
- d_style( style )
- {
- QPalette palette;
- for ( int i = 0; i < QPalette::NColorGroups; i++ )
- {
- palette.setColor( ( QPalette::ColorGroup )i,
- QPalette::Light, light );
- palette.setColor( ( QPalette::ColorGroup )i,
- QPalette::Dark, dark );
- palette.setColor( ( QPalette::ColorGroup )i,
- QPalette::Base, Qt::darkGray );
- }
- setPalette( palette );
- }
- /*!
- Draw the needle
- \param painter Painter
- \param center Center of the dial, start position for the needle
- \param length Length of the needle
- \param direction Direction of the needle, in degrees counter clockwise
- \param colorGroup Color group, used for painting
- */
- void QwtCompassMagnetNeedle::draw( QPainter *painter, const QPoint ¢er,
- int length, double direction, QPalette::ColorGroup colorGroup ) const
- {
- if ( d_style == ThinStyle )
- {
- drawThinNeedle( painter, palette(), colorGroup,
- center, length, direction );
- }
- else
- {
- drawTriangleNeedle( painter, palette(), colorGroup,
- center, length, direction );
- }
- }
- /*!
- Draw a compass needle
- \param painter Painter
- \param palette Palette
- \param colorGroup Color group
- \param center Center, where the needle starts
- \param length Length of the needle
- \param direction Direction
- */
- void QwtCompassMagnetNeedle::drawTriangleNeedle( QPainter *painter,
- const QPalette &palette, QPalette::ColorGroup colorGroup,
- const QPoint ¢er, int length, double direction )
- {
- const QBrush darkBrush = palette.brush( colorGroup, QPalette::Dark );
- const QBrush lightBrush = palette.brush( colorGroup, QPalette::Light );
- QBrush brush;
- const int width = qRound( length / 3.0 );
- const int colorOffset = 10;
- painter->save();
- painter->setPen( Qt::NoPen );
- const QPoint arrowCenter( center.x() + 1, center.y() + 1 );
- QPolygon pa( 3 );
- pa.setPoint( 0, arrowCenter );
- pa.setPoint( 1, qwtDegree2Pos( arrowCenter, length, direction ) );
- pa.setPoint( 2, qwtDegree2Pos( arrowCenter, width / 2, direction + 90.0 ) );
- brush = darkBrush;
- brush.setColor( brush.color().dark( 100 + colorOffset ) );
- painter->setBrush( brush );
- painter->drawPolygon( pa );
- pa.setPoint( 2, qwtDegree2Pos( arrowCenter, width / 2, direction - 90.0 ) );
- brush = darkBrush;
- brush.setColor( brush.color().dark( 100 - colorOffset ) );
- painter->setBrush( brush );
- painter->drawPolygon( pa );
- // --
- pa.setPoint( 1, qwtDegree2Pos( arrowCenter, length, direction + 180.0 ) );
- pa.setPoint( 2, qwtDegree2Pos( arrowCenter, width / 2, direction + 90.0 ) );
- brush = lightBrush;
- brush.setColor( brush.color().dark( 100 + colorOffset ) );
- painter->setBrush( brush );
- painter->drawPolygon( pa );
- pa.setPoint( 2, qwtDegree2Pos( arrowCenter, width / 2, direction - 90.0 ) );
- brush = lightBrush;
- brush.setColor( brush.color().dark( 100 - colorOffset ) );
- painter->setBrush( brush );
- painter->drawPolygon( pa );
- painter->restore();
- }
- /*!
- Draw a compass needle
- \param painter Painter
- \param palette Palette
- \param colorGroup Color group
- \param center Center, where the needle starts
- \param length Length of the needle
- \param direction Direction
- */
- void QwtCompassMagnetNeedle::drawThinNeedle( QPainter *painter,
- const QPalette &palette, QPalette::ColorGroup colorGroup,
- const QPoint ¢er, int length, double direction )
- {
- const QBrush darkBrush = palette.brush( colorGroup, QPalette::Dark );
- const QBrush lightBrush = palette.brush( colorGroup, QPalette::Light );
- const QBrush baseBrush = palette.brush( colorGroup, QPalette::Base );
- const int colorOffset = 10;
- const int width = qMax( qRound( length / 6.0 ), 3 );
- painter->save();
- const QPoint arrowCenter( center.x() + 1, center.y() + 1 );
- drawPointer( painter, darkBrush, colorOffset,
- arrowCenter, length, width, direction );
- drawPointer( painter, lightBrush, -colorOffset,
- arrowCenter, length, width, direction + 180.0 );
- drawKnob( painter, arrowCenter, width, baseBrush, true );
- painter->restore();
- }
- /*!
- Draw a compass needle
- \param painter Painter
- \param brush Brush
- \param colorOffset Color offset
- \param center Center, where the needle starts
- \param length Length of the needle
- \param width Width of the needle
- \param direction Direction
- */
- void QwtCompassMagnetNeedle::drawPointer(
- QPainter *painter, const QBrush &brush,
- int colorOffset, const QPoint ¢er, int length,
- int width, double direction )
- {
- painter->save();
- const int peak = qMax( qRound( length / 10.0 ), 5 );
- const int knobWidth = width + 8;
- QRect knobRect( 0, 0, knobWidth, knobWidth );
- knobRect.moveCenter( center );
- QPolygon pa( 5 );
- pa.setPoint( 0, qwtDegree2Pos( center, width / 2, direction + 90.0 ) );
- pa.setPoint( 1, center );
- pa.setPoint( 2, qwtDegree2Pos( pa.point( 1 ), length - peak, direction ) );
- pa.setPoint( 3, qwtDegree2Pos( center, length, direction ) );
- pa.setPoint( 4, qwtDegree2Pos( pa.point( 0 ), length - peak, direction ) );
- painter->setPen( Qt::NoPen );
- QBrush darkBrush = brush;
- darkBrush.setColor( darkBrush.color().dark( 100 + colorOffset ) );
- painter->setBrush( darkBrush );
- painter->drawPolygon( pa );
- painter->drawPie( knobRect, qRound( direction * 16 ), 90 * 16 );
- pa.setPoint( 0, qwtDegree2Pos( center, width / 2, direction - 90.0 ) );
- pa.setPoint( 4, qwtDegree2Pos( pa.point( 0 ), length - peak, direction ) );
- QBrush lightBrush = brush;
- lightBrush.setColor( lightBrush.color().dark( 100 - colorOffset ) );
- painter->setBrush( lightBrush );
- painter->drawPolygon( pa );
- painter->drawPie( knobRect, qRound( direction * 16 ), -90 * 16 );
- painter->restore();
- }
- /*!
- Constructor
- \param style Arrow style
- \param light Light color
- \param dark Dark color
- */
- QwtCompassWindArrow::QwtCompassWindArrow( Style style,
- const QColor &light, const QColor &dark ):
- d_style( style )
- {
- QPalette palette;
- for ( int i = 0; i < QPalette::NColorGroups; i++ )
- {
- palette.setColor( ( QPalette::ColorGroup )i,
- QPalette::Light, light );
- palette.setColor( ( QPalette::ColorGroup )i,
- QPalette::Dark, dark );
- }
- setPalette( palette );
- }
- /*!
- Draw the needle
- \param painter Painter
- \param center Center of the dial, start position for the needle
- \param length Length of the needle
- \param direction Direction of the needle, in degrees counter clockwise
- \param colorGroup Color group, used for painting
- */
- void QwtCompassWindArrow::draw( QPainter *painter, const QPoint ¢er,
- int length, double direction, QPalette::ColorGroup colorGroup ) const
- {
- if ( d_style == Style1 )
- {
- drawStyle1Needle( painter, palette(), colorGroup,
- center, length, direction );
- }
- else
- {
- drawStyle2Needle( painter, palette(), colorGroup,
- center, length, direction );
- }
- }
- /*!
- Draw a compass needle
- \param painter Painter
- \param palette Palette
- \param colorGroup colorGroup
- \param center Center of the dial, start position for the needle
- \param length Length of the needle
- \param direction Direction of the needle, in degrees counter clockwise
- */
- void QwtCompassWindArrow::drawStyle1Needle( QPainter *painter,
- const QPalette &palette, QPalette::ColorGroup colorGroup,
- const QPoint ¢er, int length, double direction )
- {
- const QBrush lightBrush = palette.brush( colorGroup, QPalette::Light );
- const double AR1[] = {0, 0.4, 0.3, 1, 0.8, 1, 0.3, 0.4};
- const double AW1[] = {0, -45, -20, -15, 0, 15, 20, 45};
- const QPoint arrowCenter( center.x() + 1, center.y() + 1 );
- QPolygon pa( 8 );
- pa.setPoint( 0, arrowCenter );
- for ( int i = 1; i < 8; i++ )
- {
- const QPoint p = qwtDegree2Pos( center,
- AR1[i] * length, direction + AW1[i] );
- pa.setPoint( i, p );
- }
- painter->save();
- painter->setPen( Qt::NoPen );
- painter->setBrush( lightBrush );
- painter->drawPolygon( pa );
- painter->restore();
- }
- /*!
- Draw a compass needle
- \param painter Painter
- \param palette Palette
- \param colorGroup colorGroup
- \param center Center of the dial, start position for the needle
- \param length Length of the needle
- \param direction Direction of the needle, in degrees counter clockwise
- */
- void QwtCompassWindArrow::drawStyle2Needle( QPainter *painter,
- const QPalette &palette, QPalette::ColorGroup colorGroup,
- const QPoint ¢er, int length, double direction )
- {
- const QBrush lightBrush = palette.brush( colorGroup, QPalette::Light );
- const QBrush darkBrush = palette.brush( colorGroup, QPalette::Dark );
- painter->save();
- painter->setPen( Qt::NoPen );
- const double angle = 12.0;
- const double ratio = 0.7;
- const QPoint arrowCenter( center.x() + 1, center.y() + 1 );
- QPolygon pa( 3 );
- pa.setPoint( 0, center );
- pa.setPoint( 2, qwtDegree2Pos( arrowCenter, ratio * length, direction ) );
- pa.setPoint( 1, qwtDegree2Pos( arrowCenter, length, direction + angle ) );
- painter->setBrush( darkBrush );
- painter->drawPolygon( pa );
- pa.setPoint( 1, qwtDegree2Pos( arrowCenter, length, direction - angle ) );
- painter->setBrush( lightBrush );
- painter->drawPolygon( pa );
- painter->restore();
- }
|