qwt_slider.cpp 21.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 41 42 43 44 45 46 47 48 49 50
/* -*- 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
 *****************************************************************************/

// vim: expandtab

#include <math.h>
#include <qevent.h>
#include <qdrawutil.h>
#include <qpainter.h>
#include "qwt_painter.h"
#include "qwt_paint_buffer.h"
#include "qwt_scale_draw.h"
#include "qwt_scale_map.h"
#include "qwt_slider.h"

class QwtSlider::PrivateData
{
public:
    QRect sliderRect;

    int thumbLength;
    int thumbWidth;
    int borderWidth;
    int scaleDist;
    int xMargin;
    int yMargin;

    QwtSlider::ScalePos scalePos;
    QwtSlider::BGSTYLE bgStyle;

    /*
      Scale and values might have different maps. This is
      confusing and I can't see strong arguments for such
      a feature. TODO ...
     */
    QwtScaleMap map; // linear map
    mutable QSize sizeHintCache;
};

/*!
  \brief Constructor
  \param parent parent widget
  \param orientation Orientation of the slider. Can be Qt::Horizontal
         or Qt::Vertical. Defaults to Qt::Horizontal.
51
  \param scalePos Position of the scale.
pixhawk's avatar
pixhawk committed
52 53 54 55 56 57 58
         Defaults to QwtSlider::NoScale.
  \param bgStyle Background style. QwtSlider::BgTrough draws the
         slider button in a trough, QwtSlider::BgSlot draws
         a slot underneath the button. An or-combination of both
         may also be used. The default is QwtSlider::BgTrough.

  QwtSlider enforces valid combinations of its orientation and scale position.
59
  If the combination is invalid, the scale position will be set to NoScale.
pixhawk's avatar
pixhawk committed
60 61 62 63 64
  Valid combinations are:
  - Qt::Horizonal with NoScale, TopScale, or BottomScale;
  - Qt::Vertical with NoScale, LeftScale, or RightScale.
*/
QwtSlider::QwtSlider(QWidget *parent,
65
                     Qt::Orientation orientation, ScalePos scalePos, BGSTYLE bgStyle):
pixhawk's avatar
pixhawk committed
66 67 68 69 70 71 72 73 74
    QwtAbstractSlider(orientation, parent)
{
    initSlider(orientation, scalePos, bgStyle);
}

#if QT_VERSION < 0x040000
/*!
  \brief Constructor

75
  Build a horizontal slider with no scale and BgTrough as
pixhawk's avatar
pixhawk committed
76 77 78 79 80 81 82 83 84 85 86 87 88
  background style

  \param parent parent widget
  \param name Object name
*/
QwtSlider::QwtSlider(QWidget *parent, const char* name):
    QwtAbstractSlider(Qt::Horizontal, parent)
{
    setName(name);
    initSlider(Qt::Horizontal, NoScale, BgTrough);
}
#endif

89 90
void QwtSlider::initSlider(Qt::Orientation orientation,
                           ScalePos scalePos, BGSTYLE bgStyle)
