27#define CLEAR_ICON_SIZE 16
33static QChar SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );
35static constexpr QChar SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );
40 : QDoubleSpinBox( parent )
42 mLineEdit =
new QgsSpinBoxLineEdit();
45 setLineEdit( mLineEdit );
47 const QSize msz = minimumSizeHint();
48 setMinimumSize( msz.width() +
CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
52 connect(
this,
static_cast < void ( QDoubleSpinBox::* )(
double )
> ( &QDoubleSpinBox::valueChanged ),
this, &QgsDoubleSpinBox::changed );
63 mExpressionsEnabled = enabled;
68 QDoubleSpinBox::changeEvent( event );
70 if ( event->type() == QEvent::FontChange )
72 lineEdit()->setFont( font() );
75 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
80 const double step = singleStep();
81 if ( event->modifiers() & Qt::ControlModifier )
84 double newStep = step / 10;
87 newStep = std::max( newStep, std::pow( 10.0, 0.0 - decimals() ) );
89 setSingleStep( newStep );
94 event->setModifiers( event->modifiers() & ~Qt::ControlModifier );
96 QDoubleSpinBox::wheelEvent( event );
97 setSingleStep( step );
106 qApp->processEvents();
107 if ( QApplication::mouseButtons() & Qt::LeftButton )
108 QDoubleSpinBox::timerEvent( event );
113 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
114 QDoubleSpinBox::paintEvent( event );
117void QgsDoubleSpinBox::changed(
double value )
119 mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
125 if ( mLineEdit->isNull() )
131 if ( mClearValueMode ==
CustomValue && mCustomClearValue == customValue && QAbstractSpinBox::specialValueText() == specialValueText )
137 mCustomClearValue = customValue;
139 if ( !specialValueText.isEmpty() )
141 const double v = value();
150 if ( mClearValueMode == mode && mCustomClearValue == 0 && QAbstractSpinBox::specialValueText() == clearValueText )
155 mClearValueMode = mode;
156 mCustomClearValue = 0;
158 if ( !clearValueText.isEmpty() )
160 const double v = value();
174 return mCustomClearValue;
179 mLineEdit->setAlignment( alignment );
186 QDoubleSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
187 mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
191 QDoubleSpinBox::setSpecialValueText( txt );
192 mLineEdit->setNullValue( txt );
196QString QgsDoubleSpinBox::stripped(
const QString &originalText )
const
200 QString text = originalText;
201 if ( specialValueText().isEmpty() || text != specialValueText() )
204 if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
205 text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
207 int size = text.size();
208 bool changed =
false;
209 if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
211 from += prefix().size();
215 if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
217 size -= suffix().size();
221 text = text.mid( from, size );
224 text = text.trimmed();
231 if ( !mExpressionsEnabled )
233 return QDoubleSpinBox::valueFromText( text );
236 const QString trimmedText = stripped( text );
237 if ( trimmedText.isEmpty() )
239 return mShowClearButton ?
clearValue() : value();
247 if ( !mExpressionsEnabled )
249 const QValidator::State r = QDoubleSpinBox::validate( input, pos );
253 return QValidator::Acceptable;
256int QgsDoubleSpinBox::frameWidth()
const
258 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
261bool QgsDoubleSpinBox::shouldShowClearForValue(
const double value )
const
263 if ( !mShowClearButton || !isEnabled() )
void paintEvent(QPaintEvent *e) override
void wheelEvent(QWheelEvent *event) override
void setLineEditAlignment(Qt::Alignment alignment)
Set alignment in the embedded line edit widget.
double valueFromText(const QString &text) const override
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...
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 changeEvent(QEvent *event) override
void clear() override
Sets the current value to the value defined by the clear value.
ClearValueMode
Behavior when widget is cleared.
@ MaximumValue
Reset value to maximum()
@ CustomValue
Reset value to custom value (see setClearValue() )
@ MinimumValue
Reset value to minimum()
QValidator::State validate(QString &input, int &pos) const override
QgsDoubleSpinBox(QWidget *parent=nullptr)
Constructor for QgsDoubleSpinBox.
void setExpressionsEnabled(bool enabled)
Sets if the widget will allow entry of simple expressions, which are evaluated and then discarded.
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...
void setShowClearButton(bool showClearButton)
Sets whether the widget will show a clear button.
void timerEvent(QTimerEvent *event) override
static double evaluateToDouble(const QString &text, double fallbackValue)
Attempts to evaluate a text string as an expression to a resultant double value.
void cleared()
Emitted when the widget is cleared.