qwt_wheel.cpp 16 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
/* -*- 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 <qevent.h>
#include <qdrawutil.h>
#include <qpainter.h>
#include <qstyle.h>
#include "qwt_math.h"
#include "qwt_painter.h"
#include "qwt_paint_buffer.h"
#include "qwt_wheel.h"

#define NUM_COLORS 30

class QwtWheel::PrivateData
{
public:
24
    PrivateData() {
pixhawk's avatar
pixhawk committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
        viewAngle = 175.0;
        totalAngle = 360.0;
        tickCnt = 10;
        intBorder = 2;
        borderWidth = 2;
        wheelWidth = 20;
#if QT_VERSION < 0x040000
        allocContext = 0;
#endif
    };

    QRect sliderRect;
    double viewAngle;
    double totalAngle;
    int tickCnt;
    int intBorder;
    int borderWidth;
    int wheelWidth;
#if QT_VERSION < 0x040000
    int allocContext;
#endif
    QColor colors[NUM_COLORS];
};

//! Constructor
50
QwtWheel::QwtWheel(QWidget *parent):
pixhawk's avatar
pixhawk committed
51 52 53 54 55 56
    QwtAbstractSlider(Qt::Horizontal, parent)
{
    initWheel();
}

#if QT_VERSION < 0x040000
57
QwtWheel::QwtWheel(QWidget *parent, const char *name):
pixhawk's avatar
pixhawk committed
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
    QwtAbstractSlider(Qt::Horizontal, parent)
{
    setName(name);
    initWheel();
}
#endif

void QwtWheel::initWheel()
{
    d_data = new PrivateData;

#if QT_VERSION < 0x040000
    setWFlags(Qt::WNoAutoErase);
#endif

    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);

#if QT_VERSION >= 0x040000
    setAttribute(Qt::WA_WState_OwnSizePolicy, false);
#else
    clearWState( WState_OwnSizePolicy );
#endif

    setUpdateTime(50);
}

//! Destructor
85
QwtWheel::~QwtWheel()
pixhawk's avatar
pixhawk committed
86 87 88 89 90 91 92 93 94 95 96
{
#if QT_VERSION < 0x040000
    if ( d_data->allocContext )
        QColor::destroyAllocContext( d_data->allocContext );
#endif
    delete d_data;
}

//! Set up the color array for the background pixmap.
void QwtWheel::setColorArray()
{
97
    if ( !d_data->colors )
pixhawk's avatar
pixhawk committed
98 99 100 101 102 103 104 105 106 107 108
        return;

#if QT_VERSION < 0x040000
    const QColor light = colorGroup().light();
    const QColor dark = colorGroup().dark();
#else
    const QColor light = palette().color(QPalette::Light);
    const QColor dark = palette().color(QPalette::Dark);
#endif

    if ( !d_data->colors[0].isValid() ||
109 110
            d_data->colors[0] != light ||
            d_data->colors[NUM_COLORS - 1] != dark ) {
pixhawk's avatar
pixhawk committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#if QT_VERSION < 0x040000
        if ( d_data->allocContext )
            QColor::destroyAllocContext( d_data->allocContext );

        d_data->allocContext = QColor::enterAllocContext();
#endif

        d_data->colors[0] = light;
        d_data->colors[NUM_COLORS - 1] = dark;

        int dh, ds, dv, lh, ls, lv;
#if QT_VERSION < 0x040000
        d_data->colors[0].rgb(&lh, &ls, &lv);
        d_data->colors[NUM_COLORS - 1].rgb(&dh, &ds, &dv);
#else
        d_data->colors[0].getRgb(&lh, &ls, &lv);
        d_data->colors[NUM_COLORS - 1].getRgb(&dh, &ds, &dv);
#endif

130
        for ( int i = 1; i < NUM_COLORS - 1; ++i ) {
pixhawk's avatar
pixhawk committed
131 132 133
            const double factor = double(i) / double(NUM_COLORS);

            d_data->colors[i].setRgb( lh + int( double(dh - lh) * factor ),
134 135
                                      ls + int( double(ds - ls) * factor ),
                                      lv + int( double(dv - lv) * factor ));
pixhawk's avatar
pixhawk committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        }
#if QT_VERSION < 0x040000
        QColor::leaveAllocContext();
#endif
    }
}

/*!
  \brief Adjust the number of grooves in the wheel's surface.

  The number of grooves is limited to 6 <= cnt <= 50.
  Values outside this range will be clipped.
  The default value is 10.
  \param cnt Number of grooves per 360 degrees
*/
void QwtWheel::setTickCnt(int cnt)
{
    d_data->tickCnt = qwtLim( cnt, 6, 50 );
    update();
}

