qwt_knob.cpp 11.7 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
/* -*- 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 <qpainter.h>
#include <qpalette.h>
#include <qstyle.h>
#include <qevent.h>
#include "qwt_round_scale_draw.h"
#include "qwt_knob.h"
#include "qwt_math.h"
#include "qwt_painter.h"
#include "qwt_paint_buffer.h"

class QwtKnob::PrivateData
{
public:
23
    PrivateData() {
pixhawk's avatar
pixhawk committed
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 51 52 53 54
        angle = 0.0;
        nTurns = 0.0;
        borderWidth = 2;
        borderDist = 4;
        totalAngle = 270.0;
        scaleDist = 4;
        symbol = Line;
        maxScaleTicks = 11;
        knobWidth = 50;
        dotWidth = 8;
    }

    int borderWidth;
    int borderDist;
    int scaleDist;
    int maxScaleTicks;
    int knobWidth;
    int dotWidth;

    Symbol symbol;
    double angle;
    double totalAngle;
    double nTurns;

    QRect knobRect; // bounding rect of the knob without scale
};

/*!
  Constructor
  \param parent Parent widget
*/
55
QwtKnob::QwtKnob(QWidget* parent):
pixhawk's avatar
pixhawk committed
56 57 58 59 60 61 62 63 64 65 66
    QwtAbstractSlider(Qt::Horizontal, parent)
{
    initKnob();
}

#if QT_VERSION < 0x040000
/*!
  Constructor
  \param parent Parent widget
  \param name Object name
*/
67
QwtKnob::QwtKnob(QWidget* parent, const char *name):
pixhawk's avatar
pixhawk committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    QwtAbstractSlider(Qt::Horizontal, parent)
{
    setName(name);
    initKnob();
}
#endif

void QwtKnob::initKnob()
{
#if QT_VERSION < 0x040000
    setWFlags(Qt::WNoAutoErase);
#endif

    d_data = new PrivateData;

    setScaleDraw(new QwtRoundScaleDraw());

    setUpdateTime(50);
    setTotalAngle( 270.0 );
    recalcAngle();
    setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));

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

//! Destructor
QwtKnob::~QwtKnob()
{
    delete d_data;
}

/*!
  \brief Set the symbol of the knob
  \sa symbol()
*/
void QwtKnob::setSymbol(QwtKnob::Symbol s)
{
106
    if ( d_data->symbol != s ) {
pixhawk's avatar
pixhawk committed
107 108 109 110 111
        d_data->symbol = s;
        update();
    }
}

112
/*!
pixhawk's avatar
pixhawk committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    \return symbol of the knob
    \sa setSymbol()
*/
QwtKnob::Symbol QwtKnob::symbol() const
{
    return d_data->symbol;
}

/*!
  \brief Set the total angle by which the knob can be turned
  \param angle Angle in degrees.

  The default angle is 270 degrees. It is possible to specify
  an angle of more than 360 degrees so that the knob can be
  turned several times around its axis.
*/
void QwtKnob::setTotalAngle (double angle)
{
    if (angle < 10.0)
132
        d_data->totalAngle = 10.0;
pixhawk's avatar
pixhawk committed
133
    else
134
        d_data->totalAngle = angle;
pixhawk's avatar
pixhawk committed
135

136 137
    scaleDraw()->setAngleRange( -0.5 * d_data->totalAngle,
                                0.5 * d_data->totalAngle);
pixhawk's avatar
pixhawk committed
138 139 140 141
    layoutKnob();
}

//! Return the total angle
142
double QwtKnob::totalAngle() const
pixhawk's avatar
pixhawk committed
143 144 145 146 147 148 149 150 151
{
    return d_data->totalAngle;
}

/*!
   Change the scale draw of the knob

   For changing the labels of the scales, it
   is necessary to derive from QwtRoundScaleDraw and
152
   overload QwtRoundScaleDraw::label().
pixhawk's avatar
pixhawk committed
153 154 155 156 157 158 159 160 161

   \sa scaleDraw()
*/
void QwtKnob::setScaleDraw(QwtRoundScaleDraw *scaleDraw)
{
    setAbstractScaleDraw(scaleDraw);
    setTotalAngle(d_data->totalAngle);
}

162
/*!
pixhawk's avatar
pixhawk committed
163 164 165 166 167 168 169 170
   \return the scale draw of the knob
   \sa setScaleDraw()
*/
const QwtRoundScaleDraw *QwtKnob::scaleDraw() const
{
    return (QwtRoundScaleDraw *)abstractScaleDraw();
}

171
/*!
pixhawk's avatar
pixhawk committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
   \return the scale draw of the knob
   \sa setScaleDraw()
*/
QwtRoundScaleDraw *QwtKnob::scaleDraw()
{
    return (QwtRoundScaleDraw *)abstractScaleDraw();
}

