Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
findwidget.cpp
Go to the documentation of this file.
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
16
17using namespace Qt::StringLiterals;
18
19FindWidget::FindWidget(QWidget *parent)
20 : QWidget(parent)
21 , appPalette(qApp->palette())
22{
24 installEventFilter(this);
25 QHBoxLayout *hboxLayout = new QHBoxLayout(this);
26 QString resourcePath = ":/qt-project.org/assistant/images/"_L1;
27
28#ifndef Q_OS_MACOS
29 hboxLayout->setContentsMargins({});
30 hboxLayout->setSpacing(6);
31 resourcePath.append("win"_L1);
32#else
33 resourcePath.append("mac"_L1);
34#endif
35
36 toolClose = setupToolButton({}, resourcePath + "/closetab.png"_L1);
37 hboxLayout->addWidget(toolClose);
38 connect(toolClose, &QAbstractButton::clicked, this, &QWidget::hide);
39
40 editFind = new QLineEdit(this);
41 hboxLayout->addWidget(editFind);
42 editFind->setMinimumSize(QSize(150, 0));
43 connect(editFind, &QLineEdit::textChanged, this, &FindWidget::textChanged);
44 connect(editFind, &QLineEdit::returnPressed, this, &FindWidget::findNext);
45 connect(editFind, &QLineEdit::textChanged, this, &FindWidget::updateButtons);
46
47 toolPrevious = setupToolButton(tr("Previous"), resourcePath + "/previous.png"_L1);
48 connect(toolPrevious, &QAbstractButton::clicked, this, &FindWidget::findPrevious);
49
50 hboxLayout->addWidget(toolPrevious);
51
52 toolNext = setupToolButton(tr("Next"), resourcePath + "/next.png"_L1);
53 hboxLayout->addWidget(toolNext);
54 connect(toolNext, &QAbstractButton::clicked, this, &FindWidget::findNext);
55
56 checkCase = new QCheckBox(tr("Case Sensitive"), this);
57 hboxLayout->addWidget(checkCase);
58
59 labelWrapped = new QLabel(this);
60 labelWrapped->setScaledContents(true);
61 labelWrapped->setTextFormat(Qt::RichText);
62 labelWrapped->setMinimumSize(QSize(0, 20));
63 labelWrapped->setMaximumSize(QSize(105, 20));
64 labelWrapped->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter);
65 labelWrapped->setText(tr("<img src=\":/qt-project.org/assistant/images/wrap.png\""
66 ">&nbsp;Search wrapped"));
67 hboxLayout->addWidget(labelWrapped);
68
69 QSpacerItem *spacerItem = new QSpacerItem(20, 20, QSizePolicy::Expanding,
70 QSizePolicy::Minimum);
71 hboxLayout->addItem(spacerItem);
72 setMinimumWidth(minimumSizeHint().width());
73 labelWrapped->hide();
74
75 updateButtons();
76}
77
82
84{
86 QWidget::show();
87 editFind->selectAll();
88 editFind->setFocus(Qt::ShortcutFocusReason);
89}
90
92{
94 show();
95 editFind->clear();
96}
97
99{
101 return editFind->text();
102}
103
105{
107 return checkCase->isChecked();
108}
109
110void FindWidget::setPalette(bool found)
111{
113 QPalette palette = editFind->palette();
114 palette.setColor(QPalette::Active, QPalette::Base,
115 found ? QApplication::palette().color(QPalette::Active, QPalette::Base)
116 : QColor(255, 102, 102));
117 editFind->setPalette(palette);
118}
119
121{
123 labelWrapped->setVisible(visible);
124}
125
126void FindWidget::hideEvent(QHideEvent* event)
127{
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
138void FindWidget::showEvent(QShowEvent* event)
139{
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
156void FindWidget::updateButtons()
157{
159 const bool enable = !editFind->text().isEmpty();
160 toolNext->setEnabled(enable);
161 toolPrevious->setEnabled(enable);
162}
163
164void FindWidget::textChanged(const QString &text)
165{
167 emit find(text, true, true);
168}
169
170bool FindWidget::eventFilter(QObject *object, QEvent *e)
171{
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(object, e);
180}
181
182QToolButton* FindWidget::setupToolButton(const QString &text, const QString &icon)
183{
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
195QT_END_NAMESPACE
QString text() const
void findPrevious()
void showEvent(QShowEvent *event) override
This event handler can be reimplemented in a subclass to receive widget show events which are passed ...
void showAndClear()
void setPalette(bool found)
bool eventFilter(QObject *object, QEvent *e) override
Filters events if this object has been installed as an event filter for the watched object.
~FindWidget() override
void hideEvent(QHideEvent *event) override
This event handler can be reimplemented in a subclass to receive widget hide events.
bool caseSensitive() const
void show()
void setTextWrappedVisible(bool visible)
Combined button and popup list for selecting options.
#define TRACE_OBJ
Definition tracer.h:34