QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 
24 QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent, QString nullValue )
25  : QLineEdit( parent )
26  , mNullValue( nullValue )
27 {
28  btnClear = new QToolButton( this );
29  btnClear->setIcon( QgsApplication::getThemeIcon( "/mIconClear.svg" ) );
30  btnClear->setCursor( Qt::ArrowCursor );
31  btnClear->setFocusPolicy( Qt::NoFocus );
32  btnClear->setStyleSheet( "QToolButton { border: none; padding: 0px; }" );
33  btnClear->hide();
34 
35  connect( btnClear, SIGNAL( clicked() ), this, SLOT( clear() ) );
36  connect( btnClear, SIGNAL( clicked() ), this, SIGNAL( cleared() ) );
37  connect( this, SIGNAL( textChanged( const QString& ) ), this,
38  SLOT( toggleClearButton( const QString& ) ) );
39 
40  int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
41  setStyleSheet( QString( "QLineEdit { padding-right: %1px; } " )
42  .arg( btnClear->sizeHint().width() + frameWidth + 1 ) );
43 
44  QSize msz = minimumSizeHint();
45  setMinimumSize( qMax( msz.width(), btnClear->sizeHint().height() + frameWidth * 2 + 2 ),
46  qMax( msz.height(), btnClear->sizeHint().height() + frameWidth * 2 + 2 ) );
47 }
48 
49 void QgsFilterLineEdit::resizeEvent( QResizeEvent * )
50 {
51  QSize sz = btnClear->sizeHint();
52  int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
53  btnClear->move( rect().right() - frameWidth - sz.width(),
54  ( rect().bottom() + 1 - sz.height() ) / 2 );
55 }
56 
58 {
59  setText( mNullValue );
60  setModified( true );
61 }
62 
64 {
66  if ( !isEnabled() )
67  btnClear->setVisible( false );
68  else
69  btnClear->setVisible( text() != mNullValue );
70 }
71 
72 void QgsFilterLineEdit::toggleClearButton( const QString &text )
73 {
74  btnClear->setVisible( !isReadOnly() && text != mNullValue );
75 }