1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the tools applications of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /*! \class TextEditFindWidget
|
---|
43 |
|
---|
44 | \brief A search bar that is commonly added below the searchable text.
|
---|
45 |
|
---|
46 | \internal
|
---|
47 |
|
---|
48 | This widget implements a search bar which becomes visible when the user
|
---|
49 | wants to start searching. It is a modern replacement for the commonly used
|
---|
50 | search dialog. It is usually placed below a QTextEdit using a QVBoxLayout.
|
---|
51 |
|
---|
52 | The QTextEdit instance will need to be associated with this class using
|
---|
53 | setTextEdit().
|
---|
54 |
|
---|
55 | The search is incremental and can be set to case sensitive or whole words
|
---|
56 | using buttons available on the search bar.
|
---|
57 |
|
---|
58 | \sa QTextEdit
|
---|
59 | */
|
---|
60 |
|
---|
61 | #include "texteditfindwidget.h"
|
---|
62 |
|
---|
63 | #include <QtGui/QCheckBox>
|
---|
64 | #include <QtGui/QTextCursor>
|
---|
65 | #include <QtGui/QTextEdit>
|
---|
66 |
|
---|
67 | QT_BEGIN_NAMESPACE
|
---|
68 |
|
---|
69 | /*!
|
---|
70 | Constructs a TextEditFindWidget.
|
---|
71 |
|
---|
72 | \a flags is passed to the AbstractFindWidget constructor.
|
---|
73 | \a parent is passed to the QWidget constructor.
|
---|
74 | */
|
---|
75 | TextEditFindWidget::TextEditFindWidget(FindFlags flags, QWidget *parent)
|
---|
76 | : AbstractFindWidget(flags, parent)
|
---|
77 | , m_textEdit(0)
|
---|
78 | {
|
---|
79 | }
|
---|
80 |
|
---|
81 | /*!
|
---|
82 | Associates a QTextEdit with this find widget. Searches done using this find
|
---|
83 | widget will then apply to the given QTextEdit.
|
---|
84 |
|
---|
85 | An event filter is set on the QTextEdit which intercepts the ESC key while
|
---|
86 | the find widget is active, and uses it to deactivate the find widget.
|
---|
87 |
|
---|
88 | If the find widget is already associated with a QTextEdit, the event filter
|
---|
89 | is removed from this QTextEdit first.
|
---|
90 |
|
---|
91 | \a textEdit may be NULL.
|
---|
92 | */
|
---|
93 | void TextEditFindWidget::setTextEdit(QTextEdit *textEdit)
|
---|
94 | {
|
---|
95 | if (m_textEdit)
|
---|
96 | m_textEdit->removeEventFilter(this);
|
---|
97 |
|
---|
98 | m_textEdit = textEdit;
|
---|
99 |
|
---|
100 | if (m_textEdit)
|
---|
101 | m_textEdit->installEventFilter(this);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /*!
|
---|
105 | \reimp
|
---|
106 | */
|
---|
107 | void TextEditFindWidget::deactivate()
|
---|
108 | {
|
---|
109 | // Pass focus to the text edit
|
---|
110 | if (m_textEdit)
|
---|
111 | m_textEdit->setFocus();
|
---|
112 |
|
---|
113 | AbstractFindWidget::deactivate();
|
---|
114 | }
|
---|
115 |
|
---|
116 | /*!
|
---|
117 | \reimp
|
---|
118 | */
|
---|
119 | void TextEditFindWidget::find(const QString &ttf, bool skipCurrent, bool backward, bool *found, bool *wrapped)
|
---|
120 | {
|
---|
121 | if (!m_textEdit)
|
---|
122 | return;
|
---|
123 |
|
---|
124 | QTextCursor cursor = m_textEdit->textCursor();
|
---|
125 | QTextDocument *doc = m_textEdit->document();
|
---|
126 |
|
---|
127 | if (!doc || cursor.isNull())
|
---|
128 | return;
|
---|
129 |
|
---|
130 | if (cursor.hasSelection())
|
---|
131 | cursor.setPosition((skipCurrent && !backward) ? cursor.position() : cursor.anchor());
|
---|
132 |
|
---|
133 | *found = true;
|
---|
134 | QTextCursor newCursor = cursor;
|
---|
135 |
|
---|
136 | if (!ttf.isEmpty()) {
|
---|
137 | QTextDocument::FindFlags options;
|
---|
138 |
|
---|
139 | if (backward)
|
---|
140 | options |= QTextDocument::FindBackward;
|
---|
141 |
|
---|
142 | if (caseSensitive())
|
---|
143 | options |= QTextDocument::FindCaseSensitively;
|
---|
144 |
|
---|
145 | if (wholeWords())
|
---|
146 | options |= QTextDocument::FindWholeWords;
|
---|
147 |
|
---|
148 | newCursor = doc->find(ttf, cursor, options);
|
---|
149 | if (newCursor.isNull()) {
|
---|
150 | QTextCursor ac(doc);
|
---|
151 | ac.movePosition(options & QTextDocument::FindBackward
|
---|
152 | ? QTextCursor::End : QTextCursor::Start);
|
---|
153 | newCursor = doc->find(ttf, ac, options);
|
---|
154 | if (newCursor.isNull()) {
|
---|
155 | *found = false;
|
---|
156 | newCursor = cursor;
|
---|
157 | } else {
|
---|
158 | *wrapped = true;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | if (!isVisible())
|
---|
164 | show();
|
---|
165 |
|
---|
166 | m_textEdit->setTextCursor(newCursor);
|
---|
167 | }
|
---|
168 |
|
---|
169 | QT_END_NAMESPACE
|
---|