source: trunk/tools/shared/findwidget/texteditfindwidget.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.1 KB
RevLine 
[2]1/****************************************************************************
2**
[846]3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
[561]4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
[2]6**
7** This file is part of the tools applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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**
[561]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.
[2]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**
[561]36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
[2]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
67QT_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 */
75TextEditFindWidget::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 */
93void 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 */
107void 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 */
119void 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
169QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.