QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 #include <QIcon>
23 #include "qgis_sip.h"
24 #include "qgis_gui.h"
25 
26 class QToolButton;
27 class QgsAnimatedIcon;
28 
39 class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
40 {
41 
42 #ifdef SIP_RUN
44  if ( qobject_cast<QgsFilterLineEdit *>( sipCpp ) )
45  sipType = sipType_QgsFilterLineEdit;
46  else
47  sipType = NULL;
48  SIP_END
49 #endif
50 
51  Q_OBJECT
52  Q_PROPERTY( ClearMode clearMode READ clearMode WRITE setClearMode )
53  Q_PROPERTY( QString nullValue READ nullValue WRITE setNullValue )
54  Q_PROPERTY( QString defaultValue READ defaultValue WRITE setDefaultValue )
55  Q_PROPERTY( QString value READ value WRITE setValue NOTIFY valueChanged )
56  Q_PROPERTY( bool showClearButton READ showClearButton WRITE setShowClearButton )
57  Q_PROPERTY( bool showSearchIcon READ showSearchIcon WRITE setShowSearchIcon )
58  Q_PROPERTY( bool showSpinner READ showSpinner WRITE setShowSpinner NOTIFY showSpinnerChanged )
59 
60  public:
61 
63  enum ClearMode
64  {
65  ClearToNull = 0,
67  };
68  Q_ENUM( ClearMode )
69 
70 
75  QgsFilterLineEdit( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &nullValue = QString() );
76 
82  bool showClearButton() const { return mClearButtonVisible; }
83 
90  void setShowClearButton( bool visible );
91 
98  ClearMode clearMode() const { return mClearMode; }
99 
106  void setClearMode( ClearMode mode ) { mClearMode = mode; }
107 
115  void setNullValue( const QString &nullValue ) { mNullValue = nullValue; }
116 
122  QString nullValue() const { return mNullValue; }
123 
130  void setShowSearchIcon( bool visible );
131 
137  bool showSearchIcon() const { return static_cast< bool >( mSearchAction ); }
138 
148  void setDefaultValue( const QString &defaultValue ) { mDefaultValue = defaultValue; }
149 
158  QString defaultValue() const { return mDefaultValue; }
159 
167  void setValue( const QString &value ) { setText( value.isNull() ? mNullValue : value ); }
168 
177  QString value() const { return isNull() ? QString() : text(); }
178 
185  inline bool isNull() const { return text() == mNullValue; }
186 
193  bool showSpinner() const;
194 
201  void setShowSpinner( bool showSpinner );
202 
208  bool selectOnFocus() const;
209 
215  void setSelectOnFocus( bool selectOnFocus );
216 
223  bool event( QEvent *event ) override;
224 
229  bool hasStateStored() const {return mLineEditState.hasStateStored;}
230 
231  public slots:
232 
238  virtual void clearValue();
239 
244  void storeState();
245 
250  void restoreState();
251 
252  signals:
253 
258  void cleared();
259 
265  void valueChanged( const QString &value );
266 
274 
275 
282 
283  protected:
284  void focusInEvent( QFocusEvent *e ) override;
285  void mouseReleaseEvent( QMouseEvent *e ) override;
286 
287  private slots:
288  void onTextChanged( const QString &text );
289  void updateBusySpinner();
290  void updateClearIcon();
291 
292  private:
293  struct LineEditState
294  {
295  bool hasStateStored = false;
296  QString text;
297  int selectionStart;
298  int selectionLength;
299  int cursorPosition;
300  };
301 
302  QIcon mClearIcon;
303  QAction *mClearAction = nullptr;
304  QAction *mSearchAction = nullptr;
305  QAction *mBusySpinnerAction = nullptr;
306 
307  bool mClearButtonVisible = true;
308  bool mShowSpinner = false;
309 
310  ClearMode mClearMode = ClearToNull;
311 
312  QString mNullValue;
313  QString mDefaultValue;
314  QString mStyleSheet;
315  bool mWaitingForMouseRelease = false;
316  bool mSelectOnFocus = false;
317 
318  LineEditState mLineEditState;
319 
320  QgsAnimatedIcon *mBusySpinnerAnimatedIcon = nullptr;
321 
323  bool shouldShowClear() const;
324 
325  friend class TestQgsFeatureListComboBox;
326 };
327 
329 
336 class SIP_SKIP QgsSpinBoxLineEdit : public QgsFilterLineEdit
337 {
338  Q_OBJECT
339 
340  public:
341 
342  QgsSpinBoxLineEdit( QWidget *parent = nullptr )
343  : QgsFilterLineEdit( parent )
344  {}
345 
346  public slots:
347 
348  void clearValue() override
349  {
350  // don't change the value - let spin boxes handle that by detecting cleared() signal
351  setCursor( Qt::IBeamCursor );
352  setModified( true );
353  emit cleared();
354  }
355 
356  protected:
357  void focusInEvent( QFocusEvent *e ) override;
358 };
360 
361 #endif // QGSFILTERLINEEDIT_H
Animated icon is keeping an animation running if there are listeners connected to frameChanged.
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
void focusInEvent(QFocusEvent *e) override
void selectOnFocusChanged()
Will select all text when this widget receives the focus.
void showSpinnerChanged()
Show a spinner icon.
ClearMode clearMode() const
Returns the clear mode for the widget.
bool showClearButton() const
Returns true if the widget's clear button is visible.
virtual void clearValue()
Clears the widget and resets it to the null value.
bool showSearchIcon() const
Returns if a search icon shall be shown on the left of the image when no text is entered.
ClearMode
Behavior when clearing value of widget.
@ ClearToDefault
Reset value to default value (see defaultValue() )
bool isNull() const
Determine if the current text represents null.
QString nullValue() const
Returns the string used for representing null values in the widget.
void setValue(const QString &value)
Sets the current text for the widget with support for handling null values.
bool hasStateStored() const
Returns if a state is already saved.
QString defaultValue() const
Returns the default value for the widget.
void setNullValue(const QString &nullValue)
Sets the string representation for null values in the widget.
void cleared()
Emitted when the widget is cleared.
void setDefaultValue(const QString &defaultValue)
Sets the default value for the widget.
QString value() const
Returns the text of this edit with support for handling null values.
void setClearMode(ClearMode mode)
Sets the clear mode for the widget.
void valueChanged(const QString &value)
Same as textChanged() but with support for null values.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_END
Definition: qgis_sip.h:194