qwt_counter.cpp 13.6 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
/* -*- 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 <qlayout.h>
#include <qlineedit.h>
#include <qvalidator.h>
#include <qevent.h>
#include <qstyle.h>
#include "qwt_math.h"
#include "qwt_counter.h"
#include "qwt_arrow_button.h"

class QwtCounter::PrivateData
{
public:
    PrivateData():
25
        editable(true) {
pixhawk's avatar
pixhawk committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
        increment[Button1] = 1;
        increment[Button2] = 10;
        increment[Button3] = 100;
    }

    QwtArrowButton *buttonDown[ButtonCnt];
    QwtArrowButton *buttonUp[ButtonCnt];
    QLineEdit *valueEdit;

    int increment[ButtonCnt];
    int nButtons;

    bool editable;
};

/*!
  The default number of buttons is set to 2. The default increments are:
  \li Button 1: 1 step
  \li Button 2: 10 steps
  \li Button 3: 100 steps

  \param parent
 */
QwtCounter::QwtCounter(QWidget *parent):
50
    QWidget(parent)
pixhawk's avatar
pixhawk committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64
{
    initCounter();
}

#if QT_VERSION < 0x040000
/*!
  The default number of buttons is set to 2. The default increments are:
  \li Button 1: 1 step
  \li Button 2: 10 steps
  \li Button 3: 100 steps

  \param parent
 */
QwtCounter::QwtCounter(QWidget *parent, const char *name):
65
    QWidget(parent, name)
pixhawk's avatar
pixhawk committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
{
    initCounter();
}
#endif

void QwtCounter::initCounter()
{
    d_data = new PrivateData;

#if QT_VERSION >= 0x040000
    using namespace Qt;
#endif

    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->setSpacing(0);
    layout->setMargin(0);

    int i;
84
    for(i = ButtonCnt - 1; i >= 0; i--) {
pixhawk's avatar
pixhawk committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        QwtArrowButton *btn =
            new QwtArrowButton(i+1, Qt::DownArrow,this);
        btn->setFocusPolicy(NoFocus);
        btn->installEventFilter(this);
        layout->addWidget(btn);

        connect(btn, SIGNAL(released()), SLOT(btnReleased()));
        connect(btn, SIGNAL(clicked()), SLOT(btnClicked()));

        d_data->buttonDown[i] = btn;
    }

    d_data->valueEdit = new QLineEdit(this);
    d_data->valueEdit->setReadOnly(false);
    d_data->valueEdit->setValidator(new QDoubleValidator(d_data->valueEdit));
    layout->addWidget(d_data->valueEdit);

#if QT_VERSION >= 0x040000
103 104
    connect( d_data->valueEdit, SIGNAL(editingFinished()),
             SLOT(textChanged()) );
pixhawk's avatar
pixhawk committed
105 106 107 108 109 110 111
#else
    connect( d_data->valueEdit, SIGNAL(returnPressed()), SLOT(textChanged()) );
    connect( d_data->valueEdit, SIGNAL(lostFocus()), SLOT(textChanged()) );
#endif

    layout->setStretchFactor(d_data->valueEdit, 10);

112
    for(i = 0; i < ButtonCnt; i++) {
pixhawk's avatar
pixhawk committed
113 114 115 116 117 118 119 120 121 122 123
#if QT_VERSION >= 0x040000
        using namespace Qt;
#endif
        QwtArrowButton *btn =
            new QwtArrowButton(i+1, Qt::UpArrow, this);
        btn->setFocusPolicy(NoFocus);
        btn->installEventFilter(this);
        layout->addWidget(btn);

        connect(btn, SIGNAL(released()), SLOT(btnReleased()));
        connect(btn, SIGNAL(clicked()), SLOT(btnClicked()));
124

pixhawk's avatar
pixhawk committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        d_data->buttonUp[i] = btn;
    }

    setNumButtons(2);
    setRange(0.0,1.0,0.001);
    setValue(0.0);

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

    setFocusProxy(d_data->valueEdit);
    setFocusPolicy(StrongFocus);
}

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

/*!
  Sets the minimum width for the buttons
*/
void QwtCounter::polish()
{
    const int w = d_data->valueEdit->fontMetrics().width("W") + 8;

152
    for ( int i = 0; i < ButtonCnt; i++ ) {
pixhawk's avatar
pixhawk committed
153 154 155 156 157 158 159 160 161 162
        d_data->buttonDown[i]->setMinimumWidth(w);
        d_data->buttonUp[i]->setMinimumWidth(w);
    }

#if QT_VERSION < 0x040000
    QWidget::polish();
#endif
}