pixhawk's avatar
pixhawk committed
91
{
92
    if (orientation == Qt::Vertical)
pixhawk's avatar
pixhawk committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    else
        setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

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


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

    d_data = new QwtSlider::PrivateData;

    d_data->borderWidth = 2;
    d_data->scaleDist = 4;
    d_data->scalePos = scalePos;
    d_data->xMargin = 0;
    d_data->yMargin = 0;
    d_data->bgStyle = bgStyle;

117
    if (bgStyle == BgSlot) {
pixhawk's avatar
pixhawk committed
118 119
        d_data->thumbLength = 16;
        d_data->thumbWidth = 30;
120
    } else {
pixhawk's avatar
pixhawk committed
121 122 123 124 125 126 127
        d_data->thumbLength = 31;
        d_data->thumbWidth = 16;
    }

    d_data->sliderRect.setRect(0,0,8,8);

    QwtScaleDraw::Alignment align;
128
    if ( orientation == Qt::Vertical ) {
pixhawk's avatar
pixhawk committed
129 130 131 132 133
        // enforce a valid combination of scale position and orientation
        if ((d_data->scalePos == BottomScale) || (d_data->scalePos == TopScale))
            d_data->scalePos = NoScale;
        // adopt the policy of layoutSlider (NoScale lays out like Left)
        if (d_data->scalePos == RightScale)
134
            align = QwtScaleDraw::RightScale;
pixhawk's avatar
pixhawk committed
135
        else
136 137
            align = QwtScaleDraw::LeftScale;
    } else {
pixhawk's avatar
pixhawk committed
138 139 140 141 142
        // enforce a valid combination of scale position and orientation
        if ((d_data->scalePos == LeftScale) || (d_data->scalePos == RightScale))
            d_data->scalePos = NoScale;
        // adopt the policy of layoutSlider (NoScale lays out like Bottom)
        if (d_data->scalePos == TopScale)
143
            align = QwtScaleDraw::TopScale;
pixhawk's avatar
pixhawk committed
144
        else
145
            align = QwtScaleDraw::BottomScale;
pixhawk's avatar
pixhawk committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    }

    scaleDraw()->setAlignment(align);
    scaleDraw()->setLength(100);

    setRange(0.0, 100.0, 1.0);
    setValue(0.0);
}

QwtSlider::~QwtSlider()
{
    delete d_data;
}

/*!
  \brief Set the orientation.
  \param o Orientation. Allowed values are Qt::Horizontal and Qt::Vertical.
163

pixhawk's avatar
pixhawk committed
164 165 166 167
  If the new orientation and the old scale position are an invalid combination,
  the scale position will be set to QwtSlider::NoScale.
  \sa QwtAbstractSlider::orientation()
*/
168
void QwtSlider::setOrientation(Qt::Orientation o)
pixhawk's avatar
pixhawk committed
169 170 171 172
{
    if ( o == orientation() )
        return;

173
    if (o == Qt::Horizontal) {
pixhawk's avatar
pixhawk committed
174 175
        if ((d_data->scalePos == LeftScale) || (d_data->scalePos == RightScale))
            d_data->scalePos = NoScale;
176
    } else { // if (o == Qt::Vertical)
pixhawk's avatar
pixhawk committed
177 178 179 180 181 182 183
        if ((d_data->scalePos == BottomScale) || (d_data->scalePos == TopScale))
            d_data->scalePos = NoScale;
    }

#if QT_VERSION >= 0x040000
    if ( !testAttribute(Qt::WA_WState_OwnSizePolicy) )
#else
184
    if ( !testWState( WState_OwnSizePolicy ) )
pixhawk's avatar
pixhawk committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
#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);
    layoutSlider();
}

/*!
  \brief Change the scale position (and slider orientation).

  \param s Position of the scale.

  A valid combination of scale position and orientation is enforced:
  - if the new scale position is Left or Right, the scale orientation will
    become Qt::Vertical;
  - if the new scale position is Bottom or Top the scale orientation will
    become Qt::Horizontal;
212
  - if the new scale position is QwtSlider::NoScale, the scale
pixhawk's avatar
pixhawk committed
213 214 215 216 217 218 219 220 221
    orientation will not change.
*/
void QwtSlider::setScalePosition(ScalePos s)
{
    if ( d_data->scalePos == s )
        return;

    d_data->scalePos = s;

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    switch(d_data->scalePos) {
    case BottomScale: {
        setOrientation(Qt::Horizontal);
        scaleDraw()->setAlignment(QwtScaleDraw::BottomScale);
        break;
    }
    case TopScale: {
        setOrientation(Qt::Horizontal);
        scaleDraw()->setAlignment(QwtScaleDraw::TopScale);
        break;
    }
    case LeftScale: {
        setOrientation(Qt::Vertical);
        scaleDraw()->setAlignment(QwtScaleDraw::LeftScale);
        break;
    }
    case RightScale: {
        setOrientation(Qt::Vertical);
        scaleDraw()->setAlignment(QwtScaleDraw::RightScale);
        break;
    }
    default: {
        // nothing
    }
pixhawk's avatar
pixhawk committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    }

    layoutSlider();
}

//! Return the scale position.
QwtSlider::ScalePos QwtSlider::scalePosition() const
{
    return d_data->scalePos;
}

