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