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();
51 connect(
this,
static_cast<void ( QSpinBox::* )(
int )
>( &QSpinBox::valueChanged ),
this, &QgsSpinBox::changed );
62 mExpressionsEnabled = enabled;
67 QSpinBox::changeEvent( event );
69 if ( event->type() == QEvent::FontChange )
71 lineEdit()->setFont( font() );
74 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
79 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
80 QSpinBox::paintEvent( event );
85 const int step = singleStep();
86 if ( event->modifiers() & Qt::ControlModifier )
89 int newStep = step / 10;
91 newStep = std::max( newStep, 1 );
93 setSingleStep( newStep );
98 event->setModifiers( event->modifiers() & ~Qt::ControlModifier );
100 QSpinBox::wheelEvent( event );
101 setSingleStep( step );
110 qApp->processEvents();
111 if ( QApplication::mouseButtons() & Qt::LeftButton )
112 QSpinBox::timerEvent( event );
115void QgsSpinBox::changed(
int value )
117 mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
123 if ( mLineEdit->isNull() )
129 if ( mClearValueMode ==
CustomValue && mCustomClearValue == customValue && QAbstractSpinBox::specialValueText() == specialValueText )
135 mCustomClearValue = customValue;
137 if ( !specialValueText.isEmpty() )
139 const int v = value();
148 if ( mClearValueMode == mode && mCustomClearValue == 0 && QAbstractSpinBox::specialValueText() == specialValueText )
153 mClearValueMode = mode;
154 mCustomClearValue = 0;
156 if ( !specialValueText.isEmpty() )
158 const int v = value();
172 return mCustomClearValue;
177 mLineEdit->setAlignment( alignment );
184 QSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
185 mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
189 QSpinBox::setSpecialValueText( txt );
190 mLineEdit->setNullValue( txt );
196 if ( !mExpressionsEnabled )
198 return QSpinBox::valueFromText( text );
201 const QString trimmedText = stripped( text );
202 if ( trimmedText.isEmpty() )
204 return mShowClearButton ?
clearValue() : value();
212 if ( !mExpressionsEnabled )
214 const QValidator::State r = QSpinBox::validate( input, pos );
218 return QValidator::Acceptable;
223 const bool wasNull = mShowClearButton && value() ==
clearValue();
224 if ( wasNull && minimum() < 0 && maximum() > 0 && !( specialValueText().isEmpty() || specialValueText() == SPECIAL_TEXT_WHEN_EMPTY ) )
232 QSpinBox::stepBy( steps );
235int QgsSpinBox::frameWidth()
const
237 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
240bool QgsSpinBox::shouldShowClearForValue(
const int value )
const
242 if ( !mShowClearButton || !isEnabled() )
249QString QgsSpinBox::stripped(
const QString &originalText )
const
253 QString text = originalText;
254 if ( specialValueText().isEmpty() || text != specialValueText() )
257 if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
258 text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
260 int size = text.size();
261 bool changed =
false;
262 if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
264 from += prefix().size();
268 if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
270 size -= suffix().size();
274 text = text.mid( from, size );
277 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.