|
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 examples 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 #include <QtUiTools> |
|
43 #include <QtGui> |
|
44 #include "textfinder.h" |
|
45 |
|
46 //! [0] |
|
47 TextFinder::TextFinder(QWidget *parent) |
|
48 : QWidget(parent) |
|
49 { |
|
50 QWidget *formWidget = loadUiFile(); |
|
51 |
|
52 //! [1] |
|
53 ui_findButton = qFindChild<QPushButton*>(this, "findButton"); |
|
54 ui_textEdit = qFindChild<QTextEdit*>(this, "textEdit"); |
|
55 ui_lineEdit = qFindChild<QLineEdit*>(this, "lineEdit"); |
|
56 //! [0] //! [1] |
|
57 |
|
58 //! [2] |
|
59 QMetaObject::connectSlotsByName(this); |
|
60 //! [2] |
|
61 |
|
62 //! [3a] |
|
63 loadTextFile(); |
|
64 //! [3a] |
|
65 |
|
66 //! [3b] |
|
67 QVBoxLayout *layout = new QVBoxLayout; |
|
68 layout->addWidget(formWidget); |
|
69 setLayout(layout); |
|
70 //! [3b] |
|
71 |
|
72 //! [3c] |
|
73 setWindowTitle(tr("Text Finder")); |
|
74 isFirstTime = true; |
|
75 } |
|
76 //! [3c] |
|
77 |
|
78 //! [4] |
|
79 QWidget* TextFinder::loadUiFile() |
|
80 { |
|
81 QUiLoader loader; |
|
82 |
|
83 QFile file(":/forms/textfinder.ui"); |
|
84 file.open(QFile::ReadOnly); |
|
85 |
|
86 QWidget *formWidget = loader.load(&file, this); |
|
87 file.close(); |
|
88 |
|
89 return formWidget; |
|
90 } |
|
91 //! [4] |
|
92 |
|
93 //! [5] |
|
94 void TextFinder::loadTextFile() |
|
95 { |
|
96 QFile inputFile(":/forms/input.txt"); |
|
97 inputFile.open(QIODevice::ReadOnly); |
|
98 QTextStream in(&inputFile); |
|
99 QString line = in.readAll(); |
|
100 inputFile.close(); |
|
101 |
|
102 ui_textEdit->append(line); |
|
103 ui_textEdit->setUndoRedoEnabled(false); |
|
104 ui_textEdit->setUndoRedoEnabled(true); |
|
105 } |
|
106 //! [5] |
|
107 |
|
108 //! [6] //! [7] |
|
109 void TextFinder::on_findButton_clicked() |
|
110 { |
|
111 QString searchString = ui_lineEdit->text(); |
|
112 QTextDocument *document = ui_textEdit->document(); |
|
113 |
|
114 bool found = false; |
|
115 |
|
116 if (isFirstTime == false) |
|
117 document->undo(); |
|
118 |
|
119 if (searchString == "") { |
|
120 QMessageBox::information(this, tr("Empty Search Field"), |
|
121 "The search field is empty. Please enter a word and click Find."); |
|
122 } else { |
|
123 |
|
124 QTextCursor highlightCursor(document); |
|
125 QTextCursor cursor(document); |
|
126 |
|
127 cursor.beginEditBlock(); |
|
128 //! [6] |
|
129 |
|
130 QTextCharFormat plainFormat(highlightCursor.charFormat()); |
|
131 QTextCharFormat colorFormat = plainFormat; |
|
132 colorFormat.setForeground(Qt::red); |
|
133 |
|
134 while (!highlightCursor.isNull() && !highlightCursor.atEnd()) { |
|
135 highlightCursor = document->find(searchString, highlightCursor, QTextDocument::FindWholeWords); |
|
136 |
|
137 if (!highlightCursor.isNull()) { |
|
138 found = true; |
|
139 highlightCursor.movePosition(QTextCursor::WordRight, |
|
140 QTextCursor::KeepAnchor); |
|
141 highlightCursor.mergeCharFormat(colorFormat); |
|
142 } |
|
143 } |
|
144 |
|
145 //! [8] |
|
146 cursor.endEditBlock(); |
|
147 //! [7] //! [9] |
|
148 isFirstTime = false; |
|
149 |
|
150 if (found == false) { |
|
151 QMessageBox::information(this, tr("Word Not Found"), |
|
152 "Sorry, the word cannot be found."); |
|
153 } |
|
154 } |
|
155 } |
|
156 //! [8] //! [9] |