qwt_dial.cpp 28.3 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 31 32 33 34 35 36 37 38 39 40
/* -*- 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 <qpainter.h>
#if QT_VERSION >= 0x040000
#include <qbitmap.h>
#include <qpalette.h>
#endif
#include <qpixmap.h>
#include <qevent.h>
#include "qwt_math.h"
#include "qwt_scale_engine.h"
#include "qwt_scale_map.h"
#include "qwt_paint_buffer.h"
#include "qwt_painter.h"
#include "qwt_dial_needle.h"
#include "qwt_dial.h"

class QwtDial::PrivateData
{
public:
    PrivateData():
        visibleBackground(true),
        frameShadow(Sunken),
        lineWidth(0),
        mode(RotateNeedle),
        origin(90.0),
        minScaleArc(0.0),
        maxScaleArc(0.0),
        scaleDraw(0),
        maxMajIntv(36),
        maxMinIntv(10),
        scaleStep(0.0),
41
        needle(0) {
pixhawk's avatar
pixhawk committed
42 43
    }

44
    ~PrivateData() {
pixhawk's avatar
pixhawk committed
45 46 47 48 49 50 51 52 53 54 55 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
        delete scaleDraw;
        delete needle;
    }
    bool visibleBackground;
    Shadow frameShadow;
    int lineWidth;

    QwtDial::Mode mode;

    double origin;
    double minScaleArc;
    double maxScaleArc;

    QwtDialScaleDraw *scaleDraw;
    int maxMajIntv;
    int maxMinIntv;
    double scaleStep;

    QwtDialNeedle *needle;

    static double previousDir;
};

double QwtDial::PrivateData::previousDir = -1.0;

/*!
  Constructor

  \param parent Parent dial widget
*/
QwtDialScaleDraw::QwtDialScaleDraw(QwtDial *parent):
    d_parent(parent),
    d_penWidth(1)
{
}

/*!
  Set the pen width used for painting the scale

  \param penWidth Pen width
  \sa penWidth(), QwtDial::drawScale()
*/
87

pixhawk's avatar
pixhawk committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101
void QwtDialScaleDraw::setPenWidth(uint penWidth)
{
    d_penWidth = penWidth;
}

/*!
  \return Pen width used for painting the scale
  \sa setPenWidth, QwtDial::drawScale()
*/
uint QwtDialScaleDraw::penWidth() const
{
    return d_penWidth;
}

102
/*!
pixhawk's avatar
pixhawk committed
103 104 105
  Call QwtDial::scaleLabel of the parent dial widget.

  \param value Value to display
106

pixhawk's avatar
pixhawk committed
107
  \sa QwtDial::scaleLabel
108
*/
pixhawk's avatar
pixhawk committed
109 110 111 112 113 114 115 116 117 118 119 120
QwtText QwtDialScaleDraw::label(double value) const
{
    if ( d_parent == NULL )
        return QwtRoundScaleDraw::label(value);

    return d_parent->scaleLabel(value);
}

/*!
  \brief Constructor
  \param parent Parent widget

121
  Create a dial widget with no scale and no needle.
pixhawk's avatar
pixhawk committed
122 123 124
  The default origin is 90.0 with no valid value. It accepts
  mouse and keyboard inputs and has no step size. The default mode
  is QwtDial::RotateNeedle.
125
*/
pixhawk's avatar
pixhawk committed
126 127 128 129 130 131 132 133 134 135 136 137 138

QwtDial::QwtDial(QWidget* parent):
    QwtAbstractSlider(Qt::Horizontal, parent)
{
    initDial();
}

#if QT_VERSION < 0x040000
/*!
  \brief Constructor
  \param parent Parent widget
  \param name Object name

139
  Create a dial widget with no scale and no needle.
pixhawk's avatar
pixhawk committed
140 141 142
  The default origin is 90.0 with no valid value. It accepts
  mouse and keyboard inputs and has no step size. The default mode
  is QwtDial::RotateNeedle.
143
*/
pixhawk's avatar
pixhawk committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157
QwtDial::QwtDial(QWidget* parent, const char *name):
    QwtAbstractSlider(Qt::Horizontal, parent)
{
    setName(name);
    initDial();
}
#endif

void QwtDial::initDial()
{
    d_data = new PrivateData;

#if QT_VERSION < 0x040000
    setWFlags(Qt::WNoAutoErase);
158
#endif
pixhawk's avatar
pixhawk committed
159 160 161 162 163 164 165

#if QT_VERSION >= 0x040000
    using namespace Qt;
#endif
    setFocusPolicy(TabFocus);

    QPalette p = palette();
166
    for ( int i = 0; i < QPalette::NColorGroups; i++ ) {
pixhawk's avatar
pixhawk committed
167 168 169 170 171 172
        const QPalette::ColorGroup cg = (QPalette::ColorGroup)i;

        // Base: background color of the circle inside the frame.
        // Foreground: background color of the circle inside the scale

#if QT_VERSION < 0x040000
173 174
        p.setColor(cg, QColorGroup::Foreground,
                   p.color(cg, QColorGroup::Base));
pixhawk's avatar
pixhawk committed
175
#else
176 177
        p.setColor(cg, QPalette::Foreground,
                   p.color(cg, QPalette::Base));
pixhawk's avatar
pixhawk committed
178 179 180 181 182 183 184 185 186 187 188 189
#endif
    }
    setPalette(p);

    d_data->scaleDraw = new QwtDialScaleDraw(this);
    d_data->scaleDraw->setRadius(0);

    setScaleArc(0.0, 360.0); // scale as a full circle
    setRange(0.0, 360.0, 1.0, 10); // degrees as deafult
}

