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_MAC
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, found ? Qt::white
115 : QColor(255, 102, 102));
116 editFind->setPalette(palette);
117}
118
120{
122 labelWrapped->setVisible(visible);
123}
124
125void FindWidget::hideEvent(QHideEvent* event)
126{
128#if defined(BROWSER_QTWEBKIT)
129 // TODO: remove this once webkit supports setting the palette
130 if (!event->spontaneous())
131 qApp->setPalette(appPalette);
132#else // BROWSER_QTWEBKIT
133 Q_UNUSED(event);
134#endif
135}
136
137void FindWidget::showEvent(QShowEvent* event)
138{
140#if defined(BROWSER_QTWEBKIT)
141 // TODO: remove this once webkit supports setting the palette
142 if (!event->spontaneous()) {
143 QPalette p = appPalette;
144 p.setColor(QPalette::Inactive, QPalette::Highlight,
145 p.color(QPalette::Active, QPalette::Highlight));
146 p.setColor(QPalette::Inactive, QPalette::HighlightedText,
147 p.color(QPalette::Active, QPalette::HighlightedText));
148 qApp->setPalette(p);
149 }
150#else // BROWSER_QTWEBKIT
151 Q_UNUSED(event);
152#endif
153}
154
155void FindWidget::updateButtons()
156{
158 const bool enable = !editFind->text().isEmpty();
159 toolNext->setEnabled(enable);
160 toolPrevious->setEnabled(enable);
161}
162
163void FindWidget::textChanged(const QString &text)
164{
166 emit find(text, true, true);
167}
168
169bool FindWidget::eventFilter(QObject *object, QEvent *e)
170{
172 if (e->type() == QEvent::KeyPress) {
173 if ((static_cast<QKeyEvent*>(e))->key() == Qt::Key_Escape) {
174 hide();
175 emit escapePressed();
176 }
177 }
178 return QWidget::eventFilter(object, e);
179}
180
181QToolButton* FindWidget::setupToolButton(const QString &text, const QString &icon)
182{
184 QToolButton *toolButton = new QToolButton(this);
185
186 toolButton->setText(text);
187 toolButton->setAutoRaise(true);
188 toolButton->setIcon(QIcon(icon));
189 toolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
190
191 return toolButton;
192}
193
194QT_END_NAMESPACE
QString text() const
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