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