QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
25 QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent, QString nullValue )
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 
51 void QgsFilterLineEdit::mousePressEvent( QMouseEvent* e )
52 {
53  if ( !mFocusInEvent )
54  QLineEdit::mousePressEvent( e );
55  else
56  mFocusInEvent = false;
57 }
58 
59 void QgsFilterLineEdit::focusInEvent( QFocusEvent* e )
60 {
61  QLineEdit::focusInEvent( e );
62  if ( e->reason() == Qt::MouseFocusReason && isNull() )
63  {
64  mFocusInEvent = true;
65  selectAll();
66  }
67 }
68 
69 void QgsFilterLineEdit::resizeEvent( QResizeEvent * )
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 {
85  QLineEdit::changeEvent( e );
86  btnClear->setVisible( isEnabled() && !isReadOnly() && !isNull() );
87 }
88 
89 void QgsFilterLineEdit::paintEvent( QPaintEvent* e )
90 {
91  QLineEdit::paintEvent( e );
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.
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
void mousePressEvent(QMouseEvent *e)
void changeEvent(QEvent *e)
void paintEvent(QPaintEvent *e)
bool isNull() const
Determine if the current text represents Null.
QgsFilterLineEdit(QWidget *parent=0, QString nullValue=QString::null)
void focusInEvent(QFocusEvent *e)
void resizeEvent(QResizeEvent *e)