//!  Destructor
190
QwtDial::~QwtDial()
pixhawk's avatar
pixhawk committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204
{
    delete d_data;
}

/*!
  Show/Hide the area outside of the frame
  \param show Show if true, hide if false

  \sa hasVisibleBackground(), setMask()
  \warning When QwtDial is a toplevel widget the window
           border might disappear too.
*/
void QwtDial::showBackground(bool show)
{
205
    if ( d_data->visibleBackground != show ) {
pixhawk's avatar
pixhawk committed
206 207 208 209 210 211 212 213 214 215
        d_data->visibleBackground = show;
        updateMask();
    }
}

/*!
  true when the area outside of the frame is visible

  \sa showBackground(), setMask()
*/
216 217 218
bool QwtDial::hasVisibleBackground() const
{
    return d_data->visibleBackground;
pixhawk's avatar
pixhawk committed
219 220 221 222 223 224 225 226 227
}

/*!
  Sets the frame shadow value from the frame style.
  \param shadow Frame shadow
  \sa setLineWidth(), QFrame::setFrameShadow()
*/
void QwtDial::setFrameShadow(Shadow shadow)
{
228
    if ( shadow != d_data->frameShadow ) {
pixhawk's avatar
pixhawk committed
229 230 231 232 233 234 235 236 237 238
        d_data->frameShadow = shadow;
        if ( lineWidth() > 0 )
            update();
    }
}

/*!
  \return Frame shadow
  /sa setFrameShadow(), lineWidth(), QFrame::frameShadow
*/
239 240 241
QwtDial::Shadow QwtDial::frameShadow() const
{
    return d_data->frameShadow;
pixhawk's avatar
pixhawk committed
242 243 244 245 246 247 248 249 250 251 252 253 254
}

/*!
  Sets the line width

  \param lineWidth Line width
  \sa setFrameShadow()
*/
void QwtDial::setLineWidth(int lineWidth)
{
    if ( lineWidth < 0 )
        lineWidth = 0;

255
    if ( d_data->lineWidth != lineWidth ) {
pixhawk's avatar
pixhawk committed
256 257 258 259 260 261 262 263 264
        d_data->lineWidth = lineWidth;
        update();
    }
}

/*!
  \return Line width of the frame
  \sa setLineWidth(), frameShadow(), lineWidth()
*/
265 266 267
int QwtDial::lineWidth() const
{
    return d_data->lineWidth;
pixhawk's avatar
pixhawk committed
268 269 270 271 272 273 274 275 276 277 278
}

/*!
  \return bounding rect of the circle inside the frame
  \sa setLineWidth(), scaleContentsRect(), boundingRect()
*/
QRect QwtDial::contentsRect() const
{
    const int lw = lineWidth();

    QRect r = boundingRect();
279 280 281
    if ( lw > 0 ) {
        r.setRect(r.x() + lw, r.y() + lw,
                  r.width() - 2 * lw, r.height() - 2 * lw);
pixhawk's avatar
pixhawk committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    }
    return r;
}

/*!
  \return bounding rect of the dial including the frame
  \sa setLineWidth(), scaleContentsRect(), contentsRect()
*/
QRect QwtDial::boundingRect() const
{
    const int radius = qwtMin(width(), height()) / 2;

    QRect r(0, 0, 2 * radius, 2 * radius);
    r.moveCenter(rect().center());
    return r;
}

/*!
  \return rect inside the scale
  \sa setLineWidth(), boundingRect(), contentsRect()
*/
QRect QwtDial::scaleContentsRect() const
{
#if QT_VERSION < 0x040000
    const QPen scalePen(colorGroup().text(), 0, Qt::NoPen);
#else
    const QPen scalePen(palette().text(), 0, Qt::NoPen);
#endif

    int scaleDist = 0;
312
    if ( d_data->scaleDraw ) {
pixhawk's avatar
pixhawk committed
313 314 315 316 317 318
        scaleDist = d_data->scaleDraw->extent(scalePen, font());
        scaleDist++; // margin
    }

    const QRect rect = contentsRect();
    return QRect(rect.x() + scaleDist, rect.y() + scaleDist,
319
                 rect.width() - 2 * scaleDist, rect.height() - 2 * scaleDist);
pixhawk's avatar
pixhawk committed
320 321 322 323
}