/*!
  \brief Draw the knob
  \param painter painter
  \param r Bounding rectangle of the knob (without scale)
*/
void QwtKnob::drawKnob(QPainter *painter, const QRect &r)
{
#if QT_VERSION < 0x040000
    const QBrush buttonBrush = colorGroup().brush(QColorGroup::Button);
    const QColor buttonTextColor = colorGroup().buttonText();
    const QColor lightColor = colorGroup().light();
    const QColor darkColor = colorGroup().dark();
#else
    const QBrush buttonBrush = palette().brush(QPalette::Button);
    const QColor buttonTextColor = palette().color(QPalette::ButtonText);
    const QColor lightColor = palette().color(QPalette::Light);
    const QColor darkColor = palette().color(QPalette::Dark);
#endif

    const int bw2 = d_data->borderWidth / 2;

    const int radius = (qwtMin(r.width(), r.height()) - bw2) / 2;

203
    const QRect aRect(
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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
        r.center().x() - radius, r.center().y() - radius,
        2 * radius, 2 * radius);

    //
    // draw button face
    //
    painter->setBrush(buttonBrush);
    painter->drawEllipse(aRect);

    //
    // draw button shades
    //
    QPen pn;
    pn.setWidth(d_data->borderWidth);

    pn.setColor(lightColor);
    painter->setPen(pn);
    painter->drawArc(aRect, 45*16, 180*16);

    pn.setColor(darkColor);
    painter->setPen(pn);
    painter->drawArc(aRect, 225*16, 180*16);

    //
    // draw marker
    //
    if ( isValid() )
        drawMarker(painter, d_data->angle, buttonTextColor);
}

/*!
  \brief Notify change of value

  Sets the knob's value to the nearest multiple
  of the step size.
*/
void QwtKnob::valueChange()
{
    recalcAngle();
    update();
    QwtAbstractSlider::valueChange();
}

/*!
  \brief Determine the value corresponding to a specified position

  Called by QwtAbstractSlider
  \param p point
*/
double QwtKnob::getValue(const QPoint &p)
{
    const double dx = double((rect().x() + rect().width() / 2) - p.x() );
    const double dy = double((rect().y() + rect().height() / 2) - p.y() );

    const double arc = atan2(-dx,dy) * 180.0 / M_PI;

    double newValue =  0.5 * (minValue() + maxValue())
261 262
                       + (arc + d_data->nTurns * 360.0) * (maxValue() - minValue())
                       / d_data->totalAngle;
pixhawk's avatar
pixhawk committed
263 264 265 266

    const double oneTurn = fabs(maxValue() - minValue()) * 360.0 / d_data->totalAngle;
    const double eqValue = value() + mouseOffset();

267
    if (fabs(newValue - eqValue) > 0.5 * oneTurn) {
pixhawk's avatar
pixhawk committed
268
        if (newValue < eqValue)
269
            newValue += oneTurn;
pixhawk's avatar
pixhawk committed
270
        else
271
            newValue -= oneTurn;
pixhawk's avatar
pixhawk committed
272 273
    }

274
    return newValue;
pixhawk's avatar
pixhawk committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
}

/*!
  \brief Set the scrolling mode and direction

  Called by QwtAbstractSlider
  \param p Point in question
*/
void QwtKnob::getScrollMode(const QPoint &p, int &scrollMode, int &direction)
{
    const int r = d_data->knobRect.width() / 2;

    const int dx = d_data->knobRect.x() + r - p.x();
    const int dy = d_data->knobRect.y() + r - p.y();

290
    if ( (dx * dx) + (dy * dy) <= (r * r)) { // point is inside the knob
pixhawk's avatar
pixhawk committed
291 292
        scrollMode = ScrMouse;
        direction = 0;
293
    } else {                            // point lies outside
pixhawk's avatar
pixhawk committed
294 295 296
        scrollMode = ScrTimer;
        double arc = atan2(double(-dx),double(dy)) * 180.0 / M_PI;
        if ( arc < d_data->angle)
297
            direction = -1;
pixhawk's avatar
pixhawk committed
298
        else if (arc > d_data->angle)
299
            direction = 1;
pixhawk's avatar
pixhawk committed
300
        else
301
            direction = 0;
pixhawk's avatar
pixhawk committed
302 303 304 305 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
    }
}


/*!
  \brief Notify a change of the range

  Called by QwtAbstractSlider
*/
void QwtKnob::rangeChange()
{
    if (autoScale())
        rescale(minValue(), maxValue());

    layoutKnob();
    recalcAngle();
}

/*!
  \brief Qt Resize Event
*/
void QwtKnob::resizeEvent(QResizeEvent *)
{
    layoutKnob( false );
}

//! Recalculate the knob'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 QwtKnob::layoutKnob( bool update_geometry )
{
    const QRect r = rect();
    const int radius = d_data->knobWidth / 2;

    d_data->knobRect.setWidth(2 * radius);
    d_data->knobRect.setHeight(2 * radius);
    d_data->knobRect.moveCenter(r.center());

    scaleDraw()->setRadius(radius + d_data->scaleDist);
    scaleDraw()->moveCenter(r.center());

344
    if ( update_geometry ) {
pixhawk's avatar
pixhawk committed
345 346 347 348 349 350 351 352 353 354 355
        updateGeometry();
        update();
    }
}