157
int QwtWheel::tickCnt() const
pixhawk's avatar
pixhawk committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
{
    return d_data->tickCnt;
}

/*!
    \return mass
*/
double QwtWheel::mass() const
{
    return QwtAbstractSlider::mass();
}

/*!
  \brief Set the internal border width of the wheel.

  The internal border must not be smaller than 1
  and is limited in dependence on the wheel's size.
  Values outside the allowed range will be clipped.

  The internal border defaults to 2.
  \param w border width
*/
void QwtWheel::setInternalBorder( int w )
{
    const int d = qwtMin( width(), height() ) / 3;
    w = qwtMin( w, d );
    d_data->intBorder = qwtMax( w, 1 );
    layoutWheel();
}

188
int QwtWheel::internalBorder() const
pixhawk's avatar
pixhawk committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
{
    return d_data->intBorder;
}

//! Draw the Wheel's background gradient
void QwtWheel::drawWheelBackground( QPainter *p, const QRect &r )
{
    p->save();

    //
    // initialize pens
    //
#if QT_VERSION < 0x040000
    const QColor light = colorGroup().light();
    const QColor dark = colorGroup().dark();
#else
    const QColor light = palette().color(QPalette::Light);
    const QColor dark = palette().color(QPalette::Dark);
#endif

    QPen lightPen;
    lightPen.setColor(light);
    lightPen.setWidth(d_data->intBorder);

    QPen darkPen;
    darkPen.setColor(dark);
    darkPen.setWidth(d_data->intBorder);

    setColorArray();

    //
    // initialize auxiliary variables
    //

    const int nFields = NUM_COLORS * 13 / 10;
    const int hiPos = nFields - NUM_COLORS + 1;

226
    if ( orientation() == Qt::Horizontal ) {
pixhawk's avatar
pixhawk committed
227 228 229 230 231 232 233 234
        const int rx = r.x();
        int ry = r.y() + d_data->intBorder;
        const int rh = r.height() - 2* d_data->intBorder;
        const int rw = r.width();
        //
        //  draw shaded background
        //
        int x1 = rx;
235
        for (int i = 1; i < nFields; i++ ) {
pixhawk's avatar
pixhawk committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
            const int x2 = rx + (rw * i) / nFields;
            p->fillRect(x1, ry, x2-x1 + 1 ,rh, d_data->colors[qwtAbs(i-hiPos)]);
            x1 = x2 + 1;
        }
        p->fillRect(x1, ry, rw - (x1 - rx), rh, d_data->colors[NUM_COLORS - 1]);

        //
        // draw internal border
        //
        p->setPen(lightPen);
        ry = r.y() + d_data->intBorder / 2;
        p->drawLine(r.x(), ry, r.x() + r.width() , ry);

        p->setPen(darkPen);
        ry = r.y() + r.height() - (d_data->intBorder - d_data->intBorder / 2);
        p->drawLine(r.x(), ry , r.x() + r.width(), ry);
252
    } else { // Qt::Vertical
pixhawk's avatar
pixhawk committed
253 254 255 256 257 258 259 260 261
        int rx = r.x() + d_data->intBorder;
        const int ry = r.y();
        const int rh = r.height();
        const int rw = r.width() - 2 * d_data->intBorder;

        //
        // draw shaded background
        //
        int y1 = ry;
262
        for ( int i = 1; i < nFields; i++ ) {
pixhawk's avatar
pixhawk committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
            const int y2 = ry + (rh * i) / nFields;
            p->fillRect(rx, y1, rw, y2-y1 + 1, d_data->colors[qwtAbs(i-hiPos)]);
            y1 = y2 + 1;
        }
        p->fillRect(rx, y1, rw, rh - (y1 - ry), d_data->colors[NUM_COLORS - 1]);

        //
        //  draw internal borders
        //
        p->setPen(lightPen);
        rx = r.x() + d_data->intBorder / 2;
        p->drawLine(rx, r.y(), rx, r.y() + r.height());

        p->setPen(darkPen);
        rx = r.x() + r.width() - (d_data->intBorder - d_data->intBorder / 2);
        p->drawLine(rx, r.y(), rx , r.y() + r.height());
    }

    p->restore();
}