/*!
  \brief Change the mode of the meter.
324 325
  \param mode New mode

pixhawk's avatar
pixhawk committed
326 327 328 329 330 331
  The value of the meter is indicated by the difference
  between north of the scale and the direction of the needle.
  In case of QwtDial::RotateNeedle north is pointing
  to the origin() and the needle is rotating, in case of
  QwtDial::RotateScale, the needle points to origin()
  and the scale is rotating.
332

pixhawk's avatar
pixhawk committed
333 334 335
  The default mode is QwtDial::RotateNeedle.

  \sa mode(), setValue(), setOrigin()
336
*/
pixhawk's avatar
pixhawk committed
337
void QwtDial::setMode(Mode mode)
338 339
{
    if ( mode != d_data->mode ) {
pixhawk's avatar
pixhawk committed
340
        d_data->mode = mode;
341
        update();
pixhawk's avatar
pixhawk committed
342
    }
343
}
pixhawk's avatar
pixhawk committed
344

345
/*!
pixhawk's avatar
pixhawk committed
346
  \return mode of the dial.
347

pixhawk's avatar
pixhawk committed
348 349 350 351 352 353
  The value of the dial is indicated by the difference
  between the origin and the direction of the needle.
  In case of QwtDial::RotateNeedle the scale arc is fixed
  to the origin() and the needle is rotating, in case of
  QwtDial::RotateScale, the needle points to origin()
  and the scale is rotating.
354

pixhawk's avatar
pixhawk committed
355 356 357 358 359 360 361 362 363
  The default mode is QwtDial::RotateNeedle.

  \sa setMode(), origin(), setScaleArc(), value()
*/
QwtDial::Mode QwtDial::mode() const
{
    return d_data->mode;
}

364 365
/*!
    Sets whether it is possible to step the value from the highest value to
pixhawk's avatar
pixhawk committed
366 367 368 369 370 371
    the lowest value and vice versa to on.

    \param wrapping en/disables wrapping

    \sa wrapping(), QwtDoubleRange::periodic()
    \note The meaning of wrapping is like the wrapping property of QSpinBox,
372
          but not like it is used in QDial.
pixhawk's avatar
pixhawk committed
373 374 375 376
*/
void QwtDial::setWrapping(bool wrapping)
{
    setPeriodic(wrapping);
377
}
pixhawk's avatar
pixhawk committed
378

379 380 381
/*!
    wrapping() holds whether it is possible to step the value from the
    highest value to the lowest value and vice versa.
pixhawk's avatar
pixhawk committed
382 383 384

    \sa setWrapping(), QwtDoubleRange::setPeriodic()
    \note The meaning of wrapping is like the wrapping property of QSpinBox,
385 386
          but not like it is used in QDial.
*/
pixhawk's avatar
pixhawk committed
387 388 389 390 391
bool QwtDial::wrapping() const
{
    return periodic();
}

392
/*!
pixhawk's avatar
pixhawk committed
393 394 395 396 397 398 399 400 401 402 403
   Resize the dial widget
   \param e Resize event
*/
void QwtDial::resizeEvent(QResizeEvent *e)
{
    QWidget::resizeEvent(e);

    if ( !hasVisibleBackground() )
        updateMask();
}

404 405
/*!
   Paint the dial
pixhawk's avatar
pixhawk committed
406 407 408 409 410
   \param e Paint event
*/
void QwtDial::paintEvent(QPaintEvent *e)
{
    const QRect &ur = e->rect();
411
    if ( ur.isValid() ) {
pixhawk's avatar
pixhawk committed
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
#if QT_VERSION < 0x040000
        QwtPaintBuffer paintBuffer(this, ur);
        QPainter &painter = *paintBuffer.painter();
#else
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing, true);
#endif

        painter.save();
        drawContents(&painter);
        painter.restore();

        painter.save();
        drawFrame(&painter);
        painter.restore();

        if ( hasFocus() )
            drawFocusIndicator(&painter);
    }
}

/*!
  Draw a dotted round circle, if !isReadOnly()

  \param painter Painter
*/
void QwtDial::drawFocusIndicator(QPainter *painter) const
{
440
    if ( !isReadOnly() ) {
pixhawk's avatar
pixhawk committed
441 442 443
        QRect focusRect = contentsRect();

        const int margin = 2;
444
        focusRect.setRect(
pixhawk's avatar
pixhawk committed
445 446 447 448 449 450 451 452 453 454
            focusRect.x() + margin,
            focusRect.y() + margin,
            focusRect.width() - 2 * margin,
            focusRect.height() - 2 * margin);

#if QT_VERSION < 0x040000
        QColor color = colorGroup().color(QColorGroup::Base);
#else
        QColor color = palette().color(QPalette::Base);
#endif
455
        if (color.isValid()) {
pixhawk's avatar
pixhawk committed
456 457 458 459 460 461 462 463 464
            const QColor gray(Qt::gray);

            int h, s, v;
#if QT_VERSION < 0x040000
            color.hsv(&h, &s, &v);
#else
            color.getHsv(&h, &s, &v);
#endif
            color = (v > 128) ? gray.dark(120) : gray.light(120);
465
        } else
pixhawk's avatar
pixhawk committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
            color = Qt::darkGray;

        painter->save();
        painter->setBrush(Qt::NoBrush);
        painter->setPen(QPen(color, 0, Qt::DotLine));
        painter->drawEllipse(focusRect);
        painter->restore();
    }
}

