17 #include <QMouseEvent> 20 #include <QToolButton> 29 , mShowClearButton( true )
30 , mClearValueMode( MinimumValue )
31 , mCustomClearValue( 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(
int ) ),
this, SLOT( changed(
int ) ) );
52 mClearButton->setVisible( shouldShowClearForValue( value() ) );
57 mExpressionsEnabled = enabled;
62 QSpinBox::changeEvent( event );
63 mClearButton->setVisible( shouldShowClearForValue( value() ) );
68 mClearButton->setVisible( shouldShowClearForValue( value() ) );
69 QSpinBox::paintEvent( event );
72 void QgsSpinBox::changed(
const int& value )
74 mClearButton->setVisible( shouldShowClearForValue( value ) );
85 mCustomClearValue = customValue;
87 if ( !specialValueText.isEmpty() )
91 setSpecialValueText( specialValueText );
98 mClearValueMode = mode;
99 mCustomClearValue = 0;
101 if ( !specialValueText.isEmpty() )
105 setSpecialValueText( specialValueText );
117 return mCustomClearValue;
122 if ( !mExpressionsEnabled )
124 return QSpinBox::valueFromText( text );
127 QString trimmedText = stripped( text );
128 if ( trimmedText.isEmpty() )
130 return mShowClearButton ?
clearValue() : value();
138 if ( !mExpressionsEnabled )
140 QValidator::State r = QSpinBox::validate( input, pos );
144 return QValidator::Acceptable;
147 int QgsSpinBox::frameWidth()
const 149 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
152 bool QgsSpinBox::shouldShowClearForValue(
const int value )
const 154 if ( !mShowClearButton || !isEnabled() )
161 QString QgsSpinBox::stripped(
const QString &originalText )
const 165 QString text = originalText;
166 if ( specialValueText().size() == 0 || text != specialValueText() )
169 int size = text.size();
170 bool changed =
false;
171 if ( prefix().size() && text.startsWith( prefix() ) )
173 from += prefix().size();
177 if ( suffix().size() && text.endsWith( suffix() ) )
179 size -= suffix().size();
183 text = text.mid( from, size );
186 text = text.trimmed();
193 QSpinBox::resizeEvent( event );
195 QSize sz = mClearButton->sizeHint();
197 mClearButton->move( rect().right() - frameWidth() - 18 - sz.width(),
198 ( rect().bottom() + 1 - sz.height() ) / 2 );
void setClearValueMode(ClearValueMode mode, QString clearValueText=QString())
setClearValueMode defines if the clear value should be the minimum or maximum values of the widget or...
void setExpressionsEnabled(const bool enabled)
Sets if the widget will allow entry of simple expressions, which are evaluated and then discarded...
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
virtual QValidator::State validate(QString &input, int &pos) const override
virtual void clear() override
Set the current value to the value defined by the clear value.
virtual void resizeEvent(QResizeEvent *event) override
void setShowClearButton(const bool showClearButton)
determines if the widget will show a clear button
virtual void paintEvent(QPaintEvent *event) override
virtual int valueFromText(const QString &text) const override
int clearValue() const
returns the value used when clear() is called.
virtual void changeEvent(QEvent *event) override
void setClearValue(int customValue, QString clearValueText=QString())
setClearValue defines the clear value for the widget and will automatically set the clear value mode ...
bool showClearButton() const
QgsSpinBox(QWidget *parent=0)
static double evaluateToDouble(const QString &text, const double fallbackValue)
Attempts to evaluate a text string as an expression to a resultant double value.