The Counter Widget. More...
#include <qwt_counter.h>
Classes | |
class | PrivateData |
Public Types | |
enum | Button { Button1, Button2, Button3, ButtonCnt } |
Signals | |
void | buttonReleased (double value) |
void | valueChanged (double value) |
Public Member Functions | |
QwtCounter (QWidget *parent=NULL) | |
QwtCounter (QWidget *parent, const char *name) | |
virtual | ~QwtCounter () |
Destructor. | |
bool | editable () const |
void | setEditable (bool) |
Allow/disallow the user to manually edit the value. | |
void | setNumButtons (int n) |
Specify the number of buttons on each side of the label. | |
int | numButtons () const |
void | setIncSteps (QwtCounter::Button btn, int nSteps) |
int | incSteps (QwtCounter::Button btn) const |
virtual void | setValue (double) |
Set a new value. | |
virtual QSize | sizeHint () const |
A size hint. | |
virtual void | polish () |
double | step () const |
returns the step size | |
void | setStep (double s) |
double | minVal () const |
returns the minimum value of the range | |
void | setMinValue (double m) |
double | maxVal () const |
returns the maximum value of the range | |
void | setMaxValue (double m) |
void | setStepButton1 (int nSteps) |
int | stepButton1 () const |
void | setStepButton2 (int nSteps) |
int | stepButton2 () const |
void | setStepButton3 (int nSteps) |
int | stepButton3 () const |
virtual double | value () const |
Returns the current value. | |
Protected Member Functions | |
virtual bool | event (QEvent *) |
virtual void | wheelEvent (QWheelEvent *) |
virtual void | keyPressEvent (QKeyEvent *) |
virtual void | rangeChange () |
Notify change of range. | |
Properties | |
int | numButtons |
double | basicstep |
double | minValue |
double | maxValue |
int | stepButton1 |
returns the number of increment steps for button 1 | |
int | stepButton2 |
returns the number of increment steps for button 2 | |
int | stepButton3 |
returns the number of increment steps for button 3 | |
double | value |
bool | editable |
returns whether the line edit is edatble. (default is yes) |
The Counter Widget.
A Counter consists of a label displaying a number and one ore more (up to three) push buttons on each side of the label which can be used to increment or decrement the counter's value.
A Counter has a range from a minimum value to a maximum value and a step size. The range can be specified using QwtDblRange::setRange(). The counter's value is an integer multiple of the step size. The number of steps by which a button increments or decrements the value can be specified using QwtCounter::setIncSteps(). The number of buttons can be changed with QwtCounter::setNumButtons().
Holding the space bar down with focus on a button is the fastest method to step through the counter values. When the counter underflows/overflows, the focus is set to the smallest up/down button and counting is disabled. Counting is re-enabled on a button release event (mouse or space bar).
Example:
#include "../include/qwt_counter.h> QwtCounter *cnt; cnt = new QwtCounter(parent, name); cnt->setRange(0.0, 100.0, 1.0); // From 0.0 to 100, step 1.0 cnt->setNumButtons(2); // Two buttons each side cnt->setIncSteps(QwtCounter::Button1, 1); // Button 1 increments 1 step cnt->setIncSteps(QwtCounter::Button2, 20); // Button 2 increments 20 steps connect(cnt, SIGNAL(valueChanged(double)), my_class, SLOT(newValue(double)));
Definition at line 60 of file qwt_counter.h.
enum QwtCounter::Button |
QwtCounter::QwtCounter | ( | QWidget * | parent = NULL ) |
[explicit] |
The default number of buttons is set to 2. The default increments are:
parent |
Definition at line 50 of file qwt_counter.cpp.
: QWidget(parent) { initCounter(); }
QwtCounter::QwtCounter | ( | QWidget * | parent, |
const char * | name | ||
) | [explicit] |
The default number of buttons is set to 2. The default increments are:
parent |
Definition at line 65 of file qwt_counter.cpp.
: QWidget(parent, name) { initCounter(); }
QwtCounter::~QwtCounter | ( | ) | [virtual] |
void QwtCounter::buttonReleased | ( | double | value ) | [signal] |
This signal is emitted when a button has been released
value | The new value |
bool QwtCounter::editable | ( | ) | const |
Referenced by setEditable().
bool QwtCounter::event | ( | QEvent * | e ) | [protected, virtual] |
Handle PolishRequest events
Definition at line 206 of file qwt_counter.cpp.
References polish().
{ #if QT_VERSION >= 0x040000 if ( e->type() == QEvent::PolishRequest ) polish(); #endif return QWidget::event(e); }
int QwtCounter::incSteps | ( | QwtCounter::Button | btn ) | const |
btn | One of QwtCounter::Button1 , QwtCounter::Button2 , QwtCounter::Button3 |
Definition at line 370 of file qwt_counter.cpp.
References ButtonCnt.
void QwtCounter::keyPressEvent | ( | QKeyEvent * | e ) | [protected, virtual] |
Handle key events
Definition at line 235 of file qwt_counter.cpp.
References QwtDoubleRange::incValue(), mitk::Key_Down, mitk::Key_End, mitk::Key_Home, mitk::Key_PageDown, mitk::Key_PageUp, mitk::Key_Up, QwtDoubleRange::maxValue(), QwtDoubleRange::minValue(), and setValue().
{ bool accepted = true; switch ( e->key() ) { case Qt::Key_Home: #if QT_VERSION >= 0x040000 if ( e->modifiers() & Qt::ControlModifier ) #else if ( e->state() & Qt::ControlButton ) #endif setValue(minValue()); else accepted = false; break; case Qt::Key_End: #if QT_VERSION >= 0x040000 if ( e->modifiers() & Qt::ControlModifier ) #else if ( e->state() & Qt::ControlButton ) #endif 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 ) { #if QT_VERSION >= 0x040000 if ( e->modifiers() & Qt::ShiftModifier ) #else if ( e->state() & Qt::ShiftButton ) #endif increment = d_data->increment[2]; } if ( e->key() == Qt::Key_PageDown ) increment = -increment; incValue(increment); break; } default: accepted = false; } if ( accepted ) { e->accept(); return; } QWidget::keyPressEvent (e); }
double QwtCounter::maxVal | ( | ) | const |
returns the maximum value of the range
Definition at line 590 of file qwt_counter.cpp.
References QwtDoubleRange::maxValue().
{ return QwtDoubleRange::maxValue(); }
double QwtCounter::minVal | ( | ) | const |
returns the minimum value of the range
Definition at line 573 of file qwt_counter.cpp.
References QwtDoubleRange::minValue().
{ return minValue(); }
int QwtCounter::numButtons | ( | ) | const |
void QwtCounter::polish | ( | ) | [virtual] |
Sets the minimum width for the buttons
Definition at line 151 of file qwt_counter.cpp.
References ButtonCnt.
Referenced by event().
{ const int w = d_data->valueEdit->fontMetrics().width("W") + 8; for ( int i = 0; i < ButtonCnt; i++ ) { d_data->buttonDown[i]->setMinimumWidth(w); d_data->buttonUp[i]->setMinimumWidth(w); } #if QT_VERSION < 0x040000 QWidget::polish(); #endif }
void QwtCounter::rangeChange | ( | ) | [protected, virtual] |
Notify change of range.
This function updates the enabled property of all buttons contained in QwtCounter.
Reimplemented from QwtDoubleRange.
Definition at line 514 of file qwt_counter.cpp.
{ updateButtons(); }
void QwtCounter::setEditable | ( | bool | editable ) |
Allow/disallow the user to manually edit the value.
editable | true enables editing |
Definition at line 185 of file qwt_counter.cpp.
References editable().
void QwtCounter::setIncSteps | ( | QwtCounter::Button | btn, |
int | nSteps | ||
) |
Specify the number of steps by which the value is incremented or decremented when a specified button is pushed.
btn | One of QwtCounter::Button1 , QwtCounter::Button2 , QwtCounter::Button3 |
nSteps | Number of steps |
Definition at line 358 of file qwt_counter.cpp.
References ButtonCnt.
Referenced by setStepButton1(), setStepButton2(), and setStepButton3().
void QwtCounter::setMaxValue | ( | double | value ) |
Set the maximum value of the range
value | Maximum value |
Definition at line 601 of file qwt_counter.cpp.
References QwtDoubleRange::minValue(), QwtDoubleRange::setRange(), and step().
void QwtCounter::setMinValue | ( | double | value ) |
Set the minimum value of the range
value | Minimum value |
Definition at line 584 of file qwt_counter.cpp.
References QwtDoubleRange::maxValue(), QwtDoubleRange::setRange(), and step().
void QwtCounter::setNumButtons | ( | int | n ) |
Specify the number of buttons on each side of the label.
n | Number of buttons |
Definition at line 444 of file qwt_counter.cpp.
References ButtonCnt.
{ if ( n<0 || n>ButtonCnt ) return; for ( int i = 0; i < ButtonCnt; i++ ) { if ( i < n ) { d_data->buttonDown[i]->show(); d_data->buttonUp[i]->show(); } else { d_data->buttonDown[i]->hide(); d_data->buttonUp[i]->hide(); } } d_data->nButtons = n; }
void QwtCounter::setStep | ( | double | stepSize ) |
Set the step size
stepSize | Step size |
Reimplemented from QwtDoubleRange.
Definition at line 567 of file qwt_counter.cpp.
{ QwtDoubleRange::setStep(stepSize); }
void QwtCounter::setStepButton1 | ( | int | nSteps ) |
Set the number of increment steps for button 1
nSteps | Number of steps |
Definition at line 610 of file qwt_counter.cpp.
References Button1, and setIncSteps().
{ setIncSteps(Button1, nSteps); }
void QwtCounter::setStepButton2 | ( | int | nSteps ) |
Set the number of increment steps for button 2
nSteps | Number of steps |
Definition at line 625 of file qwt_counter.cpp.
References Button2, and setIncSteps().
{ setIncSteps(Button2, nSteps); }
void QwtCounter::setStepButton3 | ( | int | nSteps ) |
Set the number of increment steps for button 3
nSteps | Number of steps |
Definition at line 640 of file qwt_counter.cpp.
References Button3, and setIncSteps().
{ setIncSteps(Button3, nSteps); }
void QwtCounter::setValue | ( | double | v ) | [virtual] |
Set a new value.
v | new value Calls QwtDoubleRange::setValue and does all visual updates. |
Reimplemented from QwtDoubleRange.
Definition at line 385 of file qwt_counter.cpp.
References value().
Referenced by keyPressEvent().
{ QwtDoubleRange::setValue(v); showNum(value()); updateButtons(); }
QSize QwtCounter::sizeHint | ( | ) | const [virtual] |
A size hint.
Definition at line 520 of file qwt_counter.cpp.
References QwtDoubleRange::maxValue(), QwtDoubleRange::minValue(), qwtMin, and step().
{ 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 w += 2 * d_data->valueEdit->frameWidth(); #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(); const int h = qwtMin(QWidget::sizeHint().height(), d_data->valueEdit->minimumSizeHint().height()); return QSize(w, h); }
double QwtCounter::step | ( | ) | const |
returns the step size
Reimplemented from QwtDoubleRange.
Definition at line 557 of file qwt_counter.cpp.
Referenced by setMaxValue(), setMinValue(), and sizeHint().
{ return QwtDoubleRange::step(); }
int QwtCounter::stepButton1 | ( | ) | const |
int QwtCounter::stepButton2 | ( | ) | const |
int QwtCounter::stepButton3 | ( | ) | const |
virtual double QwtCounter::value | ( | ) | const [virtual] |
void QwtCounter::valueChanged | ( | double | value ) | [signal] |
This signal is emitted when the counter's value has changed
value | The new value |
void QwtCounter::wheelEvent | ( | QWheelEvent * | e ) | [protected, virtual] |
Handle wheel events
e | Wheel event |
Definition at line 304 of file qwt_counter.cpp.
References QwtDoubleRange::incValue().
{ e->accept(); if ( d_data->nButtons <= 0 ) return; int increment = d_data->increment[0]; if ( d_data->nButtons >= 2 ) { #if QT_VERSION >= 0x040000 if ( e->modifiers() & Qt::ControlModifier ) #else if ( e->state() & Qt::ControlButton ) #endif increment = d_data->increment[1]; } if ( d_data->nButtons >= 3 ) { #if QT_VERSION >= 0x040000 if ( e->modifiers() & Qt::ShiftModifier ) #else if ( e->state() & Qt::ShiftButton ) #endif increment = d_data->increment[2]; } for ( int i = 0; i < d_data->nButtons; i++ ) { if ( d_data->buttonDown[i]->geometry().contains(e->pos()) || d_data->buttonUp[i]->geometry().contains(e->pos()) ) { 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); }
double QwtCounter::basicstep [read, write] |
Definition at line 65 of file qwt_counter.h.
bool QwtCounter::editable [read, write] |
returns whether the line edit is edatble. (default is yes)
Definition at line 72 of file qwt_counter.h.
double QwtCounter::maxValue [read, write] |
Definition at line 67 of file qwt_counter.h.
double QwtCounter::minValue [read, write] |
Definition at line 66 of file qwt_counter.h.
int QwtCounter::numButtons [read, write] |
Definition at line 64 of file qwt_counter.h.
int QwtCounter::stepButton1 [read, write] |
returns the number of increment steps for button 1
Definition at line 68 of file qwt_counter.h.
int QwtCounter::stepButton2 [read, write] |
returns the number of increment steps for button 2
Definition at line 69 of file qwt_counter.h.
int QwtCounter::stepButton3 [read, write] |
returns the number of increment steps for button 3
Definition at line 70 of file qwt_counter.h.
double QwtCounter::value [read, write] |
Definition at line 71 of file qwt_counter.h.