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();
46 setLineEdit( mLineEdit );
48 const QSize msz = minimumSizeHint();
49 setMinimumSize( msz.width() +
CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
53 connect(
this,
static_cast < void ( QDoubleSpinBox::* )(
double )
> ( &QDoubleSpinBox::valueChanged ),
this, &QgsDoubleSpinBox::changed );
64 mExpressionsEnabled = enabled;
69 QDoubleSpinBox::changeEvent( event );
71 if ( event->type() == QEvent::FontChange )
73 lineEdit()->setFont( font() );
76 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
81 const double step = singleStep();
82 if ( event->modifiers() & Qt::ControlModifier )
85 double newStep = step / 10;
88 newStep = std::max( newStep, std::pow( 10.0, 0.0 - decimals() ) );
90 setSingleStep( newStep );
95 event->setModifiers( event->modifiers() & ~Qt::ControlModifier );
97 QDoubleSpinBox::wheelEvent( event );
98 setSingleStep( step );
107 qApp->processEvents();
108 if ( QApplication::mouseButtons() & Qt::LeftButton )
109 QDoubleSpinBox::timerEvent( event );
114 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
115 QDoubleSpinBox::paintEvent( event );
120 const bool wasNull = mShowClearButton && value() ==
clearValue();
121 if ( wasNull && minimum() < 0 && maximum() > 0 && !( specialValueText().isEmpty() || specialValueText() == SPECIAL_TEXT_WHEN_EMPTY ) )
129 QDoubleSpinBox::stepBy( steps );
132void QgsDoubleSpinBox::changed(
double value )
134 mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
140 if ( mLineEdit->isNull() )
146 if ( mClearValueMode ==
CustomValue && mCustomClearValue == customValue && QAbstractSpinBox::specialValueText() == specialValueText )
152 mCustomClearValue = customValue;
154 if ( !specialValueText.isEmpty() )
156 const double v = value();
165 if ( mClearValueMode == mode && mCustomClearValue == 0 && QAbstractSpinBox::specialValueText() == clearValueText )
170 mClearValueMode = mode;
171 mCustomClearValue = 0;
173 if ( !clearValueText.isEmpty() )
175 const double v = value();
189 return mCustomClearValue;
194 mLineEdit->setAlignment( alignment );
201 QDoubleSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
202 mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
206 QDoubleSpinBox::setSpecialValueText( txt );
207 mLineEdit->setNullValue( txt );
211QString QgsDoubleSpinBox::stripped(
const QString &originalText )
const
215 QString text = originalText;
216 if ( specialValueText().isEmpty() || text != specialValueText() )
219 if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
220 text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
222 int size = text.size();
223 bool changed =
false;
224 if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
226 from += prefix().size();
230 if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
232 size -= suffix().size();
236 text = text.mid( from, size );
239 text = text.trimmed();
246 if ( !mExpressionsEnabled )
248 return QDoubleSpinBox::valueFromText( text );
251 const QString trimmedText = stripped( text );
252 if ( trimmedText.isEmpty() )
254 return mShowClearButton ?
clearValue() : value();
262 if ( !mExpressionsEnabled )
264 const QValidator::State r = QDoubleSpinBox::validate( input, pos );
268 return QValidator::Acceptable;
271int QgsDoubleSpinBox::frameWidth()
const
273 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
276bool QgsDoubleSpinBox::shouldShowClearForValue(
const double value )
const
278 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.
void stepBy(int steps) override
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
void returnPressed()
Emitted when the Return or Enter key is used in the line edit.
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.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.