//! Set from lineedit
163
void QwtCounter::textChanged()
pixhawk's avatar
pixhawk committed
164
{
165
    if ( !d_data->editable )
pixhawk's avatar
pixhawk committed
166 167 168 169 170
        return;

    bool converted = false;

    const double value = d_data->valueEdit->text().toDouble(&converted);
171 172
    if ( converted )
        setValue( value );
pixhawk's avatar
pixhawk committed
173 174 175 176 177 178 179 180 181 182 183 184 185
}

/**
  \brief Allow/disallow the user to manually edit the value

  \param editable true enables editing
  \sa editable()
*/
void QwtCounter::setEditable(bool editable)
{
#if QT_VERSION >= 0x040000
    using namespace Qt;
#endif
186
    if ( editable == d_data->editable )
pixhawk's avatar
pixhawk committed
187 188 189 190 191 192 193
        return;

    d_data->editable = editable;
    d_data->valueEdit->setReadOnly(!editable);
}

//! returns whether the line edit is edatble. (default is yes)
194 195
bool QwtCounter::editable() const
{
pixhawk's avatar
pixhawk committed
196 197 198 199
    return d_data->editable;
}

/*!
200
   Handle PolishRequest events
pixhawk's avatar
pixhawk committed
201
*/
202
bool QwtCounter::event ( QEvent * e )
pixhawk's avatar
pixhawk committed
203 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
{
#if QT_VERSION >= 0x040000
    if ( e->type() == QEvent::PolishRequest )
        polish();
#endif
    return QWidget::event(e);
}

/*!
  Handles key events

  - Ctrl + Qt::Key_Home
    Step to minValue()
  - Ctrl + Qt::Key_End
    Step to maxValue()
  - Qt::Key_Up
    Increment by incSteps(QwtCounter::Button1)
  - Qt::Key_Down
    Decrement by incSteps(QwtCounter::Button1)
  - Qt::Key_PageUp
    Increment by incSteps(QwtCounter::Button2)
  - Qt::Key_PageDown
    Decrement by incSteps(QwtCounter::Button2)
  - Shift + Qt::Key_PageUp
    Increment by incSteps(QwtCounter::Button3)
  - Shift + Qt::Key_PageDown
    Decrement by incSteps(QwtCounter::Button3)
*/

void QwtCounter::keyPressEvent (QKeyEvent *e)
{
    bool accepted = true;

236 237
    switch ( e->key() ) {
    case Qt::Key_Home:
pixhawk's avatar
pixhawk committed
238
#if QT_VERSION >= 0x040000
239
        if ( e->modifiers() & Qt::ControlModifier )
pixhawk's avatar
pixhawk committed
240
#else
241
        if ( e->state() & Qt::ControlButton )
pixhawk's avatar
pixhawk committed
242
#endif
243 244 245 246 247
            setValue(minValue());
        else
            accepted = false;
        break;
    case Qt::Key_End:
pixhawk's avatar
pixhawk committed
248
#if QT_VERSION >= 0x040000
249
        if ( e->modifiers() & Qt::ControlModifier )
pixhawk's avatar
pixhawk committed
250
#else
251
        if ( e->state() & Qt::ControlButton )
pixhawk's avatar
pixhawk committed
252
#endif
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
            setValue(maxValue());
        else
            accepted = false;
        break;
    case Qt::Key_Up:
        incValue(d_data->increment[0]);
        break;
    case Qt::Key_Down:
        incValue(-d_data->increment[0]);
        break;
    case Qt::Key_PageUp:
    case Qt::Key_PageDown: {
        int increment = d_data->increment[0];
        if ( d_data->nButtons >= 2 )
            increment = d_data->increment[1];
        if ( d_data->nButtons >= 3 ) {
pixhawk's avatar
pixhawk committed
269
#if QT_VERSION >= 0x040000
270
            if ( e->modifiers() & Qt::ShiftModifier )
pixhawk's avatar
pixhawk committed
271
#else
272
            if ( e->state() & Qt::ShiftButton )
pixhawk's avatar
pixhawk committed
273
#endif
274
                increment = d_data->increment[2];
pixhawk's avatar
pixhawk committed
275
        }
276 277 278 279 280 281 282
        if ( e->key() == Qt::Key_PageDown )
            increment = -increment;
        incValue(increment);
        break;
    }
    default:
        accepted = false;
pixhawk's avatar
pixhawk committed
283 284
    }

285
    if ( accepted ) {
pixhawk's avatar
pixhawk committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
        e->accept();
        return;
    }

    QWidget::keyPressEvent (e);
}

