Changeset 14392


Ignore:
Timestamp:
Dec 21, 2014, 3:34:52 PM (8 years ago)
Author:
mikedld
Message:

Move filter text clearing button into line edit. Use Qt-provided button on Qt 5.2+.

Location:
trunk/qt
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/qt/filterbar.cc

    r14380 r14392  
    99
    1010#include <QAbstractItemView>
    11 #include <QPushButton>
    1211#include <QLabel>
    1312#include <QHBoxLayout>
    14 #include <QLineEdit>
    1513#include <QStylePainter>
    1614#include <QString>
     15#include <QToolButton>
    1716#include <QtGui>
    1817
     
    218217      s->drawItemText (&painter, rect, Qt::AlignLeft|Qt::AlignVCenter, opt.palette, true, text);
    219218    }
     219}
     220
     221/****
     222*****
     223****/
     224
     225FilterBarLineEdit::FilterBarLineEdit (QWidget * parent):
     226  QLineEdit (parent),
     227  myClearButton (nullptr)
     228{
     229#if QT_VERSION < QT_VERSION_CHECK(5, 2, 0)
     230  const QIcon icon = QIcon::fromTheme ("edit-clear", style ()->standardIcon (QStyle::SP_DialogCloseButton));
     231  const int iconSize = style ()->pixelMetric (QStyle::PM_SmallIconSize);
     232
     233  myClearButton = new QToolButton (this);
     234  myClearButton->setStyleSheet (QLatin1String ("QToolButton{border:0;padding:0;margin:0}"));
     235  myClearButton->setToolButtonStyle (Qt::ToolButtonIconOnly);
     236  myClearButton->setFocusPolicy (Qt::NoFocus);
     237  myClearButton->setCursor (Qt::ArrowCursor);
     238  myClearButton->setIconSize (QSize (iconSize, iconSize));
     239  myClearButton->setIcon (icon);
     240  myClearButton->hide ();
     241
     242  const int frameWidth = style ()->pixelMetric (QStyle::PM_DefaultFrameWidth);
     243  const QSize minSizeHint = minimumSizeHint ();
     244  const QSize buttonSizeHint = myClearButton->sizeHint ();
     245
     246  setStyleSheet (QString::fromLatin1 ("QLineEdit{padding-right:%1px}").arg (buttonSizeHint.width () + frameWidth + 1));
     247  setMinimumSize (qMax (minSizeHint.width (), buttonSizeHint.width () + frameWidth * 2 + 2),
     248                  qMax (minSizeHint.height (), buttonSizeHint.height () + frameWidth * 2 + 2));
     249
     250  connect (this, SIGNAL (textChanged (QString)), this, SLOT (updateClearButtonVisibility ()));
     251  connect (myClearButton, SIGNAL (clicked ()), this, SLOT (clear ()));
     252#else
     253  setClearButtonEnabled (true);
     254#endif
     255}
     256
     257void FilterBarLineEdit::resizeEvent (QResizeEvent * event)
     258{
     259  QLineEdit::resizeEvent (event);
     260
     261#if QT_VERSION < QT_VERSION_CHECK(5, 2, 0)
     262  const int frameWidth = style ()->pixelMetric (QStyle::PM_DefaultFrameWidth);
     263  const QRect editRect = rect();
     264  const QSize buttonSizeHint = myClearButton->sizeHint ();
     265
     266  myClearButton->move (editRect.right () - frameWidth - buttonSizeHint.width (),
     267                       editRect.top () + (editRect.height () - buttonSizeHint.height ()) / 2);
     268#endif
     269}
     270
     271void FilterBarLineEdit::updateClearButtonVisibility ()
     272{
     273#if QT_VERSION < QT_VERSION_CHECK(5, 2, 0)
     274  myClearButton->setVisible (!text ().isEmpty ());
     275#endif
    220276}
    221277
     
    445501  h->addSpacing (hmargin*2);
    446502
    447   myLineEdit = new QLineEdit (this);
     503  myLineEdit = new FilterBarLineEdit (this);
    448504  h->addWidget (myLineEdit);
    449505  connect (myLineEdit, SIGNAL (textChanged (QString)), this, SLOT (onTextChanged (QString)));
    450 
    451   QPushButton * p = new QPushButton (this);
    452   QIcon icon = QIcon::fromTheme ("edit-clear", style ()->standardIcon (QStyle::SP_DialogCloseButton));
    453   int iconSize = style ()->pixelMetric (QStyle::PM_SmallIconSize);
    454   p->setIconSize (QSize (iconSize, iconSize));
    455   p->setIcon (icon);
    456   p->setFlat (true);
    457   h->addWidget (p);
    458   connect (p, SIGNAL (clicked (bool)), myLineEdit, SLOT (clear ()));
    459506
    460507  // listen for changes from the other players
  • trunk/qt/filterbar.h

    r14241 r14392  
    1313#include <QComboBox>
    1414#include <QItemDelegate>
     15#include <QLineEdit>
    1516#include <QWidget>
    1617
     
    5758};
    5859
     60class FilterBarLineEdit: public QLineEdit
     61{
     62    Q_OBJECT
     63
     64  public:
     65    FilterBarLineEdit (QWidget * parent = 0);
     66
     67  protected:
     68    virtual void resizeEvent (QResizeEvent * event);
     69
     70  private slots:
     71    void updateClearButtonVisibility ();
     72
     73  private:
     74    QToolButton * myClearButton;
     75};
    5976
    6077class FilterBar: public QWidget
     
    83100    QTimer * myRecountTimer;
    84101    bool myIsBootstrapping;
    85     QLineEdit * myLineEdit;
     102    FilterBarLineEdit * myLineEdit;
    86103
    87104  private slots:
Note: See TracChangeset for help on using the changeset viewer.