/*!
  Draw the frame around the dial

  \param painter Painter
  \sa lineWidth(), frameShadow()
*/
void QwtDial::drawFrame(QPainter *painter)
{
    const int lw = lineWidth();
    const int off = (lw + 1) % 2;

    QRect r = boundingRect();
    r.setRect(r.x() + lw / 2 - off, r.y() + lw / 2 - off,
489
              r.width() - lw + off + 1, r.height() - lw + off + 1);
pixhawk's avatar
pixhawk committed
490 491 492 493 494 495 496 497 498
#if QT_VERSION >= 0x040000
#ifdef __GNUC__
#endif
    r.setX(r.x() + 1);
    r.setY(r.y() + 1);
    r.setWidth(r.width() - 2);
    r.setHeight(r.height() - 2);
#endif

499 500 501
    if ( lw > 0 ) {
        switch(d_data->frameShadow) {
        case QwtDial::Raised:
pixhawk's avatar
pixhawk committed
502
#if QT_VERSION < 0x040000
503 504
            QwtPainter::drawRoundFrame(painter, r,
                                       lw, colorGroup(), false);
pixhawk's avatar
pixhawk committed
505
#else
506 507
            QwtPainter::drawRoundFrame(painter, r,
                                       lw, palette(), false);
pixhawk's avatar
pixhawk committed
508
#endif
509 510
            break;
        case QwtDial::Sunken:
pixhawk's avatar
pixhawk committed
511
#if QT_VERSION < 0x040000
512 513
            QwtPainter::drawRoundFrame(painter, r,
                                       lw, colorGroup(), true);
pixhawk's avatar
pixhawk committed
514
#else
515 516
            QwtPainter::drawRoundFrame(painter, r,
                                       lw, palette(), true);
pixhawk's avatar
pixhawk committed
517
#endif
518 519 520 521 522 523 524 525
            break;
        default: { // Plain
            painter->save();
            painter->setPen(QPen(Qt::black, lw));
            painter->setBrush(Qt::NoBrush);
            painter->drawEllipse(r);
            painter->restore();
        }
pixhawk's avatar
pixhawk committed
526 527 528 529 530 531
        }
    }
}

/*!
  \brief Draw the contents inside the frame
532

pixhawk's avatar
pixhawk committed
533 534 535 536 537 538 539 540 541 542 543
  QColorGroup::Background is the background color outside of the frame.
  QColorGroup::Base is the background color inside the frame.
  QColorGroup::Foreground is the background color inside the scale.

  \param painter Painter
  \sa boundingRect(), contentsRect(),
    scaleContentsRect(), QWidget::setPalette
*/
void QwtDial::drawContents(QPainter *painter) const
{
#if QT_VERSION < 0x040000
544 545
    if ( backgroundMode() == Qt::NoBackground ||
            colorGroup().brush(QColorGroup::Base) !=
pixhawk's avatar
pixhawk committed
546 547 548
            colorGroup().brush(QColorGroup::Background) )
#else
    if ( testAttribute(Qt::WA_NoSystemBackground) ||
549
            palette().brush(QPalette::Base) !=
pixhawk's avatar
pixhawk committed
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
            palette().brush(QPalette::Background) )
#endif
    {

        const QRect br = boundingRect();

        painter->save();
        painter->setPen(Qt::NoPen);

#if QT_VERSION < 0x040000
        painter->setBrush(colorGroup().brush(QColorGroup::Base));
#else
        painter->setBrush(palette().brush(QPalette::Base));
#endif

        painter->drawEllipse(br);
        painter->restore();
    }


    const QRect insideScaleRect = scaleContentsRect();
#if QT_VERSION < 0x040000
    if ( colorGroup().brush(QColorGroup::Foreground) !=
573
            colorGroup().brush(QColorGroup::Base) )
pixhawk's avatar
pixhawk committed
574 575
#else
    if ( palette().brush(QPalette::Foreground) !=
576
            palette().brush(QPalette::Base) )
pixhawk's avatar
pixhawk committed
577 578 579 580 581 582 583 584 585 586 587 588
#endif
    {
        painter->save();
        painter->setPen(Qt::NoPen);

#if QT_VERSION < 0x040000
        painter->setBrush(colorGroup().brush(QColorGroup::Foreground));
#else
        painter->setBrush(palette().brush(QPalette::Foreground));
#endif

        painter->drawEllipse(insideScaleRect.x() - 1, insideScaleRect.y() - 1,
589
                             insideScaleRect.width(), insideScaleRect.height() );
pixhawk's avatar
pixhawk committed
590 591 592 593 594 595 596 597 598 599 600 601 602

        painter->restore();
    }

    const QPoint center = insideScaleRect.center();
    const int radius = insideScaleRect.width() / 2;

    painter->save();
    drawScaleContents(painter, center, radius);
    painter->restore();

    double direction = d_data->origin;

603
    if (isValid()) {
pixhawk's avatar
pixhawk committed
604
        direction = d_data->origin + d_data->minScaleArc;
605 606
        if ( maxValue() > minValue() && d_data->maxScaleArc > d_data->minScaleArc ) {
            const double ratio =
pixhawk's avatar
pixhawk committed
607 608 609 610 611 612 613 614 615
                (value() - minValue()) / (maxValue() - minValue());
            direction += ratio * (d_data->maxScaleArc - d_data->minScaleArc);
        }

        if ( direction >= 360.0 )
            direction -= 360.0;
    }

    double origin = d_data->origin;
616
    if ( mode() == RotateScale ) {
pixhawk's avatar
pixhawk committed
617 618 619 620 621 622 623 624
        origin -= direction - d_data->origin;
        direction = d_data->origin;
    }

    painter->save();
    drawScale(painter, center, radius, origin, d_data->minScaleArc, d_data->maxScaleArc);
    painter->restore();

625
    if ( isValid() ) {
pixhawk's avatar
pixhawk committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
        QPalette::ColorGroup cg;
        if ( isEnabled() )
            cg = hasFocus() ? QPalette::Active : QPalette::Inactive;
        else
            cg = QPalette::Disabled;

        painter->save();
        drawNeedle(painter, center, radius, direction, cg);
        painter->restore();
    }
}