/*!
  \brief Change the slider's border width
  \param bd border width
*/
void QwtSlider::setBorderWidth(int bd)
{
    if ( bd < 0 )
        bd = 0;

266
    if ( bd != d_data->borderWidth ) {
pixhawk's avatar
pixhawk committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280
        d_data->borderWidth = bd;
        layoutSlider();
    }
}

/*!
  \brief Set the slider's thumb length
  \param thumbLength new length
*/
void QwtSlider::setThumbLength(int thumbLength)
{
    if ( thumbLength < 8 )
        thumbLength = 8;

281
    if ( thumbLength != d_data->thumbLength ) {
pixhawk's avatar
pixhawk committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295
        d_data->thumbLength = thumbLength;
        layoutSlider();
    }
}

/*!
  \brief Change the width of the thumb
  \param w new width
*/
void QwtSlider::setThumbWidth(int w)
{
    if ( w < 4 )
        w = 4;

296
    if ( d_data->thumbWidth != w ) {
pixhawk's avatar
pixhawk committed
297 298 299 300 301 302 303 304 305 306 307 308
        d_data->thumbWidth = w;
        layoutSlider();
    }
}

/*!
  \brief Set a scale draw

  For changing the labels of the scales, it
  is necessary to derive from QwtScaleDraw and
  overload QwtScaleDraw::label().

309 310
  \param scaleDraw ScaleDraw object, that has to be created with
                   new and will be deleted in ~QwtSlider or the next
pixhawk's avatar
pixhawk committed
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 360 361 362
                   call of setScaleDraw().
*/
void QwtSlider::setScaleDraw(QwtScaleDraw *scaleDraw)
{
    const QwtScaleDraw *previousScaleDraw = this->scaleDraw();
    if ( scaleDraw == NULL || scaleDraw == previousScaleDraw )
        return;

    if ( previousScaleDraw )
        scaleDraw->setAlignment(previousScaleDraw->alignment());

    setAbstractScaleDraw(scaleDraw);
    layoutSlider();
}

/*!
  \return the scale draw of the slider
  \sa setScaleDraw()
*/
const QwtScaleDraw *QwtSlider::scaleDraw() const
{
    return (QwtScaleDraw *)abstractScaleDraw();
}

/*!
  \return the scale draw of the slider
  \sa setScaleDraw()
*/
QwtScaleDraw *QwtSlider::scaleDraw()
{
    return (QwtScaleDraw *)abstractScaleDraw();
}

//! Notify changed scale
void QwtSlider::scaleChange()
{
    layoutSlider();
}


//! Notify change in font
void QwtSlider::fontChange(const QFont &f)
{
    QwtAbstractSlider::fontChange( f );
    layoutSlider();
}