void QwtCounter::wheelEvent(QWheelEvent *e)
{
    e->accept();

    if ( d_data->nButtons <= 0 )
        return;

    int increment = d_data->increment[0];
301
    if ( d_data->nButtons >= 2 ) {
pixhawk's avatar
pixhawk committed
302 303 304 305 306 307 308
#if QT_VERSION >= 0x040000
        if ( e->modifiers() & Qt::ControlModifier )
#else
        if ( e->state() & Qt::ControlButton )
#endif
            increment = d_data->increment[1];
    }
309
    if ( d_data->nButtons >= 3 ) {
pixhawk's avatar
pixhawk committed
310 311 312 313 314 315 316
#if QT_VERSION >= 0x040000
        if ( e->modifiers() & Qt::ShiftModifier )
#else
        if ( e->state() & Qt::ShiftButton )
#endif
            increment = d_data->increment[2];
    }
317 318

    for ( int i = 0; i < d_data->nButtons; i++ ) {
pixhawk's avatar
pixhawk committed
319
        if ( d_data->buttonDown[i]->geometry().contains(e->pos()) ||
320
                d_data->buttonUp[i]->geometry().contains(e->pos()) ) {
pixhawk's avatar
pixhawk committed
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
            increment = d_data->increment[i];
        }
    }

    const int wheel_delta = 120;

    int delta = e->delta();
    if ( delta >= 2 * wheel_delta )
        delta /= 2; // Never saw an abs(delta) < 240

    incValue(delta / wheel_delta * increment);
}

/*!
  Specify the number of steps by which the value
  is incremented or decremented when a specified button
  is pushed.

  \param btn One of \c QwtCounter::Button1, \c QwtCounter::Button2,
             \c QwtCounter::Button3
  \param nSteps Number of steps
*/
void QwtCounter::setIncSteps(QwtCounter::Button btn, int nSteps)
{
    if (( btn >= 0) && (btn < ButtonCnt))
346
        d_data->increment[btn] = nSteps;
pixhawk's avatar
pixhawk committed
347 348 349 350 351 352 353 354 355 356 357
}

/*!
  \return the number of steps by which a specified button increments the value
  or 0 if the button is invalid.
  \param btn One of \c QwtCounter::Button1, \c QwtCounter::Button2,
  \c QwtCounter::Button3
*/
int QwtCounter::incSteps(QwtCounter::Button btn) const
{
    if (( btn >= 0) && (btn < ButtonCnt))
358
        return d_data->increment[btn];
pixhawk's avatar
pixhawk committed
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 398 399 400 401 402 403

    return 0;
}

/*!
  \brief Set a new value
  \param v new value
  Calls QwtDoubleRange::setValue and does all visual updates.
  \sa QwtDoubleRange::setValue
*/

void QwtCounter::setValue(double v)
{
    QwtDoubleRange::setValue(v);

    showNum(value());
    updateButtons();
}

/*!
  \brief Notify a change of value
*/
void QwtCounter::valueChange()
{
    if ( isValid() )
        showNum(value());
    else
        d_data->valueEdit->setText(QString::null);

    updateButtons();

    if ( isValid() )
        emit valueChanged(value());
}

/*!
  \brief Update buttons according to the current value

  When the QwtCounter under- or over-flows, the focus is set to the smallest
  up- or down-button and counting is disabled.

  Counting is re-enabled on a button release event (mouse or space bar).
*/
void QwtCounter::updateButtons()
{
404
    if ( isValid() ) {
pixhawk's avatar
pixhawk committed
405 406 407
        // 1. save enabled state of the smallest down- and up-button
        // 2. change enabled state on under- or over-flow

408
        for ( int i = 0; i < ButtonCnt; i++ ) {
pixhawk's avatar
pixhawk committed
409 410 411
            d_data->buttonDown[i]->setEnabled(value() > minValue());
            d_data->buttonUp[i]->setEnabled(value() < maxValue());
        }
412 413
    } else {
        for ( int i = 0; i < ButtonCnt; i++ ) {
pixhawk's avatar
pixhawk committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
            d_data->buttonDown[i]->setEnabled(false);
            d_data->buttonUp[i]->setEnabled(false);
        }
    }
}

