17 #include <QMouseEvent> 22 #include "qgsexpression.h" 27 #define CLEAR_ICON_SIZE 16 30 : QDoubleSpinBox( parent )
32 mLineEdit =
new QgsSpinBoxLineEdit();
36 setLineEdit( mLineEdit );
38 QSize msz = minimumSizeHint();
39 setMinimumSize( msz.width() +
CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
43 connect(
this, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsDoubleSpinBox::changed );
49 mLineEdit->setShowClearButton( showClearButton );
54 mExpressionsEnabled = enabled;
59 QDoubleSpinBox::changeEvent( event );
60 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
65 double step = singleStep();
66 if ( event->modifiers() & Qt::ControlModifier )
69 double newStep = step / 10;
72 newStep = std::max( newStep, std::pow( 10.0, 0.0 - decimals() ) );
74 setSingleStep( newStep );
79 event->setModifiers( event->modifiers() & ~Qt::ControlModifier );
81 QDoubleSpinBox::wheelEvent( event );
82 setSingleStep( step );
87 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
88 QDoubleSpinBox::paintEvent( event );
91 void QgsDoubleSpinBox::changed(
double value )
93 mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
104 mCustomClearValue = customValue;
106 if ( !specialValueText.isEmpty() )
110 setSpecialValueText( specialValueText );
117 mClearValueMode = mode;
118 mCustomClearValue = 0;
120 if ( !clearValueText.isEmpty() )
124 setSpecialValueText( clearValueText );
136 return mCustomClearValue;
141 mLineEdit->setAlignment( alignment );
144 QString QgsDoubleSpinBox::stripped(
const QString &originalText )
const 148 QString text = originalText;
149 if ( specialValueText().isEmpty() || text != specialValueText() )
152 int size = text.size();
153 bool changed =
false;
154 if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
156 from += prefix().size();
160 if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
162 size -= suffix().size();
166 text = text.mid( from, size );
169 text = text.trimmed();
176 if ( !mExpressionsEnabled )
178 return QDoubleSpinBox::valueFromText( text );
181 QString trimmedText = stripped( text );
182 if ( trimmedText.isEmpty() )
184 return mShowClearButton ?
clearValue() : value();
187 return QgsExpression::evaluateToDouble( trimmedText, value() );
192 if ( !mExpressionsEnabled )
194 QValidator::State r = QDoubleSpinBox::validate( input, pos );
198 return QValidator::Acceptable;
201 int QgsDoubleSpinBox::frameWidth()
const 203 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
206 bool QgsDoubleSpinBox::shouldShowClearForValue(
const double value )
const 208 if ( !mShowClearButton || !isEnabled() )
void wheelEvent(QWheelEvent *event) override
static QString locale()
Returns the QGIS locale.
void paintEvent(QPaintEvent *e) override
QValidator::State validate(QString &input, int &pos) const override
Reset value to custom value (see setClearValue() )
QgsDoubleSpinBox(QWidget *parent=nullptr)
Constructor for QgsDoubleSpinBox.
ClearValueMode
Behavior when widget is cleared.
void clear() override
Set the current value to the value defined by the clear value.
void setShowClearButton(const bool showClearButton)
Sets whether the widget will show a clear button.
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.
void setExpressionsEnabled(const bool enabled)
Sets if the widget will allow entry of simple expressions, which are evaluated and then discarded...