summaryrefslogtreecommitdiffstats
blob: 033db78867b4299b87dc4c9573ebafbb6ef08994 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * Copyright (c) 2014-2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

/* Contains demo Trolltech code from http://git.forwardbias.in/?p=lineeditclearbutton.git with license: */
/****************************************************************************
**
** Copyright (c) 2007 Trolltech ASA <info@trolltech.com>
**
** Use, modification and distribution is allowed without limitation,
** warranty, liability or support of any kind.
**
****************************************************************************/

#include <Swift/QtUI/QtClosableLineEdit.h>

#include <QApplication>
#include <QKeyEvent>
#include <QStyle>
#include <QToolButton>
#include <QtGlobal>

namespace Swift {

const int QtClosableLineEdit::clearButtonPadding = 2;

QtClosableLineEdit::QtClosableLineEdit(QWidget *parent) : QLineEdit(parent) {
    clearButton = new QToolButton(this);
    clearButton->setIcon(style()->standardIcon(QStyle::SP_TitleBarCloseButton));
    clearButton->setIconSize(QSize(16,16));
    clearButton->setCursor(Qt::ArrowCursor);
    clearButton->setStyleSheet("QToolButton { border: none; padding: 0px; }");
    clearButton->hide();
    connect(clearButton, SIGNAL(clicked()), this, SLOT(handleCloseButtonClicked()));
    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 minimumSize = minimumSizeHint();
    setMinimumSize(qMax(minimumSize.width(), clearButton->sizeHint().width() + frameWidth * 2 + clearButtonPadding),
                                qMax(minimumSize.height(), clearButton->sizeHint().height() + frameWidth * 2 + clearButtonPadding));
}

void QtClosableLineEdit::resizeEvent(QResizeEvent *) {
    QSize size = clearButton->sizeHint();
    int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
    int verticalAdjustment = 1;
#if defined(Q_OS_WIN32)
    // This vertical adjustment is required on Windows so the close button is vertically centered in the line edit.
    verticalAdjustment += 2;
#endif
    clearButton->move(rect().right() - frameWidth - size.width(), (rect().bottom() + verticalAdjustment - size.height())/2);
}

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

void QtClosableLineEdit::handleCloseButtonClicked() {
    clear();
    QApplication::postEvent(this, new QKeyEvent(QEvent::KeyPress, Qt::Key_Escape, Qt::NoModifier));
    QApplication::postEvent(this, new QKeyEvent(QEvent::KeyRelease, Qt::Key_Escape, Qt::NoModifier));
}

}