QGIS API Documentation  2.17.0-Master (f49f7ce)
qgsfilterlineedit.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfilterlineedit.h
3  ------------------------
4  begin : October 27, 2012
5  copyright : (C) 2012 by Alexander Bruy
6  email : alexander dot bruy at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSFILTERLINEEDIT_H
19 #define QGSFILTERLINEEDIT_H
20 
21 #include <QLineEdit>
22 
23 class QToolButton;
24 
34 class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
35 {
36  Q_OBJECT
37  Q_ENUMS( ClearMode )
38  Q_PROPERTY( ClearMode clearMode READ clearMode WRITE setClearMode )
39  Q_PROPERTY( QString nullValue READ nullValue WRITE setNullValue )
40  Q_PROPERTY( QString defaultValue READ defaultValue WRITE setDefaultValue )
41  Q_PROPERTY( QString value READ value WRITE setValue NOTIFY valueChanged )
42  Q_PROPERTY( bool showClearButton READ showClearButton WRITE setShowClearButton )
43 
44  public:
45 
47  enum ClearMode
48  {
49  ClearToNull = 0,
51  };
52 
57  QgsFilterLineEdit( QWidget* parent = nullptr, const QString& nullValue = QString::null );
58 
63  bool showClearButton() const { return mClearButtonVisible; }
64 
70  void setShowClearButton( bool visible );
71 
77  ClearMode clearMode() const { return mClearMode; }
78 
84  void setClearMode( ClearMode mode ) { mClearMode = mode; }
85 
92  void setNullValue( const QString& nullValue ) { mNullValue = nullValue; }
93 
98  QString nullValue() const { return mNullValue; }
99 
108  void setDefaultValue( const QString& defaultValue ) { mDefaultValue = defaultValue; }
109 
117  QString defaultValue() const { return mDefaultValue; }
118 
126  void setValue( const QString& value ) { setText( value.isNull() ? mNullValue : value ); }
127 
136  QString value() const { return isNull() ? QString::null : text(); }
137 
144  inline bool isNull() const { return text() == mNullValue; }
145 
146  public slots:
147 
152  virtual void clearValue();
153 
154  signals:
155 
159  void cleared();
160 
166  void valueChanged( const QString& value );
167 
168  protected:
169  void mousePressEvent( QMouseEvent* e ) override;
170  void mouseMoveEvent( QMouseEvent* e ) override;
171  void focusInEvent( QFocusEvent* e ) override;
172  void paintEvent( QPaintEvent* e ) override;
173  void leaveEvent( QEvent* e ) override;
174 
175  private slots:
176  void onTextChanged( const QString &text );
177 
178  private:
179 
180  bool mClearButtonVisible;
181 
182  ClearMode mClearMode;
183 
184  QString mNullValue;
185  QString mDefaultValue;
186  QString mStyleSheet;
187  bool mFocusInEvent;
188  bool mClearHover;
189 
190  QSize mClearIconSize;
191  QPixmap mClearIconPixmap;
192  QPixmap mClearHoverPixmap;
193 
195  bool shouldShowClear() const;
196 
197  QRect clearRect() const;
198 };
199 
201 
207 class QgsSpinBoxLineEdit : public QgsFilterLineEdit
208 {
209  Q_OBJECT
210 
211  public:
212 
213  QgsSpinBoxLineEdit( QWidget* parent = nullptr )
214  : QgsFilterLineEdit( parent )
215  {}
216 
217  public slots:
218 
219  virtual void clearValue() override
220  {
221  // don't change the value - let spin boxes handle that by detecting cleared() signal
222  setCursor( Qt::IBeamCursor );
223  setModified( true );
224  emit cleared();
225  }
226 };
228 
229 #endif // QGSFILTERLINEEDIT_H
QString defaultValue() const
Returns the default value for the widget.
void setDefaultValue(const QString &defaultValue)
Sets the default value for the widget.
void setText(const QString &)
bool showClearButton() const
Returns true if the widget&#39;s clear button is visible.
ClearMode clearMode() const
Returns the clear mode for the widget.
bool isNull() const
void setClearMode(ClearMode mode)
Sets the clear mode for the widget.
QString nullValue() const
Returns the string used for representating null values in the widget.
QString value() const
Returns the text of this edit with support for handling null values.
Reset value to default value (see defaultValue() )
ClearMode
Behaviour when clearing value of widget.
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
virtual void paintEvent(QPaintEvent *)
bool isNull() const
Determine if the current text represents null.
virtual void mouseMoveEvent(QMouseEvent *e)
virtual void focusInEvent(QFocusEvent *e)
void setNullValue(const QString &nullValue)
Sets the string representation for null values in the widget.
virtual void leaveEvent(QEvent *event)
bool isNull(const QVariant &v)
virtual void mousePressEvent(QMouseEvent *e)
void setValue(const QString &value)
Sets the current text for the widget with support for handling null values.