/*!
  Draw the needle

  \param painter Painter
  \param center Center of the dial
  \param radius Length for the needle
  \param direction Direction of the needle in degrees, counter clockwise
  \param cg ColorGroup
*/
647 648
void QwtDial::drawNeedle(QPainter *painter, const QPoint &center,
                         int radius, double direction, QPalette::ColorGroup cg) const
pixhawk's avatar
pixhawk committed
649
{
650
    if ( d_data->needle ) {
pixhawk's avatar
pixhawk committed
651 652 653 654 655 656 657 658 659 660 661 662
        direction = 360.0 - direction; // counter clockwise
        d_data->needle->draw(painter, center, radius, direction, cg);
    }
}

/*!
  Draw the scale

  \param painter Painter
  \param center Center of the dial
  \param radius Radius of the scale
  \param origin Origin of the scale
663 664 665
  \param minArc Minimum of the arc
  \param maxArc Minimum of the arc

pixhawk's avatar
pixhawk committed
666 667 668
  \sa QwtAbstractScaleDraw::setAngleRange
*/
void QwtDial::drawScale(QPainter *painter, const QPoint &center,
669
                        int radius, double origin, double minArc, double maxArc) const
pixhawk's avatar
pixhawk committed
670 671 672 673 674 675 676 677 678 679 680 681 682
{
    if ( d_data->scaleDraw == NULL )
        return;

    origin -= 270.0; // hardcoded origin of QwtScaleDraw

    double angle = maxArc - minArc;
    if ( angle > 360.0 )
        angle = fmod(angle, 360.0);

    minArc += origin;
    if ( minArc < -360.0 )
        minArc = fmod(minArc, 360.0);
683

pixhawk's avatar
pixhawk committed
684
    maxArc = minArc + angle;
685
    if ( maxArc > 360.0 ) {
pixhawk's avatar
pixhawk committed
686 687 688 689 690
        // QwtAbstractScaleDraw::setAngleRange accepts only values
        // in the range [-360.0..360.0]
        minArc -= 360.0;
        maxArc -= 360.0;
    }
691

pixhawk's avatar
pixhawk committed
692 693 694 695 696 697 698 699 700 701 702 703
    painter->setFont(font());

    d_data->scaleDraw->setAngleRange(minArc, maxArc);
    d_data->scaleDraw->setRadius(radius);
    d_data->scaleDraw->moveCenter(center);

#if QT_VERSION < 0x040000
    QColorGroup cg = colorGroup();

    const QColor textColor = cg.color(QColorGroup::Text);
    cg.setColor(QColorGroup::Foreground, textColor);
    painter->setPen(QPen(textColor, d_data->scaleDraw->penWidth()));
704

pixhawk's avatar
pixhawk committed
705 706 707 708 709 710
    d_data->scaleDraw->draw(painter, cg);
#else
    QPalette pal = palette();

    const QColor textColor = pal.color(QPalette::Text);
    pal.setColor(QPalette::Foreground, textColor); //ticks, backbone
711

pixhawk's avatar
pixhawk committed
712 713 714 715 716 717
    painter->setPen(QPen(textColor, d_data->scaleDraw->penWidth()));

    d_data->scaleDraw->draw(painter, pal);
#endif
}

718 719
void QwtDial::drawScaleContents(QPainter *,
                                const QPoint &, int) const
pixhawk's avatar
pixhawk committed
720 721 722 723 724 725 726
{
    // empty default implementation
}

/*!
  Set a needle for the dial

727
  Qwt is missing a set of good looking needles.
pixhawk's avatar
pixhawk committed
728 729 730 731 732 733 734 735
  Contributions are very welcome.

  \param needle Needle
  \warning The needle will be deleted, when a different needle is
    set or in ~QwtDial()
*/
void QwtDial::setNeedle(QwtDialNeedle *needle)
{
736
    if ( needle != d_data->needle ) {
pixhawk's avatar
pixhawk committed
737 738 739 740 741 742 743 744
        if ( d_data->needle )
            delete d_data->needle;

        d_data->needle = needle;
        update();
    }
}

