QGIS API Documentation  3.6.0-Noosa (5873452)
qgsspinbox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsspinbox.cpp
3  --------------------------------------
4  Date : 09.2014
5  Copyright : (C) 2014 Denis Rouzaud
6  Email : [email protected]
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include <QLineEdit>
17 #include <QMouseEvent>
18 #include <QSettings>
19 #include <QStyle>
20 
21 #include "qgsspinbox.h"
22 #include "qgsexpression.h"
23 #include "qgsapplication.h"
24 #include "qgslogger.h"
25 #include "qgsfilterlineedit.h"
26 
27 #define CLEAR_ICON_SIZE 16
28 
29 // This is required because private implementation of
30 // QAbstractSpinBoxPrivate checks for specialText emptiness
31 // and skips specialText handling if it's empty
32 QString QgsSpinBox::SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );
33 
34 
35 QgsSpinBox::QgsSpinBox( QWidget *parent )
36  : QSpinBox( parent )
37 {
38  mLineEdit = new QgsSpinBoxLineEdit();
39  setLineEdit( mLineEdit );
40 
41  QSize msz = minimumSizeHint();
42  setMinimumSize( msz.width() + CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
43  std::max( msz.height(), CLEAR_ICON_SIZE + frameWidth() * 2 + 2 ) );
44 
45  connect( mLineEdit, &QgsFilterLineEdit::cleared, this, &QgsSpinBox::clear );
46  connect( this, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsSpinBox::changed );
47 }
48 
50 {
51  mShowClearButton = showClearButton;
52  mLineEdit->setShowClearButton( showClearButton );
53 }
54 
55 void QgsSpinBox::setExpressionsEnabled( const bool enabled )
56 {
57  mExpressionsEnabled = enabled;
58 }
59 
60 void QgsSpinBox::changeEvent( QEvent *event )
61 {
62  QSpinBox::changeEvent( event );
63 
64  if ( event->type() == QEvent::FontChange )
65  {
66  lineEdit()->setFont( font() );
67  }
68 
69  mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
70 }
71 
72 void QgsSpinBox::paintEvent( QPaintEvent *event )
73 {
74  mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
75  QSpinBox::paintEvent( event );
76 }
77 
78 void QgsSpinBox::wheelEvent( QWheelEvent *event )
79 {
80  int step = singleStep();
81  if ( event->modifiers() & Qt::ControlModifier )
82  {
83  // ctrl modifier results in finer increments - 10% of usual step
84  int newStep = step / 10;
85  // step should be at least 1
86  newStep = std::max( newStep, 1 );
87 
88  setSingleStep( newStep );
89 
90  // clear control modifier before handing off event - Qt uses it for unwanted purposes
91  // (*increasing* step size, whereas QGIS UX convention is that control modifier
92  // results in finer changes!)
93  event->setModifiers( event->modifiers() & ~Qt::ControlModifier );
94  }
95  QSpinBox::wheelEvent( event );
96  setSingleStep( step );
97 }
98 
99 void QgsSpinBox::changed( int value )
100 {
101  mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
102 }
103 
105 {
106  setValue( clearValue() );
107  if ( mLineEdit->isNull() )
108  mLineEdit->clear();
109 }
110 
111 void QgsSpinBox::setClearValue( int customValue, const QString &specialValueText )
112 {
113  mClearValueMode = CustomValue;
114  mCustomClearValue = customValue;
115 
116  if ( !specialValueText.isEmpty() )
117  {
118  int v = value();
119  clear();
120  setSpecialValueText( specialValueText );
121  setValue( v );
122  }
123 }
124 
125 void QgsSpinBox::setClearValueMode( QgsSpinBox::ClearValueMode mode, const QString &specialValueText )
126 {
127  mClearValueMode = mode;
128  mCustomClearValue = 0;
129 
130  if ( !specialValueText.isEmpty() )
131  {
132  int v = value();
133  clear();
134  setSpecialValueText( specialValueText );
135  setValue( v );
136  }
137 }
138 
139 int QgsSpinBox::clearValue() const
140 {
141  if ( mClearValueMode == MinimumValue )
142  return minimum();
143  else if ( mClearValueMode == MaximumValue )
144  return maximum();
145  else
146  return mCustomClearValue;
147 }
148 
149 void QgsSpinBox::setLineEditAlignment( Qt::Alignment alignment )
150 {
151  mLineEdit->setAlignment( alignment );
152 }
153 
154 void QgsSpinBox::setSpecialValueText( const QString &txt )
155 {
156  if ( txt.isEmpty() )
157  {
158  QSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
159  mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
160  }
161  else
162  {
163  QSpinBox::setSpecialValueText( txt );
164  mLineEdit->setNullValue( txt );
165  }
166 }
167 
168 int QgsSpinBox::valueFromText( const QString &text ) const
169 {
170  if ( !mExpressionsEnabled )
171  {
172  return QSpinBox::valueFromText( text );
173  }
174 
175  QString trimmedText = stripped( text );
176  if ( trimmedText.isEmpty() )
177  {
178  return mShowClearButton ? clearValue() : value();
179  }
180 
181  return std::round( QgsExpression::evaluateToDouble( trimmedText, value() ) );
182 }
183 
184 QValidator::State QgsSpinBox::validate( QString &input, int &pos ) const
185 {
186  if ( !mExpressionsEnabled )
187  {
188  QValidator::State r = QSpinBox::validate( input, pos );
189  return r;
190  }
191 
192  return QValidator::Acceptable;
193 }
194 
195 int QgsSpinBox::frameWidth() const
196 {
197  return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
198 }
199 
200 bool QgsSpinBox::shouldShowClearForValue( const int value ) const
201 {
202  if ( !mShowClearButton || !isEnabled() )
203  {
204  return false;
205  }
206  return value != clearValue();
207 }
208 
209 QString QgsSpinBox::stripped( const QString &originalText ) const
210 {
211  //adapted from QAbstractSpinBoxPrivate::stripped
212  //trims whitespace, prefix and suffix from spin box text
213  QString text = originalText;
214  if ( specialValueText().isEmpty() || text != specialValueText() )
215  {
216  // Strip SPECIAL_TEXT_WHEN_EMPTY
217  if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
218  text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
219  int from = 0;
220  int size = text.size();
221  bool changed = false;
222  if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
223  {
224  from += prefix().size();
225  size -= from;
226  changed = true;
227  }
228  if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
229  {
230  size -= suffix().size();
231  changed = true;
232  }
233  if ( changed )
234  text = text.mid( from, size );
235  }
236 
237  text = text.trimmed();
238 
239  return text;
240 }
void wheelEvent(QWheelEvent *event) override
Definition: qgsspinbox.cpp:78
ClearValueMode
Behavior when widget is cleared.
Definition: qgsspinbox.h:62
Reset value to maximum()
Definition: qgsspinbox.h:65
Reset value to custom value (see setClearValue() )
Definition: qgsspinbox.h:66
bool showClearButton() const
Returns whether the widget is showing a clear button.
Definition: qgsspinbox.h:87
static double evaluateToDouble(const QString &text, double fallbackValue)
Attempts to evaluate a text string as an expression to a resultant double value.
QValidator::State validate(QString &input, int &pos) const override
Definition: qgsspinbox.cpp:184
void clear() override
Sets the current value to the value defined by the clear value.
Definition: qgsspinbox.cpp:104
void setShowClearButton(bool showClearButton)
Sets whether the widget will show a clear button.
Definition: qgsspinbox.cpp:49
void setLineEditAlignment(Qt::Alignment alignment)
Set alignment in the embedded line edit widget.
Definition: qgsspinbox.cpp:149
Reset value to minimum()
Definition: qgsspinbox.h:64
QgsSpinBox(QWidget *parent=nullptr)
Constructor for QgsSpinBox.
Definition: qgsspinbox.cpp:35
void setExpressionsEnabled(bool enabled)
Sets if the widget will allow entry of simple expressions, which are evaluated and then discarded...
Definition: qgsspinbox.cpp:55
int clearValue() const
Returns the value used when clear() is called.
void paintEvent(QPaintEvent *event) override
Definition: qgsspinbox.cpp:72
#define CLEAR_ICON_SIZE
Definition: qgsspinbox.cpp:27
int valueFromText(const QString &text) const override
Definition: qgsspinbox.cpp:168
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...
Definition: qgsspinbox.cpp:125
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...
Definition: qgsspinbox.cpp:111
void cleared()
Emitted when the widget is cleared.
void changeEvent(QEvent *event) override
Definition: qgsspinbox.cpp:60
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...
Definition: qgsspinbox.cpp:154