1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Assistant of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | #include "tracer.h" |
29 | #include "findwidget.h" |
30 | |
31 | #include <QtWidgets/QApplication> |
32 | #include <QtWidgets/QCheckBox> |
33 | #include <QtGui/QHideEvent> |
34 | #include <QtGui/QKeyEvent> |
35 | #include <QtWidgets/QLabel> |
36 | #include <QtWidgets/QLayout> |
37 | #include <QtWidgets/QLineEdit> |
38 | #include <QtWidgets/QToolButton> |
39 | |
40 | QT_BEGIN_NAMESPACE |
41 | |
42 | FindWidget::FindWidget(QWidget *parent) |
43 | : QWidget(parent) |
44 | , appPalette(qApp->palette()) |
45 | { |
46 | TRACE_OBJ |
47 | installEventFilter(filterObj: this); |
48 | QHBoxLayout *hboxLayout = new QHBoxLayout(this); |
49 | QString resourcePath = QLatin1String(":/qt-project.org/assistant/images/" ); |
50 | |
51 | #ifndef Q_OS_MAC |
52 | hboxLayout->setContentsMargins(QMargins()); |
53 | hboxLayout->setSpacing(6); |
54 | resourcePath.append(s: QLatin1String("win" )); |
55 | #else |
56 | resourcePath.append(QLatin1String("mac" )); |
57 | #endif |
58 | |
59 | toolClose = setupToolButton(text: QString(), |
60 | icon: resourcePath + QLatin1String("/closetab.png" )); |
61 | hboxLayout->addWidget(toolClose); |
62 | connect(sender: toolClose, signal: &QAbstractButton::clicked, receiver: this, slot: &QWidget::hide); |
63 | |
64 | editFind = new QLineEdit(this); |
65 | hboxLayout->addWidget(editFind); |
66 | editFind->setMinimumSize(QSize(150, 0)); |
67 | connect(sender: editFind, signal: &QLineEdit::textChanged, receiver: this, slot: &FindWidget::textChanged); |
68 | connect(sender: editFind, signal: &QLineEdit::returnPressed, receiver: this, slot: &FindWidget::findNext); |
69 | connect(sender: editFind, signal: &QLineEdit::textChanged, receiver: this, slot: &FindWidget::updateButtons); |
70 | |
71 | toolPrevious = setupToolButton(text: tr(s: "Previous" ), |
72 | icon: resourcePath + QLatin1String("/previous.png" )); |
73 | connect(sender: toolPrevious, signal: &QAbstractButton::clicked, receiver: this, slot: &FindWidget::findPrevious); |
74 | |
75 | hboxLayout->addWidget(toolPrevious); |
76 | |
77 | toolNext = setupToolButton(text: tr(s: "Next" ), |
78 | icon: resourcePath + QLatin1String("/next.png" )); |
79 | hboxLayout->addWidget(toolNext); |
80 | connect(sender: toolNext, signal: &QAbstractButton::clicked, receiver: this, slot: &FindWidget::findNext); |
81 | |
82 | checkCase = new QCheckBox(tr(s: "Case Sensitive" ), this); |
83 | hboxLayout->addWidget(checkCase); |
84 | |
85 | labelWrapped = new QLabel(this); |
86 | labelWrapped->setScaledContents(true); |
87 | labelWrapped->setTextFormat(Qt::RichText); |
88 | labelWrapped->setMinimumSize(QSize(0, 20)); |
89 | labelWrapped->setMaximumSize(QSize(105, 20)); |
90 | labelWrapped->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter); |
91 | labelWrapped->setText(tr(s: "<img src=\":/qt-project.org/assistant/images/wrap.png\"" |
92 | "> Search wrapped" )); |
93 | hboxLayout->addWidget(labelWrapped); |
94 | |
95 | QSpacerItem *spacerItem = new QSpacerItem(20, 20, QSizePolicy::Expanding, |
96 | QSizePolicy::Minimum); |
97 | hboxLayout->addItem(spacerItem); |
98 | setMinimumWidth(minimumSizeHint().width()); |
99 | labelWrapped->hide(); |
100 | |
101 | updateButtons(); |
102 | } |
103 | |
104 | FindWidget::~FindWidget() |
105 | { |
106 | TRACE_OBJ |
107 | } |
108 | |
109 | void FindWidget::show() |
110 | { |
111 | TRACE_OBJ |
112 | QWidget::show(); |
113 | editFind->selectAll(); |
114 | editFind->setFocus(Qt::ShortcutFocusReason); |
115 | } |
116 | |
117 | void FindWidget::showAndClear() |
118 | { |
119 | TRACE_OBJ |
120 | show(); |
121 | editFind->clear(); |
122 | } |
123 | |
124 | QString FindWidget::text() const |
125 | { |
126 | TRACE_OBJ |
127 | return editFind->text(); |
128 | } |
129 | |
130 | bool FindWidget::caseSensitive() const |
131 | { |
132 | TRACE_OBJ |
133 | return checkCase->isChecked(); |
134 | } |
135 | |
136 | void FindWidget::setPalette(bool found) |
137 | { |
138 | TRACE_OBJ |
139 | QPalette palette = editFind->palette(); |
140 | palette.setColor(acg: QPalette::Active, acr: QPalette::Base, acolor: found ? Qt::white |
141 | : QColor(255, 102, 102)); |
142 | editFind->setPalette(palette); |
143 | } |
144 | |
145 | void FindWidget::setTextWrappedVisible(bool visible) |
146 | { |
147 | TRACE_OBJ |
148 | labelWrapped->setVisible(visible); |
149 | } |
150 | |
151 | void FindWidget::hideEvent(QHideEvent* event) |
152 | { |
153 | TRACE_OBJ |
154 | #if defined(BROWSER_QTWEBKIT) |
155 | // TODO: remove this once webkit supports setting the palette |
156 | if (!event->spontaneous()) |
157 | qApp->setPalette(appPalette); |
158 | #else // BROWSER_QTWEBKIT |
159 | Q_UNUSED(event); |
160 | #endif |
161 | } |
162 | |
163 | void FindWidget::showEvent(QShowEvent* event) |
164 | { |
165 | TRACE_OBJ |
166 | #if defined(BROWSER_QTWEBKIT) |
167 | // TODO: remove this once webkit supports setting the palette |
168 | if (!event->spontaneous()) { |
169 | QPalette p = appPalette; |
170 | p.setColor(QPalette::Inactive, QPalette::Highlight, |
171 | p.color(QPalette::Active, QPalette::Highlight)); |
172 | p.setColor(QPalette::Inactive, QPalette::HighlightedText, |
173 | p.color(QPalette::Active, QPalette::HighlightedText)); |
174 | qApp->setPalette(p); |
175 | } |
176 | #else // BROWSER_QTWEBKIT |
177 | Q_UNUSED(event); |
178 | #endif |
179 | } |
180 | |
181 | void FindWidget::updateButtons() |
182 | { |
183 | TRACE_OBJ |
184 | const bool enable = !editFind->text().isEmpty(); |
185 | toolNext->setEnabled(enable); |
186 | toolPrevious->setEnabled(enable); |
187 | } |
188 | |
189 | void FindWidget::textChanged(const QString &text) |
190 | { |
191 | TRACE_OBJ |
192 | emit find(text, forward: true, incremental: true); |
193 | } |
194 | |
195 | bool FindWidget::eventFilter(QObject *object, QEvent *e) |
196 | { |
197 | TRACE_OBJ |
198 | if (e->type() == QEvent::KeyPress) { |
199 | if ((static_cast<QKeyEvent*>(e))->key() == Qt::Key_Escape) { |
200 | hide(); |
201 | emit escapePressed(); |
202 | } |
203 | } |
204 | return QWidget::eventFilter(watched: object, event: e); |
205 | } |
206 | |
207 | QToolButton* FindWidget::setupToolButton(const QString &text, const QString &icon) |
208 | { |
209 | TRACE_OBJ |
210 | QToolButton *toolButton = new QToolButton(this); |
211 | |
212 | toolButton->setText(text); |
213 | toolButton->setAutoRaise(true); |
214 | toolButton->setIcon(QIcon(icon)); |
215 | toolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
216 | |
217 | return toolButton; |
218 | } |
219 | |
220 | QT_END_NAMESPACE |
221 | |