From f3102bbf8f92484e0f06148fabc429ab411f2d39 Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Mon, 6 Feb 2017 16:19:31 +0100 Subject: [PATCH] option to show a search icon in QgsFilterLineEdit --- images/images.qrc | 1 + images/themes/default/search.svg | 66 ++++++++++++++++++++++++++++++++ python/gui/qgsfilterlineedit.sip | 13 +++++++ src/gui/qgsfilterlineedit.cpp | 31 +++++++++++++++ src/gui/qgsfilterlineedit.h | 19 +++++++++ 5 files changed, 130 insertions(+) create mode 100644 images/themes/default/search.svg diff --git a/images/images.qrc b/images/images.qrc index c71b40617cd..0e878ddd16e 100644 --- a/images/images.qrc +++ b/images/images.qrc @@ -554,6 +554,7 @@ themes/default/processingModel.svg themes/default/processingScript.svg themes/default/processingAlgorithm.svg + themes/default/search.svg qgis_tips/symbol_levels.png diff --git a/images/themes/default/search.svg b/images/themes/default/search.svg new file mode 100644 index 00000000000..b185bba190a --- /dev/null +++ b/images/themes/default/search.svg @@ -0,0 +1,66 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + diff --git a/python/gui/qgsfilterlineedit.sip b/python/gui/qgsfilterlineedit.sip index 57f9dad21d2..4eb80ca2a20 100644 --- a/python/gui/qgsfilterlineedit.sip +++ b/python/gui/qgsfilterlineedit.sip @@ -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. diff --git a/src/gui/qgsfilterlineedit.cpp b/src/gui/qgsfilterlineedit.cpp index c1b0a6de1ad..c8a70e95743 100644 --- a/src/gui/qgsfilterlineedit.cpp +++ b/src/gui/qgsfilterlineedit.cpp @@ -26,6 +26,7 @@ QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent, const QString& nullValue ) : QLineEdit( parent ) , mClearButtonVisible( true ) + , mSearchIconVisible( false ) , mClearMode( ClearToNull ) , mNullValue( nullValue ) , mFocusInEvent( false ) @@ -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& ) ) ); } @@ -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 ) @@ -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 ) @@ -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() ); +} diff --git a/src/gui/qgsfilterlineedit.h b/src/gui/qgsfilterlineedit.h index c52c7a3c4e3..d4c211c59fe 100644 --- a/src/gui/qgsfilterlineedit.h +++ b/src/gui/qgsfilterlineedit.h @@ -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: @@ -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. @@ -179,6 +193,7 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit private: bool mClearButtonVisible; + bool mSearchIconVisible; ClearMode mClearMode; @@ -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