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
texteditfindwidget.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
4/*! \class TextEditFindWidget
5
6 \brief A search bar that is commonly added below the searchable text.
7
8 \internal
9
10 This widget implements a search bar which becomes visible when the user
11 wants to start searching. It is a modern replacement for the commonly used
12 search dialog. It is usually placed below a QTextEdit using a QVBoxLayout.
13
14 The QTextEdit instance will need to be associated with this class using
15 setTextEdit().
16
17 The search is incremental and can be set to case sensitive or whole words
18 using buttons available on the search bar.
19
20 \sa QTextEdit
21 */
22
24
25#include <QtWidgets/qcheckbox.h>
26#include <QtWidgets/qtextedit.h>
27
28#include <QtGui/qtextcursor.h>
29
31
32/*!
33 Constructs a TextEditFindWidget.
34
35 \a flags is passed to the AbstractFindWidget constructor.
36 \a parent is passed to the QWidget constructor.
37 */
38TextEditFindWidget::TextEditFindWidget(FindFlags flags, QWidget *parent)
40 , m_textEdit(0)
41{
42}
43
44/*!
45 Associates a QTextEdit with this find widget. Searches done using this find
46 widget will then apply to the given QTextEdit.
47
48 An event filter is set on the QTextEdit which intercepts the ESC key while
49 the find widget is active, and uses it to deactivate the find widget.
50
51 If the find widget is already associated with a QTextEdit, the event filter
52 is removed from this QTextEdit first.
53
54 \a textEdit may be NULL.
55 */
56void TextEditFindWidget::setTextEdit(QTextEdit *textEdit)
57{
58 if (m_textEdit)
59 m_textEdit->removeEventFilter(this);
60
61 m_textEdit = textEdit;
62
63 if (m_textEdit)
64 m_textEdit->installEventFilter(this);
65}
66
67/*!
68 \reimp
69 */
71{
72 // Pass focus to the text edit
73 if (m_textEdit)
74 m_textEdit->setFocus();
75
77}
78
79/*!
80 \reimp
81 */
82void TextEditFindWidget::find(const QString &ttf, bool skipCurrent, bool backward, bool *found, bool *wrapped)
83{
84 if (!m_textEdit)
85 return;
86
87 QTextCursor cursor = m_textEdit->textCursor();
88 QTextDocument *doc = m_textEdit->document();
89
90 if (!doc || cursor.isNull())
91 return;
92
93 if (cursor.hasSelection())
94 cursor.setPosition((skipCurrent && !backward) ? cursor.position() : cursor.anchor());
95
96 *found = true;
97 QTextCursor newCursor = cursor;
98
99 if (!ttf.isEmpty()) {
100 QTextDocument::FindFlags options;
101
102 if (backward)
103 options |= QTextDocument::FindBackward;
104
105 if (caseSensitive())
106 options |= QTextDocument::FindCaseSensitively;
107
108 if (wholeWords())
109 options |= QTextDocument::FindWholeWords;
110
111 newCursor = doc->find(ttf, cursor, options);
112 if (newCursor.isNull()) {
113 QTextCursor ac(doc);
114 ac.movePosition(options & QTextDocument::FindBackward
115 ? QTextCursor::End : QTextCursor::Start);
116 newCursor = doc->find(ttf, ac, options);
117 if (newCursor.isNull()) {
118 *found = false;
119 newCursor = cursor;
120 } else {
121 *wrapped = true;
122 }
123 }
124 }
125
126 if (!isVisible())
127 show();
128
129 m_textEdit->setTextCursor(newCursor);
130}
131
132QT_END_NAMESPACE
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.
A search bar that is commonly added below the searchable text.
void setTextEdit(QTextEdit *textEdit)
Associates a QTextEdit with this find widget.
void deactivate() override
\reimp
Combined button and popup list for selecting options.