From 4ddbd550d8c97f178bba9be03ea207b0df5fefee Mon Sep 17 00:00:00 2001 From: Mike Gelfand Date: Sun, 21 Dec 2014 15:34:52 +0000 Subject: [PATCH] Move filter text clearing button into line edit. Use Qt-provided button on Qt 5.2+. --- qt/filterbar.cc | 71 ++++++++++++++++++++++++++++++++++++++++--------- qt/filterbar.h | 19 ++++++++++++- 2 files changed, 77 insertions(+), 13 deletions(-) diff --git a/qt/filterbar.cc b/qt/filterbar.cc index d0e59df5c..335bf4d1f 100644 --- a/qt/filterbar.cc +++ b/qt/filterbar.cc @@ -8,12 +8,11 @@ */ #include -#include #include #include -#include #include #include +#include #include #include "app.h" @@ -219,6 +218,63 @@ FilterBarComboBox::paintEvent (QPaintEvent * e) } } +/**** +***** +****/ + +FilterBarLineEdit::FilterBarLineEdit (QWidget * parent): + QLineEdit (parent), + myClearButton (nullptr) +{ +#if QT_VERSION < QT_VERSION_CHECK(5, 2, 0) + const QIcon icon = QIcon::fromTheme ("edit-clear", style ()->standardIcon (QStyle::SP_DialogCloseButton)); + const int iconSize = style ()->pixelMetric (QStyle::PM_SmallIconSize); + + myClearButton = new QToolButton (this); + myClearButton->setStyleSheet (QLatin1String ("QToolButton{border:0;padding:0;margin:0}")); + myClearButton->setToolButtonStyle (Qt::ToolButtonIconOnly); + myClearButton->setFocusPolicy (Qt::NoFocus); + myClearButton->setCursor (Qt::ArrowCursor); + myClearButton->setIconSize (QSize (iconSize, iconSize)); + myClearButton->setIcon (icon); + myClearButton->hide (); + + const int frameWidth = style ()->pixelMetric (QStyle::PM_DefaultFrameWidth); + const QSize minSizeHint = minimumSizeHint (); + const QSize buttonSizeHint = myClearButton->sizeHint (); + + setStyleSheet (QString::fromLatin1 ("QLineEdit{padding-right:%1px}").arg (buttonSizeHint.width () + frameWidth + 1)); + setMinimumSize (qMax (minSizeHint.width (), buttonSizeHint.width () + frameWidth * 2 + 2), + qMax (minSizeHint.height (), buttonSizeHint.height () + frameWidth * 2 + 2)); + + connect (this, SIGNAL (textChanged (QString)), this, SLOT (updateClearButtonVisibility ())); + connect (myClearButton, SIGNAL (clicked ()), this, SLOT (clear ())); +#else + setClearButtonEnabled (true); +#endif +} + +void FilterBarLineEdit::resizeEvent (QResizeEvent * event) +{ + QLineEdit::resizeEvent (event); + +#if QT_VERSION < QT_VERSION_CHECK(5, 2, 0) + const int frameWidth = style ()->pixelMetric (QStyle::PM_DefaultFrameWidth); + const QRect editRect = rect(); + const QSize buttonSizeHint = myClearButton->sizeHint (); + + myClearButton->move (editRect.right () - frameWidth - buttonSizeHint.width (), + editRect.top () + (editRect.height () - buttonSizeHint.height ()) / 2); +#endif +} + +void FilterBarLineEdit::updateClearButtonVisibility () +{ +#if QT_VERSION < QT_VERSION_CHECK(5, 2, 0) + myClearButton->setVisible (!text ().isEmpty ()); +#endif +} + /**** ***** ***** ACTIVITY @@ -444,19 +500,10 @@ FilterBar::FilterBar (Prefs& prefs, TorrentModel& torrents, TorrentFilter& filte h->addWidget (myTrackerCombo, 1); h->addSpacing (hmargin*2); - myLineEdit = new QLineEdit (this); + myLineEdit = new FilterBarLineEdit (this); h->addWidget (myLineEdit); connect (myLineEdit, SIGNAL (textChanged (QString)), this, SLOT (onTextChanged (QString))); - QPushButton * p = new QPushButton (this); - QIcon icon = QIcon::fromTheme ("edit-clear", style ()->standardIcon (QStyle::SP_DialogCloseButton)); - int iconSize = style ()->pixelMetric (QStyle::PM_SmallIconSize); - p->setIconSize (QSize (iconSize, iconSize)); - p->setIcon (icon); - p->setFlat (true); - h->addWidget (p); - connect (p, SIGNAL (clicked (bool)), myLineEdit, SLOT (clear ())); - // listen for changes from the other players connect (&myPrefs, SIGNAL (changed (int)), this, SLOT (refreshPref (int))); connect (myActivityCombo, SIGNAL (currentIndexChanged (int)), this, SLOT (onActivityIndexChanged (int))); diff --git a/qt/filterbar.h b/qt/filterbar.h index 5c6393d58..9c828c96a 100644 --- a/qt/filterbar.h +++ b/qt/filterbar.h @@ -12,6 +12,7 @@ #include #include +#include #include class QLabel; @@ -56,6 +57,22 @@ class FilterBarComboBox: public QComboBox virtual void paintEvent (QPaintEvent * e); }; +class FilterBarLineEdit: public QLineEdit +{ + Q_OBJECT + + public: + FilterBarLineEdit (QWidget * parent = 0); + + protected: + virtual void resizeEvent (QResizeEvent * event); + + private slots: + void updateClearButtonVisibility (); + + private: + QToolButton * myClearButton; +}; class FilterBar: public QWidget { @@ -82,7 +99,7 @@ class FilterBar: public QWidget QStandardItemModel * myTrackerModel; QTimer * myRecountTimer; bool myIsBootstrapping; - QLineEdit * myLineEdit; + FilterBarLineEdit * myLineEdit; private slots: void recount (); -- 2.40.0