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;
219int QgsSpinBox::frameWidth()
const
221 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
224bool QgsSpinBox::shouldShowClearForValue(
const int value )
const
226 if ( !mShowClearButton || !isEnabled() )
233QString QgsSpinBox::stripped(
const QString &originalText )
const
237 QString text = originalText;
238 if ( specialValueText().isEmpty() || text != specialValueText() )
241 if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
242 text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
244 int size = text.size();
245 bool changed =
false;
246 if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
248 from += prefix().size();
252 if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
254 size -= suffix().size();
258 text = text.mid( from, size );
261 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 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.