1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | #include "tracer.h" |
4 | #include "findwidget.h" |
5 | |
6 | #include <QtWidgets/QApplication> |
7 | #include <QtWidgets/QCheckBox> |
8 | #include <QtGui/QHideEvent> |
9 | #include <QtGui/QKeyEvent> |
10 | #include <QtWidgets/QLabel> |
11 | #include <QtWidgets/QLayout> |
12 | #include <QtWidgets/QLineEdit> |
13 | #include <QtWidgets/QToolButton> |
14 | |
15 | QT_BEGIN_NAMESPACE |
16 | |
17 | FindWidget::FindWidget(QWidget *parent) |
18 | : QWidget(parent) |
19 | , appPalette(qApp->palette()) |
20 | { |
21 | TRACE_OBJ |
22 | installEventFilter(filterObj: this); |
23 | QHBoxLayout *hboxLayout = new QHBoxLayout(this); |
24 | QString resourcePath = QLatin1String(":/qt-project.org/assistant/images/" ); |
25 | |
26 | #ifndef Q_OS_MAC |
27 | hboxLayout->setContentsMargins(QMargins()); |
28 | hboxLayout->setSpacing(6); |
29 | resourcePath.append(s: QLatin1String("win" )); |
30 | #else |
31 | resourcePath.append(QLatin1String("mac" )); |
32 | #endif |
33 | |
34 | toolClose = setupToolButton(text: QString(), |
35 | icon: resourcePath + QLatin1String("/closetab.png" )); |
36 | hboxLayout->addWidget(toolClose); |
37 | connect(sender: toolClose, signal: &QAbstractButton::clicked, context: this, slot: &QWidget::hide); |
38 | |
39 | editFind = new QLineEdit(this); |
40 | hboxLayout->addWidget(editFind); |
41 | editFind->setMinimumSize(QSize(150, 0)); |
42 | connect(sender: editFind, signal: &QLineEdit::textChanged, context: this, slot: &FindWidget::textChanged); |
43 | connect(sender: editFind, signal: &QLineEdit::returnPressed, context: this, slot: &FindWidget::findNext); |
44 | connect(sender: editFind, signal: &QLineEdit::textChanged, context: this, slot: &FindWidget::updateButtons); |
45 | |
46 | toolPrevious = setupToolButton(text: tr(s: "Previous" ), |
47 | icon: resourcePath + QLatin1String("/previous.png" )); |
48 | connect(sender: toolPrevious, signal: &QAbstractButton::clicked, context: this, slot: &FindWidget::findPrevious); |
49 | |
50 | hboxLayout->addWidget(toolPrevious); |
51 | |
52 | toolNext = setupToolButton(text: tr(s: "Next" ), |
53 | icon: resourcePath + QLatin1String("/next.png" )); |
54 | hboxLayout->addWidget(toolNext); |
55 | connect(sender: toolNext, signal: &QAbstractButton::clicked, context: this, slot: &FindWidget::findNext); |
56 | |
57 | checkCase = new QCheckBox(tr(s: "Case Sensitive" ), this); |
58 | hboxLayout->addWidget(checkCase); |
59 | |
60 | labelWrapped = new QLabel(this); |
61 | labelWrapped->setScaledContents(true); |
62 | labelWrapped->setTextFormat(Qt::RichText); |
63 | labelWrapped->setMinimumSize(QSize(0, 20)); |
64 | labelWrapped->setMaximumSize(QSize(105, 20)); |
65 | labelWrapped->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter); |
66 | labelWrapped->setText(tr(s: "<img src=\":/qt-project.org/assistant/images/wrap.png\"" |
67 | "> Search wrapped" )); |
68 | hboxLayout->addWidget(labelWrapped); |
69 | |
70 | QSpacerItem *spacerItem = new QSpacerItem(20, 20, QSizePolicy::Expanding, |
71 | QSizePolicy::Minimum); |
72 | hboxLayout->addItem(spacerItem); |
73 | setMinimumWidth(minimumSizeHint().width()); |
74 | labelWrapped->hide(); |
75 | |
76 | updateButtons(); |
77 | } |
78 | |
79 | FindWidget::~FindWidget() |
80 | { |
81 | TRACE_OBJ |
82 | } |
83 | |
84 | void FindWidget::show() |
85 | { |
86 | TRACE_OBJ |
87 | QWidget::show(); |
88 | editFind->selectAll(); |
89 | editFind->setFocus(Qt::ShortcutFocusReason); |
90 | } |
91 | |
92 | void FindWidget::showAndClear() |
93 | { |
94 | TRACE_OBJ |
95 | show(); |
96 | editFind->clear(); |
97 | } |
98 | |
99 | QString FindWidget::text() const |
100 | { |
101 | TRACE_OBJ |
102 | return editFind->text(); |
103 | } |
104 | |
105 | bool FindWidget::caseSensitive() const |
106 | { |
107 | TRACE_OBJ |
108 | return checkCase->isChecked(); |
109 | } |
110 | |
111 | void FindWidget::setPalette(bool found) |
112 | { |
113 | TRACE_OBJ |
114 | QPalette palette = editFind->palette(); |
115 | palette.setColor(acg: QPalette::Active, acr: QPalette::Base, acolor: found ? Qt::white |
116 | : QColor(255, 102, 102)); |
117 | editFind->setPalette(palette); |
118 | } |
119 | |
120 | void FindWidget::setTextWrappedVisible(bool visible) |
121 | { |
122 | TRACE_OBJ |
123 | labelWrapped->setVisible(visible); |
124 | } |
125 | |
126 | void FindWidget::hideEvent(QHideEvent* event) |
127 | { |
128 | TRACE_OBJ |
129 | #if defined(BROWSER_QTWEBKIT) |
130 | // TODO: remove this once webkit supports setting the palette |
131 | if (!event->spontaneous()) |
132 | qApp->setPalette(appPalette); |
133 | #else // BROWSER_QTWEBKIT |
134 | Q_UNUSED(event); |
135 | #endif |
136 | } |
137 | |
138 | void FindWidget::showEvent(QShowEvent* event) |
139 | { |
140 | TRACE_OBJ |
141 | #if defined(BROWSER_QTWEBKIT) |
142 | // TODO: remove this once webkit supports setting the palette |
143 | if (!event->spontaneous()) { |
144 | QPalette p = appPalette; |
145 | p.setColor(QPalette::Inactive, QPalette::Highlight, |
146 | p.color(QPalette::Active, QPalette::Highlight)); |
147 | p.setColor(QPalette::Inactive, QPalette::HighlightedText, |
148 | p.color(QPalette::Active, QPalette::HighlightedText)); |
149 | qApp->setPalette(p); |
150 | } |
151 | #else // BROWSER_QTWEBKIT |
152 | Q_UNUSED(event); |
153 | #endif |
154 | } |
155 | |
156 | void FindWidget::updateButtons() |
157 | { |
158 | TRACE_OBJ |
159 | const bool enable = !editFind->text().isEmpty(); |
160 | toolNext->setEnabled(enable); |
161 | toolPrevious->setEnabled(enable); |
162 | } |
163 | |
164 | void FindWidget::textChanged(const QString &text) |
165 | { |
166 | TRACE_OBJ |
167 | emit find(text, forward: true, incremental: true); |
168 | } |
169 | |
170 | bool FindWidget::eventFilter(QObject *object, QEvent *e) |
171 | { |
172 | TRACE_OBJ |
173 | if (e->type() == QEvent::KeyPress) { |
174 | if ((static_cast<QKeyEvent*>(e))->key() == Qt::Key_Escape) { |
175 | hide(); |
176 | emit escapePressed(); |
177 | } |
178 | } |
179 | return QWidget::eventFilter(watched: object, event: e); |
180 | } |
181 | |
182 | QToolButton* FindWidget::setupToolButton(const QString &text, const QString &icon) |
183 | { |
184 | TRACE_OBJ |
185 | QToolButton *toolButton = new QToolButton(this); |
186 | |
187 | toolButton->setText(text); |
188 | toolButton->setAutoRaise(true); |
189 | toolButton->setIcon(QIcon(icon)); |
190 | toolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
191 | |
192 | return toolButton; |
193 | } |
194 | |
195 | QT_END_NAMESPACE |
196 | |