//! Draw the slider into the specified rectangle.
void QwtSlider::drawSlider(QPainter *p, const QRect &r)
{
    QRect cr(r);

363
    if (d_data->bgStyle & BgTrough) {
pixhawk's avatar
pixhawk committed
364
        qDrawShadePanel(p, r.x(), r.y(),
365
                        r.width(), r.height(),
pixhawk's avatar
pixhawk committed
366
#if QT_VERSION < 0x040000
367
                        colorGroup(),
pixhawk's avatar
pixhawk committed
368
#else
369
                        palette(),
pixhawk's avatar
pixhawk committed
370
#endif
371
                        true, d_data->borderWidth,0);
pixhawk's avatar
pixhawk committed
372 373

        cr.setRect(r.x() + d_data->borderWidth,
374 375 376
                   r.y() + d_data->borderWidth,
                   r.width() - 2 * d_data->borderWidth,
                   r.height() - 2 * d_data->borderWidth);
pixhawk's avatar
pixhawk committed
377

378
        p->fillRect(cr.x(), cr.y(), cr.width(), cr.height(),
pixhawk's avatar
pixhawk committed
379
#if QT_VERSION < 0x040000
380
                    colorGroup().brush(QColorGroup::Mid)
pixhawk's avatar
pixhawk committed
381
#else
382
                    palette().brush(QPalette::Mid)
pixhawk's avatar
pixhawk committed
383
#endif
384
                   );
pixhawk's avatar
pixhawk committed
385 386
    }

387
    if ( d_data->bgStyle & BgSlot) {
pixhawk's avatar
pixhawk committed
388 389 390 391 392 393
        int ws = 4;
        int ds = d_data->thumbLength / 2 - 4;
        if ( ds < 1 )
            ds = 1;

        QRect rSlot;
394
        if (orientation() == Qt::Horizontal) {
pixhawk's avatar
pixhawk committed
395 396
            if ( cr.height() & 1 )
                ws++;
397 398 399 400
            rSlot = QRect(cr.x() + ds,
                          cr.y() + (cr.height() - ws) / 2,
                          cr.width() - 2 * ds, ws);
        } else {
pixhawk's avatar
pixhawk committed
401 402
            if ( cr.width() & 1 )
                ws++;
403 404 405
            rSlot = QRect(cr.x() + (cr.width() - ws) / 2,
                          cr.y() + ds,
                          ws, cr.height() - 2 * ds);
pixhawk's avatar
pixhawk committed
406 407 408
        }
        p->fillRect(rSlot.x(), rSlot.y(), rSlot.width(), rSlot.height(),
#if QT_VERSION < 0x040000
409
                    colorGroup().brush(QColorGroup::Dark)
pixhawk's avatar
pixhawk committed
410
#else
411
                    palette().brush(QPalette::Dark)
pixhawk's avatar
pixhawk committed
412
#endif
413
                   );
pixhawk's avatar
pixhawk committed
414
        qDrawShadePanel(p, rSlot.x(), rSlot.y(),
415
                        rSlot.width(), rSlot.height(),
pixhawk's avatar
pixhawk committed
416
#if QT_VERSION < 0x040000
417
                        colorGroup(),
pixhawk's avatar
pixhawk committed
418
#else
419
                        palette(),
pixhawk's avatar
pixhawk committed
420
#endif
421
                        true, 1 ,0);
pixhawk's avatar
pixhawk committed
422 423 424 425 426 427 428 429 430 431 432

    }

    if ( isValid() )
        drawThumb(p, cr, xyPosition(value()));
}

//! Draw the thumb at a position
void QwtSlider::drawThumb(QPainter *p, const QRect &sliderRect, int pos)
{
    pos++; // shade line points one pixel below
433 434 435
    if (orientation() == Qt::Horizontal) {
        qDrawShadePanel(p, pos - d_data->thumbLength / 2,
                        sliderRect.y(), d_data->thumbLength, sliderRect.height(),
pixhawk's avatar
pixhawk committed
436
#if QT_VERSION < 0x040000
437
                        colorGroup(),
pixhawk's avatar
pixhawk committed
438
#else
439
                        palette(),
pixhawk's avatar
pixhawk committed
440
#endif
441
                        false, d_data->borderWidth,
pixhawk's avatar
pixhawk committed
442
#if QT_VERSION < 0x040000
443
                        &colorGroup().brush(QColorGroup::Button)
pixhawk's avatar
pixhawk committed
444
#else
445
                        &palette().brush(QPalette::Button)
pixhawk's avatar
pixhawk committed
446
#endif
447
                       );
pixhawk's avatar
pixhawk committed
448

449 450
        qDrawShadeLine(p, pos, sliderRect.y(),
                       pos, sliderRect.y() + sliderRect.height() - 2,
pixhawk's avatar
pixhawk committed
451
#if QT_VERSION < 0x040000
452
                       colorGroup(),
pixhawk's avatar
pixhawk committed
453
#else
454
                       palette(),
pixhawk's avatar
pixhawk committed
455
#endif
456 457 458 459
                       true, 1);
    } else { // Vertical
        qDrawShadePanel(p,sliderRect.x(), pos - d_data->thumbLength / 2,
                        sliderRect.width(), d_data->thumbLength,
pixhawk's avatar
pixhawk committed
460
#if QT_VERSION < 0x040000
461
                        colorGroup(),
pixhawk's avatar
pixhawk committed
462
#else
463
                        palette(),
pixhawk's avatar
pixhawk committed
464
#endif
465
                        false, d_data->borderWidth,
pixhawk's avatar
pixhawk committed
466
#if QT_VERSION < 0x040000
467
                        &colorGroup().brush(QColorGroup::Button)
pixhawk's avatar
pixhawk committed
468
#else
469
                        &palette().brush(QPalette::Button)
pixhawk's avatar
pixhawk committed
470
#endif
471
                       );
pixhawk's avatar
pixhawk committed
472 473

        qDrawShadeLine(p, sliderRect.x(), pos,
474
                       sliderRect.x() + sliderRect.width() - 2, pos,
pixhawk's avatar
pixhawk committed
475
#if QT_VERSION < 0x040000
476
                       colorGroup(),
pixhawk's avatar
pixhawk committed
477
#else
478
                       palette(),
pixhawk's avatar
pixhawk committed
479
#endif
480
                       true, 1);
pixhawk's avatar
pixhawk committed
481 482 483 484 485 486 487 488 489 490 491 492 493
    }
}

