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 const 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 const 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() )
136 const double v = value();
145 mClearValueMode = mode;
146 mCustomClearValue = 0;
148 if ( !clearValueText.isEmpty() )
150 const double v = value();
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 const QString trimmedText = stripped( text );
227 if ( trimmedText.isEmpty() )
229 return mShowClearButton ?
clearValue() : value();
237 if ( !mExpressionsEnabled )
239 const 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() )