00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <math.h>
00011 #include <qpainter.h>
00012 #include <qpixmap.h>
00013 #include "qwt_math.h"
00014 #include "qwt_scldraw.h"
00015 #include "qwt_paint_buffer.h"
00016 #include "qwt_painter.h"
00017 #include "qwt_dial_needle.h"
00018 #include "qwt_compass_rose.h"
00019 #include "qwt_compass.h"
00020
00032 QwtCompass::QwtCompass(QWidget* parent, const char* name):
00033 QwtDial(parent, name),
00034 d_rose(0)
00035 {
00036 setOrigin(270.0);
00037 setWrapping(TRUE);
00038
00039 QwtScaleDraw *sd = scaleDraw();
00040 if ( sd )
00041 {
00042 sd->setOptions(sd->options() & ~QwtScaleDraw::Backbone);
00043 sd->setTickLength(0, 0, 0);
00044 }
00045
00046 d_labelMap.insert(0.0, "N");
00047 d_labelMap.insert(45.0, "NE");
00048 d_labelMap.insert(90.0, "E");
00049 d_labelMap.insert(135.0, "SE");
00050 d_labelMap.insert(180.0, "S");
00051 d_labelMap.insert(225.0, "SW");
00052 d_labelMap.insert(270.0, "W");
00053 d_labelMap.insert(315.0, "NW");
00054
00055 #if 0
00056 d_labelMap.insert(22.5, "NNE");
00057 d_labelMap.insert(67.5, "NEE");
00058 d_labelMap.insert(112.5, "SEE");
00059 d_labelMap.insert(157.5, "SSE");
00060 d_labelMap.insert(202.5, "SSW");
00061 d_labelMap.insert(247.5, "SWW");
00062 d_labelMap.insert(292.5, "NWW");
00063 d_labelMap.insert(337.5, "NNW");
00064 #endif
00065 }
00066
00068 QwtCompass::~QwtCompass()
00069 {
00070 delete d_rose;
00071 }
00072
00073 void QwtCompass::drawScaleContents(QPainter *painter,
00074 const QPoint ¢er, int radius) const
00075 {
00076 QPalette::ColorGroup cg;
00077 if ( isEnabled() )
00078 cg = hasFocus() ? QPalette::Active : QPalette::Inactive;
00079 else
00080 cg = QPalette::Disabled;
00081
00082 double north = origin();
00083 if ( isValid() )
00084 {
00085 if ( mode() == RotateScale )
00086 north -= value();
00087 }
00088
00089 const int margin = 4;
00090 drawRose(painter, center, radius - margin, 360.0 - north, cg);
00091 }
00092
00093 void QwtCompass::drawRose(QPainter *painter, const QPoint ¢er,
00094 int radius, double north, QPalette::ColorGroup cg) const
00095 {
00096 if ( d_rose )
00097 d_rose->draw(painter, center, radius, north, cg);
00098 }
00099
00108 void QwtCompass::setRose(QwtCompassRose *rose)
00109 {
00110 if ( rose != d_rose )
00111 {
00112 if ( d_rose )
00113 delete d_rose;
00114
00115 d_rose = rose;
00116 update();
00117 }
00118 }
00119
00125 const QwtCompassRose *QwtCompass::rose() const
00126 {
00127 return d_rose;
00128 }
00129
00135 QwtCompassRose *QwtCompass::rose()
00136 {
00137 return d_rose;
00138 }
00139
00140 void QwtCompass::keyPressEvent(QKeyEvent *kev)
00141 {
00142 if (isReadOnly())
00143 return;
00144
00145 #if 0
00146 if ( kev->key() == Key_5 )
00147 {
00148 invalidate();
00149 return;
00150 }
00151 #endif
00152
00153 double newValue = value();
00154
00155 if ( kev->key() >= Key_1 && kev->key() <= Key_9 )
00156 {
00157 if ( mode() != RotateNeedle || kev->key() == Key_5 )
00158 return;
00159
00160 switch (kev->key())
00161 {
00162 case Key_6:
00163 newValue = 180.0 * 0.0;
00164 break;
00165 case Key_3:
00166 newValue = 180.0 * 0.25;
00167 break;
00168 case Key_2:
00169 newValue = 180.0 * 0.5;
00170 break;
00171 case Key_1:
00172 newValue = 180.0 * 0.75;
00173 break;
00174 case Key_4:
00175 newValue = 180.0 * 1.0;
00176 break;
00177 case Key_7:
00178 newValue = 180.0 * 1.25;
00179 break;
00180 case Key_8:
00181 newValue = 180.0 * 1.5;
00182 break;
00183 case Key_9:
00184 newValue = 180.0 * 1.75;
00185 break;
00186 }
00187 newValue -= origin();
00188 setValue(newValue);
00189 }
00190 else
00191 {
00192 QwtDial::keyPressEvent(kev);
00193 }
00194 }
00195
00201 const QMap<double, QString> &QwtCompass::labelMap() const
00202 {
00203 return d_labelMap;
00204 }
00205
00211 QMap<double, QString> &QwtCompass::labelMap()
00212 {
00213 return d_labelMap;
00214 }
00215
00229 void QwtCompass::setLabelMap(const QMap<double, QString> &map)
00230 {
00231 d_labelMap = map;
00232 }
00233
00244 QString QwtCompass::scaleLabel(double value) const
00245 {
00246 #if 0
00247
00248 if ( value == -0 )
00249 value = 0.0;
00250 #endif
00251
00252 if ( value < 0.0 )
00253 value += 360.0;
00254
00255 if ( d_labelMap.contains(value) )
00256 return d_labelMap[value];
00257
00258 return QString::null;
00259 }