|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the tools applications of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
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 |