Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "qwt_compass.h"
00011 #include "qwt_compass_rose.h"
00012 #include "qwt_math.h"
00013 #include "qwt_scale_draw.h"
00014 #include "qwt_painter.h"
00015 #include "qwt_dial_needle.h"
00016 #include <qpainter.h>
00017 #include <qpixmap.h>
00018 #include <qevent.h>
00019
00025 QwtCompassScaleDraw::QwtCompassScaleDraw()
00026 {
00027 enableComponent( QwtAbstractScaleDraw::Backbone, false );
00028 enableComponent( QwtAbstractScaleDraw::Ticks, false );
00029
00030 d_labelMap.insert( 0.0, QString::fromLatin1( "N" ) );
00031 d_labelMap.insert( 45.0, QString::fromLatin1( "NE" ) );
00032 d_labelMap.insert( 90.0, QString::fromLatin1( "E" ) );
00033 d_labelMap.insert( 135.0, QString::fromLatin1( "SE" ) );
00034 d_labelMap.insert( 180.0, QString::fromLatin1( "S" ) );
00035 d_labelMap.insert( 225.0, QString::fromLatin1( "SW" ) );
00036 d_labelMap.insert( 270.0, QString::fromLatin1( "W" ) );
00037 d_labelMap.insert( 315.0, QString::fromLatin1( "NW" ) );
00038
00039 #if 0
00040 d_labelMap.insert( 22.5, QString::fromLatin1( "NNE" ) );
00041 d_labelMap.insert( 67.5, QString::fromLatin1( "NEE" ) );
00042 d_labelMap.insert( 112.5, QString::fromLatin1( "SEE" ) );
00043 d_labelMap.insert( 157.5, QString::fromLatin1( "SSE" ) );
00044 d_labelMap.insert( 202.5, QString::fromLatin1( "SSW" ) );
00045 d_labelMap.insert( 247.5, QString::fromLatin1( "SWW" ) );
00046 d_labelMap.insert( 292.5, QString::fromLatin1( "NWW" ) );
00047 d_labelMap.insert( 337.5, QString::fromLatin1( "NNW" ) );
00048 #endif
00049 }
00050
00056 QwtCompassScaleDraw::QwtCompassScaleDraw( const QMap<double, QString> &map ):
00057 d_labelMap( map )
00058 {
00059 enableComponent( QwtAbstractScaleDraw::Backbone, false );
00060 enableComponent( QwtAbstractScaleDraw::Ticks, false );
00061 }
00062
00075 void QwtCompassScaleDraw::setLabelMap( const QMap<double, QString> &map )
00076 {
00077 d_labelMap = map;
00078 }
00079
00080
00085 QMap<double, QString> QwtCompassScaleDraw::labelMap() const
00086 {
00087 return d_labelMap;
00088 }
00089
00102 QwtText QwtCompassScaleDraw::label( double value ) const
00103 {
00104 if ( qFuzzyCompare( value + 1.0, 1.0 ) )
00105 value = 0.0;
00106
00107 if ( value < 0.0 )
00108 value += 360.0;
00109
00110 if ( d_labelMap.contains( value ) )
00111 return d_labelMap[value];
00112
00113 return QwtText();
00114 }
00115
00116 class QwtCompass::PrivateData
00117 {
00118 public:
00119 PrivateData():
00120 rose( NULL )
00121 {
00122 }
00123
00124 ~PrivateData()
00125 {
00126 delete rose;
00127 }
00128
00129 QwtCompassRose *rose;
00130 };
00131
00141 QwtCompass::QwtCompass( QWidget* parent ):
00142 QwtDial( parent )
00143 {
00144 d_data = new PrivateData;
00145
00146 setScaleDraw( new QwtCompassScaleDraw() );
00147
00148 setOrigin( 270.0 );
00149 setWrapping( true );
00150
00151 setScaleMaxMajor( 36 );
00152 setScaleMaxMinor( 10 );
00153
00154 setScale( 0.0, 360.0 );
00155 setTotalSteps( 360 );
00156 }
00157
00159 QwtCompass::~QwtCompass()
00160 {
00161 delete d_data;
00162 }
00163
00164
00172 void QwtCompass::drawScaleContents( QPainter *painter,
00173 const QPointF ¢er, double radius ) const
00174 {
00175 QPalette::ColorGroup cg;
00176 if ( isEnabled() )
00177 cg = hasFocus() ? QPalette::Active : QPalette::Inactive;
00178 else
00179 cg = QPalette::Disabled;
00180
00181 double north = origin();
00182 if ( isValid() )
00183 {
00184 if ( mode() == RotateScale )
00185 north -= value();
00186 }
00187
00188 const int margin = 4;
00189 drawRose( painter, center, radius - margin, 360.0 - north, cg );
00190 }
00191
00201 void QwtCompass::drawRose( QPainter *painter, const QPointF ¢er,
00202 double radius, double north, QPalette::ColorGroup cg ) const
00203 {
00204 if ( d_data->rose )
00205 d_data->rose->draw( painter, center, radius, north, cg );
00206 }
00207
00215 void QwtCompass::setRose( QwtCompassRose *rose )
00216 {
00217 if ( rose != d_data->rose )
00218 {
00219 if ( d_data->rose )
00220 delete d_data->rose;
00221
00222 d_data->rose = rose;
00223 update();
00224 }
00225 }
00226
00231 const QwtCompassRose *QwtCompass::rose() const
00232 {
00233 return d_data->rose;
00234 }
00235
00240 QwtCompassRose *QwtCompass::rose()
00241 {
00242 return d_data->rose;
00243 }
00244
00254 void QwtCompass::keyPressEvent( QKeyEvent *kev )
00255 {
00256 if ( isReadOnly() )
00257 return;
00258
00259 #if 0
00260 if ( kev->key() == Key_5 )
00261 {
00262 invalidate();
00263 return;
00264 }
00265 #endif
00266
00267 double newValue = value();
00268
00269 if ( kev->key() >= Qt::Key_1 && kev->key() <= Qt::Key_9 )
00270 {
00271 if ( mode() != RotateNeedle || kev->key() == Qt::Key_5 )
00272 return;
00273
00274 switch ( kev->key() )
00275 {
00276 case Qt::Key_6:
00277 newValue = 180.0 * 0.0;
00278 break;
00279 case Qt::Key_3:
00280 newValue = 180.0 * 0.25;
00281 break;
00282 case Qt::Key_2:
00283 newValue = 180.0 * 0.5;
00284 break;
00285 case Qt::Key_1:
00286 newValue = 180.0 * 0.75;
00287 break;
00288 case Qt::Key_4:
00289 newValue = 180.0 * 1.0;
00290 break;
00291 case Qt::Key_7:
00292 newValue = 180.0 * 1.25;
00293 break;
00294 case Qt::Key_8:
00295 newValue = 180.0 * 1.5;
00296 break;
00297 case Qt::Key_9:
00298 newValue = 180.0 * 1.75;
00299 break;
00300 }
00301 newValue -= origin();
00302 setValue( newValue );
00303 }
00304 else
00305 {
00306 QwtDial::keyPressEvent( kev );
00307 }
00308 }