forked from YACReader/yacreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yacreader_search_line_edit.cpp
123 lines (102 loc) · 4.43 KB
/
yacreader_search_line_edit.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "yacreader_search_line_edit.h"
#include "yacreader_global.h"
#include "yacreader_global_gui.h"
#include <QToolButton>
#include <QStyle>
#include <QLabel>
#include "QsLog.h"
YACReaderSearchLineEdit::YACReaderSearchLineEdit(QWidget *parent)
: QLineEdit(parent)
{
clearButton = new QToolButton(this);
searchLabel = new QLabel(this);
#ifdef Y_MAC_UI
QPixmap clearIcon;
QPixmap searchIcon;
clearIcon.setDevicePixelRatio(devicePixelRatioF());
searchIcon.setDevicePixelRatio(devicePixelRatioF());
if (devicePixelRatioF() > 1) {
if (!clearIcon.load(":/images/[email protected]")) {
clearIcon.load(":/images/clearSearch.png");
}
if (!searchIcon.load(":/images/[email protected]")) {
searchIcon.load(":/images/iconSearch.png");
}
} else {
clearIcon.load(":/images/clearSearch.png");
searchIcon.load(":/images/iconSearch.png");
}
#else
QPixmap clearIcon = YACReader::hdpiPixmap(":/images/clearSearch.svg", QSize(15, 15));
QPixmap searchIcon = YACReader::hdpiPixmap(":/images/iconSearch.svg", QSize(15, 15));
#endif
searchLabel->setStyleSheet("QLabel { border: none; padding: 0px; }");
searchLabel->setPixmap(searchIcon);
clearButton->setIcon(QIcon(clearIcon));
#ifdef Y_MAC_UI
clearButton->setIconSize(QSize(14, 14));
#else
clearButton->setIconSize(QSize(12, 12));
#endif
clearButton->setCursor(Qt::ArrowCursor);
clearButton->setStyleSheet("QToolButton { border: none; padding: 0px; }");
clearButton->hide();
connect(clearButton, &QAbstractButton::clicked, this, &QLineEdit::clear);
connect(this, &QLineEdit::textChanged, this, &YACReaderSearchLineEdit::updateCloseButton);
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
#ifdef Y_MAC_UI
setStyleSheet(QString("QLineEdit {border-top:1px solid #9F9F9F; border-bottom:1px solid #ACACAC; border-right:1px solid #ACACAC; border-left:1px solid #ACACAC; border-radius: 4px; background-color:#EEEEEE; padding-left: %1px; padding-right: %2px; padding-bottom: 1px; margin-bottom: 1px;} ").arg(searchLabel->sizeHint().width() + frameWidth + 6).arg(clearButton->sizeHint().width() + frameWidth + 2));
#else
setStyleSheet(QString("QLineEdit {color: #ABABAB; border:none; border-radius: 4px; background-color:#404040; padding-left: %1px; padding-right: %2px; padding-bottom: 1px; margin-right: 9px;} ").arg(searchLabel->sizeHint().width() + frameWidth + 6 + 5).arg(clearButton->sizeHint().width() + frameWidth + 2));
#endif
QSize msz = minimumSizeHint();
setMinimumSize(qMax(msz.width(), clearButton->sizeHint().height() + frameWidth * 2 + 2),
qMax(msz.height(), clearButton->sizeHint().height() + frameWidth * 2 + 2));
#ifdef Y_MAC_UI
setMaximumWidth(212);
setFixedHeight(26);
#else
setMaximumWidth(255);
setFixedHeight(26);
#endif
setAttribute(Qt::WA_MacShowFocusRect, false);
setPlaceholderText(tr("type to search"));
connect(this, &QLineEdit::textChanged, this, &YACReaderSearchLineEdit::processText);
}
void YACReaderSearchLineEdit::clearText()
{
disconnect(this, &QLineEdit::textChanged, this, &YACReaderSearchLineEdit::processText);
clear();
connect(this, &QLineEdit::textChanged, this, &YACReaderSearchLineEdit::processText);
}
const QString YACReaderSearchLineEdit::text()
{
return QLineEdit::text();
}
void YACReaderSearchLineEdit::resizeEvent(QResizeEvent *)
{
#ifdef Y_MAC_UI
QSize sz = clearButton->sizeHint();
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
clearButton->move(rect().right() - frameWidth - sz.width(),
(rect().bottom() + 1 - sz.height()) / 2);
QSize szl = searchLabel->sizeHint();
searchLabel->move(6, (rect().bottom() + 1 - szl.height()) / 2);
#else
QSize sz = clearButton->sizeHint();
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
int marginRight = style()->pixelMetric(QStyle::PM_LayoutRightMargin);
clearButton->move(rect().right() - frameWidth - sz.width() - marginRight - 6,
(rect().bottom() + 2 - sz.height()) / 2);
QSize szl = searchLabel->sizeHint();
searchLabel->move(8, (rect().bottom() + 2 - szl.height()) / 2);
#endif
}
void YACReaderSearchLineEdit::updateCloseButton(const QString &text)
{
clearButton->setVisible(!text.isEmpty());
}
void YACReaderSearchLineEdit::processText(const QString &text)
{
emit filterChanged(text);
}