qwt_dial_needle.cpp 15.9 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* -*- 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 <math.h>
#include <qapplication.h>
#include <qpainter.h>
#include "qwt_math.h"
#include "qwt_painter.h"
#include "qwt_polygon.h"
#include "qwt_dial_needle.h"

#if QT_VERSION < 0x040000
typedef QColorGroup QwtPalette;
#else
typedef QPalette QwtPalette;
#endif

//! Constructor
QwtDialNeedle::QwtDialNeedle():
    d_palette(QApplication::palette())
{
}

//! Destructor
31
QwtDialNeedle::~QwtDialNeedle()
pixhawk's avatar
pixhawk committed
32 33 34 35 36 37 38 39
{
}

/*!
    Sets the palette for the needle.

    \param palette New Palette
*/
40 41 42
void QwtDialNeedle::setPalette(const QPalette &palette)
{
    d_palette = palette;
pixhawk's avatar
pixhawk committed
43 44 45 46 47
}

/*!
  \return the palette of the needle.
*/
48 49 50
const QPalette &QwtDialNeedle::palette() const
{
    return d_palette;
pixhawk's avatar
pixhawk committed
51 52
}

53
//!  Draw the knob
pixhawk's avatar
pixhawk committed
54
void QwtDialNeedle::drawKnob(QPainter *painter,
55
                             const QPoint &pos, int width, const QBrush &brush, bool sunken)
pixhawk's avatar
pixhawk committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
{
    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
*/
91
QwtDialSimpleNeedle::QwtDialSimpleNeedle(Style style, bool hasKnob,
pixhawk's avatar
pixhawk committed
92 93 94 95 96 97
        const QColor &mid, const QColor &base):
    d_style(style),
    d_hasKnob(hasKnob),
    d_width(-1)
{
    QPalette palette;
98
    for ( int i = 0; i < QPalette::NColorGroups; i++ ) {
pixhawk's avatar
pixhawk committed
99
        palette.setColor((QPalette::ColorGroup)i,
100
                         QwtPalette::Mid, mid);
pixhawk's avatar
pixhawk committed
101
        palette.setColor((QPalette::ColorGroup)i,
102
                         QwtPalette::Base, base);
pixhawk's avatar
pixhawk committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    }

    setPalette(palette);
}

//! Set the width of the needle
void QwtDialSimpleNeedle::setWidth(int width)
{
    d_width = width;
}

/*!
  \return the width of the needle
*/
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 &center,
132
                               int length, double direction, QPalette::ColorGroup colorGroup) const
pixhawk's avatar
pixhawk committed
133
{
134
    if ( d_style == Arrow ) {
pixhawk's avatar
pixhawk committed
135
        drawArrowNeedle(painter, palette(), colorGroup,
136 137 138 139
                        center, length, d_width, direction, d_hasKnob);
    } else {
        drawRayNeedle(painter, palette(), colorGroup,
                      center, length, d_width, direction, d_hasKnob);
pixhawk's avatar
pixhawk committed
140 141 142 143 144 145
    }
}

/*!
  Draw a needle looking like a ray
*/
146 147 148 149
void QwtDialSimpleNeedle::drawRayNeedle(QPainter *painter,
                                        const QPalette &palette, QPalette::ColorGroup colorGroup,
                                        const QPoint &center, int length, int width, double direction,
                                        bool hasKnob)
pixhawk's avatar
pixhawk committed
150 151 152 153 154 155 156 157 158 159 160
{
    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);

161
    if ( width == 1 ) {
pixhawk's avatar
pixhawk committed
162 163 164 165 166
        const QColor midColor =
            palette.color(colorGroup, QwtPalette::Mid);

        painter->setPen(QPen(midColor, 1));
        painter->drawLine(p1, p2);
167
    } else {
pixhawk's avatar
pixhawk committed
168 169 170 171 172 173 174 175 176 177
        QwtPolygon 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, QwtPalette::Mid));
        painter->drawPolygon(pa);
    }
