QGIS API Documentation  2.17.0-Master (0497e4a)
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  mClearIconSize = QSize( 16, 16 );
39  mClearIconPixmap = clearIcon.pixmap( mClearIconSize );
40  QIcon hoverIcon = QgsApplication::getThemeIcon( "/mIconClearTextHover.svg" );
41  mClearHoverPixmap = hoverIcon.pixmap( mClearIconSize );
42 
43  connect( this, SIGNAL( textChanged( const QString& ) ), this,
44  SLOT( onTextChanged( const QString& ) ) );
45 }
46 
48 {
49  bool changed = mClearButtonVisible != visible;
50  mClearButtonVisible = visible;
51  if ( !visible )
52  mClearHover = false;
53 
54  if ( changed )
55  update();
56 }
57 
59 {
60  if ( !mFocusInEvent )
62  else
63  mFocusInEvent = false;
64 
65  if ( shouldShowClear() && clearRect().contains( e->pos() ) )
66  {
67  clearValue();
68  }
69 }
70 
72 {
74  if ( shouldShowClear() && clearRect().contains( e->pos() ) )
75  {
76  if ( !mClearHover )
77  {
78  setCursor( Qt::ArrowCursor );
79  mClearHover = true;
80  update();
81  }
82  }
83  else if ( mClearHover )
84  {
85  setCursor( Qt::IBeamCursor );
86  mClearHover = false;
87  update();
88  }
89 }
90 
92 {
94  if ( e->reason() == Qt::MouseFocusReason && isNull() )
95  {
96  mFocusInEvent = true;
97  selectAll();
98  }
99 }
100 
102 {
103  switch ( mClearMode )
104  {
105  case ClearToNull:
106  setText( mNullValue );
107  break;
108 
109  case ClearToDefault:
110  setText( mDefaultValue );
111  break;
112  }
113 
114  if ( mClearHover )
115  {
116  setCursor( Qt::IBeamCursor );
117  mClearHover = false;
118  }
119 
120  setModified( true );
121  emit cleared();
122 }
123 
125 {
127  if ( shouldShowClear() )
128  {
129  QRect r = clearRect();
130  QPainter p( this );
131  if ( mClearHover )
132  p.drawPixmap( r.left() , r.top() , mClearHoverPixmap );
133  else
134  p.drawPixmap( r.left() , r.top() , mClearIconPixmap );
135  }
136 }
137 
139 {
140  if ( mClearHover )
141  {
142  mClearHover = false;
143  update();
144  }
145 
147 }
148 
149 void QgsFilterLineEdit::onTextChanged( const QString &text )
150 {
151  if ( isNull() )
152  {
153  setStyleSheet( QString( "QLineEdit { font: italic; color: gray; } %1" ).arg( mStyleSheet ) );
154  emit valueChanged( QString::null );
155  }
156  else
157  {
158  setStyleSheet( mStyleSheet );
159  emit valueChanged( text );
160  }
161 
162  if ( mClearHover && !shouldShowClear() )
163  {
164  setCursor( Qt::IBeamCursor );
165  mClearHover = false;
166  }
167 }
168 
169 bool QgsFilterLineEdit::shouldShowClear() const
170 {
171  if ( !isEnabled() || isReadOnly() || !mClearButtonVisible )
172  return false;
173 
174  switch ( mClearMode )
175  {
176  case ClearToNull:
177  return !isNull();
178 
179  case ClearToDefault:
180  return value() != mDefaultValue;
181  }
182  return false; //avoid warnings
183 }
184 
185 QRect QgsFilterLineEdit::clearRect() const
186 {
187  int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
188  return QRect( rect().right() - frameWidth * 2 - mClearIconSize.width(),
189  ( rect().bottom() + 1 - mClearIconSize.height() ) / 2,
190  mClearIconSize.width(),
191  mClearIconSize.height() );
192 }
void valueChanged(const QString &value)
Same as textChanged() but with support for null values.
void setStyleSheet(const QString &styleSheet)
void leaveEvent(QEvent *e) override
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
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 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)
void cleared()
Emitted when the widget is cleared.
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.