QGIS API Documentation  2.14.0-Essen
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 
26  : QLineEdit( parent )
27  , mNullValue( nullValue )
28  , mFocusInEvent( false )
29 {
30  btnClear = new QToolButton( this );
31  btnClear->setIcon( QgsApplication::getThemeIcon( "/mIconClear.svg" ) );
32  btnClear->setCursor( Qt::ArrowCursor );
33  btnClear->setFocusPolicy( Qt::NoFocus );
34  btnClear->setStyleSheet( "QToolButton { border: none; padding: 0px; }" );
35  btnClear->hide();
36 
37  connect( btnClear, SIGNAL( clicked() ), this, SLOT( clear() ) );
38  connect( btnClear, SIGNAL( clicked() ), this, SIGNAL( cleared() ) );
39  connect( this, SIGNAL( textChanged( const QString& ) ), this,
40  SLOT( onTextChanged( const QString& ) ) );
41 
42  int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
43  mStyleSheet = QString( "QLineEdit { padding-right: %1px; } " )
44  .arg( btnClear->sizeHint().width() + frameWidth + 1 );
45 
46  QSize msz = minimumSizeHint();
47  setMinimumSize( qMax( msz.width(), btnClear->sizeHint().height() + frameWidth * 2 + 2 ),
48  qMax( msz.height(), btnClear->sizeHint().height() + frameWidth * 2 + 2 ) );
49 }
50 
52 {
53  if ( !mFocusInEvent )
55  else
56  mFocusInEvent = false;
57 }
58 
60 {
62  if ( e->reason() == Qt::MouseFocusReason && isNull() )
63  {
64  mFocusInEvent = true;
65  selectAll();
66  }
67 }
68 
70 {
71  QSize sz = btnClear->sizeHint();
72  int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
73  btnClear->move( rect().right() - frameWidth - sz.width(),
74  ( rect().bottom() + 1 - sz.height() ) / 2 );
75 }
76 
77 void QgsFilterLineEdit::clear()
78 {
79  setText( mNullValue );
80  setModified( true );
81 }
82 
84 {
86  btnClear->setVisible( isEnabled() && !isReadOnly() && !isNull() );
87 }
88 
90 {
92  btnClear->setVisible( isEnabled() && !isReadOnly() && !isNull() );
93 }
94 
95 
96 void QgsFilterLineEdit::onTextChanged( const QString &text )
97 {
98  btnClear->setVisible( isEnabled() && !isReadOnly() && !isNull() );
99 
100  if ( isNull() )
101  {
102  setStyleSheet( QString( "QLineEdit { font: italic; color: gray; } %1" ).arg( mStyleSheet ) );
103  emit valueChanged( QString::null );
104  }
105  else
106  {
107  setStyleSheet( mStyleSheet );
108  emit valueChanged( text );
109  }
110 }
void valueChanged(const QString &value)
Same as textChanged(const QString& ) but with support for Null values.
void setStyleSheet(const QString &styleSheet)
int width() const
void setCursor(const QCursor &)
virtual QSize sizeHint() const
void setText(const QString &)
virtual QSize minimumSizeHint() const
void setFocusPolicy(Qt::FocusPolicy policy)
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
virtual void setVisible(bool visible)
void textChanged(const QString &text)
void setIcon(const QIcon &icon)
virtual void changeEvent(QEvent *ev)
void setModified(bool)
bool isReadOnly() const
void changeEvent(QEvent *e) override
void setMinimumSize(const QSize &)
Qt::FocusReason reason() const
bool isEnabled() const
void mousePressEvent(QMouseEvent *e) override
void move(int x, int y)
void hide()
void selectAll()
virtual void paintEvent(QPaintEvent *)
void resizeEvent(QResizeEvent *e) override
QRect rect() const
bool isNull() const
Determine if the current text represents Null.
QgsFilterLineEdit(QWidget *parent=nullptr, const QString &nullValue=QString::null)
void focusInEvent(QFocusEvent *e) override
virtual void focusInEvent(QFocusEvent *e)
int height() const
int bottom() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
virtual void mousePressEvent(QMouseEvent *e)