/*!
  \brief Specify the number of buttons on each side of the label
  \param n Number of buttons
*/
void QwtCounter::setNumButtons(int n)
{
    if ( n<0 || n>ButtonCnt )
        return;

429 430
    for ( int i = 0; i < ButtonCnt; i++ ) {
        if ( i < n ) {
pixhawk's avatar
pixhawk committed
431 432
            d_data->buttonDown[i]->show();
            d_data->buttonUp[i]->show();
433
        } else {
pixhawk's avatar
pixhawk committed
434 435 436 437 438 439 440 441 442 443 444
            d_data->buttonDown[i]->hide();
            d_data->buttonUp[i]->hide();
        }
    }

    d_data->nButtons = n;
}

/*!
    \return The number of buttons on each side of the widget.
*/
445 446 447
int QwtCounter::numButtons() const
{
    return d_data->nButtons;
pixhawk's avatar
pixhawk committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
}

//!  Display number string
void QwtCounter::showNum(double d)
{
    QString v;
    v.setNum(d);

    const int cursorPos = d_data->valueEdit->cursorPosition();
    d_data->valueEdit->setText(v);
    d_data->valueEdit->setCursorPosition(cursorPos);
}

//!  Button clicked
void QwtCounter::btnClicked()
{
464
    for ( int i = 0; i < ButtonCnt; i++ ) {
pixhawk's avatar
pixhawk committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 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
        if ( d_data->buttonUp[i] == sender() )
            incValue(d_data->increment[i]);

        if ( d_data->buttonDown[i] == sender() )
            incValue(-d_data->increment[i]);
    }
}

//!  Button released
void QwtCounter::btnReleased()
{
    emit buttonReleased(value());
}

/*!
  \brief Notify change of range

  This function updates the enabled property of
  all buttons contained in QwtCounter.
*/
void QwtCounter::rangeChange()
{
    updateButtons();
}

//! A size hint
QSize QwtCounter::sizeHint() const
{
    QString tmp;

    int w = tmp.setNum(minValue()).length();
    int w1 = tmp.setNum(maxValue()).length();
    if ( w1 > w )
        w = w1;
    w1 = tmp.setNum(minValue() + step()).length();
    if ( w1 > w )
        w = w1;
    w1 = tmp.setNum(maxValue() - step()).length();
    if ( w1 > w )
        w = w1;

    tmp.fill('9', w);

    QFontMetrics fm(d_data->valueEdit->font());
    w = fm.width(tmp) + 2;
#if QT_VERSION >= 0x040000
    if ( d_data->valueEdit->hasFrame() )
        w += 2 * style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
#else
514
    w += 2 * d_data->valueEdit->frameWidth();
pixhawk's avatar
pixhawk committed
515 516 517 518 519 520 521
#endif

    // Now we replace default sizeHint contribution of d_data->valueEdit by
    // what we really need.

    w += QWidget::sizeHint().width() - d_data->valueEdit->sizeHint().width();

522 523
    const int h = qwtMin(QWidget::sizeHint().height(),
                         d_data->valueEdit->minimumSizeHint().height());
pixhawk's avatar
pixhawk committed
524 525 526 527 528 529 530 531
    return QSize(w, h);
}

//! returns the step size
double QwtCounter::step() const
{
    return QwtDoubleRange::step();
}
532

pixhawk's avatar
pixhawk committed
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 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
//! sets the step size
void QwtCounter::setStep(double s)
{
    QwtDoubleRange::setStep(s);
}

//! returns the minimum value of the range
double QwtCounter::minVal() const
{
    return minValue();
}

//! sets the minimum value of the range
void QwtCounter::setMinValue(double m)
{
    setRange(m, maxValue(), step());
}

//! returns the maximum value of the range
double QwtCounter::maxVal() const
{
    return QwtDoubleRange::maxValue();
}

//! sets the maximum value of the range
void QwtCounter::setMaxValue(double m)
{
    setRange(minValue(), m, step());
}

//! set the number of increment steps for button 1
void QwtCounter::setStepButton1(int nSteps)
{
    setIncSteps(Button1, nSteps);
}

//! returns the number of increment steps for button 1
int QwtCounter::stepButton1() const
{
    return incSteps(Button1);
}

//! set the number of increment steps for button 2
void QwtCounter::setStepButton2(int nSteps)
{
    setIncSteps(Button2, nSteps);
}

//! returns the number of increment steps for button 2
int QwtCounter::stepButton2() const
{
    return incSteps(Button2);
}

//! set the number of increment steps for button 3
void QwtCounter::setStepButton3(int nSteps)
{
    setIncSteps(Button3, nSteps);
}

//! returns the number of increment steps for button 3
int QwtCounter::stepButton3() const
{
    return incSteps(Button3);
}

double QwtCounter::value() const
{
    return QwtDoubleRange::value();
}