This repository has been archived by the owner on Jul 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathfilterlineedit.cpp
89 lines (75 loc) · 3.1 KB
/
filterlineedit.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* QCMA: Cross-platform content manager assistant for the PS Vita
*
* Copyright (C) 2013 Xian Nox
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "filterlineedit.h"
#include <QIcon>
#include <QStyle>
FilterLineEdit::FilterLineEdit(QWidget *obj_parent) :
QLineEdit(obj_parent)
{
int frame_width = frameWidth();
clearButton = new QToolButton(this);
QIcon clearIcon(":/main/resources/images/edit-clear-locationbar-rtl.png");
clearButton->setIcon(clearIcon);
clearButton->setIconSize(QSize(sizeHint().height() - 4 * frame_width,
sizeHint().height() - 4 * frame_width));
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&)));
setStyleSheet(QString("LineEdit { color:black; font-style:normal; padding-right:%1px; }").arg(
clearButton->sizeHint().width() + frame_width + 1));
QSize min_size_hint = minimumSizeHint();
setMinimumSize(qMax(min_size_hint.width(), clearButton->sizeHint().height() + frame_width),
qMax(min_size_hint.height(), clearButton->sizeHint().height() + frame_width));
}
void FilterLineEdit::updateCloseButton(const QString& filter_text)
{
if(filter_text.isEmpty() || filter_text == tr("Filter")) {
clearButton->setVisible(false);
} else {
clearButton->setVisible(true);
}
}
void FilterLineEdit::focusInEvent(QFocusEvent *e)
{
if(this->styleSheet() == "FilterLineEdit { color:gray; font-style:italic; }") {
this->clear();
}
setStyleSheet(QString("FilterLineEdit { color:black; font-style:normal; padding-right:%1px; }").arg(clearButton->sizeHint().width() + frameWidth() + 1));
QLineEdit::focusInEvent(e);
}
void FilterLineEdit::focusOutEvent(QFocusEvent *e)
{
if(this->text().isEmpty()) {
this->setText(tr("Filter"));
this->setStyleSheet("FilterLineEdit { color:gray; font-style:italic; }");
}
QLineEdit::focusOutEvent(e);
}
int FilterLineEdit::frameWidth() const
{
return style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, this);
}
void FilterLineEdit::resizeEvent(QResizeEvent *e)
{
QSize sz = clearButton->sizeHint();
clearButton->move(rect().right() - sz.width(), rect().bottom() - sz.height() + frameWidth());
QLineEdit::resizeEvent(e);
}