//! Find the x/y position for a given value v
int QwtSlider::xyPosition(double v) const
{
    return d_data->map.transform(v);
}

//! Determine the value corresponding to a specified mouse location.
double QwtSlider::getValue(const QPoint &p)
{
    return d_data->map.invTransform(
494
               orientation() == Qt::Horizontal ? p.x() : p.y());
pixhawk's avatar
pixhawk committed
495 496 497 498 499 500 501 502 503
}


/*!
  \brief Determine scrolling mode and direction
  \param p point
  \param scrollMode Scrolling mode
  \param direction Direction
*/
504 505
void QwtSlider::getScrollMode(const QPoint &p,
                              int &scrollMode, int &direction )
pixhawk's avatar
pixhawk committed
506
{
507
    if (!d_data->sliderRect.contains(p)) {
pixhawk's avatar
pixhawk committed
508 509 510 511 512 513 514 515 516
        scrollMode = ScrNone;
        direction = 0;
        return;
    }

    const int pos = ( orientation() == Qt::Horizontal ) ? p.x() : p.y();
    const int markerPos = xyPosition(value());

    if ((pos > markerPos - d_data->thumbLength / 2)
517
            && (pos < markerPos + d_data->thumbLength / 2)) {
pixhawk's avatar
pixhawk committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
        scrollMode = ScrMouse;
        direction = 0;
        return;
    }

    scrollMode = ScrPage;
    direction = (pos > markerPos) ? 1 : -1;

    if ( scaleDraw()->map().p1() > scaleDraw()->map().p2() )
        direction = -direction;
}

//! Qt paint event
void QwtSlider::paintEvent(QPaintEvent *e)
{
    const QRect &ur = e->rect();
534
    if ( ur.isValid() ) {
pixhawk's avatar
pixhawk committed
535 536 537 538 539 540 541 542 543 544 545 546 547
#if QT_VERSION < 0x040000
        QwtPaintBuffer paintBuffer(this, ur);
        draw(paintBuffer.painter(), ur);
#else
        QPainter painter(this);
        draw(&painter, ur);
#endif
    }
}

//! Draw the QwtSlider
void QwtSlider::draw(QPainter *painter, const QRect&)
{
548
    if (d_data->scalePos != NoScale) {
pixhawk's avatar
pixhawk committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
#if QT_VERSION < 0x040000
        scaleDraw()->draw(painter, colorGroup());
#else
        scaleDraw()->draw(painter, palette());
#endif
    }

    drawSlider(painter, d_data->sliderRect);

    if ( hasFocus() )
        QwtPainter::drawFocusRect(painter, this, d_data->sliderRect);
}

