QGIS API Documentation  3.6.0-Noosa (5873452)
qgsdoublespinbox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdoublespinbox.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 "qgsdoublespinbox.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 QgsDoubleSpinBox::SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );
33 
34 
36  : QDoubleSpinBox( parent )
37 {
38  mLineEdit = new QgsSpinBoxLineEdit();
39 
40  // By default, group separator is off
41  setLineEdit( mLineEdit );
42 
43  QSize msz = minimumSizeHint();
44  setMinimumSize( msz.width() + CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
45  std::max( msz.height(), CLEAR_ICON_SIZE + frameWidth() * 2 + 2 ) );
46 
47  connect( mLineEdit, &QgsFilterLineEdit::cleared, this, &QgsDoubleSpinBox::clear );
48  connect( this, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsDoubleSpinBox::changed );
49 }
50 
52 {
53  mShowClearButton = showClearButton;
54  mLineEdit->setShowClearButton( showClearButton );
55 }
56 
57 void QgsDoubleSpinBox::setExpressionsEnabled( const bool enabled )
58 {
59  mExpressionsEnabled = enabled;
60 }
61 
62 void QgsDoubleSpinBox::changeEvent( QEvent *event )
63 {
64  QDoubleSpinBox::changeEvent( event );
65 
66  if ( event->type() == QEvent::FontChange )
67  {
68  lineEdit()->setFont( font() );
69  }
70 
71  mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
72 }
73 
74 void QgsDoubleSpinBox::wheelEvent( QWheelEvent *event )
75 {
76  double step = singleStep();
77  if ( event->modifiers() & Qt::ControlModifier )
78  {
79  // ctrl modifier results in finer increments - 10% of usual step
80  double newStep = step / 10;
81  // but don't ever use an increment smaller than would be visible in the widget
82  // i.e. if showing 2 decimals, smallest increment will be 0.01
83  newStep = std::max( newStep, std::pow( 10.0, 0.0 - decimals() ) );
84 
85  setSingleStep( newStep );
86 
87  // clear control modifier before handing off event - Qt uses it for unwanted purposes
88  // (*increasing* step size, whereas QGIS UX convention is that control modifier
89  // results in finer changes!)
90  event->setModifiers( event->modifiers() & ~Qt::ControlModifier );
91  }
92  QDoubleSpinBox::wheelEvent( event );
93  setSingleStep( step );
94 }
95 
96 void QgsDoubleSpinBox::paintEvent( QPaintEvent *event )
97 {
98  mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
99  QDoubleSpinBox::paintEvent( event );
100 }
101 
102 void QgsDoubleSpinBox::changed( double value )
103 {
104  mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
105 }
106 
108 {
109  setValue( clearValue() );
110  if ( mLineEdit->isNull() )
111  mLineEdit->clear();
112 }
113 
114 void QgsDoubleSpinBox::setClearValue( double customValue, const QString &specialValueText )
115 {
116  mClearValueMode = CustomValue;
117  mCustomClearValue = customValue;
118 
119  if ( !specialValueText.isEmpty() )
120  {
121  double v = value();
122  clear();
123  setSpecialValueText( specialValueText );
124  setValue( v );
125  }
126 }
127 
129 {
130  mClearValueMode = mode;
131  mCustomClearValue = 0;
132 
133  if ( !clearValueText.isEmpty() )
134  {
135  double v = value();
136  clear();
137  setSpecialValueText( clearValueText );
138  setValue( v );
139  }
140 }
141 
142 double QgsDoubleSpinBox::clearValue() const
143 {
144  if ( mClearValueMode == MinimumValue )
145  return minimum();
146  else if ( mClearValueMode == MaximumValue )
147  return maximum();
148  else
149  return mCustomClearValue;
150 }
151 
152 void QgsDoubleSpinBox::setLineEditAlignment( Qt::Alignment alignment )
153 {
154  mLineEdit->setAlignment( alignment );
155 }
156 
157 void QgsDoubleSpinBox::setSpecialValueText( const QString &txt )
158 {
159  if ( txt.isEmpty() )
160  {
161  QDoubleSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
162  mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
163  }
164  else
165  {
166  QDoubleSpinBox::setSpecialValueText( txt );
167  mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
168  }
169 }
170 
171 QString QgsDoubleSpinBox::stripped( const QString &originalText ) const
172 {
173  //adapted from QAbstractSpinBoxPrivate::stripped
174  //trims whitespace, prefix and suffix from spin box text
175  QString text = originalText;
176  if ( specialValueText().isEmpty() || text != specialValueText() )
177  {
178  // Strip SPECIAL_TEXT_WHEN_EMPTY
179  if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
180  text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
181  int from = 0;
182  int size = text.size();
183  bool changed = false;
184  if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
185  {
186  from += prefix().size();
187  size -= from;
188  changed = true;
189  }
190  if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
191  {
192  size -= suffix().size();
193  changed = true;
194  }
195  if ( changed )
196  text = text.mid( from, size );
197  }
198 
199  text = text.trimmed();
200 
201  return text;
202 }
203 
204 double QgsDoubleSpinBox::valueFromText( const QString &text ) const
205 {
206  if ( !mExpressionsEnabled )
207  {
208  return QDoubleSpinBox::valueFromText( text );
209  }
210 
211  QString trimmedText = stripped( text );
212  if ( trimmedText.isEmpty() )
213  {
214  return mShowClearButton ? clearValue() : value();
215  }
216 
217  return QgsExpression::evaluateToDouble( trimmedText, value() );
218 }
219 
220 QValidator::State QgsDoubleSpinBox::validate( QString &input, int &pos ) const
221 {
222  if ( !mExpressionsEnabled )
223  {
224  QValidator::State r = QDoubleSpinBox::validate( input, pos );
225  return r;
226  }
227 
228  return QValidator::Acceptable;
229 }
230 
231 int QgsDoubleSpinBox::frameWidth() const
232 {
233  return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
234 }
235 
236 bool QgsDoubleSpinBox::shouldShowClearForValue( const double value ) const
237 {
238  if ( !mShowClearButton || !isEnabled() )
239  {
240  return false;
241  }
242  return value != clearValue();
243 }
void wheelEvent(QWheelEvent *event) override
void paintEvent(QPaintEvent *e) override
void setExpressionsEnabled(bool enabled)
Sets if the widget will allow entry of simple expressions, which are evaluated and then discarded...
QValidator::State validate(QString &input, int &pos) const override
void setShowClearButton(bool showClearButton)
Sets whether the widget will show a clear button.
Reset value to custom value (see setClearValue() )
static double evaluateToDouble(const QString &text, double fallbackValue)
Attempts to evaluate a text string as an expression to a resultant double value.
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...
Reset value to maximum()
QgsDoubleSpinBox(QWidget *parent=nullptr)
Constructor for QgsDoubleSpinBox.
ClearValueMode
Behavior when widget is cleared.
#define CLEAR_ICON_SIZE
void clear() override
Sets the current value to the value defined by the clear value.
bool showClearButton() const
Returns whether the widget is showing a clear button.
double valueFromText(const QString &text) const override
void setClearValue(double customValue, const QString &clearValueText=QString())
Defines the clear value as a custom value and will automatically set the clear value mode to CustomVa...
double clearValue() const
Returns the value used when clear() is called.
void changeEvent(QEvent *event) override
void setLineEditAlignment(Qt::Alignment alignment)
Set alignment in the embedded line edit widget.
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...
void cleared()
Emitted when the widget is cleared.
Reset value to minimum()