178
    if ( hasKnob ) {
pixhawk's avatar
pixhawk committed
179 180 181 182
        int knobWidth = qwtMax(qRound(width * 0.7), 5);
        if ( knobWidth % 2 == 0 )
            knobWidth++;

183 184 185
        drawKnob(painter, center, knobWidth,
                 palette.brush(colorGroup, QwtPalette::Base),
                 false);
pixhawk's avatar
pixhawk committed
186 187 188 189 190 191 192 193
    }

    painter->restore();
}

/*!
  Draw a needle looking like an arrow
*/
194 195 196 197
void QwtDialSimpleNeedle::drawArrowNeedle(QPainter *painter,
        const QPalette &palette, QPalette::ColorGroup colorGroup,
        const QPoint &center, int length, int width,
        double direction, bool hasKnob)
pixhawk's avatar
pixhawk committed
198 199 200 201 202
{
    direction *= M_PI / 180.0;

    painter->save();

203
    if ( width <= 0 ) {
pixhawk's avatar
pixhawk committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
        width = (int)qwtMax(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);

    QwtPolygon 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, QwtPalette::Mid));
    painter->drawPolygon(pa);

    QwtPolygon 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, QwtPalette::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);

244 245 246 247
    if ( hasKnob ) {
        drawKnob(painter, center, qRound(width * 1.3),
                 palette.brush(colorGroup, QwtPalette::Base),
                 false);
pixhawk's avatar
pixhawk committed
248 249 250 251 252 253 254 255 256 257
    }

    painter->restore();
}

//! Constructor

QwtCompassMagnetNeedle::QwtCompassMagnetNeedle(Style style,
        const QColor &light, const QColor &dark):
    d_style(style)
258
{
pixhawk's avatar
pixhawk committed
259
    QPalette palette;
260
    for ( int i = 0; i < QPalette::NColorGroups; i++ ) {
pixhawk's avatar
pixhawk committed
261
        palette.setColor((QPalette::ColorGroup)i,
262
                         QwtPalette::Light, light);
pixhawk's avatar
pixhawk committed
263
        palette.setColor((QPalette::ColorGroup)i,
264
                         QwtPalette::Dark, dark);
pixhawk's avatar
pixhawk committed
265
        palette.setColor((QPalette::ColorGroup)i,
266
                         QwtPalette::Base, Qt::darkGray);
pixhawk's avatar
pixhawk committed
267 268
    }

269
    setPalette(palette);
pixhawk's avatar
pixhawk committed
270 271 272 273 274 275 276 277 278 279 280 281
}

/*!
    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 &center,
282
                                  int length, double direction, QPalette::ColorGroup colorGroup) const
pixhawk's avatar
pixhawk committed
283
{
284 285 286 287
    if ( d_style == ThinStyle ) {
        drawThinNeedle(painter, palette(), colorGroup,
                       center, length, direction);
    } else {
pixhawk's avatar
pixhawk committed
288
        drawTriangleNeedle(painter, palette(), colorGroup,
289
                           center, length, direction);
pixhawk's avatar
pixhawk committed
290 291 292 293
    }
}

/*!
294
  Draw a compass needle
pixhawk's avatar
pixhawk committed
295 296 297 298 299 300 301 302

  \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
*/
303 304 305
void QwtCompassMagnetNeedle::drawTriangleNeedle(QPainter *painter,
        const QPalette &palette, QPalette::ColorGroup colorGroup,
        const QPoint &center, int length, double direction)
pixhawk's avatar
pixhawk committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
{
    const QBrush darkBrush = palette.brush(colorGroup, QwtPalette::Dark);
    const QBrush lightBrush = palette.brush(colorGroup, QwtPalette::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);

    QwtPolygon 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();
}

/*!
360
  Draw a compass needle
pixhawk's avatar
pixhawk committed
361 362 363 364 365 366 367 368

  \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
*/
369 370 371
void QwtCompassMagnetNeedle::drawThinNeedle(QPainter *painter,
        const QPalette &palette, QPalette::ColorGroup colorGroup,
        const QPoint &center, int length, double direction)
