]> granicus.if.org Git - transmission/commitdiff
Move filter text clearing button into line edit. Use Qt-provided button on Qt 5.2+.
authorMike Gelfand <mikedld@mikedld.com>
Sun, 21 Dec 2014 15:34:52 +0000 (15:34 +0000)
committerMike Gelfand <mikedld@mikedld.com>
Sun, 21 Dec 2014 15:34:52 +0000 (15:34 +0000)
qt/filterbar.cc
qt/filterbar.h

index d0e59df5cda71458e96191f9d3a167ca98295773..335bf4d1f72ea2f84ccf6b00256d579bc5ba35a5 100644 (file)
@@ -8,12 +8,11 @@
  */
 
 #include <QAbstractItemView>
-#include <QPushButton>
 #include <QLabel>
 #include <QHBoxLayout>
-#include <QLineEdit>
 #include <QStylePainter>
 #include <QString>
+#include <QToolButton>
 #include <QtGui>
 
 #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)));
index 5c6393d58a03c15981231b20a186c9458904d1c6..9c828c96a0db7034b08d903f1c7e38841db7217b 100644 (file)
@@ -12,6 +12,7 @@
 
 #include <QComboBox>
 #include <QItemDelegate>
+#include <QLineEdit>
 #include <QWidget>
 
 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 ();