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