00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <math.h>
00011 #include <qpainter.h>
00012 #include "qwt_math.h"
00013 #include "qwt_painter.h"
00014 #include "qwt_dial_needle.h"
00015
00016 QwtDialNeedle::QwtDialNeedle()
00017 {
00018 const QColor dark(16,16,16);
00019 const QColor light(64,64,64);
00020
00021 QPalette palette;
00022 for ( int i = 0; i < QPalette::NColorGroups; i++ )
00023 {
00024 palette.setColor((QPalette::ColorGroup)i,
00025 QColorGroup::Dark, dark);
00026 palette.setColor((QPalette::ColorGroup)i,
00027 QColorGroup::Light, light);
00028 }
00029 setPalette(palette);
00030 }
00031
00032 QwtDialNeedle::~QwtDialNeedle()
00033 {
00034 }
00035
00036 void QwtDialNeedle::setPalette(const QPalette &p)
00037 {
00038 d_palette = p;
00039 }
00040
00041 const QPalette &QwtDialNeedle::palette() const
00042 {
00043 return d_palette;
00044 }
00045
00046 const QColorGroup &QwtDialNeedle::colorGroup(QPalette::ColorGroup cg) const
00047 {
00048 switch(cg)
00049 {
00050 case QPalette::Disabled:
00051 return d_palette.disabled();
00052 case QPalette::Inactive:
00053 return d_palette.inactive();
00054 default:
00055 return d_palette.active();
00056 }
00057 }
00058
00059 QwtCompassNeedle3::QwtCompassNeedle3(const QColor &base, const QColor &mid)
00060 {
00061 QPalette palette;
00062 for ( int i = 0; i < QPalette::NColorGroups; i++ )
00063 {
00064 palette.setColor((QPalette::ColorGroup)i,
00065 QColorGroup::Dark, base);
00066 palette.setColor((QPalette::ColorGroup)i,
00067 QColorGroup::Mid, mid);
00068 }
00069
00070 setPalette(palette);
00071 }
00072
00073 void QwtCompassNeedle3::draw(QPainter *painter, const QPoint ¢er,
00074 int length, double direction, QPalette::ColorGroup cg) const
00075 {
00076 drawNeedle(painter, center, length, direction,
00077 colorGroup(cg).dark(), colorGroup(cg).mid());
00078 }
00079
00080 void QwtCompassNeedle3::drawNeedle(QPainter *painter, const QPoint ¢er,
00081 int length, double direction, const QColor &c,
00082 const QColor &arrowColor)
00083 {
00084 direction *= M_PI / 180.0;
00085
00086 painter->save();
00087 painter->setPen(Qt::NoPen);
00088
00089 int width = (int)QMAX(length * 0.03, 9);
00090 if ( width % 2 == 0 )
00091 width++;
00092
00093 const double peak = width;
00094
00095 const QPoint p1 = qwtPolar2Pos(center, 0.2 * length, M_PI + direction);
00096 const QPoint p2 = qwtPolar2Pos(center, length - peak, direction);
00097 const QPoint p3 = qwtPolar2Pos(center, length, direction);
00098
00099 QPointArray pa(7);
00100 pa.setPoint(0, qwtPolar2Pos(p1, width / 2, direction + M_PI_2));
00101 pa.setPoint(1, qwtPolar2Pos(p1, width / 2, direction - M_PI_2));
00102 pa.setPoint(2, qwtPolar2Pos(p2, width / 2, direction - M_PI_2));
00103 pa.setPoint(3, qwtPolar2Pos(p2, width, direction - M_PI_2));
00104 pa.setPoint(4, p3);
00105 pa.setPoint(5, qwtPolar2Pos(p2, width, direction + M_PI_2));
00106 pa.setPoint(6, qwtPolar2Pos(p2, width / 2, direction + M_PI_2));
00107
00108 painter->setBrush(arrowColor);
00109 painter->drawPolygon(pa);
00110
00111 QRect r(0, 0, 2 * width, 2 * width);
00112 r.moveCenter(center);
00113
00114 painter->setBrush(c);
00115 painter->drawEllipse(r);
00116
00117 painter->restore();
00118 }
00119
00120 QwtCompassNeedle4::QwtCompassNeedle4(
00121 const QColor &light, const QColor &dark, const QColor &mid)
00122 {
00123 QPalette palette;
00124 for ( int i = 0; i < QPalette::NColorGroups; i++ )
00125 {
00126 palette.setColor((QPalette::ColorGroup)i,
00127 QColorGroup::Light, light);
00128 palette.setColor((QPalette::ColorGroup)i,
00129 QColorGroup::Dark, dark);
00130 palette.setColor((QPalette::ColorGroup)i,
00131 QColorGroup::Mid, mid);
00132 }
00133
00134 setPalette(palette);
00135 }
00136
00137
00138 void QwtCompassNeedle4::draw(QPainter *painter, const QPoint ¢er,
00139 int length, double direction, QPalette::ColorGroup cg) const
00140 {
00141 drawNeedle(painter, center, length, direction,
00142 colorGroup(cg).light(), colorGroup(cg).dark(), colorGroup(cg).mid());
00143 }
00144
00145 void QwtCompassNeedle4::drawNeedle(QPainter *painter, const QPoint ¢er,
00146 int length, double direction,
00147 const QColor &light, const QColor &dark, const QColor &mid)
00148 {
00149 painter->save();
00150
00151 int colorOffset = 10;
00152 if ( direction < 135.0 || direction > 135.0 + 180.0 )
00153 colorOffset = -colorOffset;
00154
00155 const int width = QMAX(qRound(length / 6.0), 3);
00156
00157 drawPointer(painter, dark, colorOffset,
00158 center, length, width, direction);
00159 drawPointer(painter, light, -colorOffset,
00160 center, length, width, direction + 180.0);
00161
00162 QRect knobRect(0, 0, width, width);
00163 knobRect.moveCenter(center);
00164
00165 painter->setBrush(QBrush(mid));
00166 painter->drawEllipse(knobRect);
00167
00168 QwtPainter::drawRoundFrame(painter, knobRect, 3, FALSE);
00169
00170 painter->restore();
00171 }
00172
00173 void QwtCompassNeedle4::drawPointer(QPainter *painter, const QColor &color,
00174 int colorOffset, const QPoint ¢er, int length,
00175 int width, double direction)
00176 {
00177 painter->save();
00178
00179 const int peak = QMAX(qRound(length / 10.0), 5);
00180
00181 const int knobWidth = width + 8;
00182 QRect knobRect(0, 0, knobWidth, knobWidth);
00183 knobRect.moveCenter(center);
00184
00185 QPointArray pa(5);
00186
00187 pa.setPoint(0, qwtDegree2Pos(center, width / 2, direction + 90.0));
00188 pa.setPoint(1, center);
00189 pa.setPoint(2, qwtDegree2Pos(pa.point(1), length - peak, direction));
00190 pa.setPoint(3, qwtDegree2Pos(center, length, direction));
00191 pa.setPoint(4, qwtDegree2Pos(pa.point(0), length - peak, direction));
00192
00193 painter->setPen(Qt::NoPen);
00194 painter->setBrush(color.dark(100 + colorOffset));
00195 painter->drawPolygon(pa);
00196 painter->drawPie(knobRect, qRound(direction * 16), 90 * 16);
00197
00198 pa.setPoint(0, qwtDegree2Pos(center, width / 2, direction - 90.0));
00199 pa.setPoint(4, qwtDegree2Pos(pa.point(0), length - peak, direction));
00200
00201 painter->setBrush(color.dark(100 - colorOffset));
00202 painter->drawPolygon(pa);
00203 painter->drawPie(knobRect, qRound(direction * 16), -90 * 16);
00204
00205 painter->restore();
00206 }
00207
00208 QwtCompassMagnetNeedle::QwtCompassMagnetNeedle(
00209 const QColor &light, const QColor &dark)
00210 {
00211 QPalette palette;
00212 for ( int i = 0; i < QPalette::NColorGroups; i++ )
00213 {
00214 palette.setColor((QPalette::ColorGroup)i,
00215 QColorGroup::Light, light);
00216 palette.setColor((QPalette::ColorGroup)i,
00217 QColorGroup::Dark, dark);
00218 }
00219
00220 setPalette(palette);
00221 }
00222
00223
00224 void QwtCompassMagnetNeedle::draw(QPainter *painter, const QPoint ¢er,
00225 int length, double direction, QPalette::ColorGroup cg) const
00226 {
00227 drawNeedle(painter, center, length, direction,
00228 colorGroup(cg).light(), colorGroup(cg).dark());
00229 }
00230
00231 void QwtCompassMagnetNeedle::drawNeedle(QPainter *painter,
00232 const QPoint ¢er, int length, double direction,
00233 const QColor &light, const QColor &dark)
00234 {
00235 const int width = qRound(length / 3.0);
00236
00237 int colorOffset = 10;
00238 if ( direction < 135.0 || direction > 135.0 + 180.0 )
00239 colorOffset = -colorOffset;
00240
00241 painter->save();
00242
00243 QPointArray pa(3);
00244 pa.setPoint(0, center);
00245 pa.setPoint(1, qwtDegree2Pos(center, length, direction));
00246
00247 pa.setPoint(2, qwtDegree2Pos(center, width / 2, direction + 90.0));
00248 painter->setBrush(dark.dark(100 + colorOffset));
00249 painter->drawPolygon(pa);
00250
00251 pa.setPoint(2, qwtDegree2Pos(center, width / 2, direction - 90.0));
00252 painter->setBrush(dark.dark(100 - colorOffset));
00253 painter->drawPolygon(pa);
00254
00255
00256
00257 pa.setPoint(1, qwtDegree2Pos(center, length, direction + 180.0));
00258
00259 pa.setPoint(2, qwtDegree2Pos(center, width / 2, direction + 90.0));
00260 painter->setBrush(light.dark(100 + colorOffset));
00261 painter->drawPolygon(pa);
00262
00263 pa.setPoint(2, qwtDegree2Pos(center, width / 2, direction - 90.0));
00264 painter->setBrush(light.dark(100 - colorOffset));
00265 painter->drawPolygon(pa);
00266
00267 painter->restore();
00268 }
00269
00270
00271 QwtCompassNeedle1::QwtCompassNeedle1(const QColor &mid)
00272 {
00273 QPalette palette;
00274 for ( int i = 0; i < QPalette::NColorGroups; i++ )
00275 {
00276 palette.setColor((QPalette::ColorGroup)i,
00277 QColorGroup::Mid, mid);
00278 }
00279
00280 setPalette(palette);
00281 }
00282
00283 void QwtCompassNeedle1::draw(QPainter *painter, const QPoint ¢er,
00284 int length, double direction, QPalette::ColorGroup cg) const
00285 {
00286 drawNeedle(painter, center, length, direction, colorGroup(cg).mid());
00287 }
00288
00289 void QwtCompassNeedle1::drawNeedle(QPainter *painter, const QPoint ¢er,
00290 int length, double direction, const QColor &color)
00291 {
00292 const double AR1[] = {0, 0.4, 0.3, 1, 0.8, 1, 0.3, 0.4};
00293 const double AW1[] = {0, -45, -20, -15, 0, 15, 20, 45};
00294
00295 QPointArray pa(8);
00296 pa.setPoint(0, center);
00297 for (int i=1; i<8; i++)
00298 {
00299 const QPoint p = qwtDegree2Pos(center,
00300 AR1[i] * length, direction + AW1[i]);
00301 pa.setPoint(i, p);
00302 }
00303
00304 painter->save();
00305 painter->setPen(Qt::NoPen);
00306 painter->setBrush(color);
00307 painter->drawPolygon(pa);
00308 painter->restore();
00309 }
00310
00311 QwtCompassNeedle2::QwtCompassNeedle2(const QColor &light, const QColor &mid)
00312 {
00313 QPalette palette;
00314 for ( int i = 0; i < QPalette::NColorGroups; i++ )
00315 {
00316 palette.setColor((QPalette::ColorGroup)i,
00317 QColorGroup::Light, light);
00318 palette.setColor((QPalette::ColorGroup)i,
00319 QColorGroup::Mid, mid);
00320 }
00321
00322 setPalette(palette);
00323 }
00324
00325 void QwtCompassNeedle2::draw(QPainter *painter, const QPoint ¢er,
00326 int length, double direction, QPalette::ColorGroup cg) const
00327 {
00328 drawNeedle(painter, center, length, direction,
00329 colorGroup(cg).light(), colorGroup(cg).mid());
00330 }
00331
00332 void QwtCompassNeedle2::drawNeedle(QPainter *painter, const QPoint ¢er,
00333 int length, double direction, const QColor &light, const QColor &mid)
00334 {
00335 painter->save();
00336 painter->setPen(Qt::NoPen);
00337
00338 const double angle = 12.0;
00339 const double ratio = 0.7;
00340
00341 QPointArray pa(3);
00342
00343 pa.setPoint(0, center);
00344 pa.setPoint(2, qwtDegree2Pos(center, ratio * length, direction));
00345
00346 pa.setPoint(1, qwtDegree2Pos(center, length, direction + angle));
00347 painter->setBrush(mid);
00348 painter->drawPolygon(pa);
00349
00350 pa.setPoint(1, qwtDegree2Pos(center, length, direction - angle));
00351 painter->setBrush(light);
00352 painter->drawPolygon(pa);
00353
00354 painter->restore();
00355 }
00356
00357 QwtCompassLineNeedle::QwtCompassLineNeedle(const QColor &foreground)
00358 {
00359 QPalette palette;
00360 for ( int i = 0; i < QPalette::NColorGroups; i++ )
00361 {
00362 palette.setColor((QPalette::ColorGroup)i,
00363 QColorGroup::Foreground, foreground);
00364 }
00365
00366 setPalette(palette);
00367 }
00368
00369 void QwtCompassLineNeedle::draw(QPainter *painter, const QPoint ¢er,
00370 int length, double direction, QPalette::ColorGroup cg) const
00371 {
00372 drawNeedle(painter, center, length, direction, colorGroup(cg).foreground());
00373 }
00374
00375 void QwtCompassLineNeedle::drawNeedle(QPainter *painter, const QPoint ¢er,
00376 int length, double direction, const QColor &color)
00377 {
00378 painter->save();
00379 painter->setPen(QPen(color, 3));
00380 painter->drawLine(center, qwtDegree2Pos(center, length, direction));
00381 painter->restore();
00382 }
00383