745
/*!
pixhawk's avatar
pixhawk committed
746 747 748
  \return needle
  \sa setNeedle()
*/
749 750 751
const QwtDialNeedle *QwtDial::needle() const
{
    return d_data->needle;
pixhawk's avatar
pixhawk committed
752 753
}

754
/*!
pixhawk's avatar
pixhawk committed
755 756 757
  \return needle
  \sa setNeedle()
*/
758 759 760
QwtDialNeedle *QwtDial::needle()
{
    return d_data->needle;
pixhawk's avatar
pixhawk committed
761 762 763 764 765 766 767 768
}

//! QwtDoubleRange update hook
void QwtDial::rangeChange()
{
    updateScale();
}

769
/*!
pixhawk's avatar
pixhawk committed
770 771 772 773 774
  Update the scale with the current attributes
  \sa setScale()
*/
void QwtDial::updateScale()
{
775
    if ( d_data->scaleDraw ) {
pixhawk's avatar
pixhawk committed
776 777 778
        QwtLinearScaleEngine scaleEngine;

        const QwtScaleDiv scaleDiv = scaleEngine.divideScale(
779 780
                                         minValue(), maxValue(),
                                         d_data->maxMajIntv, d_data->maxMinIntv, d_data->scaleStep);
pixhawk's avatar
pixhawk committed
781 782 783 784 785 786 787

        d_data->scaleDraw->setTransformation(scaleEngine.transformation());
        d_data->scaleDraw->setScaleDiv(scaleDiv);
    }
}

//! Return the scale draw
788 789 790
QwtDialScaleDraw *QwtDial::scaleDraw()
{
    return d_data->scaleDraw;
pixhawk's avatar
pixhawk committed
791 792 793
}

//! Return the scale draw
794 795 796
const QwtDialScaleDraw *QwtDial::scaleDraw() const
{
    return d_data->scaleDraw;
pixhawk's avatar
pixhawk committed
797 798 799 800 801 802 803 804 805 806
}

/*!
  Set an individual scale draw

  \param scaleDraw Scale draw
  \warning The previous scale draw is deleted
*/
void QwtDial::setScaleDraw(QwtDialScaleDraw *scaleDraw)
{
807
    if ( scaleDraw != d_data->scaleDraw ) {
pixhawk's avatar
pixhawk committed
808 809
        if ( d_data->scaleDraw )
            delete d_data->scaleDraw;
810

pixhawk's avatar
pixhawk committed
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
        d_data->scaleDraw = scaleDraw;
        updateScale();
        update();
    }
}

/*!
  Change the intervals of the scale
  \sa QwtAbstractScaleDraw::setScale
*/
void QwtDial::setScale(int maxMajIntv, int maxMinIntv, double step)
{
    d_data->maxMajIntv = maxMajIntv;
    d_data->maxMinIntv = maxMinIntv;
    d_data->scaleStep = step;

    updateScale();
}

/*!
831
  A wrapper method for accessing the scale draw.
pixhawk's avatar
pixhawk committed
832 833 834 835 836 837 838 839 840

  - options == 0\n
    No visible scale: setScaleDraw(NULL)
  - options & ScaleBackbone\n
    En/disable the backbone of the scale.
  - options & ScaleTicks\n
    En/disable the ticks of the scale.
  - options & ScaleLabel\n
    En/disable scale labels
841 842

  \sa QwtAbstractScaleDraw::enableComponent
pixhawk's avatar
pixhawk committed
843 844 845 846 847 848 849 850 851 852
*/
void QwtDial::setScaleOptions(int options)
{
    if ( options == 0 )
        setScaleDraw(NULL);

    QwtDialScaleDraw *sd = d_data->scaleDraw;
    if ( sd == NULL )
        return;

853 854 855 856 857
    sd->enableComponent(QwtAbstractScaleDraw::Backbone,
                        options & ScaleBackbone);

    sd->enableComponent(QwtAbstractScaleDraw::Ticks,
                        options & ScaleTicks);
pixhawk's avatar
pixhawk committed
858

859 860
    sd->enableComponent(QwtAbstractScaleDraw::Labels,
                        options & ScaleLabel);
pixhawk's avatar
pixhawk committed
861 862 863
}

//! See: QwtAbstractScaleDraw::setTickLength, QwtDialScaleDraw::setPenWidth
864 865
void QwtDial::setScaleTicks(int minLen, int medLen,
                            int majLen, int penWidth)
pixhawk's avatar
pixhawk committed
866 867
{
    QwtDialScaleDraw *sd = d_data->scaleDraw;
868
    if ( sd ) {
pixhawk's avatar
pixhawk committed
869 870 871 872 873 874 875 876 877 878 879
        sd->setTickLength(QwtScaleDiv::MinorTick, minLen);
        sd->setTickLength(QwtScaleDiv::MediumTick, medLen);
        sd->setTickLength(QwtScaleDiv::MajorTick, majLen);
        sd->setPenWidth(penWidth);
    }
}

