QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
30  : QDoubleSpinBox( parent )
31  , mShowClearButton( true )
32  , mClearValueMode( MinimumValue )
33  , mCustomClearValue( 0.0 )
34  , mExpressionsEnabled( true )
35 {
36  mLineEdit = new QgsSpinBoxLineEdit();
37 
38  setLineEdit( mLineEdit );
39 
40  QSize msz = minimumSizeHint();
41  setMinimumSize( msz.width() + CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
42  qMax( msz.height(), CLEAR_ICON_SIZE + frameWidth() * 2 + 2 ) );
43 
44  connect( mLineEdit, SIGNAL( cleared() ), this, SLOT( clear() ) );
45  connect( this, SIGNAL( valueChanged( double ) ), this, SLOT( changed( double ) ) );
46 }
47 
49 {
50  mShowClearButton = showClearButton;
51  mLineEdit->setShowClearButton( showClearButton );
52 }
53 
55 {
56  mExpressionsEnabled = enabled;
57 }
58 
60 {
62  mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
63 }
64 
66 {
67  mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
69 }
70 
71 void QgsDoubleSpinBox::changed( double value )
72 {
73  mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
74 }
75 
77 {
78  setValue( clearValue() );
79 }
80 
81 void QgsDoubleSpinBox::setClearValue( double customValue, const QString& specialValueText )
82 {
83  mClearValueMode = CustomValue;
84  mCustomClearValue = customValue;
85 
86  if ( !specialValueText.isEmpty() )
87  {
88  double v = value();
89  clear();
90  setSpecialValueText( specialValueText );
91  setValue( v );
92  }
93 }
94 
96 {
97  mClearValueMode = mode;
98  mCustomClearValue = 0;
99 
100  if ( !clearValueText.isEmpty() )
101  {
102  double v = value();
103  clear();
104  setSpecialValueText( clearValueText );
105  setValue( v );
106  }
107 }
108 
109 double QgsDoubleSpinBox::clearValue() const
110 {
111  if ( mClearValueMode == MinimumValue )
112  return minimum() ;
113  else if ( mClearValueMode == MaximumValue )
114  return maximum();
115  else
116  return mCustomClearValue;
117 }
118 
119 QString QgsDoubleSpinBox::stripped( const QString &originalText ) const
120 {
121  //adapted from QAbstractSpinBoxPrivate::stripped
122  //trims whitespace, prefix and suffix from spin box text
123  QString text = originalText;
124  if ( specialValueText().isEmpty() || text != specialValueText() )
125  {
126  int from = 0;
127  int size = text.size();
128  bool changed = false;
129  if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
130  {
131  from += prefix().size();
132  size -= from;
133  changed = true;
134  }
135  if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
136  {
137  size -= suffix().size();
138  changed = true;
139  }
140  if ( changed )
141  text = text.mid( from, size );
142  }
143 
144  text = text.trimmed();
145 
146  return text;
147 }
148 
150 {
151  if ( !mExpressionsEnabled )
152  {
153  return QDoubleSpinBox::valueFromText( text );
154  }
155 
156  QString trimmedText = stripped( text );
157  if ( trimmedText.isEmpty() )
158  {
159  return mShowClearButton ? clearValue() : value();
160  }
161 
162  return QgsExpression::evaluateToDouble( trimmedText, value() );
163 }
164 
165 QValidator::State QgsDoubleSpinBox::validate( QString &input, int &pos ) const
166 {
167  if ( !mExpressionsEnabled )
168  {
169  QValidator::State r = QDoubleSpinBox::validate( input, pos );
170  return r;
171  }
172 
173  return QValidator::Acceptable;
174 }
175 
176 int QgsDoubleSpinBox::frameWidth() const
177 {
178  return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
179 }
180 
181 bool QgsDoubleSpinBox::shouldShowClearForValue( const double value ) const
182 {
183  if ( !mShowClearButton || !isEnabled() )
184  {
185  return false;
186  }
187  return value != clearValue();
188 }
virtual QValidator::State validate(QString &text, int &pos) const
void setLineEdit(QLineEdit *lineEdit)
virtual bool event(QEvent *event)
int width() const
void paintEvent(QPaintEvent *e) override
virtual QSize minimumSizeHint() const
virtual QValidator::State validate(QString &input, int &pos) const override
Reset value to custom value (see setClearValue() )
QStyle * style() const
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const=0
int size() const
Reset value to maximum()
QgsDoubleSpinBox(QWidget *parent=nullptr)
Constructor for QgsDoubleSpinBox.
ClearValueMode
Behaviour when widget is cleared.
#define CLEAR_ICON_SIZE
QString text() const
virtual void clear() override
Set the current value to the value defined by the clear value.
void setShowClearButton(const bool showClearButton)
Sets whether the widget will show a clear button.
QString prefix() const
QSize size() const
void setMinimumSize(const QSize &)
bool showClearButton() const
Returns whether the widget is showing a clear button.
virtual double valueFromText(const QString &text) const override
void valueChanged(double d)
bool isEmpty() const
QString trimmed() const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QPoint pos() const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
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...
QString suffix() const
double value() const
double clearValue() const
Returns the value used when clear() is called.
virtual void changeEvent(QEvent *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...
QString mid(int position, int n) const
virtual void paintEvent(QPaintEvent *event)
virtual void changeEvent(QEvent *event)
int height() const
QString specialValueText() const
virtual double valueFromText(const QString &text) const
double minimum() const
Reset value to minimum()
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
static double evaluateToDouble(const QString &text, const double fallbackValue)
Attempts to evaluate a text string as an expression to a resultant double value.
double maximum() const
void setExpressionsEnabled(const bool enabled)
Sets if the widget will allow entry of simple expressions, which are evaluated and then discarded...