17 #include <QMouseEvent> 27 #define CLEAR_ICON_SIZE 16 32 QString QgsDoubleSpinBox::SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );
36 : QDoubleSpinBox( parent )
38 mLineEdit =
new QgsSpinBoxLineEdit();
41 setLineEdit( mLineEdit );
43 QSize msz = minimumSizeHint();
44 setMinimumSize( msz.width() +
CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
48 connect(
this, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsDoubleSpinBox::changed );
54 mLineEdit->setShowClearButton( showClearButton );
59 mExpressionsEnabled = enabled;
64 QDoubleSpinBox::changeEvent( event );
66 if ( event->type() == QEvent::FontChange )
68 lineEdit()->setFont( font() );
71 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
76 double step = singleStep();
77 if ( event->modifiers() & Qt::ControlModifier )
80 double newStep = step / 10;
83 newStep = std::max( newStep, std::pow( 10.0, 0.0 - decimals() ) );
85 setSingleStep( newStep );
90 event->setModifiers( event->modifiers() & ~Qt::ControlModifier );
92 QDoubleSpinBox::wheelEvent( event );
93 setSingleStep( step );
102 qApp->processEvents();
103 if ( QApplication::mouseButtons() & Qt::LeftButton )
104 QDoubleSpinBox::timerEvent( event );
109 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
110 QDoubleSpinBox::paintEvent( event );
113 void QgsDoubleSpinBox::changed(
double value )
115 mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
121 if ( mLineEdit->isNull() )
128 mCustomClearValue = customValue;
130 if ( !specialValueText.isEmpty() )
141 mClearValueMode = mode;
142 mCustomClearValue = 0;
144 if ( !clearValueText.isEmpty() )
160 return mCustomClearValue;
165 mLineEdit->setAlignment( alignment );
172 QDoubleSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
173 mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
177 QDoubleSpinBox::setSpecialValueText( txt );
178 mLineEdit->setNullValue( txt );
182 QString QgsDoubleSpinBox::stripped(
const QString &originalText )
const 186 QString text = originalText;
187 if ( specialValueText().isEmpty() || text != specialValueText() )
190 if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
191 text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
193 int size = text.size();
194 bool changed =
false;
195 if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
197 from += prefix().size();
201 if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
203 size -= suffix().size();
207 text = text.mid( from, size );
210 text = text.trimmed();
217 if ( !mExpressionsEnabled )
219 return QDoubleSpinBox::valueFromText( text );
222 QString trimmedText = stripped( text );
223 if ( trimmedText.isEmpty() )
225 return mShowClearButton ?
clearValue() : value();
233 if ( !mExpressionsEnabled )
235 QValidator::State r = QDoubleSpinBox::validate( input, pos );
239 return QValidator::Acceptable;
242 int QgsDoubleSpinBox::frameWidth()
const 244 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
247 bool QgsDoubleSpinBox::shouldShowClearForValue(
const double value )
const 249 if ( !mShowClearButton || !isEnabled() )
void wheelEvent(QWheelEvent *event) override
void paintEvent(QPaintEvent *e) override
void setExpressionsEnabled(bool enabled)
Sets if the widget will allow entry of simple expressions, which are evaluated and then discarded...
QValidator::State validate(QString &input, int &pos) const override
void setShowClearButton(bool showClearButton)
Sets whether the widget will show a clear button.
Reset value to custom value (see setClearValue() )
static double evaluateToDouble(const QString &text, double fallbackValue)
Attempts to evaluate a text string as an expression to a resultant double value.
void setSpecialValueText(const QString &txt)
Set the special-value text to be txt If set, the spin box will display this text instead of a numeric...
QgsDoubleSpinBox(QWidget *parent=nullptr)
Constructor for QgsDoubleSpinBox.
ClearValueMode
Behavior when widget is cleared.
void clear() override
Sets the current value to the value defined by the clear value.
bool showClearButton() const
Returns whether the widget is showing a clear button.
double valueFromText(const QString &text) const override
void setClearValue(double customValue, const QString &clearValueText=QString())
Defines the clear value as a custom value and will automatically set the clear value mode to CustomVa...
double clearValue() const
Returns the value used when clear() is called.
void timerEvent(QTimerEvent *event) override
void changeEvent(QEvent *event) override
void setLineEditAlignment(Qt::Alignment alignment)
Set alignment in the embedded line edit widget.
void setClearValueMode(ClearValueMode mode, const QString &clearValueText=QString())
Defines if the clear value should be the minimum or maximum values of the widget or a custom value...
void cleared()
Emitted when the widget is cleared.