Skip to content

Commit 8694cb8

Browse files
committed
[FEATURE] add lineedit with builtin clear button
1 parent 603c0dc commit 8694cb8

File tree

7 files changed

+120
-0
lines changed

7 files changed

+120
-0
lines changed

images/images.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
<file>themes/default/mIconClose.png</file>
173173
<file>themes/default/mIconCollapse.png</file>
174174
<file>themes/default/mIconConnect.png</file>
175+
<file>themes/default/mIconClear.png</file>
175176
<file>themes/default/mIconDbSchema.png</file>
176177
<file>themes/default/mIconDelete.png</file>
177178
<file>themes/default/mIconEditable.png</file>

images/themes/default/mIconClear.png

773 Bytes
Loading

python/gui/gui.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
%Include qgsexpressionhighlighter.sip
2424
%Include qgsfieldvalidator.sip
2525
%Include qgsfiledropedit.sip
26+
%Include qgsfilterlineedit.sip
2627
%Include qgsformannotationitem.sip
2728
%Include qgsgenericprojectionselector.sip
2829
%Include qgshtmlannotationitem.sip

python/gui/qgsfilterlineedit.sip

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
/** LineEdit with builtin clear button
3+
*/
4+
class QgsFilterLineEdit : QLineEdit
5+
{
6+
%TypeHeaderCode
7+
#include <qgsfilterlineedit.h>
8+
%End
9+
10+
public:
11+
QgsFilterLineEdit( QWidget* parent = 0 );
12+
13+
protected:
14+
void resizeEvent( QResizeEvent * );
15+
};

src/gui/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ qgsexpressionbuilderdialog.cpp
9898
qgsexpressionhighlighter.cpp
9999
qgsquerybuilder.cpp
100100
qgscollapsiblegroupbox.cpp
101+
qgsfilterlineedit.cpp
101102
)
102103

103104
IF (WITH_TOUCH)
@@ -186,6 +187,7 @@ qgsexpressionbuilderwidget.h
186187
qgsexpressionhighlighter.h
187188
qgsquerybuilder.h
188189
qgscollapsiblegroupbox.h
190+
qgsfilterlineedit.h
189191
)
190192

191193
QT4_WRAP_CPP(QGIS_GUI_MOC_SRCS ${QGIS_GUI_MOC_HDRS})
@@ -225,6 +227,7 @@ qgsexpressionbuilderwidget.h
225227
qgsexpressionbuilderdialog.h
226228
qgsexpressionhighlighter.h
227229
qgscollapsiblegroupbox.h
230+
qgsfilterlineedit.h
228231

229232
attributetable/qgsattributetablemodel.h
230233
attributetable/qgsattributetablememorymodel.h

src/gui/qgsfilterlineedit.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 ) : QLineEdit( parent )
25+
{
26+
btnClear = new QToolButton( this );
27+
btnClear->setIcon( QgsApplication::getThemeIcon( "/mIconClear.png" ) );
28+
btnClear->setCursor(Qt::ArrowCursor);
29+
btnClear->setStyleSheet( "QToolButton { border: none; padding: 0px; }" );
30+
btnClear->hide();
31+
32+
connect( btnClear, SIGNAL( clicked() ), this, SLOT( clear() ) );
33+
connect( this, SIGNAL( textChanged( const QString& ) ), this,
34+
SLOT( toggleClearButton( const QString& ) ) );
35+
36+
int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
37+
setStyleSheet( QString( "QLineEdit { padding-right: %1px; } " )
38+
.arg( btnClear->sizeHint().width() + frameWidth + 1 ) );
39+
40+
QSize msz = minimumSizeHint();
41+
setMinimumSize( qMax( msz.width(), btnClear->sizeHint().height() + frameWidth * 2 + 2 ),
42+
qMax( msz.height(), btnClear->sizeHint().height() + frameWidth * 2 + 2 ) );
43+
}
44+
45+
void QgsFilterLineEdit::resizeEvent( QResizeEvent * )
46+
{
47+
QSize sz = btnClear->sizeHint();
48+
int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
49+
btnClear->move( rect().right() - frameWidth - sz.width(),
50+
( rect().bottom() + 1 - sz.height() ) / 2 );
51+
}
52+
53+
void QgsFilterLineEdit::toggleClearButton( const QString &text )
54+
{
55+
btnClear->setVisible( !text.isEmpty() );
56+
}

src/gui/qgsfilterlineedit.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/***************************************************************************
2+
qgsfilterlineedit.h
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+
#ifndef QGSFILTERLINEEDIT_H
19+
#define QGSFILTERLINEEDIT_H
20+
21+
#include <QLineEdit>
22+
23+
class QToolButton;
24+
25+
/** \ingroup gui
26+
* Lineedit with builtin clear button
27+
**/
28+
class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
29+
{
30+
Q_OBJECT
31+
public:
32+
QgsFilterLineEdit( QWidget* parent = 0 );
33+
34+
protected:
35+
void resizeEvent( QResizeEvent * );
36+
37+
private slots:
38+
void toggleClearButton( const QString &text );
39+
40+
private:
41+
QToolButton *btnClear;
42+
};
43+
44+
#endif // QGSFILTERLINEEDIT_H

0 commit comments

Comments
 (0)