aboutsummaryrefslogtreecommitdiff
path: root/libs/uibase/src/lineeditclear.cpp
blob: 476c8749d00de4d400b2e64a137c04751c9b4bf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/****************************************************************************
**
** Copyright (c) 2007 Trolltech ASA <info@trolltech.com>
**
** Use, modification and distribution is allowed without limitation,
** warranty, liability or support of any kind.
**
****************************************************************************/

#include <uibase/lineeditclear.h>
#include <QStyle>
#include <QToolButton>

namespace MOBase
{

LineEditClear::LineEditClear(QWidget* parent) : QLineEdit(parent)
{
  clearButton = new QToolButton(this);
  QPixmap pixmap(":/MO/gui/edit_clear");
  clearButton->setIcon(QIcon(pixmap));
  clearButton->setIconSize(pixmap.size());
  clearButton->setCursor(Qt::ArrowCursor);
  clearButton->setStyleSheet("QToolButton { border: none; padding: 0px; }");
  clearButton->hide();
  connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
  connect(this, SIGNAL(textChanged(const QString&)), this,
          SLOT(updateCloseButton(const QString&)));
  int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
  setStyleSheet(QString("QLineEdit { padding-right: %1px; } ")
                    .arg(clearButton->sizeHint().width() + frameWidth + 1));
  /*  QSize msz = minimumSizeHint();
    setMinimumSize(qMax(msz.width(), clearButton->sizeHint().height() + frameWidth * 2 +
    2), qMax(msz.height(), clearButton->sizeHint().height() + frameWidth * 2 + 2));*/
}

void LineEditClear::resizeEvent(QResizeEvent*)
{
  QSize sz       = clearButton->sizeHint();
  int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
  clearButton->move(rect().right() - frameWidth - sz.width(),
                    (rect().bottom() + 1 - sz.height()) / 2);
}

void LineEditClear::updateCloseButton(const QString& text)
{
  clearButton->setVisible(!text.isEmpty());
}

}  // namespace MOBase