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 );
105 qApp->processEvents();
106 if ( QApplication::mouseButtons() & Qt::LeftButton )
107 QSpinBox::timerEvent( event );
110 void QgsSpinBox::changed(
int value )
112 mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
118 if ( mLineEdit->isNull() )
125 mCustomClearValue = customValue;
127 if ( !specialValueText.isEmpty() )
138 mClearValueMode = mode;
139 mCustomClearValue = 0;
141 if ( !specialValueText.isEmpty() )
157 return mCustomClearValue;
162 mLineEdit->setAlignment( alignment );
169 QSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
170 mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
174 QSpinBox::setSpecialValueText( txt );
175 mLineEdit->setNullValue( txt );
181 if ( !mExpressionsEnabled )
183 return QSpinBox::valueFromText( text );
186 QString trimmedText = stripped( text );
187 if ( trimmedText.isEmpty() )
189 return mShowClearButton ?
clearValue() : value();
197 if ( !mExpressionsEnabled )
199 QValidator::State r = QSpinBox::validate( input, pos );
203 return QValidator::Acceptable;
206 int QgsSpinBox::frameWidth()
const 208 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
211 bool QgsSpinBox::shouldShowClearForValue(
const int value )
const 213 if ( !mShowClearButton || !isEnabled() )
220 QString QgsSpinBox::stripped(
const QString &originalText )
const 224 QString text = originalText;
225 if ( specialValueText().isEmpty() || text != specialValueText() )
228 if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
229 text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
231 int size = text.size();
232 bool changed =
false;
233 if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
235 from += prefix().size();
239 if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
241 size -= suffix().size();
245 text = text.mid( from, size );
248 text = text.trimmed();
void wheelEvent(QWheelEvent *event) override
void timerEvent(QTimerEvent *event) override
ClearValueMode
Behavior when widget is cleared.
Reset value to custom value (see setClearValue() )
bool showClearButton() const
Returns whether the widget is showing a clear button.
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...
int clearValue() const
Returns the value used when clear() is called.
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.
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...