22#include "moc_qgsspinbox.cpp"
28#define CLEAR_ICON_SIZE 16
34static QChar SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );
36static constexpr QChar SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );
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 ( QSpinBox::* )(
int )
> ( &QSpinBox::valueChanged ),
this, &QgsSpinBox::changed );
63 mExpressionsEnabled = enabled;
68 QSpinBox::changeEvent( event );
70 if ( event->type() == QEvent::FontChange )
72 lineEdit()->setFont( font() );
75 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
80 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
81 QSpinBox::paintEvent( event );
86 const int step = singleStep();
87 if ( event->modifiers() & Qt::ControlModifier )
90 int newStep = step / 10;
92 newStep = std::max( newStep, 1 );
94 setSingleStep( newStep );
99 event->setModifiers( event->modifiers() & ~Qt::ControlModifier );
101 QSpinBox::wheelEvent( event );
102 setSingleStep( step );
111 qApp->processEvents();
112 if ( QApplication::mouseButtons() & Qt::LeftButton )
113 QSpinBox::timerEvent( event );
116void QgsSpinBox::changed(
int value )
118 mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
124 if ( mLineEdit->isNull() )
130 if ( mClearValueMode ==
CustomValue && mCustomClearValue == customValue && QAbstractSpinBox::specialValueText() == specialValueText )
136 mCustomClearValue = customValue;
138 if ( !specialValueText.isEmpty() )
140 const int v = value();
149 if ( mClearValueMode == mode && mCustomClearValue == 0 && QAbstractSpinBox::specialValueText() == specialValueText )
154 mClearValueMode = mode;
155 mCustomClearValue = 0;
157 if ( !specialValueText.isEmpty() )
159 const int v = value();
173 return mCustomClearValue;
178 mLineEdit->setAlignment( alignment );
185 QSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
186 mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
190 QSpinBox::setSpecialValueText( txt );
191 mLineEdit->setNullValue( txt );
197 if ( !mExpressionsEnabled )
199 return QSpinBox::valueFromText( text );
202 const QString trimmedText = stripped( text );
203 if ( trimmedText.isEmpty() )
205 return mShowClearButton ?
clearValue() : value();
213 if ( !mExpressionsEnabled )
215 const QValidator::State r = QSpinBox::validate( input, pos );
219 return QValidator::Acceptable;
224 const bool wasNull = mShowClearButton && value() ==
clearValue();
225 if ( wasNull && minimum() < 0 && maximum() > 0 && !( specialValueText().isEmpty() || specialValueText() == SPECIAL_TEXT_WHEN_EMPTY ) )
233 QSpinBox::stepBy( steps );
236int QgsSpinBox::frameWidth()
const
238 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
241bool QgsSpinBox::shouldShowClearForValue(
const int value )
const
243 if ( !mShowClearButton || !isEnabled() )
250QString QgsSpinBox::stripped(
const QString &originalText )
const
254 QString text = originalText;
255 if ( specialValueText().isEmpty() || text != specialValueText() )
258 if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
259 text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
261 int size = text.size();
262 bool changed =
false;
263 if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
265 from += prefix().size();
269 if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
271 size -= suffix().size();
275 text = text.mid( from, size );
278 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.
void returnPressed()
Emitted when the Return or Enter key is used in the line edit.
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 textEdited(const QString &text)
Emitted when the the value has been manually edited via line edit.
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.