Skip to content

Commit

Permalink
[FEATURE] add lineedit with builtin clear button
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Nov 2, 2012
1 parent 603c0dc commit 8694cb8
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions images/images.qrc
Expand Up @@ -172,6 +172,7 @@
<file>themes/default/mIconClose.png</file>
<file>themes/default/mIconCollapse.png</file>
<file>themes/default/mIconConnect.png</file>
<file>themes/default/mIconClear.png</file>
<file>themes/default/mIconDbSchema.png</file>
<file>themes/default/mIconDelete.png</file>
<file>themes/default/mIconEditable.png</file>
Expand Down
Binary file added images/themes/default/mIconClear.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions python/gui/gui.sip
Expand Up @@ -23,6 +23,7 @@
%Include qgsexpressionhighlighter.sip
%Include qgsfieldvalidator.sip
%Include qgsfiledropedit.sip
%Include qgsfilterlineedit.sip
%Include qgsformannotationitem.sip
%Include qgsgenericprojectionselector.sip
%Include qgshtmlannotationitem.sip
Expand Down
15 changes: 15 additions & 0 deletions python/gui/qgsfilterlineedit.sip
@@ -0,0 +1,15 @@

/** LineEdit with builtin clear button
*/
class QgsFilterLineEdit : QLineEdit
{
%TypeHeaderCode
#include <qgsfilterlineedit.h>
%End

public:
QgsFilterLineEdit( QWidget* parent = 0 );

protected:
void resizeEvent( QResizeEvent * );
};
3 changes: 3 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -98,6 +98,7 @@ qgsexpressionbuilderdialog.cpp
qgsexpressionhighlighter.cpp
qgsquerybuilder.cpp
qgscollapsiblegroupbox.cpp
qgsfilterlineedit.cpp
)

IF (WITH_TOUCH)
Expand Down Expand Up @@ -186,6 +187,7 @@ qgsexpressionbuilderwidget.h
qgsexpressionhighlighter.h
qgsquerybuilder.h
qgscollapsiblegroupbox.h
qgsfilterlineedit.h
)

QT4_WRAP_CPP(QGIS_GUI_MOC_SRCS ${QGIS_GUI_MOC_HDRS})
Expand Down Expand Up @@ -225,6 +227,7 @@ qgsexpressionbuilderwidget.h
qgsexpressionbuilderdialog.h
qgsexpressionhighlighter.h
qgscollapsiblegroupbox.h
qgsfilterlineedit.h

attributetable/qgsattributetablemodel.h
attributetable/qgsattributetablememorymodel.h
Expand Down
56 changes: 56 additions & 0 deletions src/gui/qgsfilterlineedit.cpp
@@ -0,0 +1,56 @@
/***************************************************************************
qgsfilterlineedit.cpp
------------------------
begin : October 27, 2012
copyright : (C) 2012 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsfilterlineedit.h"
#include "qgsapplication.h"

#include <QToolButton>
#include <QStyle>

QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent ) : QLineEdit( parent )
{
btnClear = new QToolButton( this );
btnClear->setIcon( QgsApplication::getThemeIcon( "/mIconClear.png" ) );
btnClear->setCursor(Qt::ArrowCursor);
btnClear->setStyleSheet( "QToolButton { border: none; padding: 0px; }" );
btnClear->hide();

connect( btnClear, SIGNAL( clicked() ), this, SLOT( clear() ) );
connect( this, SIGNAL( textChanged( const QString& ) ), this,
SLOT( toggleClearButton( const QString& ) ) );

int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
setStyleSheet( QString( "QLineEdit { padding-right: %1px; } " )
.arg( btnClear->sizeHint().width() + frameWidth + 1 ) );

QSize msz = minimumSizeHint();
setMinimumSize( qMax( msz.width(), btnClear->sizeHint().height() + frameWidth * 2 + 2 ),
qMax( msz.height(), btnClear->sizeHint().height() + frameWidth * 2 + 2 ) );
}

void QgsFilterLineEdit::resizeEvent( QResizeEvent * )
{
QSize sz = btnClear->sizeHint();
int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
btnClear->move( rect().right() - frameWidth - sz.width(),
( rect().bottom() + 1 - sz.height() ) / 2 );
}

void QgsFilterLineEdit::toggleClearButton( const QString &text )
{
btnClear->setVisible( !text.isEmpty() );
}
44 changes: 44 additions & 0 deletions src/gui/qgsfilterlineedit.h
@@ -0,0 +1,44 @@
/***************************************************************************
qgsfilterlineedit.h
------------------------
begin : October 27, 2012
copyright : (C) 2012 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSFILTERLINEEDIT_H
#define QGSFILTERLINEEDIT_H

#include <QLineEdit>

class QToolButton;

/** \ingroup gui
* Lineedit with builtin clear button
**/
class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
{
Q_OBJECT
public:
QgsFilterLineEdit( QWidget* parent = 0 );

protected:
void resizeEvent( QResizeEvent * );

private slots:
void toggleClearButton( const QString &text );

private:
QToolButton *btnClear;
};

#endif // QGSFILTERLINEEDIT_H

0 comments on commit 8694cb8

Please sign in to comment.