/*!
   Find the label for a value

   \param value Value
880
   \return label
pixhawk's avatar
pixhawk committed
881 882 883 884 885 886 887 888 889 890 891 892
*/
QwtText QwtDial::scaleLabel(double value) const
{
#if 1
    if ( value == -0 )
        value = 0;
#endif

    return QString::number(value);
}

//! \return Lower limit of the scale arc
893 894 895
double QwtDial::minScaleArc() const
{
    return d_data->minScaleArc;
pixhawk's avatar
pixhawk committed
896 897 898
}

//! \return Upper limit of the scale arc
899 900 901
double QwtDial::maxScaleArc() const
{
    return d_data->maxScaleArc;
pixhawk's avatar
pixhawk committed
902 903 904
}

/*!
905 906
  \brief Change the origin

pixhawk's avatar
pixhawk committed
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
  The origin is the angle where scale and needle is relative to.

  \param origin New origin
  \sa origin()
*/
void QwtDial::setOrigin(double origin)
{
    d_data->origin = origin;
    update();
}

/*!
  The origin is the angle where scale and needle is relative to.

  \return Origin of the dial
  \sa setOrigin()
*/
double QwtDial::origin() const
{
    return d_data->origin;
}

/*!
  Change the arc of the scale

  \param minArc Lower limit
  \param maxArc Upper limit
*/
void QwtDial::setScaleArc(double minArc, double maxArc)
{
    if ( minArc != 360.0 && minArc != -360.0 )
        minArc = fmod(minArc, 360.0);
    if ( maxArc != 360.0 && maxArc != -360.0 )
        maxArc = fmod(maxArc, 360.0);

    d_data->minScaleArc = qwtMin(minArc, maxArc);
    d_data->maxScaleArc = qwtMax(minArc, maxArc);
    if ( d_data->maxScaleArc - d_data->minScaleArc > 360.0 )
        d_data->maxScaleArc = d_data->minScaleArc + 360.0;
946

pixhawk's avatar
pixhawk committed
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
    update();
}

//! QwtDoubleRange update hook
void QwtDial::valueChange()
{
    update();
    QwtAbstractSlider::valueChange();
}

/*!
  \return Size hint
*/
QSize QwtDial::sizeHint() const
{
    int sh = 0;
    if ( d_data->scaleDraw )
        sh = d_data->scaleDraw->extent( QPen(), font() );

    const int d = 6 * sh + 2 * lineWidth();
967

pixhawk's avatar
pixhawk committed
968 969 970
    return QSize( d, d );
}

971
/*!
pixhawk's avatar
pixhawk committed
972 973 974
  \brief Return a minimum size hint
  \warning The return value of QwtDial::minimumSizeHint() depends on the
           font and the scale.
975
*/
pixhawk's avatar
pixhawk committed
976
QSize QwtDial::minimumSizeHint() const
977
{
pixhawk's avatar
pixhawk committed
978 979 980 981 982
    int sh = 0;
    if ( d_data->scaleDraw )
        sh = d_data->scaleDraw->extent(QPen(), font() );

    const int d = 3 * sh + 2 * lineWidth();
983

pixhawk's avatar
pixhawk committed
984 985 986 987 988 989 990 991 992 993
    return QSize( d, d );
}

static double line2Radians(const QPoint &p1, const QPoint &p2)
{
    const QPoint p = p2 - p1;

    double angle;
    if ( p.x() == 0 )
        angle = ( p.y() <= 0 ) ? M_PI_2 : 3 * M_PI_2;
994
    else {
pixhawk's avatar
pixhawk committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
        angle = atan(double(-p.y()) / double(p.x()));
        if ( p.x() < 0 )
            angle += M_PI;
        if ( angle < 0.0 )
            angle += 2 * M_PI;
    }
    return 360.0 - angle * 180.0 / M_PI;
}

