Skip to content

Commit

Permalink
option to show a search icon in QgsFilterLineEdit
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Feb 11, 2017
1 parent fe75e1f commit f3102bb
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions images/images.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@
<file>themes/default/processingModel.svg</file>
<file>themes/default/processingScript.svg</file>
<file>themes/default/processingAlgorithm.svg</file>
<file>themes/default/search.svg</file>
</qresource>
<qresource prefix="/images/tips">
<file alias="symbol_levels.png">qgis_tips/symbol_levels.png</file>
Expand Down
66 changes: 66 additions & 0 deletions images/themes/default/search.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions python/gui/qgsfilterlineedit.sip
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ class QgsFilterLineEdit : QLineEdit
*/
QString nullValue() const;

/** Define if a search icon shall be shown on the left of the image
* when no text is entered
* @param visible set to false to hide the search icon
* @note added in QGIS 3.0
*/
void setShowSearchIcon( bool visible );

/** Returns if a search icon shall be shown on the left of the image
* when no text is entered
* @note added in QGIS 3.0
*/
bool showSearchIcon() const;

/** Sets the default value for the widget. The default value is a value
* which the widget will be reset to if it is cleared and the clearMode()
* is equal to ClearToDefault.
Expand Down
31 changes: 31 additions & 0 deletions src/gui/qgsfilterlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent, const QString& nullValue )
: QLineEdit( parent )
, mClearButtonVisible( true )
, mSearchIconVisible( false )
, mClearMode( ClearToNull )
, mNullValue( nullValue )
, mFocusInEvent( false )
Expand All @@ -40,6 +41,10 @@ QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent, const QString& nullValue
QIcon hoverIcon = QgsApplication::getThemeIcon( "/mIconClearTextHover.svg" );
mClearHoverPixmap = hoverIcon.pixmap( mClearIconSize );

QIcon searchIcon = QgsApplication::getThemeIcon( "/search.svg" );
mSearchIconSize = QSize( 16, 16 );
mSearchIconPixmap = searchIcon.pixmap( mSearchIconSize );

connect( this, SIGNAL( textChanged( const QString& ) ), this,
SLOT( onTextChanged( const QString& ) ) );
}
Expand All @@ -55,6 +60,16 @@ void QgsFilterLineEdit::setShowClearButton( bool visible )
update();
}

void QgsFilterLineEdit::setShowSearchIcon( bool visible )
{
bool changed = mSearchIconVisible != visible;
if ( changed )
{
mSearchIconVisible = visible;
update();
}
}

void QgsFilterLineEdit::mousePressEvent( QMouseEvent* e )
{
if ( !mFocusInEvent )
Expand Down Expand Up @@ -134,6 +149,13 @@ void QgsFilterLineEdit::paintEvent( QPaintEvent* e )
else
p.drawPixmap( r.left() , r.top() , mClearIconPixmap );
}

if ( mSearchIconVisible && !shouldShowClear() )
{
QRect r = searchRect();
QPainter p( this );
p.drawPixmap( r.left() , r.top() , mSearchIconPixmap );
}
}

void QgsFilterLineEdit::leaveEvent( QEvent* e )
Expand Down Expand Up @@ -191,3 +213,12 @@ QRect QgsFilterLineEdit::clearRect() const
mClearIconSize.width(),
mClearIconSize.height() );
}

QRect QgsFilterLineEdit::searchRect() const
{
int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
return QRect( rect().left() + frameWidth * 2,
( rect().bottom() + 1 - mSearchIconSize.height() ) / 2,
mSearchIconSize.width(),
mSearchIconSize.height() );
}
19 changes: 19 additions & 0 deletions src/gui/qgsfilterlineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
Q_PROPERTY( QString defaultValue READ defaultValue WRITE setDefaultValue )
Q_PROPERTY( QString value READ value WRITE setValue NOTIFY valueChanged )
Q_PROPERTY( bool showClearButton READ showClearButton WRITE setShowClearButton )
Q_PROPERTY( bool showSearchIcon READ showSearchIcon WRITE setShowSearchIcon )

public:

Expand Down Expand Up @@ -98,6 +99,19 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
*/
QString nullValue() const { return mNullValue; }

/** Define if a search icon shall be shown on the left of the image
* when no text is entered
* @param visible set to false to hide the search icon
* @note added in QGIS 3.0
*/
void setShowSearchIcon( bool visible );

/** Returns if a search icon shall be shown on the left of the image
* when no text is entered
* @note added in QGIS 3.0
*/
bool showSearchIcon() const { return mSearchIconVisible; }

/** Sets the default value for the widget. The default value is a value
* which the widget will be reset to if it is cleared and the clearMode()
* is equal to ClearToDefault.
Expand Down Expand Up @@ -179,6 +193,7 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
private:

bool mClearButtonVisible;
bool mSearchIconVisible;

ClearMode mClearMode;

Expand All @@ -192,10 +207,14 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
QPixmap mClearIconPixmap;
QPixmap mClearHoverPixmap;

QSize mSearchIconSize;
QPixmap mSearchIconPixmap;

//! Returns true if clear button should be shown
bool shouldShowClear() const;

QRect clearRect() const;
QRect searchRect() const;
};

/// @cond PRIVATE
Expand Down

0 comments on commit f3102bb

Please sign in to comment.