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   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   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() )
 
  141   mClearValueMode = mode;
 
  142   mCustomClearValue = 0;
 
  144   if ( !specialValueText.isEmpty() )
 
  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   QString trimmedText = stripped( text );
 
  190   if ( trimmedText.isEmpty() )
 
  192     return mShowClearButton ? 
clearValue() : value();
 
  200   if ( !mExpressionsEnabled )
 
  202     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();
 
static double evaluateToDouble(const QString &text, double fallbackValue)
Attempts to evaluate a text string as an expression to a resultant double value.
void cleared()
Emitted when the widget is cleared.
void setLineEditAlignment(Qt::Alignment alignment)
Set alignment in the embedded line edit widget.
ClearValueMode
Behavior when widget is cleared.
@ MaximumValue
Reset value to maximum()
@ MinimumValue
Reset value to minimum()
@ CustomValue
Reset value to custom value (see setClearValue() )
void wheelEvent(QWheelEvent *event) override
void setShowClearButton(bool showClearButton)
Sets whether the widget will show a clear button.
QgsSpinBox(QWidget *parent=nullptr)
Constructor for QgsSpinBox.
QValidator::State validate(QString &input, int &pos) const override
void paintEvent(QPaintEvent *event) override
void setClearValueMode(ClearValueMode mode, const QString &clearValueText=QString())
Defines if the clear value should be the minimum or maximum values of the widget or a custom value.
int valueFromText(const QString &text) const override
void timerEvent(QTimerEvent *event) override
void setClearValue(int customValue, const QString &clearValueText=QString())
Defines the clear value as a custom value and will automatically set the clear value mode to CustomVa...
void setExpressionsEnabled(bool enabled)
Sets if the widget will allow entry of simple expressions, which are evaluated and then discarded.
void setSpecialValueText(const QString &txt)
Set the special-value text to be txt If set, the spin box will display this text instead of a numeric...
void changeEvent(QEvent *event) override
void clear() override
Sets the current value to the value defined by the clear value.