QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsfilterlineedit.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfilterlineedit.cpp
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 #include "qgsfilterlineedit.h"
19 #include "qgsapplication.h"
20 
21 #include <QToolButton>
22 #include <QStyle>
23 #include <QFocusEvent>
24 #include <QPainter>
25 
27  : QLineEdit( parent )
28  , mClearButtonVisible( true )
29  , mClearMode( ClearToNull )
30  , mNullValue( nullValue )
31  , mFocusInEvent( false )
32  , mClearHover( false )
33 {
34  // need mouse tracking to handle cursor changes
35  setMouseTracking( true );
36 
37  QIcon clearIcon = QgsApplication::getThemeIcon( "/mIconClearText.svg" );
38  int size = fontMetrics().height();
39  mClearIconSize = QSize( size, size );
40  mClearIconPixmap = clearIcon.pixmap( mClearIconSize );
41  QIcon hoverIcon = QgsApplication::getThemeIcon( "/mIconClearTextHover.svg" );
42  mClearHoverPixmap = hoverIcon.pixmap( mClearIconSize );
43  // Make some space for the clear icon
44  QMargins margins( textMargins( ) );
45  margins.setRight( size );
46  setTextMargins( margins );
47  connect( this, SIGNAL( textChanged( const QString& ) ), this,
48  SLOT( onTextChanged( const QString& ) ) );
49 }
50 
52 {
53  bool changed = mClearButtonVisible != visible;
54  mClearButtonVisible = visible;
55  if ( !visible )
56  mClearHover = false;
57 
58  if ( changed )
59  update();
60 }
61 
63 {
64  if ( !mFocusInEvent )
66  else
67  mFocusInEvent = false;
68 
69  if ( shouldShowClear() && clearRect().contains( e->pos() ) )
70  {
71  clearValue();
72  }
73 }
74 
76 {
78  if ( shouldShowClear() && clearRect().contains( e->pos() ) )
79  {
80  if ( !mClearHover )
81  {
82  setCursor( Qt::ArrowCursor );
83  mClearHover = true;
84  update();
85  }
86  }
87  else if ( mClearHover )
88  {
89  setCursor( Qt::IBeamCursor );
90  mClearHover = false;
91  update();
92  }
93 }
94 
96 {
98  if ( e->reason() == Qt::MouseFocusReason && isNull() )
99  {
100  mFocusInEvent = true;
101  selectAll();
102  }
103 }
104 
106 {
107  switch ( mClearMode )
108  {
109  case ClearToNull:
110  setText( mNullValue );
111  selectAll();
112  break;
113 
114  case ClearToDefault:
115  setText( mDefaultValue );
116  break;
117  }
118 
119  if ( mClearHover )
120  {
121  setCursor( Qt::IBeamCursor );
122  mClearHover = false;
123  }
124 
125  setModified( true );
126  emit cleared();
127 }
128 
130 {
132  if ( shouldShowClear() )
133  {
134  QRect r = clearRect();
135  QPainter p( this );
136  if ( mClearHover )
137  p.drawPixmap( r.left() , r.top() , mClearHoverPixmap );
138  else
139  p.drawPixmap( r.left() , r.top() , mClearIconPixmap );
140  }
141 }
142 
144 {
145  if ( mClearHover )
146  {
147  mClearHover = false;
148  update();
149  }
150 
152 }
153 
154 void QgsFilterLineEdit::onTextChanged( const QString &text )
155 {
156  if ( isNull() )
157  {
158  setStyleSheet( QString( "QLineEdit { font: italic; color: gray; } %1" ).arg( mStyleSheet ) );
159  emit valueChanged( QString::null );
160  }
161  else
162  {
163  setStyleSheet( mStyleSheet );
164  emit valueChanged( text );
165  }
166 
167  if ( mClearHover && !shouldShowClear() )
168  {
169  setCursor( Qt::IBeamCursor );
170  mClearHover = false;
171  }
172 }
173 
174 bool QgsFilterLineEdit::shouldShowClear() const
175 {
176  if ( !isEnabled() || isReadOnly() || !mClearButtonVisible )
177  return false;
178 
179  switch ( mClearMode )
180  {
181  case ClearToNull:
182  return !isNull();
183 
184  case ClearToDefault:
185  return value() != mDefaultValue;
186  }
187  return false; //avoid warnings
188 }
189 
190 QRect QgsFilterLineEdit::clearRect() const
191 {
192  int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
193  return QRect( rect().right() - frameWidth * 2 - mClearIconSize.width(),
194  ( rect().bottom() + 1 - mClearIconSize.height() ) / 2,
195  mClearIconSize.width(),
196  mClearIconSize.height() );
197 }
void valueChanged(const QString &value)
Same as textChanged() but with support for null values.
void setStyleSheet(const QString &styleSheet)
void leaveEvent(QEvent *e) override
QMargins textMargins() const
int width() const
void setCursor(const QCursor &)
void mouseMoveEvent(QMouseEvent *e) override
void setText(const QString &)
QStyle * style() const
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const=0
void paintEvent(QPaintEvent *e) override
void textChanged(const QString &text)
void update()
void setModified(bool)
QPixmap pixmap(const QSize &size, Mode mode, State state) const
bool isReadOnly() const
QSize size() const
Qt::FocusReason reason() const
bool isEnabled() const
void mousePressEvent(QMouseEvent *e) override
Reset value to default value (see defaultValue() )
int top() const
virtual void clearValue()
Clears the widget and resets it to the null value.
int left() const
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
void setRight(int right)
void setTextMargins(int left, int top, int right, int bottom)
void selectAll()
virtual void paintEvent(QPaintEvent *)
QRect rect() const
QgsFilterLineEdit(QWidget *parent=nullptr, const QString &nullValue=QString::null)
Constructor for QgsFilterLineEdit.
virtual void mouseMoveEvent(QMouseEvent *e)
void focusInEvent(QFocusEvent *e) override
virtual void focusInEvent(QFocusEvent *e)
QFontMetrics fontMetrics() const
void cleared()
Emitted when the widget is cleared.
int height() const
int height() const
int bottom() const
void setMouseTracking(bool enable)
bool isNull() const
Determine if the current text represents null.
const QPoint & pos() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setShowClearButton(bool visible)
Sets whether the widget&#39;s clear button is visible.
virtual void leaveEvent(QEvent *event)
virtual void mousePressEvent(QMouseEvent *e)
QString value() const
Returns the text of this edit with support for handling null values.