/*!
  \brief Repaint the knob
*/
void QwtKnob::paintEvent(QPaintEvent *e)
{
    const QRect &ur = e->rect();
356
    if ( ur.isValid() ) {
pixhawk's avatar
pixhawk committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
#if QT_VERSION < 0x040000
        QwtPaintBuffer paintBuffer(this, ur);
        draw(paintBuffer.painter(), ur);
#else
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
        draw(&painter, ur);
#endif
    }
}

/*!
  \brief Repaint the knob
*/
void QwtKnob::draw(QPainter *painter, const QRect& ur)
{
373
    if ( !d_data->knobRect.contains( ur ) ) { // event from valueChange()
pixhawk's avatar
pixhawk committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
#if QT_VERSION < 0x040000
        scaleDraw()->draw( painter, colorGroup() );
#else
        scaleDraw()->draw( painter, palette() );
#endif
    }

    drawKnob( painter, d_data->knobRect );

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

/*!
  \brief Draw the marker at the knob's front
  \param p Painter
  \param arc Angle of the marker
  \param c Marker color
*/
void QwtKnob::drawMarker(QPainter *p, double arc, const QColor &c)
{
    const double rarc = arc * M_PI / 180.0;
    const double ca = cos(rarc);
    const double sa = - sin(rarc);

    int radius = d_data->knobRect.width() / 2 - d_data->borderWidth;
400 401
    if (radius < 3)
        radius = 3;
pixhawk's avatar
pixhawk committed
402 403 404 405

    const int ym = d_data->knobRect.y() + radius + d_data->borderWidth;
    const int xm = d_data->knobRect.x() + radius + d_data->borderWidth;

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
    switch (d_data->symbol) {
    case Dot: {
        p->setBrush(c);
        p->setPen(Qt::NoPen);

        const double rb = double(qwtMax(radius - 4 - d_data->dotWidth / 2, 0));
        p->drawEllipse(xm - qRound(sa * rb) - d_data->dotWidth / 2,
                       ym - qRound(ca * rb) - d_data->dotWidth / 2,
                       d_data->dotWidth, d_data->dotWidth);
        break;
    }
    case Line: {
        p->setPen(QPen(c, 2));

        const double rb = qwtMax(double((radius - 4) / 3.0), 0.0);
        const double re = qwtMax(double(radius - 4), 0.0);

        p->drawLine ( xm - qRound(sa * rb), ym - qRound(ca * rb),
                      xm - qRound(sa * re), ym - qRound(ca * re));

        break;
    }
pixhawk's avatar
pixhawk committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    }
}

/*!
  \brief Change the knob's width.

  The specified width must be >= 5, or it will be clipped.
  \param w New width
*/
void QwtKnob::setKnobWidth(int w)
{
    d_data->knobWidth = qwtMax(w,5);
    layoutKnob();
}

//! Return the width of the knob
444
int QwtKnob::knobWidth() const
pixhawk's avatar
pixhawk committed
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
{
    return d_data->knobWidth;
}

/*!
  \brief Set the knob's border width
  \param bw new border width
*/
void QwtKnob::setBorderWidth(int bw)
{
    d_data->borderWidth = qwtMax(bw, 0);
    layoutKnob();
}

//! Return the border width
460
int QwtKnob::borderWidth() const
pixhawk's avatar
pixhawk committed
461 462 463 464 465 466 467 468 469 470 471 472 473
{
    return d_data->borderWidth;
}

/*!
  \brief Recalculate the marker angle corresponding to the
    current value
*/
void QwtKnob::recalcAngle()
{
    //
    // calculate the angle corresponding to the value
    //
474
    if (maxValue() == minValue()) {
pixhawk's avatar
pixhawk committed
475 476
        d_data->angle = 0;
        d_data->nTurns = 0;
477
    } else {
pixhawk's avatar
pixhawk committed
478
        d_data->angle = (value() - 0.5 * (minValue() + maxValue()))
479
                        / (maxValue() - minValue()) * d_data->totalAngle;
pixhawk's avatar
pixhawk committed
480 481 482 483 484 485 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
        d_data->nTurns = floor((d_data->angle + 180.0) / 360.0);
        d_data->angle = d_data->angle - d_data->nTurns * 360.0;
    }
}


/*!
    Recalculates the layout
    \sa layoutKnob()
*/
void QwtKnob::scaleChange()
{
    layoutKnob();
}

/*!
    Recalculates the layout
    \sa layoutKnob()
*/
void QwtKnob::fontChange(const QFont &f)
{
    QwtAbstractSlider::fontChange( f );
    layoutKnob();
}

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

/*!
  \brief Return a minimum size hint
515
  \warning The return value of QwtKnob::minimumSizeHint() depends on the
pixhawk's avatar
pixhawk committed
516 517 518 519 520 521 522 523 524 525
           font and the scale.
*/
QSize QwtKnob::minimumSizeHint() const
{
    // Add the scale radial thickness to the knobWidth
    const int sh = scaleDraw()->extent( QPen(), font() );
    const int d = 2 * sh + 2 * d_data->scaleDist + d_data->knobWidth;

    return QSize( d, d );
}