27#define CLEAR_ICON_SIZE 16
33static QChar SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );
35static constexpr QChar SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );
41 mLineEdit =
new QgsSpinBoxLineEdit();
42 setLineEdit( mLineEdit );
44 const QSize msz = minimumSizeHint();
45 setMinimumSize( msz.width() +
CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
49 connect(
this,
static_cast < void ( QSpinBox::* )(
int )
> ( &QSpinBox::valueChanged ),
this, &QgsSpinBox::changed );
60 mExpressionsEnabled = enabled;
65 QSpinBox::changeEvent( event );
67 if ( event->type() == QEvent::FontChange )
69 lineEdit()->setFont( font() );
72 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
77 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
78 QSpinBox::paintEvent( event );
83 const int step = singleStep();
84 if ( event->modifiers() & Qt::ControlModifier )
87 int newStep = step / 10;
89 newStep = std::max( newStep, 1 );
91 setSingleStep( newStep );
96 event->setModifiers( event->modifiers() & ~Qt::ControlModifier );
98 QSpinBox::wheelEvent( event );
99 setSingleStep( step );
108 qApp->processEvents();
109 if ( QApplication::mouseButtons() & Qt::LeftButton )
110 QSpinBox::timerEvent( event );
113void QgsSpinBox::changed(
int value )
115 mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
121 if ( mLineEdit->isNull() )
127 if ( mClearValueMode ==
CustomValue && mCustomClearValue == customValue && QAbstractSpinBox::specialValueText() == specialValueText )
133 mCustomClearValue = customValue;
135 if ( !specialValueText.isEmpty() )
137 const int v = value();
146 if ( mClearValueMode == mode && mCustomClearValue == 0 && QAbstractSpinBox::specialValueText() == specialValueText )
151 mClearValueMode = mode;
152 mCustomClearValue = 0;
154 if ( !specialValueText.isEmpty() )
156 const int v = value();
170 return mCustomClearValue;
175 mLineEdit->setAlignment( alignment );
182 QSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
183 mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
187 QSpinBox::setSpecialValueText( txt );
188 mLineEdit->setNullValue( txt );
194 if ( !mExpressionsEnabled )
196 return QSpinBox::valueFromText( text );
199 const QString trimmedText = stripped( text );
200 if ( trimmedText.isEmpty() )
202 return mShowClearButton ?
clearValue() : value();
210 if ( !mExpressionsEnabled )
212 const QValidator::State r = QSpinBox::validate( input, pos );
216 return QValidator::Acceptable;
221 const bool wasNull = mShowClearButton && value() ==
clearValue();
222 if ( wasNull && minimum() < 0 && maximum() > 0 )
230 QSpinBox::stepBy( steps );
233int QgsSpinBox::frameWidth()
const
235 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
238bool QgsSpinBox::shouldShowClearForValue(
const int value )
const
240 if ( !mShowClearButton || !isEnabled() )
247QString QgsSpinBox::stripped(
const QString &originalText )
const
251 QString text = originalText;
252 if ( specialValueText().isEmpty() || text != specialValueText() )
255 if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
256 text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
258 int size = text.size();
259 bool changed =
false;
260 if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
262 from += prefix().size();
266 if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
268 size -= suffix().size();
272 text = text.mid( from, size );
275 text = text.trimmed();
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.
void setLineEditAlignment(Qt::Alignment alignment)
Set alignment in the embedded line edit widget.
ClearValueMode
Behavior when widget is cleared.
@ MaximumValue
Reset value to maximum()
@ MinimumValue
Reset value to minimum()
@ CustomValue
Reset value to custom value (see setClearValue() )
void wheelEvent(QWheelEvent *event) override
void setShowClearButton(bool showClearButton)
Sets whether the widget will show a clear button.
QgsSpinBox(QWidget *parent=nullptr)
Constructor for QgsSpinBox.
QValidator::State validate(QString &input, int &pos) const override
void stepBy(int steps) override
void paintEvent(QPaintEvent *event) override
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.
int valueFromText(const QString &text) const override
void timerEvent(QTimerEvent *event) override
void setClearValue(int customValue, const QString &clearValueText=QString())
Defines the clear value as a custom value and will automatically set the clear value mode to CustomVa...
void setExpressionsEnabled(bool enabled)
Sets if the widget will allow entry of simple expressions, which are evaluated and then discarded.
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 changeEvent(QEvent *event) override
void clear() override
Sets the current value to the value defined by the clear value.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.