17 #include <QMouseEvent> 27 #define CLEAR_ICON_SIZE 16 30 : QDoubleSpinBox( parent )
32 mLineEdit =
new QgsSpinBoxLineEdit();
35 setLineEdit( mLineEdit );
37 QSize msz = minimumSizeHint();
38 setMinimumSize( msz.width() +
CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
42 connect(
this, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsDoubleSpinBox::changed );
48 mLineEdit->setShowClearButton( showClearButton );
53 mExpressionsEnabled = enabled;
58 QDoubleSpinBox::changeEvent( event );
59 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
64 double step = singleStep();
65 if ( event->modifiers() & Qt::ControlModifier )
68 double newStep = step / 10;
71 newStep = std::max( newStep, std::pow( 10.0, 0.0 - decimals() ) );
73 setSingleStep( newStep );
78 event->setModifiers( event->modifiers() & ~Qt::ControlModifier );
80 QDoubleSpinBox::wheelEvent( event );
81 setSingleStep( step );
86 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
87 QDoubleSpinBox::paintEvent( event );
90 void QgsDoubleSpinBox::changed(
double value )
92 mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
103 mCustomClearValue = customValue;
105 if ( !specialValueText.isEmpty() )
109 setSpecialValueText( specialValueText );
116 mClearValueMode = mode;
117 mCustomClearValue = 0;
119 if ( !clearValueText.isEmpty() )
123 setSpecialValueText( clearValueText );
135 return mCustomClearValue;
140 mLineEdit->setAlignment( alignment );
143 QString QgsDoubleSpinBox::stripped(
const QString &originalText )
const 147 QString text = originalText;
148 if ( specialValueText().isEmpty() || text != specialValueText() )
151 int size = text.size();
152 bool changed =
false;
153 if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
155 from += prefix().size();
159 if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
161 size -= suffix().size();
165 text = text.mid( from, size );
168 text = text.trimmed();
175 if ( !mExpressionsEnabled )
177 return QDoubleSpinBox::valueFromText( text );
180 QString trimmedText = stripped( text );
181 if ( trimmedText.isEmpty() )
183 return mShowClearButton ?
clearValue() : value();
191 if ( !mExpressionsEnabled )
193 QValidator::State r = QDoubleSpinBox::validate( input, pos );
197 return QValidator::Acceptable;
200 int QgsDoubleSpinBox::frameWidth()
const 202 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
205 bool QgsDoubleSpinBox::shouldShowClearForValue(
const double value )
const 207 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() )
static double evaluateToDouble(const QString &text, double fallbackValue)
Attempts to evaluate a text string as an expression to a resultant double value.
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.
bool showClearButton() const
Returns whether the widget is showing a clear button.
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...
double clearValue() const
Returns the value used when clear() is called.
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...
void cleared()
Emitted when the widget is cleared.