Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <qlayout.h>
00013 #include <qlineedit.h>
00014 #include <qvalidator.h>
00015 #include <qevent.h>
00016 #include <qstyle.h>
00017 #include "qwt_math.h"
00018 #include "qwt_counter.h"
00019 #include "qwt_arrow_button.h"
00020
00021 class QwtCounter::PrivateData
00022 {
00023 public:
00024 PrivateData():
00025 editable(true)
00026 {
00027 increment[Button1] = 1;
00028 increment[Button2] = 10;
00029 increment[Button3] = 100;
00030 }
00031
00032 QwtArrowButton *buttonDown[ButtonCnt];
00033 QwtArrowButton *buttonUp[ButtonCnt];
00034 QLineEdit *valueEdit;
00035
00036 int increment[ButtonCnt];
00037 int nButtons;
00038
00039 bool editable;
00040 };
00041
00050 QwtCounter::QwtCounter(QWidget *parent):
00051 QWidget(parent)
00052 {
00053 initCounter();
00054 }
00055
00056 #if QT_VERSION < 0x040000
00057
00065 QwtCounter::QwtCounter(QWidget *parent, const char *name):
00066 QWidget(parent, name)
00067 {
00068 initCounter();
00069 }
00070 #endif
00071
00072 void QwtCounter::initCounter()
00073 {
00074 d_data = new PrivateData;
00075
00076 #if QT_VERSION >= 0x040000
00077 using namespace Qt;
00078 #endif
00079
00080 QHBoxLayout *layout = new QHBoxLayout(this);
00081 layout->setSpacing(0);
00082 layout->setMargin(0);
00083
00084 int i;
00085 for(i = ButtonCnt - 1; i >= 0; i--)
00086 {
00087 QwtArrowButton *btn =
00088 new QwtArrowButton(i+1, Qt::DownArrow,this);
00089 btn->setFocusPolicy(NoFocus);
00090 btn->installEventFilter(this);
00091 layout->addWidget(btn);
00092
00093 connect(btn, SIGNAL(released()), SLOT(btnReleased()));
00094 connect(btn, SIGNAL(clicked()), SLOT(btnClicked()));
00095
00096 d_data->buttonDown[i] = btn;
00097 }
00098
00099 d_data->valueEdit = new QLineEdit(this);
00100 d_data->valueEdit->setReadOnly(false);
00101 d_data->valueEdit->setValidator(new QDoubleValidator(d_data->valueEdit));
00102 layout->addWidget(d_data->valueEdit);
00103
00104 #if QT_VERSION >= 0x040000
00105 connect( d_data->valueEdit, SIGNAL(editingFinished()),
00106 SLOT(textChanged()) );
00107 #else
00108 connect( d_data->valueEdit, SIGNAL(returnPressed()), SLOT(textChanged()) );
00109 connect( d_data->valueEdit, SIGNAL(lostFocus()), SLOT(textChanged()) );
00110 #endif
00111
00112 layout->setStretchFactor(d_data->valueEdit, 10);
00113
00114 for(i = 0; i < ButtonCnt; i++)
00115 {
00116 #if QT_VERSION >= 0x040000
00117 using namespace Qt;
00118 #endif
00119 QwtArrowButton *btn =
00120 new QwtArrowButton(i+1, Qt::UpArrow, this);
00121 btn->setFocusPolicy(NoFocus);
00122 btn->installEventFilter(this);
00123 layout->addWidget(btn);
00124
00125 connect(btn, SIGNAL(released()), SLOT(btnReleased()));
00126 connect(btn, SIGNAL(clicked()), SLOT(btnClicked()));
00127
00128 d_data->buttonUp[i] = btn;
00129 }
00130
00131 setNumButtons(2);
00132 setRange(0.0,1.0,0.001);
00133 setValue(0.0);
00134
00135 setSizePolicy(
00136 QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
00137
00138 setFocusProxy(d_data->valueEdit);
00139 setFocusPolicy(StrongFocus);
00140 }
00141
00143 QwtCounter::~QwtCounter()
00144 {
00145 delete d_data;
00146 }
00147
00151 void QwtCounter::polish()
00152 {
00153 const int w = d_data->valueEdit->fontMetrics().width("W") + 8;
00154
00155 for ( int i = 0; i < ButtonCnt; i++ )
00156 {
00157 d_data->buttonDown[i]->setMinimumWidth(w);
00158 d_data->buttonUp[i]->setMinimumWidth(w);
00159 }
00160
00161 #if QT_VERSION < 0x040000
00162 QWidget::polish();
00163 #endif
00164 }
00165
00167 void QwtCounter::textChanged()
00168 {
00169 if ( !d_data->editable )
00170 return;
00171
00172 bool converted = false;
00173
00174 const double value = d_data->valueEdit->text().toDouble(&converted);
00175 if ( converted )
00176 setValue( value );
00177 }
00178
00185 void QwtCounter::setEditable(bool editable)
00186 {
00187 #if QT_VERSION >= 0x040000
00188 using namespace Qt;
00189 #endif
00190 if ( editable == d_data->editable )
00191 return;
00192
00193 d_data->editable = editable;
00194 d_data->valueEdit->setReadOnly(!editable);
00195 }
00196
00198 bool QwtCounter::editable() const
00199 {
00200 return d_data->editable;
00201 }
00202
00206 bool QwtCounter::event ( QEvent * e )
00207 {
00208 #if QT_VERSION >= 0x040000
00209 if ( e->type() == QEvent::PolishRequest )
00210 polish();
00211 #endif
00212 return QWidget::event(e);
00213 }
00214
00235 void QwtCounter::keyPressEvent (QKeyEvent *e)
00236 {
00237 bool accepted = true;
00238
00239 switch ( e->key() )
00240 {
00241 case Qt::Key_Home:
00242 #if QT_VERSION >= 0x040000
00243 if ( e->modifiers() & Qt::ControlModifier )
00244 #else
00245 if ( e->state() & Qt::ControlButton )
00246 #endif
00247 setValue(minValue());
00248 else
00249 accepted = false;
00250 break;
00251 case Qt::Key_End:
00252 #if QT_VERSION >= 0x040000
00253 if ( e->modifiers() & Qt::ControlModifier )
00254 #else
00255 if ( e->state() & Qt::ControlButton )
00256 #endif
00257 setValue(maxValue());
00258 else
00259 accepted = false;
00260 break;
00261 case Qt::Key_Up:
00262 incValue(d_data->increment[0]);
00263 break;
00264 case Qt::Key_Down:
00265 incValue(-d_data->increment[0]);
00266 break;
00267 case Qt::Key_PageUp:
00268 case Qt::Key_PageDown:
00269 {
00270 int increment = d_data->increment[0];
00271 if ( d_data->nButtons >= 2 )
00272 increment = d_data->increment[1];
00273 if ( d_data->nButtons >= 3 )
00274 {
00275 #if QT_VERSION >= 0x040000
00276 if ( e->modifiers() & Qt::ShiftModifier )
00277 #else
00278 if ( e->state() & Qt::ShiftButton )
00279 #endif
00280 increment = d_data->increment[2];
00281 }
00282 if ( e->key() == Qt::Key_PageDown )
00283 increment = -increment;
00284 incValue(increment);
00285 break;
00286 }
00287 default:
00288 accepted = false;
00289 }
00290
00291 if ( accepted )
00292 {
00293 e->accept();
00294 return;
00295 }
00296
00297 QWidget::keyPressEvent (e);
00298 }
00299
00304 void QwtCounter::wheelEvent(QWheelEvent *e)
00305 {
00306 e->accept();
00307
00308 if ( d_data->nButtons <= 0 )
00309 return;
00310
00311 int increment = d_data->increment[0];
00312 if ( d_data->nButtons >= 2 )
00313 {
00314 #if QT_VERSION >= 0x040000
00315 if ( e->modifiers() & Qt::ControlModifier )
00316 #else
00317 if ( e->state() & Qt::ControlButton )
00318 #endif
00319 increment = d_data->increment[1];
00320 }
00321 if ( d_data->nButtons >= 3 )
00322 {
00323 #if QT_VERSION >= 0x040000
00324 if ( e->modifiers() & Qt::ShiftModifier )
00325 #else
00326 if ( e->state() & Qt::ShiftButton )
00327 #endif
00328 increment = d_data->increment[2];
00329 }
00330
00331 for ( int i = 0; i < d_data->nButtons; i++ )
00332 {
00333 if ( d_data->buttonDown[i]->geometry().contains(e->pos()) ||
00334 d_data->buttonUp[i]->geometry().contains(e->pos()) )
00335 {
00336 increment = d_data->increment[i];
00337 }
00338 }
00339
00340 const int wheel_delta = 120;
00341
00342 int delta = e->delta();
00343 if ( delta >= 2 * wheel_delta )
00344 delta /= 2;
00345
00346 incValue(delta / wheel_delta * increment);
00347 }
00348
00358 void QwtCounter::setIncSteps(QwtCounter::Button btn, int nSteps)
00359 {
00360 if (( btn >= 0) && (btn < ButtonCnt))
00361 d_data->increment[btn] = nSteps;
00362 }
00363
00370 int QwtCounter::incSteps(QwtCounter::Button btn) const
00371 {
00372 if (( btn >= 0) && (btn < ButtonCnt))
00373 return d_data->increment[btn];
00374
00375 return 0;
00376 }
00377
00385 void QwtCounter::setValue(double v)
00386 {
00387 QwtDoubleRange::setValue(v);
00388
00389 showNum(value());
00390 updateButtons();
00391 }
00392
00396 void QwtCounter::valueChange()
00397 {
00398 if ( isValid() )
00399 showNum(value());
00400 else
00401 d_data->valueEdit->setText(QString::null);
00402
00403 updateButtons();
00404
00405 if ( isValid() )
00406 emit valueChanged(value());
00407 }
00408
00417 void QwtCounter::updateButtons()
00418 {
00419 if ( isValid() )
00420 {
00421
00422
00423
00424 for ( int i = 0; i < ButtonCnt; i++ )
00425 {
00426 d_data->buttonDown[i]->setEnabled(value() > minValue());
00427 d_data->buttonUp[i]->setEnabled(value() < maxValue());
00428 }
00429 }
00430 else
00431 {
00432 for ( int i = 0; i < ButtonCnt; i++ )
00433 {
00434 d_data->buttonDown[i]->setEnabled(false);
00435 d_data->buttonUp[i]->setEnabled(false);
00436 }
00437 }
00438 }
00439
00444 void QwtCounter::setNumButtons(int n)
00445 {
00446 if ( n<0 || n>ButtonCnt )
00447 return;
00448
00449 for ( int i = 0; i < ButtonCnt; i++ )
00450 {
00451 if ( i < n )
00452 {
00453 d_data->buttonDown[i]->show();
00454 d_data->buttonUp[i]->show();
00455 }
00456 else
00457 {
00458 d_data->buttonDown[i]->hide();
00459 d_data->buttonUp[i]->hide();
00460 }
00461 }
00462
00463 d_data->nButtons = n;
00464 }
00465
00469 int QwtCounter::numButtons() const
00470 {
00471 return d_data->nButtons;
00472 }
00473
00479 void QwtCounter::showNum(double number)
00480 {
00481 QString v;
00482 v.setNum(number);
00483
00484 const int cursorPos = d_data->valueEdit->cursorPosition();
00485 d_data->valueEdit->setText(v);
00486 d_data->valueEdit->setCursorPosition(cursorPos);
00487 }
00488
00490 void QwtCounter::btnClicked()
00491 {
00492 for ( int i = 0; i < ButtonCnt; i++ )
00493 {
00494 if ( d_data->buttonUp[i] == sender() )
00495 incValue(d_data->increment[i]);
00496
00497 if ( d_data->buttonDown[i] == sender() )
00498 incValue(-d_data->increment[i]);
00499 }
00500 }
00501
00503 void QwtCounter::btnReleased()
00504 {
00505 emit buttonReleased(value());
00506 }
00507
00514 void QwtCounter::rangeChange()
00515 {
00516 updateButtons();
00517 }
00518
00520 QSize QwtCounter::sizeHint() const
00521 {
00522 QString tmp;
00523
00524 int w = tmp.setNum(minValue()).length();
00525 int w1 = tmp.setNum(maxValue()).length();
00526 if ( w1 > w )
00527 w = w1;
00528 w1 = tmp.setNum(minValue() + step()).length();
00529 if ( w1 > w )
00530 w = w1;
00531 w1 = tmp.setNum(maxValue() - step()).length();
00532 if ( w1 > w )
00533 w = w1;
00534
00535 tmp.fill('9', w);
00536
00537 QFontMetrics fm(d_data->valueEdit->font());
00538 w = fm.width(tmp) + 2;
00539 #if QT_VERSION >= 0x040000
00540 if ( d_data->valueEdit->hasFrame() )
00541 w += 2 * style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
00542 #else
00543 w += 2 * d_data->valueEdit->frameWidth();
00544 #endif
00545
00546
00547
00548
00549 w += QWidget::sizeHint().width() - d_data->valueEdit->sizeHint().width();
00550
00551 const int h = qwtMin(QWidget::sizeHint().height(),
00552 d_data->valueEdit->minimumSizeHint().height());
00553 return QSize(w, h);
00554 }
00555
00557 double QwtCounter::step() const
00558 {
00559 return QwtDoubleRange::step();
00560 }
00561
00567 void QwtCounter::setStep(double stepSize)
00568 {
00569 QwtDoubleRange::setStep(stepSize);
00570 }
00571
00573 double QwtCounter::minVal() const
00574 {
00575 return minValue();
00576 }
00577
00584 void QwtCounter::setMinValue(double value)
00585 {
00586 setRange(value, maxValue(), step());
00587 }
00588
00590 double QwtCounter::maxVal() const
00591 {
00592 return QwtDoubleRange::maxValue();
00593 }
00594
00601 void QwtCounter::setMaxValue(double value)
00602 {
00603 setRange(minValue(), value, step());
00604 }
00605
00610 void QwtCounter::setStepButton1(int nSteps)
00611 {
00612 setIncSteps(Button1, nSteps);
00613 }
00614
00616 int QwtCounter::stepButton1() const
00617 {
00618 return incSteps(Button1);
00619 }
00620
00625 void QwtCounter::setStepButton2(int nSteps)
00626 {
00627 setIncSteps(Button2, nSteps);
00628 }
00629
00631 int QwtCounter::stepButton2() const
00632 {
00633 return incSteps(Button2);
00634 }
00635
00640 void QwtCounter::setStepButton3(int nSteps)
00641 {
00642 setIncSteps(Button3, nSteps);
00643 }
00644
00646 int QwtCounter::stepButton3() const
00647 {
00648 return incSteps(Button3);
00649 }
00650
00652 double QwtCounter::value() const
00653 {
00654 return QwtDoubleRange::value();
00655 }
00656