/*!
  \brief Set the total angle which the wheel can be turned.

  One full turn of the wheel corresponds to an angle of
  360 degrees. A total angle of n*360 degrees means
  that the wheel has to be turned n times around its axis
  to get from the minimum value to the maximum value.

  The default setting of the total angle is 360 degrees.
  \param angle total angle in degrees
*/
void QwtWheel::setTotalAngle(double angle)
{
    if ( angle < 0.0 )
        angle = 0.0;

    d_data->totalAngle = angle;
    update();
}

305
double QwtWheel::totalAngle() const
pixhawk's avatar
pixhawk committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
{
    return d_data->totalAngle;
}

/*!
  \brief Set the wheel's orientation.
  \param o Orientation. Allowed values are
           Qt::Horizontal and Qt::Vertical.
   Defaults to Qt::Horizontal.
  \sa QwtAbstractSlider::orientation()
*/
void QwtWheel::setOrientation(Qt::Orientation o)
{
    if ( orientation() == o )
        return;

#if QT_VERSION >= 0x040000
    if ( !testAttribute(Qt::WA_WState_OwnSizePolicy) )
#else
325
    if ( !testWState( WState_OwnSizePolicy ) )
pixhawk's avatar
pixhawk committed
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
#endif
    {
        QSizePolicy sp = sizePolicy();
        sp.transpose();
        setSizePolicy(sp);

#if QT_VERSION >= 0x040000
        setAttribute(Qt::WA_WState_OwnSizePolicy, false);
#else
        clearWState( WState_OwnSizePolicy );
#endif
    }

    QwtAbstractSlider::setOrientation(o);
    layoutWheel();
}

/*!
  \brief Specify the visible portion of the wheel.

  You may use this function for fine-tuning the appearance of
  the wheel. The default value is 175 degrees. The value is
  limited from 10 to 175 degrees.
  \param angle Visible angle in degrees
*/
void QwtWheel::setViewAngle(double angle)
{
    d_data->viewAngle = qwtLim( angle, 10.0, 175.0 );
    update();
}

357
double QwtWheel::viewAngle() const
pixhawk's avatar
pixhawk committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
{
    return d_data->viewAngle;
}

