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;
63 mClearButton->setVisible( shouldShowClearForValue( value() ) );
68 mClearButton->setVisible( shouldShowClearForValue( value() ) );
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 )
127 QString trimmedText = stripped( text );
128 if ( trimmedText.isEmpty() )
130 return mShowClearButton ?
clearValue() : value();
138 if ( !mExpressionsEnabled )
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();
195 QSize sz = mClearButton->sizeHint();
197 mClearButton->move( rect().right() - frameWidth() - 18 - sz.width(),
198 ( rect().bottom() + 1 - sz.height() ) / 2 );