QGIS API Documentation 3.39.0-Master (47f7b3a4989)
Loading...
Searching...
No Matches
qgsdoublespinbox.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdoublespinbox.cpp
3 --------------------------------------
4 Date : 09.2014
5 Copyright : (C) 2014 Denis Rouzaud
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#ifdef _MSC_VER
33static QChar SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );
34#else
35static constexpr QChar SPECIAL_TEXT_WHEN_EMPTY = QChar( 0x2063 );
36#endif
37
38
40 : QDoubleSpinBox( parent )
41{
42 mLineEdit = new QgsSpinBoxLineEdit();
43
44 // By default, group separator is off
45 setLineEdit( mLineEdit );
46
47 const QSize msz = minimumSizeHint();
48 setMinimumSize( msz.width() + CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
49 std::max( msz.height(), CLEAR_ICON_SIZE + frameWidth() * 2 + 2 ) );
50
51 connect( mLineEdit, &QgsFilterLineEdit::cleared, this, &QgsDoubleSpinBox::clear );
52 connect( this, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsDoubleSpinBox::changed );
53}
54
55void QgsDoubleSpinBox::setShowClearButton( const bool showClearButton )
56{
57 mShowClearButton = showClearButton;
58 mLineEdit->setShowClearButton( showClearButton );
59}
60
62{
63 mExpressionsEnabled = enabled;
64}
65
66void QgsDoubleSpinBox::changeEvent( QEvent *event )
67{
68 QDoubleSpinBox::changeEvent( event );
69
70 if ( event->type() == QEvent::FontChange )
71 {
72 lineEdit()->setFont( font() );
73 }
74
75 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
76}
77
78void QgsDoubleSpinBox::wheelEvent( QWheelEvent *event )
79{
80 const double step = singleStep();
81 if ( event->modifiers() & Qt::ControlModifier )
82 {
83 // ctrl modifier results in finer increments - 10% of usual step
84 double newStep = step / 10;
85 // but don't ever use an increment smaller than would be visible in the widget
86 // i.e. if showing 2 decimals, smallest increment will be 0.01
87 newStep = std::max( newStep, std::pow( 10.0, 0.0 - decimals() ) );
88
89 setSingleStep( newStep );
90
91 // clear control modifier before handing off event - Qt uses it for unwanted purposes
92 // (*increasing* step size, whereas QGIS UX convention is that control modifier
93 // results in finer changes!)
94 event->setModifiers( event->modifiers() & ~Qt::ControlModifier );
95 }
96 QDoubleSpinBox::wheelEvent( event );
97 setSingleStep( step );
98}
99
100void QgsDoubleSpinBox::timerEvent( QTimerEvent *event )
101{
102 // Process all events, which may include a mouse release event
103 // Only allow the timer to trigger additional value changes if the user
104 // has in fact held the mouse button, rather than the timer expiry
105 // simply appearing before the mouse release in the event queue
106 qApp->processEvents();
107 if ( QApplication::mouseButtons() & Qt::LeftButton )
108 QDoubleSpinBox::timerEvent( event );
109}
110
111void QgsDoubleSpinBox::paintEvent( QPaintEvent *event )
112{
113 mLineEdit->setShowClearButton( shouldShowClearForValue( value() ) );
114 QDoubleSpinBox::paintEvent( event );
115}
116
118{
119 const bool wasNull = mShowClearButton && value() == clearValue();
120 if ( wasNull && minimum() < 0 && maximum() > 0 )
121 {
122 // value is currently null, and range allows both positive and negative numbers
123 // in this case we DON'T do the default step, as that would add one step to the NULL value,
124 // which is usually the minimum acceptable value... so the user will get a very large negative number!
125 // Instead, treat the initial value as 0 instead, and then perform the step.
126 whileBlocking( this )->setValue( 0 );
127 }
128 QDoubleSpinBox::stepBy( steps );
129}
130
131void QgsDoubleSpinBox::changed( double value )
132{
133 mLineEdit->setShowClearButton( shouldShowClearForValue( value ) );
134}
135
137{
138 setValue( clearValue() );
139 if ( mLineEdit->isNull() )
140 mLineEdit->clear();
141}
142
143void QgsDoubleSpinBox::setClearValue( double customValue, const QString &specialValueText )
144{
145 if ( mClearValueMode == CustomValue && mCustomClearValue == customValue && QAbstractSpinBox::specialValueText() == specialValueText )
146 {
147 return;
148 }
149
150 mClearValueMode = CustomValue;
151 mCustomClearValue = customValue;
152
153 if ( !specialValueText.isEmpty() )
154 {
155 const double v = value();
156 clear();
157 setSpecialValueText( specialValueText );
158 setValue( v );
159 }
160}
161
163{
164 if ( mClearValueMode == mode && mCustomClearValue == 0 && QAbstractSpinBox::specialValueText() == clearValueText )
165 {
166 return;
167 }
168
169 mClearValueMode = mode;
170 mCustomClearValue = 0;
171
172 if ( !clearValueText.isEmpty() )
173 {
174 const double v = value();
175 clear();
176 setSpecialValueText( clearValueText );
177 setValue( v );
178 }
179}
180
182{
183 if ( mClearValueMode == MinimumValue )
184 return minimum();
185 else if ( mClearValueMode == MaximumValue )
186 return maximum();
187 else
188 return mCustomClearValue;
189}
190
191void QgsDoubleSpinBox::setLineEditAlignment( Qt::Alignment alignment )
192{
193 mLineEdit->setAlignment( alignment );
194}
195
197{
198 if ( txt.isEmpty() )
199 {
200 QDoubleSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
201 mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
202 }
203 else
204 {
205 QDoubleSpinBox::setSpecialValueText( txt );
206 mLineEdit->setNullValue( txt );
207 }
208}
209
210QString QgsDoubleSpinBox::stripped( const QString &originalText ) const
211{
212 //adapted from QAbstractSpinBoxPrivate::stripped
213 //trims whitespace, prefix and suffix from spin box text
214 QString text = originalText;
215 if ( specialValueText().isEmpty() || text != specialValueText() )
216 {
217 // Strip SPECIAL_TEXT_WHEN_EMPTY
218 if ( text.contains( SPECIAL_TEXT_WHEN_EMPTY ) )
219 text = text.replace( SPECIAL_TEXT_WHEN_EMPTY, QString() );
220 int from = 0;
221 int size = text.size();
222 bool changed = false;
223 if ( !prefix().isEmpty() && text.startsWith( prefix() ) )
224 {
225 from += prefix().size();
226 size -= from;
227 changed = true;
228 }
229 if ( !suffix().isEmpty() && text.endsWith( suffix() ) )
230 {
231 size -= suffix().size();
232 changed = true;
233 }
234 if ( changed )
235 text = text.mid( from, size );
236 }
237
238 text = text.trimmed();
239
240 return text;
241}
242
243double QgsDoubleSpinBox::valueFromText( const QString &text ) const
244{
245 if ( !mExpressionsEnabled )
246 {
247 return QDoubleSpinBox::valueFromText( text );
248 }
249
250 const QString trimmedText = stripped( text );
251 if ( trimmedText.isEmpty() )
252 {
253 return mShowClearButton ? clearValue() : value();
254 }
255
256 return QgsExpression::evaluateToDouble( trimmedText, value() );
257}
258
259QValidator::State QgsDoubleSpinBox::validate( QString &input, int &pos ) const
260{
261 if ( !mExpressionsEnabled )
262 {
263 const QValidator::State r = QDoubleSpinBox::validate( input, pos );
264 return r;
265 }
266
267 return QValidator::Acceptable;
268}
269
270int QgsDoubleSpinBox::frameWidth() const
271{
272 return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
273}
274
275bool QgsDoubleSpinBox::shouldShowClearForValue( const double value ) const
276{
277 if ( !mShowClearButton || !isEnabled() )
278 {
279 return false;
280 }
281 return value != clearValue();
282}
void paintEvent(QPaintEvent *e) override
void wheelEvent(QWheelEvent *event) override
void setLineEditAlignment(Qt::Alignment alignment)
Set alignment in the embedded line edit widget.
void stepBy(int steps) override
double valueFromText(const QString &text) const override
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 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 changeEvent(QEvent *event) override
void clear() override
Sets the current value to the value defined by the clear value.
ClearValueMode
Behavior when widget is cleared.
@ MaximumValue
Reset value to maximum()
@ CustomValue
Reset value to custom value (see setClearValue() )
@ MinimumValue
Reset value to minimum()
QValidator::State validate(QString &input, int &pos) const override
QgsDoubleSpinBox(QWidget *parent=nullptr)
Constructor for QgsDoubleSpinBox.
void setExpressionsEnabled(bool enabled)
Sets if the widget will allow entry of simple expressions, which are evaluated and then discarded.
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...
void setShowClearButton(bool showClearButton)
Sets whether the widget will show a clear button.
void timerEvent(QTimerEvent *event) override
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.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:5369
#define CLEAR_ICON_SIZE