17 #include <QMouseEvent>
27 #define CLEAR_ICON_SIZE 16
33 static QChar SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );
35 static constexpr QChar SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );
40 : QDoubleSpinBox( parent )
42 mLineEdit =
new QgsSpinBoxLineEdit();
45 setLineEdit( mLineEdit );
47 QSize msz = minimumSizeHint();
48 setMinimumSize( msz.width() +
CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
52 connect(
this,
static_cast < void ( QDoubleSpinBox::* )(
double )
> ( &QDoubleSpinBox::valueChanged ),
this, &QgsDoubleSpinBox::changed );
63 mExpressionsEnabled = enabled;
68 QDoubleSpinBox::changeEvent( event );
70 if ( event->type() == QEvent::FontChange )
72 lineEdit()->setFont( font() );
75 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
80 double step = singleStep();
81 if ( event->modifiers() & Qt::ControlModifier )
84 double newStep = step / 10;
87 newStep = std::max( newStep, std::pow( 10.0, 0.0 - decimals() ) );
89 setSingleStep( newStep );
94 event->setModifiers( event->modifiers() & ~Qt::ControlModifier );
96 QDoubleSpinBox::wheelEvent( event );
97 setSingleStep( step );
106 qApp->processEvents();
107 if ( QApplication::mouseButtons() & Qt::LeftButton )
108 QDoubleSpinBox::timerEvent( event );
113 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
114 QDoubleSpinBox::paintEvent( event );
117 void QgsDoubleSpinBox::changed(
double value )
119 mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
125 if ( mLineEdit->isNull() )
132 mCustomClearValue = customValue;
134 if ( !specialValueText.isEmpty() )
145 mClearValueMode = mode;
146 mCustomClearValue = 0;
148 if ( !clearValueText.isEmpty() )
164 return mCustomClearValue;
169 mLineEdit->setAlignment( alignment );
176 QDoubleSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
177 mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
181 QDoubleSpinBox::setSpecialValueText( txt );
182 mLineEdit->setNullValue( txt );
186 QString QgsDoubleSpinBox::stripped(
const QString &originalText )
const
190 QString text = originalText;
191 if ( specialValueText().isEmpty() || text != specialValueText() )
194 if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
195 text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
197 int size = text.size();
198 bool changed =
false;
199 if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
201 from += prefix().size();
205 if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
207 size -= suffix().size();
211 text = text.mid( from, size );
214 text = text.trimmed();
221 if ( !mExpressionsEnabled )
223 return QDoubleSpinBox::valueFromText( text );
226 QString trimmedText = stripped( text );
227 if ( trimmedText.isEmpty() )
229 return mShowClearButton ?
clearValue() : value();
237 if ( !mExpressionsEnabled )
239 QValidator::State r = QDoubleSpinBox::validate( input, pos );
243 return QValidator::Acceptable;
246 int QgsDoubleSpinBox::frameWidth()
const
248 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
251 bool QgsDoubleSpinBox::shouldShowClearForValue(
const double value )
const
253 if ( !mShowClearButton || !isEnabled() )
void paintEvent(QPaintEvent *e) override
void wheelEvent(QWheelEvent *event) override
void setLineEditAlignment(Qt::Alignment alignment)
Set alignment in the embedded line edit widget.
double valueFromText(const QString &text) const 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...
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 changeEvent(QEvent *event) override
void clear() override
Sets the current value to the value defined by the clear value.
ClearValueMode
Behavior when widget is cleared.
@ MaximumValue
Reset value to maximum()
@ CustomValue
Reset value to custom value (see setClearValue() )
@ MinimumValue
Reset value to minimum()
QValidator::State validate(QString &input, int &pos) const override
QgsDoubleSpinBox(QWidget *parent=nullptr)
Constructor for QgsDoubleSpinBox.
void setExpressionsEnabled(bool enabled)
Sets if the widget will allow entry of simple expressions, which are evaluated and then discarded.
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 setShowClearButton(bool showClearButton)
Sets whether the widget will show a clear button.
void timerEvent(QTimerEvent *event) override
static double evaluateToDouble(const QString &text, double fallbackValue)
Attempts to evaluate a text string as an expression to a resultant double value.
void cleared()
Emitted when the widget is cleared.