/*!
  \brief Redraw the wheel
  \param p painter
  \param r contents rectangle
*/
void QwtWheel::drawWheel( QPainter *p, const QRect &r )
{
    //
    // draw background gradient
    //
    drawWheelBackground( p, r );

    if ( maxValue() == minValue() || d_data->totalAngle == 0.0 )
        return;

#if QT_VERSION < 0x040000
    const QColor light = colorGroup().light();
    const QColor dark = colorGroup().dark();
#else
    const QColor light = palette().color(QPalette::Light);
    const QColor dark = palette().color(QPalette::Dark);
#endif

    const double sign = (minValue() < maxValue()) ? 1.0 : -1.0;
    double cnvFactor = qwtAbs(d_data->totalAngle / (maxValue() - minValue()));
    const double halfIntv = 0.5 * d_data->viewAngle / cnvFactor;
    const double loValue = value() - halfIntv;
    const double hiValue = value() + halfIntv;
    const double tickWidth = 360.0 / double(d_data->tickCnt) / cnvFactor;
    const double sinArc = sin(d_data->viewAngle * M_PI / 360.0);
    cnvFactor *= M_PI / 180.0;


    //
    // draw grooves
    //
398
    if ( orientation() == Qt::Horizontal ) {
pixhawk's avatar
pixhawk committed
399 400 401 402 403 404
        const double halfSize = double(r.width()) * 0.5;

        int l1 = r.y() + d_data->intBorder;
        int l2 = r.y() + r.height() - d_data->intBorder - 1;

        // draw one point over the border if border > 1
405
        if ( d_data->intBorder > 1 ) {
pixhawk's avatar
pixhawk committed
406 407 408 409 410 411 412 413 414 415 416
            l1 --;
            l2 ++;
        }

        const int maxpos = r.x() + r.width() - 2;
        const int minpos = r.x() + 2;

        //
        // draw tick marks
        //
        for ( double tickValue = ceil(loValue / tickWidth) * tickWidth;
417
                tickValue < hiValue; tickValue += tickWidth ) {
pixhawk's avatar
pixhawk committed
418 419 420 421
            //
            //  calculate position
            //
            const int tickPos = r.x() + r.width()
422 423 424
                                - int( halfSize
                                       * (sinArc + sign *  sin((tickValue - value()) * cnvFactor))
                                       / sinArc);
pixhawk's avatar
pixhawk committed
425 426 427
            //
            // draw vertical line
            //
428
            if ( (tickPos <= maxpos) && (tickPos > minpos) ) {
pixhawk's avatar
pixhawk committed
429
                p->setPen(dark);
430
                p->drawLine(tickPos -1 , l1, tickPos - 1,  l2 );
pixhawk's avatar
pixhawk committed
431
                p->setPen(light);
432
                p->drawLine(tickPos, l1, tickPos, l2);
pixhawk's avatar
pixhawk committed
433 434
            }
        }
435
    } else if ( orientation() == Qt::Vertical ) {
pixhawk's avatar
pixhawk committed
436 437 438 439 440
        const double halfSize = double(r.height()) * 0.5;

        int l1 = r.x() + d_data->intBorder;
        int l2 = r.x() + r.width() - d_data->intBorder - 1;

441
        if ( d_data->intBorder > 1 ) {
pixhawk's avatar
pixhawk committed
442 443 444 445 446 447 448 449 450 451 452
            l1--;
            l2++;
        }

        const int maxpos = r.y() + r.height() - 2;
        const int minpos = r.y() + 2;

        //
        // draw tick marks
        //
        for ( double tickValue = ceil(loValue / tickWidth) * tickWidth;
453
                tickValue < hiValue; tickValue += tickWidth ) {
pixhawk's avatar
pixhawk committed
454 455 456 457 458

            //
            // calculate position
            //
            const int tickPos = r.y() + int( halfSize *
459 460
                                             (sinArc + sign * sin((tickValue - value()) * cnvFactor))
                                             / sinArc);
pixhawk's avatar
pixhawk committed
461 462 463 464

            //
            //  draw horizontal line
            //
465
            if ( (tickPos <= maxpos) && (tickPos > minpos) ) {
pixhawk's avatar
pixhawk committed
466
                p->setPen(dark);
467
                p->drawLine(l1, tickPos - 1 ,l2, tickPos - 1);
pixhawk's avatar
pixhawk committed
468
                p->setPen(light);
469
                p->drawLine(l1, tickPos, l2, tickPos);
pixhawk's avatar
pixhawk committed
470 471 472 473 474 475 476 477 478 479 480 481
            }
        }
    }
}


//! Determine the value corresponding to a specified point
double QwtWheel::getValue( const QPoint &p )
{
    // The reference position is arbitrary, but the
    // sign of the offset is important
    int w, dx;
482
    if ( orientation() == Qt::Vertical ) {
pixhawk's avatar
pixhawk committed
483 484
        w = d_data->sliderRect.height();
        dx = d_data->sliderRect.y() - p.y();
485
    } else {
pixhawk's avatar
pixhawk committed
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
        w = d_data->sliderRect.width();
        dx = p.x() - d_data->sliderRect.x();
    }

    // w pixels is an arc of viewAngle degrees,
    // so we convert change in pixels to change in angle
    const double ang = dx * d_data->viewAngle / w;

    // value range maps to totalAngle degrees,
    // so convert the change in angle to a change in value
    const double val = ang * ( maxValue() - minValue() ) / d_data->totalAngle;

    // Note, range clamping and rasterizing to step is automatically
    // handled by QwtAbstractSlider, so we simply return the change in value
    return val;
}

