17 #include <QMouseEvent> 27 #define CLEAR_ICON_SIZE 16 32 QString QgsSpinBox::SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );
38 mLineEdit =
new QgsSpinBoxLineEdit();
39 setLineEdit( mLineEdit );
41 QSize msz = minimumSizeHint();
42 setMinimumSize( msz.width() +
CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
46 connect(
this, static_cast <
void ( QSpinBox::* )(
int ) > ( &QSpinBox::valueChanged ),
this, &QgsSpinBox::changed );
52 mLineEdit->setShowClearButton( showClearButton );
57 mExpressionsEnabled = enabled;
62 QSpinBox::changeEvent( event );
64 if ( event->type() == QEvent::FontChange )
66 lineEdit()->setFont( font() );
69 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
74 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
75 QSpinBox::paintEvent( event );
80 int step = singleStep();
81 if ( event->modifiers() & Qt::ControlModifier )
84 int newStep = step / 10;
86 newStep = std::max( newStep, 1 );
88 setSingleStep( newStep );
93 event->setModifiers( event->modifiers() & ~Qt::ControlModifier );
95 QSpinBox::wheelEvent( event );
96 setSingleStep( step );
99 void QgsSpinBox::changed(
int value )
101 mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
107 if ( mLineEdit->isNull() )
114 mCustomClearValue = customValue;
116 if ( !specialValueText.isEmpty() )
127 mClearValueMode = mode;
128 mCustomClearValue = 0;
130 if ( !specialValueText.isEmpty() )
146 return mCustomClearValue;
151 mLineEdit->setAlignment( alignment );
158 QSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
159 mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
163 QSpinBox::setSpecialValueText( txt );
164 mLineEdit->setNullValue( txt );
170 if ( !mExpressionsEnabled )
172 return QSpinBox::valueFromText( text );
175 QString trimmedText = stripped( text );
176 if ( trimmedText.isEmpty() )
178 return mShowClearButton ?
clearValue() : value();
186 if ( !mExpressionsEnabled )
188 QValidator::State r = QSpinBox::validate( input, pos );
192 return QValidator::Acceptable;
195 int QgsSpinBox::frameWidth()
const 197 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
200 bool QgsSpinBox::shouldShowClearForValue(
const int value )
const 202 if ( !mShowClearButton || !isEnabled() )
209 QString QgsSpinBox::stripped(
const QString &originalText )
const 213 QString text = originalText;
214 if ( specialValueText().isEmpty() || text != specialValueText() )
217 if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
218 text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
220 int size = text.size();
221 bool changed =
false;
222 if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
224 from += prefix().size();
228 if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
230 size -= suffix().size();
234 text = text.mid( from, size );
237 text = text.trimmed();
void wheelEvent(QWheelEvent *event) override
ClearValueMode
Behavior when widget is cleared.
Reset value to custom value (see setClearValue() )
static double evaluateToDouble(const QString &text, double fallbackValue)
Attempts to evaluate a text string as an expression to a resultant double value.
QValidator::State validate(QString &input, int &pos) const override
void clear() override
Sets the current value to the value defined by the clear value.
void setShowClearButton(bool showClearButton)
Sets whether the widget will show a clear button.
void setLineEditAlignment(Qt::Alignment alignment)
Set alignment in the embedded line edit widget.
QgsSpinBox(QWidget *parent=nullptr)
Constructor for QgsSpinBox.
void setExpressionsEnabled(bool enabled)
Sets if the widget will allow entry of simple expressions, which are evaluated and then discarded...
void paintEvent(QPaintEvent *event) override
int valueFromText(const QString &text) const 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...
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 cleared()
Emitted when the widget is cleared.
int clearValue() const
Returns the value used when clear() is called.
void changeEvent(QEvent *event) 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...
bool showClearButton() const
Returns whether the widget is showing a clear button.