17 #include <QMouseEvent> 20 #include <QToolButton> 28 : QDoubleSpinBox( parent )
29 , mShowClearButton( true )
30 , mClearValueMode( MinimumValue )
31 , mCustomClearValue( 0.0 )
32 , mExpressionsEnabled( true )
34 mClearButton =
new QToolButton(
this );
36 mClearButton->setCursor( Qt::ArrowCursor );
37 mClearButton->setStyleSheet(
"position: absolute; border: none; padding: 0px;" );
38 connect( mClearButton, SIGNAL( clicked() ),
this, SLOT(
clear() ) );
40 setStyleSheet( QString(
"padding-right: %1px;" ).arg( mClearButton->sizeHint().width() + 18 + frameWidth() + 1 ) );
42 QSize msz = minimumSizeHint();
43 setMinimumSize( qMax( msz.width(), mClearButton->sizeHint().height() + frameWidth() * 2 + 2 ),
44 qMax( msz.height(), mClearButton->sizeHint().height() + frameWidth() * 2 + 2 ) );
46 connect(
this, SIGNAL( valueChanged(
double ) ),
this, SLOT( changed(
double ) ) );
52 mClearButton->setVisible( shouldShowClearForValue( value() ) );
57 mExpressionsEnabled = enabled;
62 QDoubleSpinBox::changeEvent( event );
63 mClearButton->setVisible( shouldShowClearForValue( value() ) );
68 mClearButton->setVisible( shouldShowClearForValue( value() ) );
69 QDoubleSpinBox::paintEvent( event );
72 void QgsDoubleSpinBox::changed(
const double& value )
74 mClearButton->setVisible( shouldShowClearForValue( value ) );
85 mCustomClearValue = customValue;
87 if ( !specialValueText.isEmpty() )
91 setSpecialValueText( specialValueText );
98 mClearValueMode = mode;
99 mCustomClearValue = 0;
101 if ( !clearValueText.isEmpty() )
105 setSpecialValueText( clearValueText );
117 return mCustomClearValue;
120 QString QgsDoubleSpinBox::stripped(
const QString &originalText )
const 124 QString text = originalText;
125 if ( specialValueText().size() == 0 || text != specialValueText() )
128 int size = text.size();
129 bool changed =
false;
130 if ( prefix().size() && text.startsWith( prefix() ) )
132 from += prefix().size();
136 if ( suffix().size() && text.endsWith( suffix() ) )
138 size -= suffix().size();
142 text = text.mid( from, size );
145 text = text.trimmed();
152 if ( !mExpressionsEnabled )
154 return QDoubleSpinBox::valueFromText( text );
157 QString trimmedText = stripped( text );
158 if ( trimmedText.isEmpty() )
160 return mShowClearButton ?
clearValue() : value();
168 if ( !mExpressionsEnabled )
170 QValidator::State r = QDoubleSpinBox::validate( input, pos );
174 return QValidator::Acceptable;
177 int QgsDoubleSpinBox::frameWidth()
const 179 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
182 bool QgsDoubleSpinBox::shouldShowClearForValue(
const double value )
const 184 if ( !mShowClearButton || !isEnabled() )
193 QDoubleSpinBox::resizeEvent( event );
195 QSize sz = mClearButton->sizeHint();
197 mClearButton->move( rect().right() - frameWidth() - 18 - sz.width(),
198 ( rect().bottom() + 1 - sz.height() ) / 2 );
virtual QValidator::State validate(QString &input, int &pos) const override
virtual void resizeEvent(QResizeEvent *event) override
bool showClearButton() const
QgsDoubleSpinBox(QWidget *parent=0)
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
virtual void clear() override
Set the current value to the value defined by the clear value.
void setClearValue(double customValue, QString clearValueText=QString())
setClearValue defines the clear value as a custom value and will automatically set the clear value mo...
void setShowClearButton(const bool showClearButton)
determines if the widget will show a clear button
virtual void paintEvent(QPaintEvent *event) override
virtual double valueFromText(const QString &text) const override
void setClearValueMode(ClearValueMode mode, QString clearValueText=QString())
setClearValueMode defines if the clear value should be the minimum or maximum values of the widget or...
virtual void changeEvent(QEvent *event) override
double clearValue() const
returns the value used when clear() is called.
static double evaluateToDouble(const QString &text, const double fallbackValue)
Attempts to evaluate a text string as an expression to a resultant double value.
void setExpressionsEnabled(const bool enabled)
Sets if the widget will allow entry of simple expressions, which are evaluated and then discarded...