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 );
41 mLineEdit =
new QgsSpinBoxLineEdit();
42 setLineEdit( mLineEdit );
44 const QSize msz = minimumSizeHint();
45 setMinimumSize( msz.width() +
CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
49 connect(
this,
static_cast < void ( QSpinBox::* )(
int )
> ( &QSpinBox::valueChanged ),
this, &QgsSpinBox::changed );
60 mExpressionsEnabled = enabled;
65 QSpinBox::changeEvent( event );
67 if ( event->type() == QEvent::FontChange )
69 lineEdit()->setFont( font() );
72 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
77 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
78 QSpinBox::paintEvent( event );
83 const int step = singleStep();
84 if ( event->modifiers() & Qt::ControlModifier )
87 int newStep = step / 10;
89 newStep = std::max( newStep, 1 );
91 setSingleStep( newStep );
96 event->setModifiers( event->modifiers() & ~Qt::ControlModifier );
98 QSpinBox::wheelEvent( event );
99 setSingleStep( step );
108 qApp->processEvents();
109 if ( QApplication::mouseButtons() & Qt::LeftButton )
110 QSpinBox::timerEvent( event );
113 void QgsSpinBox::changed(
int value )
115 mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
121 if ( mLineEdit->isNull() )
128 mCustomClearValue = customValue;
130 if ( !specialValueText.isEmpty() )
132 const int v = value();
141 mClearValueMode = mode;
142 mCustomClearValue = 0;
144 if ( !specialValueText.isEmpty() )
146 const int v = value();
160 return mCustomClearValue;
165 mLineEdit->setAlignment( alignment );
172 QSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
173 mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
177 QSpinBox::setSpecialValueText( txt );
178 mLineEdit->setNullValue( txt );
184 if ( !mExpressionsEnabled )
186 return QSpinBox::valueFromText( text );
189 const QString trimmedText = stripped( text );
190 if ( trimmedText.isEmpty() )
192 return mShowClearButton ?
clearValue() : value();
200 if ( !mExpressionsEnabled )
202 const QValidator::State r = QSpinBox::validate( input, pos );
206 return QValidator::Acceptable;
209 int QgsSpinBox::frameWidth()
const
211 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
214 bool QgsSpinBox::shouldShowClearForValue(
const int value )
const
216 if ( !mShowClearButton || !isEnabled() )
223 QString QgsSpinBox::stripped(
const QString &originalText )
const
227 QString text = originalText;
228 if ( specialValueText().isEmpty() || text != specialValueText() )
231 if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
232 text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
234 int size = text.size();
235 bool changed =
false;
236 if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
238 from += prefix().size();
242 if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
244 size -= suffix().size();
248 text = text.mid( from, size );
251 text = text.trimmed();