//! Qt Resize Event
void QwtWheel::resizeEvent(QResizeEvent *)
{
    layoutWheel( false );
}

//! Recalculate the slider's geometry and layout based on
//  the current rect and fonts.
//  \param update_geometry  notify the layout system and call update
//         to redraw the scale
void QwtWheel::layoutWheel( bool update_geometry )
{
    const QRect r = this->rect();
    d_data->sliderRect.setRect(r.x() + d_data->borderWidth, r.y() + d_data->borderWidth,
517
                               r.width() - 2*d_data->borderWidth, r.height() - 2*d_data->borderWidth);
pixhawk's avatar
pixhawk committed
518

519
    if ( update_geometry ) {
pixhawk's avatar
pixhawk committed
520 521 522 523 524 525 526 527 528 529
        updateGeometry();
        update();
    }
}

//! Qt Paint Event
void QwtWheel::paintEvent(QPaintEvent *e)
{
    // Use double-buffering
    const QRect &ur = e->rect();
530
    if ( ur.isValid() ) {
pixhawk's avatar
pixhawk committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544
#if QT_VERSION < 0x040000
        QwtPaintBuffer paintBuffer(this, ur);
        draw(paintBuffer.painter(), ur);
#else
        QPainter painter(this);
        draw(&painter, ur);
#endif
    }
}

//! Redraw panel and wheel
void QwtWheel::draw(QPainter *painter, const QRect&)
{
    qDrawShadePanel( painter, rect().x(), rect().y(),
545
                     rect().width(), rect().height(),
pixhawk's avatar
pixhawk committed
546
#if QT_VERSION < 0x040000
547
                     colorGroup(),
pixhawk's avatar
pixhawk committed
548
#else
549
                     palette(),
pixhawk's avatar
pixhawk committed
550
#endif
551
                     true, d_data->borderWidth );
pixhawk's avatar
pixhawk committed
552 553 554 555 556 557 558

    drawWheel( painter, d_data->sliderRect );

    if ( hasFocus() )
        QwtPainter::drawFocusRect(painter, this);
}

559
//! Notify value change
pixhawk's avatar
pixhawk committed
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
void QwtWheel::valueChange()
{
    QwtAbstractSlider::valueChange();
    update();
}


/*!
  \brief Determine the scrolling mode and direction corresponding
         to a specified point
  \param p point
  \param scrollMode scrolling mode
  \param direction direction
*/
void QwtWheel::getScrollMode( const QPoint &p, int &scrollMode, int &direction)
{
    if ( d_data->sliderRect.contains(p) )
        scrollMode = ScrMouse;
    else
        scrollMode = ScrNone;

    direction = 0;
}

/*!
  \brief Set the mass of the wheel

  Assigning a mass turns the wheel into a flywheel.
  \param val the wheel's mass
*/
void QwtWheel::setMass(double val)
{
    QwtAbstractSlider::setMass(val);
}

/*!
  \brief Set the width of the wheel

  Corresponds to the wheel height for horizontal orientation,
  and the wheel width for vertical orientation.
  \param w the wheel's width
*/
void QwtWheel::setWheelWidth(int w)
{
    d_data->wheelWidth = w;
    layoutWheel();
}

/*!
  \return a size hint
*/
QSize QwtWheel::sizeHint() const
{
    return minimumSizeHint();
}

/*!
  \brief Return a minimum size hint
  \warning The return value is based on the wheel width.
*/
QSize QwtWheel::minimumSizeHint() const
{
    QSize sz( 3*d_data->wheelWidth + 2*d_data->borderWidth,
623
              d_data->wheelWidth + 2*d_data->borderWidth );
pixhawk's avatar
pixhawk committed
624 625 626 627 628 629 630 631 632 633 634 635 636
    if ( orientation() != Qt::Horizontal )
        sz.transpose();
    return sz;
}

/*!
  \brief Call update() when the palette changes
*/
void QwtWheel::paletteChange( const QPalette& )
{
    update();
}