10#include "qwt_arrow_button.h"
11#include "qwt_counter.h"
12#include "qwt_painter.h"
17#include <qvalidator.h>
21class QwtCounter::PrivateData
71void QwtCounter::initCounter()
73 m_data =
new PrivateData;
75 QHBoxLayout* layout =
new QHBoxLayout(
this );
76 layout->setSpacing( 0 );
77 layout->setContentsMargins( QMargins() );
79 for (
int i =
ButtonCnt - 1; i >= 0; i-- )
83 btn->setFocusPolicy( Qt::NoFocus );
84 layout->addWidget( btn );
86 connect( btn, SIGNAL(released()), SLOT(btnReleased()) );
87 connect( btn, SIGNAL(clicked()), SLOT(btnClicked()) );
89 m_data->buttonDown[i] = btn;
92 m_data->valueEdit =
new QLineEdit(
this );
93 m_data->valueEdit->setReadOnly(
false );
94 m_data->valueEdit->setValidator(
new QDoubleValidator( m_data->valueEdit ) );
95 layout->addWidget( m_data->valueEdit );
97 connect( m_data->valueEdit, SIGNAL(editingFinished()), SLOT(textChanged()) );
99 layout->setStretchFactor( m_data->valueEdit, 10 );
105 btn->setFocusPolicy( Qt::NoFocus );
106 layout->addWidget( btn );
108 connect( btn, SIGNAL(released()), SLOT(btnReleased()) );
109 connect( btn, SIGNAL(clicked()), SLOT(btnClicked()) );
111 m_data->buttonUp[i] = btn;
120 QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed ) );
122 setFocusProxy( m_data->valueEdit );
123 setFocusPolicy( Qt::StrongFocus );
144 if ( on != m_data->isValid )
146 m_data->isValid = on;
150 if ( m_data->isValid )
152 showNumber(
value() );
157 m_data->valueEdit->setText( QString() );
168 return m_data->isValid;
179 m_data->valueEdit->setReadOnly( on );
188 return m_data->valueEdit->isReadOnly();
204 const double vmin = qwtMinF( m_data->minimum, m_data->maximum );
205 const double vmax = qwtMaxF( m_data->minimum, m_data->maximum );
209 if ( !m_data->isValid ||
value != m_data->value )
211 m_data->isValid =
true;
212 m_data->value =
value;
227 return m_data->value;
243 max = qwtMaxF( min, max );
245 if ( m_data->maximum == max && m_data->minimum == min )
248 m_data->minimum = min;
249 m_data->maximum = max;
253 const double value = qBound( min, m_data->value, max );
255 if (
value != m_data->value )
257 m_data->value =
value;
259 if ( m_data->isValid )
288 return m_data->minimum;
308 return m_data->maximum;
321 m_data->singleStep = qwtMaxF( stepSize, 0.0 );
330 return m_data->singleStep;
344 m_data->wrapping = on;
353 return m_data->wrapping;
371 m_data->buttonDown[i]->show();
372 m_data->buttonUp[i]->show();
376 m_data->buttonDown[i]->hide();
377 m_data->buttonUp[i]->hide();
390 return m_data->numButtons;
406 m_data->increment[ button ] = numSteps;
419 return m_data->increment[ button ];
471void QwtCounter::textChanged()
473 bool converted =
false;
475 const double value = m_data->valueEdit->text().toDouble( &converted );
487 if (
event->type() == QEvent::PolishRequest )
489 const QFontMetrics fm = m_data->valueEdit->fontMetrics();
494 m_data->buttonDown[i]->setMinimumWidth( w );
495 m_data->buttonUp[i]->setMinimumWidth( w );
499 return QWidget::event(
event );
526 bool accepted =
true;
528 switch (
event->key() )
532 if (
event->modifiers() & Qt::ControlModifier )
540 if (
event->modifiers() & Qt::ControlModifier )
548 incrementValue( m_data->increment[0] );
553 incrementValue( -m_data->increment[0] );
557 case Qt::Key_PageDown:
559 int increment = m_data->increment[0];
560 if ( m_data->numButtons >= 2 )
561 increment = m_data->increment[1];
562 if ( m_data->numButtons >= 3 )
564 if (
event->modifiers() & Qt::ShiftModifier )
565 increment = m_data->increment[2];
567 if (
event->key() == Qt::Key_PageDown )
568 increment = -increment;
569 incrementValue( increment );
584 QWidget::keyPressEvent (
event );
595 if ( m_data->numButtons <= 0 )
598 int increment = m_data->increment[0];
599 if ( m_data->numButtons >= 2 )
601 if (
event->modifiers() & Qt::ControlModifier )
602 increment = m_data->increment[1];
604 if ( m_data->numButtons >= 3 )
606 if (
event->modifiers() & Qt::ShiftModifier )
607 increment = m_data->increment[2];
610#if QT_VERSION < 0x050e00
611 const QPoint wheelPos =
event->pos();
612 const int wheelDelta =
event->delta();
614 const QPoint wheelPos =
event->position().toPoint();
616 const QPoint delta =
event->angleDelta();
617 const int wheelDelta = ( qAbs( delta.x() ) > qAbs( delta.y() ) )
618 ? delta.x() : delta.y();
621 for (
int i = 0; i < m_data->numButtons; i++ )
623 if ( m_data->buttonDown[i]->geometry().contains( wheelPos ) ||
624 m_data->buttonUp[i]->geometry().contains( wheelPos ) )
626 increment = m_data->increment[i];
630 incrementValue( wheelDelta / 120 * increment );
633void QwtCounter::incrementValue(
int numSteps )
635 const double min = m_data->minimum;
636 const double max = m_data->maximum;
637 double stepSize = m_data->singleStep;
639 if ( !m_data->isValid || min >= max || stepSize <= 0.0 )
644 stepSize = qwtMaxF( stepSize, 1.0e-10 * ( max - min ) );
647 double value = m_data->value + numSteps * stepSize;
649 if ( m_data->wrapping )
651 const double range = max - min;
655 value += std::ceil( ( min -
value ) / range ) * range;
657 else if (
value > max )
659 value -= std::ceil( (
value - max ) / range ) * range;
667 value = min + qRound( (
value - min ) / stepSize ) * stepSize;
669 if ( stepSize > 1e-12 )
671 if ( qFuzzyCompare(
value + 1.0, 1.0 ) )
676 else if ( qFuzzyCompare(
value, max ) )
683 if (
value != m_data->value )
685 m_data->value =
value;
686 showNumber( m_data->value );
702void QwtCounter::updateButtons()
704 if ( m_data->isValid )
711 m_data->buttonDown[i]->setEnabled(
value() >
minimum() );
719 m_data->buttonDown[i]->setEnabled(
false );
720 m_data->buttonUp[i]->setEnabled(
false );
729void QwtCounter::showNumber(
double number )
732 text.setNum( number );
734 const int cursorPos = m_data->valueEdit->cursorPosition();
735 m_data->valueEdit->setText( text );
736 m_data->valueEdit->setCursorPosition( cursorPos );
740void QwtCounter::btnClicked()
744 if ( m_data->buttonUp[i] == sender() )
745 incrementValue( m_data->increment[i] );
747 if ( m_data->buttonDown[i] == sender() )
748 incrementValue( -m_data->increment[i] );
753void QwtCounter::btnReleased()
763 int w = tmp.setNum(
minimum() ).length();
764 int w1 = tmp.setNum(
maximum() ).length();
778 if ( m_data->valueEdit->hasFrame() )
779 w += 2 * style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
784 w += QWidget::sizeHint().width() - m_data->valueEdit->sizeHint().width();
786 const int h = qMin( QWidget::sizeHint().height(),
787 m_data->valueEdit->minimumSizeHint().height() );
789 return QSize( w, h );
792#include "moc_qwt_counter.cpp"
@ Button1
Button intended for minor steps.
@ Button3
Button intended for large steps.
@ Button2
Button intended for medium steps.
@ ButtonCnt
Number of buttons.
void setStepButton2(int nSteps)
int stepButton3() const
returns the number of increment steps for button 3
void setValue(double)
Set a new value without adjusting to the step raster.
virtual QSize sizeHint() const override
A size hint.
virtual ~QwtCounter()
Destructor.
void buttonReleased(double value)
void setStepButton3(int nSteps)
double singleStep() const
void setWrapping(bool)
En/Disable wrapping.
int incSteps(QwtCounter::Button) const
virtual bool event(QEvent *) override
virtual void keyPressEvent(QKeyEvent *) override
void setIncSteps(QwtCounter::Button, int numSteps)
int stepButton2() const
returns the number of increment steps for button 2
void setReadOnly(bool)
Allow/disallow the user to manually edit the value.
void setRange(double min, double max)
Set the minimum and maximum values.
void valueChanged(double value)
void setStepButton1(int nSteps)
QwtCounter(QWidget *parent=NULL)
void setSingleStep(double stepSize)
Set the step size of the counter.
virtual void wheelEvent(QWheelEvent *) override
int stepButton1() const
returns the number of increment steps for button 1
static int horizontalAdvance(const QFontMetrics &, const QString &)