1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
3
* Copyright (C) 1997 Josef Wilgen
4
* Copyright (C) 2002 Uwe Rathmann
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the Qwt License, Version 1.0
8
*****************************************************************************/
13
#include <qlineedit.h>
14
#include <qvalidator.h>
18
#include "qwt_counter.h"
19
#include "qwt_arrow_button.h"
21
class QwtCounter::PrivateData
27
increment[Button1] = 1;
28
increment[Button2] = 10;
29
increment[Button3] = 100;
32
QwtArrowButton *buttonDown[ButtonCnt];
33
QwtArrowButton *buttonUp[ButtonCnt];
36
int increment[ButtonCnt];
43
The default number of buttons is set to 2. The default increments are:
45
\li Button 2: 10 steps
46
\li Button 3: 100 steps
50
QwtCounter::QwtCounter(QWidget *parent):
56
#if QT_VERSION < 0x040000
58
The default number of buttons is set to 2. The default increments are:
60
\li Button 2: 10 steps
61
\li Button 3: 100 steps
65
QwtCounter::QwtCounter(QWidget *parent, const char *name):
72
void QwtCounter::initCounter()
74
d_data = new PrivateData;
76
#if QT_VERSION >= 0x040000
80
QHBoxLayout *layout = new QHBoxLayout(this);
81
layout->setSpacing(0);
85
for(i = ButtonCnt - 1; i >= 0; i--)
88
new QwtArrowButton(i+1, Qt::DownArrow,this);
89
btn->setFocusPolicy(NoFocus);
90
btn->installEventFilter(this);
91
layout->addWidget(btn);
93
connect(btn, SIGNAL(released()), SLOT(btnReleased()));
94
connect(btn, SIGNAL(clicked()), SLOT(btnClicked()));
96
d_data->buttonDown[i] = btn;
99
d_data->valueEdit = new QLineEdit(this);
100
d_data->valueEdit->setReadOnly(false);
101
d_data->valueEdit->setValidator(new QDoubleValidator(d_data->valueEdit));
102
layout->addWidget(d_data->valueEdit);
104
#if QT_VERSION >= 0x040000
105
connect( d_data->valueEdit, SIGNAL(editingFinished()),
106
SLOT(textChanged()) );
108
connect( d_data->valueEdit, SIGNAL(returnPressed()), SLOT(textChanged()) );
109
connect( d_data->valueEdit, SIGNAL(lostFocus()), SLOT(textChanged()) );
112
layout->setStretchFactor(d_data->valueEdit, 10);
114
for(i = 0; i < ButtonCnt; i++)
116
#if QT_VERSION >= 0x040000
119
QwtArrowButton *btn =
120
new QwtArrowButton(i+1, Qt::UpArrow, this);
121
btn->setFocusPolicy(NoFocus);
122
btn->installEventFilter(this);
123
layout->addWidget(btn);
125
connect(btn, SIGNAL(released()), SLOT(btnReleased()));
126
connect(btn, SIGNAL(clicked()), SLOT(btnClicked()));
128
d_data->buttonUp[i] = btn;
132
setRange(0.0,1.0,0.001);
136
QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
138
setFocusProxy(d_data->valueEdit);
139
setFocusPolicy(StrongFocus);
143
QwtCounter::~QwtCounter()
149
Sets the minimum width for the buttons
151
void QwtCounter::polish()
153
const int w = d_data->valueEdit->fontMetrics().width("W") + 8;
155
for ( int i = 0; i < ButtonCnt; i++ )
157
d_data->buttonDown[i]->setMinimumWidth(w);
158
d_data->buttonUp[i]->setMinimumWidth(w);
161
#if QT_VERSION < 0x040000
166
//! Set from lineedit
167
void QwtCounter::textChanged()
169
if ( !d_data->editable )
172
bool converted = false;
174
const double value = d_data->valueEdit->text().toDouble(&converted);
180
\brief Allow/disallow the user to manually edit the value
182
\param editable true enables editing
185
void QwtCounter::setEditable(bool editable)
187
#if QT_VERSION >= 0x040000
190
if ( editable == d_data->editable )
193
d_data->editable = editable;
194
d_data->valueEdit->setReadOnly(!editable);
197
//! returns whether the line edit is edatble. (default is yes)
198
bool QwtCounter::editable() const
200
return d_data->editable;
204
Handle PolishRequest events
206
bool QwtCounter::event ( QEvent * e )
208
#if QT_VERSION >= 0x040000
209
if ( e->type() == QEvent::PolishRequest )
212
return QWidget::event(e);
218
- Ctrl + Qt::Key_Home
223
Increment by incSteps(QwtCounter::Button1)
225
Decrement by incSteps(QwtCounter::Button1)
227
Increment by incSteps(QwtCounter::Button2)
229
Decrement by incSteps(QwtCounter::Button2)
230
- Shift + Qt::Key_PageUp
231
Increment by incSteps(QwtCounter::Button3)
232
- Shift + Qt::Key_PageDown
233
Decrement by incSteps(QwtCounter::Button3)
235
void QwtCounter::keyPressEvent (QKeyEvent *e)
237
bool accepted = true;
242
#if QT_VERSION >= 0x040000
243
if ( e->modifiers() & Qt::ControlModifier )
245
if ( e->state() & Qt::ControlButton )
247
setValue(minValue());
252
#if QT_VERSION >= 0x040000
253
if ( e->modifiers() & Qt::ControlModifier )
255
if ( e->state() & Qt::ControlButton )
257
setValue(maxValue());
262
incValue(d_data->increment[0]);
265
incValue(-d_data->increment[0]);
268
case Qt::Key_PageDown:
270
int increment = d_data->increment[0];
271
if ( d_data->nButtons >= 2 )
272
increment = d_data->increment[1];
273
if ( d_data->nButtons >= 3 )
275
#if QT_VERSION >= 0x040000
276
if ( e->modifiers() & Qt::ShiftModifier )
278
if ( e->state() & Qt::ShiftButton )
280
increment = d_data->increment[2];
282
if ( e->key() == Qt::Key_PageDown )
283
increment = -increment;
297
QWidget::keyPressEvent (e);
304
void QwtCounter::wheelEvent(QWheelEvent *e)
308
if ( d_data->nButtons <= 0 )
311
int increment = d_data->increment[0];
312
if ( d_data->nButtons >= 2 )
314
#if QT_VERSION >= 0x040000
315
if ( e->modifiers() & Qt::ControlModifier )
317
if ( e->state() & Qt::ControlButton )
319
increment = d_data->increment[1];
321
if ( d_data->nButtons >= 3 )
323
#if QT_VERSION >= 0x040000
324
if ( e->modifiers() & Qt::ShiftModifier )
326
if ( e->state() & Qt::ShiftButton )
328
increment = d_data->increment[2];
331
for ( int i = 0; i < d_data->nButtons; i++ )
333
if ( d_data->buttonDown[i]->geometry().contains(e->pos()) ||
334
d_data->buttonUp[i]->geometry().contains(e->pos()) )
336
increment = d_data->increment[i];
340
const int wheel_delta = 120;
342
int delta = e->delta();
343
if ( delta >= 2 * wheel_delta )
344
delta /= 2; // Never saw an abs(delta) < 240
346
incValue(delta / wheel_delta * increment);
350
Specify the number of steps by which the value
351
is incremented or decremented when a specified button
354
\param btn One of \c QwtCounter::Button1, \c QwtCounter::Button2,
355
\c QwtCounter::Button3
356
\param nSteps Number of steps
358
void QwtCounter::setIncSteps(QwtCounter::Button btn, int nSteps)
360
if (( btn >= 0) && (btn < ButtonCnt))
361
d_data->increment[btn] = nSteps;
365
\return the number of steps by which a specified button increments the value
366
or 0 if the button is invalid.
367
\param btn One of \c QwtCounter::Button1, \c QwtCounter::Button2,
368
\c QwtCounter::Button3
370
int QwtCounter::incSteps(QwtCounter::Button btn) const
372
if (( btn >= 0) && (btn < ButtonCnt))
373
return d_data->increment[btn];
379
\brief Set a new value
381
Calls QwtDoubleRange::setValue and does all visual updates.
382
\sa QwtDoubleRange::setValue()
385
void QwtCounter::setValue(double v)
387
QwtDoubleRange::setValue(v);
394
\brief Notify a change of value
396
void QwtCounter::valueChange()
401
d_data->valueEdit->setText(QString::null);
406
emit valueChanged(value());
410
\brief Update buttons according to the current value
412
When the QwtCounter under- or over-flows, the focus is set to the smallest
413
up- or down-button and counting is disabled.
415
Counting is re-enabled on a button release event (mouse or space bar).
417
void QwtCounter::updateButtons()
421
// 1. save enabled state of the smallest down- and up-button
422
// 2. change enabled state on under- or over-flow
424
for ( int i = 0; i < ButtonCnt; i++ )
426
d_data->buttonDown[i]->setEnabled(value() > minValue());
427
d_data->buttonUp[i]->setEnabled(value() < maxValue());
432
for ( int i = 0; i < ButtonCnt; i++ )
434
d_data->buttonDown[i]->setEnabled(false);
435
d_data->buttonUp[i]->setEnabled(false);
441
\brief Specify the number of buttons on each side of the label
442
\param n Number of buttons
444
void QwtCounter::setNumButtons(int n)
446
if ( n<0 || n>ButtonCnt )
449
for ( int i = 0; i < ButtonCnt; i++ )
453
d_data->buttonDown[i]->show();
454
d_data->buttonUp[i]->show();
458
d_data->buttonDown[i]->hide();
459
d_data->buttonUp[i]->hide();
463
d_data->nButtons = n;
467
\return The number of buttons on each side of the widget.
469
int QwtCounter::numButtons() const
471
return d_data->nButtons;
475
Display number string
479
void QwtCounter::showNum(double number)
484
const int cursorPos = d_data->valueEdit->cursorPosition();
485
d_data->valueEdit->setText(v);
486
d_data->valueEdit->setCursorPosition(cursorPos);
490
void QwtCounter::btnClicked()
492
for ( int i = 0; i < ButtonCnt; i++ )
494
if ( d_data->buttonUp[i] == sender() )
495
incValue(d_data->increment[i]);
497
if ( d_data->buttonDown[i] == sender() )
498
incValue(-d_data->increment[i]);
503
void QwtCounter::btnReleased()
505
emit buttonReleased(value());
509
\brief Notify change of range
511
This function updates the enabled property of
512
all buttons contained in QwtCounter.
514
void QwtCounter::rangeChange()
520
QSize QwtCounter::sizeHint() const
524
int w = tmp.setNum(minValue()).length();
525
int w1 = tmp.setNum(maxValue()).length();
528
w1 = tmp.setNum(minValue() + step()).length();
531
w1 = tmp.setNum(maxValue() - step()).length();
537
QFontMetrics fm(d_data->valueEdit->font());
538
w = fm.width(tmp) + 2;
539
#if QT_VERSION >= 0x040000
540
if ( d_data->valueEdit->hasFrame() )
541
w += 2 * style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
543
w += 2 * d_data->valueEdit->frameWidth();
546
// Now we replace default sizeHint contribution of d_data->valueEdit by
547
// what we really need.
549
w += QWidget::sizeHint().width() - d_data->valueEdit->sizeHint().width();
551
const int h = qwtMin(QWidget::sizeHint().height(),
552
d_data->valueEdit->minimumSizeHint().height());
556
//! returns the step size
557
double QwtCounter::step() const
559
return QwtDoubleRange::step();
564
\param stepSize Step size
565
\sa QwtDoubleRange::setStep()
567
void QwtCounter::setStep(double stepSize)
569
QwtDoubleRange::setStep(stepSize);
572
//! returns the minimum value of the range
573
double QwtCounter::minVal() const
579
Set the minimum value of the range
581
\param value Minimum value
582
\sa setMaxValue(), minVal()
584
void QwtCounter::setMinValue(double value)
586
setRange(value, maxValue(), step());
589
//! returns the maximum value of the range
590
double QwtCounter::maxVal() const
592
return QwtDoubleRange::maxValue();
596
Set the maximum value of the range
598
\param value Maximum value
599
\sa setMinValue(), maxVal()
601
void QwtCounter::setMaxValue(double value)
603
setRange(minValue(), value, step());
607
Set the number of increment steps for button 1
608
\param nSteps Number of steps
610
void QwtCounter::setStepButton1(int nSteps)
612
setIncSteps(Button1, nSteps);
615
//! returns the number of increment steps for button 1
616
int QwtCounter::stepButton1() const
618
return incSteps(Button1);
622
Set the number of increment steps for button 2
623
\param nSteps Number of steps
625
void QwtCounter::setStepButton2(int nSteps)
627
setIncSteps(Button2, nSteps);
630
//! returns the number of increment steps for button 2
631
int QwtCounter::stepButton2() const
633
return incSteps(Button2);
637
Set the number of increment steps for button 3
638
\param nSteps Number of steps
640
void QwtCounter::setStepButton3(int nSteps)
642
setIncSteps(Button3, nSteps);
645
//! returns the number of increment steps for button 3
646
int QwtCounter::stepButton3() const
648
return incSteps(Button3);
651
//! \return Current value
652
double QwtCounter::value() const
654
return QwtDoubleRange::value();