pixhawk's avatar
pixhawk committed
372 373 374 375 376 377 378 379 380 381 382 383
{
    const QBrush darkBrush = palette.brush(colorGroup, QwtPalette::Dark);
    const QBrush lightBrush = palette.brush(colorGroup, QwtPalette::Light);
    const QBrush baseBrush = palette.brush(colorGroup, QwtPalette::Base);

    const int colorOffset = 10;
    const int width = qwtMax(qRound(length / 6.0), 3);

    painter->save();

    const QPoint arrowCenter(center.x() + 1, center.y() + 1);

384 385 386 387 388
    drawPointer(painter, darkBrush, colorOffset,
                arrowCenter, length, width, direction);
    drawPointer(painter, lightBrush, -colorOffset,
                arrowCenter, length, width, direction + 180.0);

pixhawk's avatar
pixhawk committed
389 390 391 392 393 394
    drawKnob(painter, arrowCenter, width, baseBrush, true);

    painter->restore();
}

/*!
395
  Draw a compass needle
pixhawk's avatar
pixhawk committed
396 397 398 399 400 401 402 403 404 405 406

  \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,
407
    int colorOffset, const QPoint &center, int length,
pixhawk's avatar
pixhawk committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
    int width, double direction)
{
    painter->save();

    const int peak = qwtMax(qRound(length / 10.0), 5);

    const int knobWidth = width + 8;
    QRect knobRect(0, 0, knobWidth, knobWidth);
    knobRect.moveCenter(center);

    QwtPolygon 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();
}

446
/*!
pixhawk's avatar
pixhawk committed
447 448 449 450 451 452
   Constructor

   \param style Arrow style
   \param light Light color
   \param dark Dark color
*/
453
QwtCompassWindArrow::QwtCompassWindArrow(Style style,
pixhawk's avatar
pixhawk committed
454 455 456 457
        const QColor &light, const QColor &dark):
    d_style(style)
{
    QPalette palette;
458
    for ( int i = 0; i < QPalette::NColorGroups; i++ ) {
pixhawk's avatar
pixhawk committed
459
        palette.setColor((QPalette::ColorGroup)i,
460
                         QwtPalette::Light, light);
pixhawk's avatar
pixhawk committed
461
        palette.setColor((QPalette::ColorGroup)i,
462
                         QwtPalette::Dark, dark);
pixhawk's avatar
pixhawk committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
    }

    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 &center,
478
                               int length, double direction, QPalette::ColorGroup colorGroup) const
pixhawk's avatar
pixhawk committed
479
{
480
    if ( d_style == Style1 ) {
pixhawk's avatar
pixhawk committed
481
        drawStyle1Needle(painter, palette(), colorGroup,
482 483
                         center, length, direction);
    } else {
pixhawk's avatar
pixhawk committed
484
        drawStyle2Needle(painter, palette(), colorGroup,
485
                         center, length, direction);
pixhawk's avatar
pixhawk committed
486 487 488 489
    }
}

/*!
490
  Draw a compass needle
pixhawk's avatar
pixhawk committed
491 492 493 494 495 496 497 498

 \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
*/
499 500 501
void QwtCompassWindArrow::drawStyle1Needle(QPainter *painter,
        const QPalette &palette, QPalette::ColorGroup colorGroup,
        const QPoint &center, int length, double direction)
pixhawk's avatar
pixhawk committed
502 503 504 505 506 507 508 509 510 511
{
    const QBrush lightBrush = palette.brush(colorGroup, QwtPalette::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);

    QwtPolygon pa(8);
    pa.setPoint(0, arrowCenter);
512 513 514
    for (int i=1; i<8; i++) {
        const QPoint p = qwtDegree2Pos(center,
                                       AR1[i] * length, direction + AW1[i]);
pixhawk's avatar
pixhawk committed
515 516 517 518 519 520 521 522 523 524 525
        pa.setPoint(i, p);
    }

    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(lightBrush);
    painter->drawPolygon(pa);
    painter->restore();
}

/*!
526
  Draw a compass needle
pixhawk's avatar
pixhawk committed
527 528 529 530 531 532 533 534

 \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
*/
535 536 537
void QwtCompassWindArrow::drawStyle2Needle(QPainter *painter,
        const QPalette &palette, QPalette::ColorGroup colorGroup,
        const QPoint &center, int length, double direction)
pixhawk's avatar
pixhawk committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
{
    const QBrush lightBrush = palette.brush(colorGroup, QwtPalette::Light);
    const QBrush darkBrush = palette.brush(colorGroup, QwtPalette::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);

    QwtPolygon 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();
}