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