1 /***************************************************************************
2     qgsspinbox.h
3      --------------------------------------
4     Date                 : 09.2014
5     Copyright            : (C) 2014 Denis Rouzaud
6     Email                : denis.rouzaud@gmail.com
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 #ifndef QGSSPINBOX_H
17 #define QGSSPINBOX_H
18 
19 #include <QSpinBox>
20 #include "qgis_sip.h"
21 #include "qgis_gui.h"
22 
23 class QgsSpinBoxLineEdit;
24 
25 
26 #ifdef SIP_RUN
27 % ModuleHeaderCode
28 // fix to allow compilation with sip 4.7 that for some reason
29 // doesn't add this include to the file where the code from
30 // ConvertToSubClassCode goes.
31 #include <qgsspinbox.h>
32 % End
33 #endif
34 
35 
36 /**
37  * \ingroup gui
38  * \brief The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
39  * The clear value can be either the minimum or the maiximum value of the spin box or a custom value.
40  * This value can then be handled by a special value text.
41  */
42 class GUI_EXPORT QgsSpinBox : public QSpinBox
43 {
44 
45 #ifdef SIP_RUN
46     SIP_CONVERT_TO_SUBCLASS_CODE
47     if ( qobject_cast<QgsSpinBox *>( sipCpp ) )
48       sipType = sipType_QgsSpinBox;
49     else
50       sipType = NULL;
51     SIP_END
52 #endif
53 
54     Q_OBJECT
55     Q_PROPERTY( bool showClearButton READ showClearButton WRITE setShowClearButton )
56     Q_PROPERTY( bool clearValue READ clearValue WRITE setClearValue )
57     Q_PROPERTY( bool expressionsEnabled READ expressionsEnabled WRITE setExpressionsEnabled )
58 
59   public:
60 
61     //! Behavior when widget is cleared.
62     enum ClearValueMode
63     {
64       MinimumValue, //!< Reset value to minimum()
65       MaximumValue, //!< Reset value to maximum()
66       CustomValue, //!< Reset value to custom value (see setClearValue() )
67     };
68 
69     /**
70      * Constructor for QgsSpinBox.
71      * \param parent parent widget
72      */
73     explicit QgsSpinBox( QWidget *parent SIP_TRANSFERTHIS = nullptr );
74 
75     /**
76      * Sets whether the widget will show a clear button. The clear button
77      * allows users to reset the widget to a default or empty state.
78      * \param showClearButton set to TRUE to show the clear button, or FALSE to hide it
79      * \see showClearButton()
80      */
81     void setShowClearButton( bool showClearButton );
82 
83     /**
84      * Returns whether the widget is showing a clear button.
85      * \see setShowClearButton()
86      */
showClearButton()87     bool showClearButton() const {return mShowClearButton;}
88 
89     /**
90      * Sets if the widget will allow entry of simple expressions, which are
91      * evaluated and then discarded.
92      * \param enabled set to TRUE to allow expression entry
93      * \since QGIS 2.7
94      */
95     void setExpressionsEnabled( bool enabled );
96 
97     /**
98      * Returns whether the widget will allow entry of simple expressions, which are
99      * evaluated and then discarded.
100      * \returns TRUE if spin box allows expression entry
101      * \since QGIS 2.7
102      */
expressionsEnabled()103     bool expressionsEnabled() const {return mExpressionsEnabled;}
104 
105     //! Sets the current value to the value defined by the clear value.
106     void clear() override;
107 
108     /**
109      * Defines the clear value as a custom value and will automatically set the clear value mode to CustomValue.
110      * \param customValue defines the numerical value used as the clear value
111      * \param clearValueText is the text displayed when the spin box is at the clear value. If not specified, no special value text is used.
112      * \see setClearValue()
113      */
114     void setClearValue( int customValue, const QString &clearValueText = QString() );
115 
116     /**
117      * Defines if the clear value should be the minimum or maximum values of the widget or a custom value.
118      * \param mode mode to user for clear value
119      * \param clearValueText is the text displayed when the spin box is at the clear value. If not specified, no special value text is used.
120      */
121     void setClearValueMode( ClearValueMode mode, const QString &clearValueText = QString() );
122 
123     /**
124      * Returns the value used when clear() is called.
125      * \see setClearValue()
126      */
127     int clearValue() const;
128 
129     /**
130      * Set alignment in the embedded line edit widget
131      * \param alignment
132      */
133     void setLineEditAlignment( Qt::Alignment alignment );
134 
135     /**
136      * Set the special-value text to be \a txt
137      * If set, the spin box will display this text instead of a numeric value whenever the current value
138      * is equal to minimum(). Typical use is to indicate that this choice has a special (default) meaning.
139      */
140     void setSpecialValueText( const QString &txt );
141 
142     int valueFromText( const QString &text ) const override;
143     QValidator::State validate( QString &input, int &pos ) const override;
144 
145   protected:
146 
147     void changeEvent( QEvent *event ) override;
148     void paintEvent( QPaintEvent *event ) override;
149     void wheelEvent( QWheelEvent *event ) override;
150     // This is required because private implementation of
151     // QAbstractSpinBoxPrivate may trigger a second
152     // undesired event from the auto-repeat mouse timer
153     void timerEvent( QTimerEvent *event ) override;
154 
155   private slots:
156     void changed( int value );
157 
158   private:
159     int frameWidth() const;
160     bool shouldShowClearForValue( int value ) const;
161 
162     QgsSpinBoxLineEdit *mLineEdit = nullptr;
163 
164     bool mShowClearButton = true;
165     ClearValueMode mClearValueMode = MinimumValue;
166     int mCustomClearValue = 0;
167 
168     bool mExpressionsEnabled = true;
169 
170     QString stripped( const QString &originalText ) const;
171 
172     friend class TestQgsRangeWidgetWrapper;
173 
174 };
175 
176 #endif // QGSSPINBOX_H
177