17 #include <QMouseEvent> 27 #define CLEAR_ICON_SIZE 16 32 QString QgsDoubleSpinBox::SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );
36 : QDoubleSpinBox( parent )
38 mLineEdit =
new QgsSpinBoxLineEdit();
41 setLineEdit( mLineEdit );
43 QSize msz = minimumSizeHint();
44 setMinimumSize( msz.width() +
CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
48 connect(
this, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsDoubleSpinBox::changed );
54 mLineEdit->setShowClearButton( showClearButton );
59 mExpressionsEnabled = enabled;
64 QDoubleSpinBox::changeEvent( event );
66 if ( event->type() == QEvent::FontChange )
68 lineEdit()->setFont( font() );
71 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
76 double step = singleStep();
77 if ( event->modifiers() & Qt::ControlModifier )
80 double newStep = step / 10;
83 newStep = std::max( newStep, std::pow( 10.0, 0.0 - decimals() ) );
85 setSingleStep( newStep );
90 event->setModifiers( event->modifiers() & ~Qt::ControlModifier );
92 QDoubleSpinBox::wheelEvent( event );
93 setSingleStep( step );
98 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
99 QDoubleSpinBox::paintEvent( event );
102 void QgsDoubleSpinBox::changed(
double value )
104 mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
110 if ( mLineEdit->isNull() )
117 mCustomClearValue = customValue;
119 if ( !specialValueText.isEmpty() )
130 mClearValueMode = mode;
131 mCustomClearValue = 0;
133 if ( !clearValueText.isEmpty() )
149 return mCustomClearValue;
154 mLineEdit->setAlignment( alignment );
161 QDoubleSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
162 mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
166 QDoubleSpinBox::setSpecialValueText( txt );
167 mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
171 QString QgsDoubleSpinBox::stripped(
const QString &originalText )
const 175 QString text = originalText;
176 if ( specialValueText().isEmpty() || text != specialValueText() )
179 if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
180 text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
182 int size = text.size();
183 bool changed =
false;
184 if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
186 from += prefix().size();
190 if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
192 size -= suffix().size();
196 text = text.mid( from, size );
199 text = text.trimmed();
206 if ( !mExpressionsEnabled )
208 return QDoubleSpinBox::valueFromText( text );
211 QString trimmedText = stripped( text );
212 if ( trimmedText.isEmpty() )
214 return mShowClearButton ?
clearValue() : value();
222 if ( !mExpressionsEnabled )
224 QValidator::State r = QDoubleSpinBox::validate( input, pos );
228 return QValidator::Acceptable;
231 int QgsDoubleSpinBox::frameWidth()
const 233 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
236 bool QgsDoubleSpinBox::shouldShowClearForValue(
const double value )
const 238 if ( !mShowClearButton || !isEnabled() )
void wheelEvent(QWheelEvent *event) override
void paintEvent(QPaintEvent *e) override
void setExpressionsEnabled(bool enabled)
Sets if the widget will allow entry of simple expressions, which are evaluated and then discarded...
QValidator::State validate(QString &input, int &pos) const override
void setShowClearButton(bool showClearButton)
Sets whether the widget will show a clear button.
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.
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...
QgsDoubleSpinBox(QWidget *parent=nullptr)
Constructor for QgsDoubleSpinBox.
ClearValueMode
Behavior when widget is cleared.
void clear() override
Sets the current value to the value defined by the clear value.
double valueFromText(const QString &text) const override
void setClearValue(double customValue, const QString &clearValueText=QString())
Defines the clear value as a custom value and will automatically set the clear value mode to CustomVa...
void changeEvent(QEvent *event) override
void setLineEditAlignment(Qt::Alignment alignment)
Set alignment in the embedded line edit widget.
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...
double clearValue() const
Returns the value used when clear() is called.
void cleared()
Emitted when the widget is cleared.