17 #include <QMouseEvent> 27 #define CLEAR_ICON_SIZE 16 32 mLineEdit =
new QgsSpinBoxLineEdit();
33 setLineEdit( mLineEdit );
35 QSize msz = minimumSizeHint();
36 setMinimumSize( msz.width() +
CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
40 connect(
this, static_cast <
void ( QSpinBox::* )(
int ) > ( &QSpinBox::valueChanged ),
this, &QgsSpinBox::changed );
46 mLineEdit->setShowClearButton( showClearButton );
51 mExpressionsEnabled = enabled;
56 QSpinBox::changeEvent( event );
57 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
62 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
63 QSpinBox::paintEvent( event );
68 int step = singleStep();
69 if ( event->modifiers() & Qt::ControlModifier )
72 int newStep = step / 10;
74 newStep = std::max( newStep, 1 );
76 setSingleStep( newStep );
81 event->setModifiers( event->modifiers() & ~Qt::ControlModifier );
83 QSpinBox::wheelEvent( event );
84 setSingleStep( step );
87 void QgsSpinBox::changed(
int value )
89 mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
100 mCustomClearValue = customValue;
102 if ( !specialValueText.isEmpty() )
106 setSpecialValueText( specialValueText );
113 mClearValueMode = mode;
114 mCustomClearValue = 0;
116 if ( !specialValueText.isEmpty() )
120 setSpecialValueText( specialValueText );
132 return mCustomClearValue;
137 mLineEdit->setAlignment( alignment );
142 if ( !mExpressionsEnabled )
144 return QSpinBox::valueFromText( text );
147 QString trimmedText = stripped( text );
148 if ( trimmedText.isEmpty() )
150 return mShowClearButton ?
clearValue() : value();
158 if ( !mExpressionsEnabled )
160 QValidator::State r = QSpinBox::validate( input, pos );
164 return QValidator::Acceptable;
167 int QgsSpinBox::frameWidth()
const 169 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
172 bool QgsSpinBox::shouldShowClearForValue(
const int value )
const 174 if ( !mShowClearButton || !isEnabled() )
181 QString QgsSpinBox::stripped(
const QString &originalText )
const 185 QString text = originalText;
186 if ( specialValueText().isEmpty() || text != specialValueText() )
189 int size = text.size();
190 bool changed =
false;
191 if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
193 from += prefix().size();
197 if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
199 size -= suffix().size();
203 text = text.mid( from, size );
206 text = text.trimmed();
void wheelEvent(QWheelEvent *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