/*!
  Find the value for a given position

  \param pos Position
  \return Value
*/
double QwtDial::getValue(const QPoint &pos)
{
    if ( d_data->maxScaleArc == d_data->minScaleArc || maxValue() == minValue() )
        return minValue();

    double dir = line2Radians(rect().center(), pos) - d_data->origin;
    if ( dir < 0.0 )
        dir += 360.0;

    if ( mode() == RotateScale )
        dir = 360.0 - dir;

    // The position might be in the area that is outside the scale arc.
    // We need the range of the scale if it was a complete circle.

1025 1026
    const double completeCircle = 360.0 / (d_data->maxScaleArc - d_data->minScaleArc)
                                  * (maxValue() - minValue());
pixhawk's avatar
pixhawk committed
1027 1028 1029

    double posValue = minValue() + completeCircle * dir / 360.0;

1030 1031
    if ( scrollMode() == ScrMouse ) {
        if ( d_data->previousDir >= 0.0 ) { // valid direction
pixhawk's avatar
pixhawk committed
1032 1033 1034 1035 1036 1037 1038 1039 1040
            // We have to find out whether the mouse is moving
            // clock or counter clockwise

            bool clockWise = false;

            const double angle = dir - d_data->previousDir;
            if ( (angle >= 0.0 && angle <= 180.0) || angle < -180.0 )
                clockWise = true;

1041 1042
            if ( clockWise ) {
                if ( dir < d_data->previousDir && mouseOffset() > 0.0 ) {
pixhawk's avatar
pixhawk committed
1043 1044 1045 1046
                    // We passed 360 -> 0
                    setMouseOffset(mouseOffset() - completeCircle);
                }

1047 1048
                if ( wrapping() ) {
                    if ( posValue - mouseOffset() > maxValue() ) {
pixhawk's avatar
pixhawk committed
1049 1050 1051 1052 1053
                        // We passed maxValue and the value will be set
                        // to minValue. We have to adjust the mouseOffset.

                        setMouseOffset(posValue - minValue());
                    }
1054
                } else {
pixhawk's avatar
pixhawk committed
1055
                    if ( posValue - mouseOffset() > maxValue() ||
1056
                            value() == maxValue() ) {
pixhawk's avatar
pixhawk committed
1057 1058 1059 1060 1061 1062
                        // We fix the value at maxValue by adjusting
                        // the mouse offset.

                        setMouseOffset(posValue - maxValue());
                    }
                }
1063 1064 1065 1066
            } else {
                if ( dir > d_data->previousDir && mouseOffset() < 0.0 ) {
                    // We passed 0 -> 360
                    setMouseOffset(mouseOffset() + completeCircle);
pixhawk's avatar
pixhawk committed
1067 1068
                }

1069 1070
                if ( wrapping() ) {
                    if ( posValue - mouseOffset() < minValue() ) {
pixhawk's avatar
pixhawk committed
1071 1072 1073 1074 1075
                        // We passed minValue and the value will be set
                        // to maxValue. We have to adjust the mouseOffset.

                        setMouseOffset(posValue - maxValue());
                    }
1076
                } else {
pixhawk's avatar
pixhawk committed
1077
                    if ( posValue - mouseOffset() < minValue() ||
1078
                            value() == minValue() ) {
pixhawk's avatar
pixhawk committed
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
                        // We fix the value at minValue by adjusting
                        // the mouse offset.

                        setMouseOffset(posValue - minValue());
                    }
                }
            }
        }
        d_data->previousDir = dir;
    }

    return posValue;
}

/*!
  \sa QwtAbstractSlider::getScrollMode
*/
void QwtDial::getScrollMode(const QPoint &p, int &scrollMode, int &direction)
{
    direction = 0;
    scrollMode = ScrNone;

    const QRegion region(contentsRect(), QRegion::Ellipse);
1102
    if ( region.contains(p) && p != rect().center() ) {
pixhawk's avatar
pixhawk committed
1103 1104 1105 1106 1107
        scrollMode = ScrMouse;
        d_data->previousDir = -1.0;
    }
}

1108
/*!
pixhawk's avatar
pixhawk committed
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
  Handles key events

  - Key_Down, KeyLeft\n
    Decrement by 1
  - Key_Prior\n
    Decrement by pageSize()
  - Key_Home\n
    Set the value to minValue()

  - Key_Up, KeyRight\n
    Increment by 1
  - Key_Next\n
    Increment by pageSize()
  - Key_End\n
    Set the value to maxValue()

  \sa isReadOnly()
*/
void QwtDial::keyPressEvent(QKeyEvent *e)
{
1129
    if ( isReadOnly() ) {
pixhawk's avatar
pixhawk committed
1130 1131 1132 1133 1134 1135 1136 1137
        e->ignore();
        return;
    }

    if ( !isValid() )
        return;

    double previous = prevValue();
1138 1139 1140 1141 1142
    switch ( e->key() ) {
    case Qt::Key_Down:
    case Qt::Key_Left:
        QwtDoubleRange::incValue(-1);
        break;
pixhawk's avatar
pixhawk committed
1143
#if QT_VERSION < 0x040000
1144
    case Qt::Key_Prior:
pixhawk's avatar
pixhawk committed
1145
#else
1146
    case Qt::Key_PageUp:
pixhawk's avatar
pixhawk committed
1147
#endif
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
        QwtDoubleRange::incValue(-pageSize());
        break;
    case Qt::Key_Home:
        setValue(minValue());
        break;

    case Qt::Key_Up:
    case Qt::Key_Right:
        QwtDoubleRange::incValue(1);
        break;
pixhawk's avatar
pixhawk committed
1158
#if QT_VERSION < 0x040000
1159
    case Qt::Key_Next:
pixhawk's avatar
pixhawk committed
1160
#else
1161
    case Qt::Key_PageDown:
pixhawk's avatar
pixhawk committed
1162
#endif
1163 1164 1165 1166 1167 1168 1169 1170
        QwtDoubleRange::incValue(pageSize());
        break;
    case Qt::Key_End:
        setValue(maxValue());
        break;
    default:
        ;
        e->ignore();
pixhawk's avatar
pixhawk committed
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
    }

    if (value() != previous)
        emit sliderMoved(value());
}

/*!
   \brief Update the mask of the dial

   In case of "hasVisibleBackground() == false", the backgound is
   transparent by a mask.

   \sa showBackground(), hasVisibleBackground()
*/
void QwtDial::updateMask()
{
    if ( d_data->visibleBackground )
        clearMask();
    else
        setMask(QRegion(boundingRect(), QRegion::Ellipse));
}