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
abstractfindwidget.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5/*! \class AbstractFindWidget
6
7 \brief A search bar that is commonly added below a searchable widget.
8
9 \internal
10
11 This widget implements a search bar which becomes visible when the user
12 wants to start searching. It is a modern replacement for the commonly used
13 search dialog. It is usually placed below the target widget using a QVBoxLayout.
14
15 The search is incremental and can be set to case sensitive or whole words
16 using buttons available on the search bar.
17 */
18
20
21#include <QtWidgets/qcheckbox.h>
22#include <QtWidgets/qlabel.h>
23#include <QtWidgets/qlayout.h>
24#include <QtWidgets/qlayoutitem.h>
25#include <QtWidgets/qlineedit.h>
26#include <QtWidgets/qtoolbutton.h>
27
28#include <QtGui/qaction.h>
29#include <QtGui/qevent.h>
30#include <QtGui/qshortcut.h>
31
32#include <QtCore/qcoreapplication.h>
33#include <QtCore/qcoreevent.h>
34#include <QtCore/qfile.h>
35#include <QtCore/qtimer.h>
36
37QT_BEGIN_NAMESPACE
38
39using namespace Qt::StringLiterals;
40
41static QIcon afwCreateIconSet(const QString &name)
42{
43 QStringList candidates = QStringList()
44 << (QString::fromUtf8(":/qt-project.org/shared/images/") + name)
45#ifdef Q_OS_MACOS
46 << (QString::fromUtf8(":/qt-project.org/shared/images/mac/") + name);
47#else
48 << (QString::fromUtf8(":/qt-project.org/shared/images/win/") + name);
49#endif
50
51 for (const QString &f : std::as_const(candidates)) {
52 if (QFile::exists(f))
53 return QIcon(f);
54 }
55
56 return QIcon();
57}
58
59/*!
60 Constructs an AbstractFindWidget.
61
62 \a flags can change the layout and turn off certain features.
63 \a parent is passed to the QWidget constructor.
64 */
65AbstractFindWidget::AbstractFindWidget(FindFlags flags, QWidget *parent)
66 : QWidget(parent)
67{
68 QBoxLayout *topLayOut;
69 QBoxLayout *layOut;
70 if (flags & NarrowLayout) {
71 topLayOut = new QVBoxLayout(this);
72 layOut = new QHBoxLayout;
73 topLayOut->addLayout(layOut);
74 } else {
75 topLayOut = layOut = new QHBoxLayout(this);
76 }
77#ifndef Q_OS_MACOS
78 topLayOut->setSpacing(6);
79 topLayOut->setContentsMargins(QMargins());
80#endif
81
82 m_toolClose = new QToolButton(this);
83 m_toolClose->setIcon(afwCreateIconSet("closetab.png"_L1));
84 m_toolClose->setAutoRaise(true);
85 layOut->addWidget(m_toolClose);
86 connect(m_toolClose, &QAbstractButton::clicked, this, &AbstractFindWidget::deactivate);
87
88 m_editFind = new QLineEdit(this);
89 layOut->addWidget(m_editFind);
90 connect(m_editFind, &QLineEdit::returnPressed, this, &AbstractFindWidget::findNext);
91 connect(m_editFind, &QLineEdit::textChanged, this, &AbstractFindWidget::findCurrentText);
92 connect(m_editFind, &QLineEdit::textChanged, this, &AbstractFindWidget::updateButtons);
93
94 m_toolPrevious = new QToolButton(this);
95 m_toolPrevious->setAutoRaise(true);
96 m_toolPrevious->setText(tr("&Previous"));
97 m_toolPrevious->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
98 m_toolPrevious->setIcon(afwCreateIconSet("previous.png"_L1));
99 layOut->addWidget(m_toolPrevious);
100 connect(m_toolPrevious, &QAbstractButton::clicked, this, &AbstractFindWidget::findPrevious);
101
102 m_toolNext = new QToolButton(this);
103 m_toolNext->setAutoRaise(true);
104 m_toolNext->setText(tr("&Next"));
105 m_toolNext->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
106 m_toolNext->setIcon(afwCreateIconSet("next.png"_L1));
107 layOut->addWidget(m_toolNext);
108 connect(m_toolNext, &QAbstractButton::clicked, this, &AbstractFindWidget::findNext);
109
110 if (flags & NarrowLayout) {
111 QSizePolicy sp(QSizePolicy::Preferred, QSizePolicy::Fixed);
112 m_toolPrevious->setSizePolicy(sp);
113 m_toolPrevious->setMinimumWidth(m_toolPrevious->minimumSizeHint().height());
114 m_toolNext->setSizePolicy(sp);
115 m_toolNext->setMinimumWidth(m_toolNext->minimumSizeHint().height());
116
117 QSpacerItem *spacerItem =
118 new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum);
119 layOut->addItem(spacerItem);
120
121 layOut = new QHBoxLayout;
122 topLayOut->addLayout(layOut);
123 } else {
124 m_editFind->setMinimumWidth(150);
125 }
126
127 if (!(flags & NoCaseSensitive)) {
128 m_checkCase = new QCheckBox(tr("&Case sensitive"), this);
129 layOut->addWidget(m_checkCase);
130 connect(m_checkCase, &QAbstractButton::toggled,
132 } else {
133 m_checkCase = 0;
134 }
135
136 if (!(flags & NoWholeWords)) {
137 m_checkWholeWords = new QCheckBox(tr("Whole &words"), this);
138 layOut->addWidget(m_checkWholeWords);
139 connect(m_checkWholeWords, &QAbstractButton::toggled,
141 } else {
142 m_checkWholeWords = 0;
143 }
144
145 m_labelWrapped = new QLabel(this);
146 m_labelWrapped->setTextFormat(Qt::RichText);
147 m_labelWrapped->setAlignment(
148 Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter);
149 m_labelWrapped->setText(
150 tr("<img src=\":/qt-project.org/shared/images/wrap.png\">"
151 "&nbsp;Search wrapped"));
152 m_labelWrapped->hide();
153 layOut->addWidget(m_labelWrapped);
154
155 QSpacerItem *spacerItem =
156 new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum);
157 layOut->addItem(spacerItem);
158
159 setMinimumWidth(minimumSizeHint().width());
160
161 updateButtons();
162 hide();
163}
164
165/*!
166 Destroys the AbstractFindWidget.
167 */
169
170/*!
171 Returns the icon set to be used for the action that initiates a search.
172 */
173QIcon AbstractFindWidget::findIconSet()
174{
175 return afwCreateIconSet("searchfind.png"_L1);
176}
177
178/*!
179 Creates an actions with standard icon and shortcut to activate the widget.
180 */
181QAction *AbstractFindWidget::createFindAction(QObject *parent)
182{
183
184 auto result = new QAction(AbstractFindWidget::findIconSet(),
185 tr("&Find in Text..."), parent);
186 connect(result, &QAction::triggered, this, &AbstractFindWidget::activate);
187 result->setShortcut(QKeySequence::Find);
188 return result;
189}
190
191/*!
192 Activates the find widget, making it visible and having focus on its input
193 field.
194 */
195void AbstractFindWidget::activate()
196{
197 show();
198 m_editFind->selectAll();
199 m_editFind->setFocus(Qt::ShortcutFocusReason);
200}
201
202/*!
203 Deactivates the find widget, making it invisible and handing focus to any
204 associated QTextEdit.
205 */
207{
208 hide();
209}
210
212{
213 findInternal(m_editFind->text(), true, false);
214}
215
217{
218 findInternal(m_editFind->text(), true, true);
219}
220
222{
223 findInternal(m_editFind->text(), false, false);
224}
225
226void AbstractFindWidget::keyPressEvent(QKeyEvent *event)
227{
228 if (event->key() == Qt::Key_Escape) {
230 return;
231 }
232
233 QWidget::keyPressEvent(event);
234}
235
236void AbstractFindWidget::updateButtons()
237{
238 const bool en = !m_editFind->text().isEmpty();
239 m_toolPrevious->setEnabled(en);
240 m_toolNext->setEnabled(en);
241}
242
243void AbstractFindWidget::findInternal(const QString &ttf, bool skipCurrent, bool backward)
244{
245 bool found = false;
246 bool wrapped = false;
247 find(ttf, skipCurrent, backward, &found, &wrapped);
248 QPalette p;
249 p.setColor(QPalette::Active, QPalette::Base, found ? Qt::white : QColor(255, 102, 102));
250 m_editFind->setPalette(p);
251 m_labelWrapped->setVisible(wrapped);
252}
253
255{
256 return m_checkCase && m_checkCase->isChecked();
257}
258
260{
261 return m_checkWholeWords && m_checkWholeWords->isChecked();
262}
263
264bool AbstractFindWidget::eventFilter(QObject *object, QEvent *e)
265{
266 if (isVisible() && e->type() == QEvent::KeyPress) {
267 QKeyEvent *ke = static_cast<QKeyEvent*>(e);
268 if (ke->key() == Qt::Key_Escape) {
269 hide();
270 return true;
271 }
272 }
273
274 return QWidget::eventFilter(object, e);
275}
276
277QT_END_NAMESPACE
static QIcon afwCreateIconSet(const QString &name)
A search bar that is commonly added below a searchable widget.
virtual void deactivate()
Deactivates the find widget, making it invisible and handing focus to any associated QTextEdit.
~AbstractFindWidget() override
Destroys the AbstractFindWidget.
void keyPressEvent(QKeyEvent *event) override
This event handler, for event event, can be reimplemented in a subclass to receive key press events f...
QAction * createFindAction(QObject *parent)
Creates an actions with standard icon and shortcut to activate the widget.
bool eventFilter(QObject *object, QEvent *e) override
Filters events if this object has been installed as an event filter for the watched object.