1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "textfinder.h"
52#include <QFile>
53#include <QLineEdit>
54#include <QMessageBox>
55#include <QPushButton>
56#include <QTextEdit>
57#include <QTextStream>
58#include <QUiLoader>
59#include <QVBoxLayout>
60
61//! [4]
62static QWidget *loadUiFile(QWidget *parent)
63{
64 QFile file(":/forms/textfinder.ui");
65 file.open(flags: QIODevice::ReadOnly);
66
67 QUiLoader loader;
68 return loader.load(device: &file, parentWidget: parent);
69}
70//! [4]
71
72//! [5]
73static QString loadTextFile()
74{
75 QFile inputFile(":/forms/input.txt");
76 inputFile.open(flags: QIODevice::ReadOnly);
77 QTextStream in(&inputFile);
78 in.setCodec("UTF-8");
79 return in.readAll();
80}
81//! [5]
82
83//! [0]
84TextFinder::TextFinder(QWidget *parent)
85 : QWidget(parent)
86{
87 QWidget *formWidget = loadUiFile(parent: this);
88
89//! [1]
90 ui_findButton = findChild<QPushButton*>(aName: "findButton");
91 ui_textEdit = findChild<QTextEdit*>(aName: "textEdit");
92 ui_lineEdit = findChild<QLineEdit*>(aName: "lineEdit");
93//! [0] //! [1]
94
95//! [2]
96 QMetaObject::connectSlotsByName(o: this);
97//! [2]
98
99//! [3a]
100 ui_textEdit->setText(loadTextFile());
101//! [3a]
102
103//! [3b]
104 QVBoxLayout *layout = new QVBoxLayout;
105 layout->addWidget(formWidget);
106 setLayout(layout);
107//! [3b]
108
109//! [3c]
110 setWindowTitle(tr(s: "Text Finder"));
111}
112//! [3c]
113
114//! [6] //! [7]
115void TextFinder::on_findButton_clicked()
116{
117 QString searchString = ui_lineEdit->text();
118 QTextDocument *document = ui_textEdit->document();
119
120 bool found = false;
121
122 // undo previous change (if any)
123 document->undo();
124
125 if (searchString.isEmpty()) {
126 QMessageBox::information(parent: this, title: tr(s: "Empty Search Field"),
127 text: tr(s: "The search field is empty. "
128 "Please enter a word and click Find."));
129 } else {
130 QTextCursor highlightCursor(document);
131 QTextCursor cursor(document);
132
133 cursor.beginEditBlock();
134//! [6]
135
136 QTextCharFormat plainFormat(highlightCursor.charFormat());
137 QTextCharFormat colorFormat = plainFormat;
138 colorFormat.setForeground(Qt::red);
139
140 while (!highlightCursor.isNull() && !highlightCursor.atEnd()) {
141 highlightCursor = document->find(subString: searchString, cursor: highlightCursor,
142 options: QTextDocument::FindWholeWords);
143
144 if (!highlightCursor.isNull()) {
145 found = true;
146 highlightCursor.movePosition(op: QTextCursor::WordRight,
147 QTextCursor::KeepAnchor);
148 highlightCursor.mergeCharFormat(modifier: colorFormat);
149 }
150 }
151
152//! [8]
153 cursor.endEditBlock();
154//! [7] //! [9]
155
156 if (found == false) {
157 QMessageBox::information(parent: this, title: tr(s: "Word Not Found"),
158 text: tr(s: "Sorry, the word cannot be found."));
159 }
160 }
161}
162//! [8] //! [9]
163

source code of qttools/examples/uitools/textfinder/textfinder.cpp