//! Qt resize event
void QwtSlider::resizeEvent(QResizeEvent *)
{
    layoutSlider( 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 QwtSlider::layoutSlider( bool update_geometry )
{
    int sliderWidth = d_data->thumbWidth;
    int sld1 = d_data->thumbLength / 2 - 1;
    int sld2 = d_data->thumbLength / 2 + d_data->thumbLength % 2;
579
    if ( d_data->bgStyle & BgTrough ) {
pixhawk's avatar
pixhawk committed
580 581 582 583 584 585
        sliderWidth += 2 * d_data->borderWidth;
        sld1 += d_data->borderWidth;
        sld2 += d_data->borderWidth;
    }

    int scd = 0;
586
    if ( d_data->scalePos != NoScale ) {
pixhawk's avatar
pixhawk committed
587 588 589 590 591 592 593 594 595 596 597 598
        int d1, d2;
        scaleDraw()->getBorderDistHint(font(), d1, d2);
        scd = qwtMax(d1, d2);
    }

    int slo = scd - sld1;
    if ( slo < 0 )
        slo = 0;

    int x, y, length;

    const QRect r = rect();
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
    if (orientation() == Qt::Horizontal) {
        switch (d_data->scalePos) {
        case TopScale: {
            d_data->sliderRect.setRect(
                r.x() + d_data->xMargin + slo,
                r.y() + r.height() -
                d_data->yMargin - sliderWidth,
                r.width() - 2 * d_data->xMargin - 2 * slo,
                sliderWidth);

            x = d_data->sliderRect.x() + sld1;
            y = d_data->sliderRect.y() - d_data->scaleDist;

            break;
        }

        case BottomScale: {
            d_data->sliderRect.setRect(
                r.x() + d_data->xMargin + slo,
                r.y() + d_data->yMargin,
                r.width() - 2 * d_data->xMargin - 2 * slo,
                sliderWidth);

            x = d_data->sliderRect.x() + sld1;
            y = d_data->sliderRect.y() + d_data->sliderRect.height()
                + d_data->scaleDist;

            break;
        }

        case NoScale: // like Bottom, but no scale. See QwtSlider().
        default: { // inconsistent orientation and scale position
            d_data->sliderRect.setRect(
                r.x() + d_data->xMargin + slo,
                r.y() + d_data->yMargin,
                r.width() - 2 * d_data->xMargin - 2 * slo,
                sliderWidth);

            x = d_data->sliderRect.x() + sld1;
            y = 0;

            break;
        }
pixhawk's avatar
pixhawk committed
642 643
        }
        length = d_data->sliderRect.width() - (sld1 + sld2);
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
    } else { // if (orientation() == Qt::Vertical
        switch (d_data->scalePos) {
        case RightScale:
            d_data->sliderRect.setRect(
                r.x() + d_data->xMargin,
                r.y() + d_data->yMargin + slo,
                sliderWidth,
                r.height() - 2 * d_data->yMargin - 2 * slo);

            x = d_data->sliderRect.x() + d_data->sliderRect.width()
                + d_data->scaleDist;
            y = d_data->sliderRect.y() + sld1;

            break;

        case LeftScale:
            d_data->sliderRect.setRect(
                r.x() + r.width() - sliderWidth - d_data->xMargin,
                r.y() + d_data->yMargin + slo,
                sliderWidth,
                r.height() - 2 * d_data->yMargin - 2 * slo);

            x = d_data->sliderRect.x() - d_data->scaleDist;
            y = d_data->sliderRect.y() + sld1;

            break;

        case NoScale: // like Left, but no scale. See QwtSlider().
        default:   // inconsistent orientation and scale position
            d_data->sliderRect.setRect(
                r.x() + r.width() - sliderWidth - d_data->xMargin,
                r.y() + d_data->yMargin + slo,
                sliderWidth,
                r.height() - 2 * d_data->yMargin - 2 * slo);

            x = 0;
            y = d_data->sliderRect.y() + sld1;

            break;
pixhawk's avatar
pixhawk committed
683 684 685 686 687 688 689 690
        }
        length = d_data->sliderRect.height() - (sld1 + sld2);
    }

    scaleDraw()->move(x, y);
    scaleDraw()->setLength(length);

    d_data->map.setPaintXInterval(scaleDraw()->map().p1(),
691
                                  scaleDraw()->map().p2());
pixhawk's avatar
pixhawk committed
692

693
    if ( update_geometry ) {
pixhawk's avatar
pixhawk committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
        d_data->sizeHintCache = QSize(); // invalidate
        updateGeometry();
        update();
    }
}

//! Notify change of value
void QwtSlider::valueChange()
{
    QwtAbstractSlider::valueChange();
    update();
}


//! Notify change of range
void QwtSlider::rangeChange()
{
    d_data->map.setScaleInterval(minValue(), maxValue());

    if (autoScale())
        rescale(minValue(), maxValue());

    QwtAbstractSlider::rangeChange();
    layoutSlider();
}

/*!
  \brief Set distances between the widget's border and internals.
  \param xMargin Horizontal margin
  \param yMargin Vertical margin
*/
void QwtSlider::setMargins(int xMargin, int yMargin)
{
    if ( xMargin < 0 )
        xMargin = 0;
    if ( yMargin < 0 )
        yMargin = 0;

732
    if ( xMargin != d_data->xMargin || yMargin != d_data->yMargin ) {
pixhawk's avatar
pixhawk committed
733 734 735 736 737 738 739 740 741
        d_data->xMargin = xMargin;
        d_data->yMargin = yMargin;
        layoutSlider();
    }
}

/*!
  Set the background style.
*/
742
void QwtSlider::setBgStyle(BGSTYLE st)
pixhawk's avatar
pixhawk committed
743
{
744
    d_data->bgStyle = st;
pixhawk's avatar
pixhawk committed
745 746 747 748 749 750
    layoutSlider();
}

/*!
  \return the background style.
*/
751 752 753
QwtSlider::BGSTYLE QwtSlider::bgStyle() const
{
    return d_data->bgStyle;
pixhawk's avatar
pixhawk committed
754 755 756 757 758
}

/*!
  \return the thumb length.
*/
759
int QwtSlider::thumbLength() const
pixhawk's avatar
pixhawk committed
760 761 762 763 764 765 766
{
    return d_data->thumbLength;
}

/*!
  \return the thumb width.
*/
767
int QwtSlider::thumbWidth() const
pixhawk's avatar
pixhawk committed
768 769 770 771 772 773 774
{
    return d_data->thumbWidth;
}

/*!
  \return the border width.
*/
775
int QwtSlider::borderWidth() const
pixhawk's avatar
pixhawk committed
776 777 778 779 780 781 782 783 784 785 786 787 788 789
{
    return d_data->borderWidth;
}

/*!
  \return QwtSlider::minimumSizeHint()
*/
QSize QwtSlider::sizeHint() const
{
    return minimumSizeHint();
}

/*!
  \brief Return a minimum size hint
790
  \warning The return value of QwtSlider::minimumSizeHint() depends on
pixhawk's avatar
pixhawk committed
791 792 793 794
           the font and the scale.
*/
QSize QwtSlider::minimumSizeHint() const
{
795
    if (!d_data->sizeHintCache.isEmpty())
pixhawk's avatar
pixhawk committed
796 797 798 799 800 801 802
        return d_data->sizeHintCache;

    int sliderWidth = d_data->thumbWidth;
    if (d_data->bgStyle & BgTrough)
        sliderWidth += 2 * d_data->borderWidth;

    int w = 0, h = 0;
803
    if (d_data->scalePos != NoScale) {
pixhawk's avatar
pixhawk committed
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
        int d1, d2;
        scaleDraw()->getBorderDistHint(font(), d1, d2);
        int msMbd = qwtMax(d1, d2);

        int mbd = d_data->thumbLength / 2;
        if (d_data->bgStyle & BgTrough)
            mbd += d_data->borderWidth;

        if ( mbd < msMbd )
            mbd = msMbd;

        const int sdExtent = scaleDraw()->extent( QPen(), font() );
        const int sdLength = scaleDraw()->minLength( QPen(), font() );

        h = sliderWidth + sdExtent + d_data->scaleDist;
        w = sdLength - 2 * msMbd + 2 * mbd;
820
    } else { // no scale
pixhawk's avatar
pixhawk committed
821 822 823 824 825 826 827 828 829 830 831 832 833
        w = 200;
        h = sliderWidth;
    }

    if ( orientation() == Qt::Vertical )
        qSwap(w, h);

    w += 2 * d_data->xMargin;
    h += 2 * d_data->yMargin;

    d_data->sizeHintCache = QSize(w, h);
    return d_data->sizeHintCache;
}