| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples 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 qt-sales@nokia.com.
|
|---|
| 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]
|
|---|