forked from Palamariuk/TextAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.cpp
368 lines (290 loc) · 9.76 KB
/
application.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "application.h"
#include "ui_application.h"
#include "FileOpenningException.h"
Application::Application(QWidget *parent):
QMainWindow(parent), ui(new Ui::Application){
ui->setupUi(this);
ui->statusbar->addPermanentWidget(ui->btAdvansedStatistics);
ui->menuFile->setToolTipsVisible(true);
ui->menuEdit->setToolTipsVisible(true);
ui->menuAnalysis->setToolTipsVisible(true);
text = new Text();
}
Application::~Application()
{
delete ui;
}
void Application::on_openFile_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this, "Open some file");
try {
text->readFromFile(fileName);
} catch (FileOpenningException& error){
QMessageBox::critical(this, "File Openning Error", error.what());
return;
}
ui->textEdit << (*text);
}
void Application::on_saveFile_triggered()
{
ui->textEdit >> *(text);
try {
if(!(text->file().isEmpty())){
text->saveToFile(text->file());
} else {
on_saveFileAs_triggered();
}
} catch (FileSavingException& error) {
QMessageBox::critical(this, "File Saving Error", error.what());
return;
}
}
void Application::on_saveFileAs_triggered()
{
try {
QString fileName = QFileDialog::getSaveFileName(this, "Save as");
text->saveToFile(fileName);
} catch (FileSavingException& error) {
QMessageBox::critical(this, "File Saving Error", error.what());
return;
}
}
void Application::on_closeFile_triggered()
{
if(!text->saved()){
QMessageBox warning;
warning.setWindowTitle("Clearing text");
warning.setIcon(QMessageBox::Warning);
warning.setText("You forgot to save data.\n"
"Do you want to clear text without saving?");
warning.setStandardButtons(QMessageBox::Cancel | QMessageBox::No |
QMessageBox::Yes);
int result = warning.exec();
if(result == QMessageBox::Cancel){
return;
} else if(result == QMessageBox::No) {
on_saveFile_triggered();
}
}
ui->textEdit->clear();
text->clearAll();
}
void Application::on_actionUndo_triggered()
{
ui->textEdit->undo();
}
void Application::on_actionRedo_triggered()
{
ui->textEdit->redo();
}
void Application::on_actionCopy_triggered()
{
ui->textEdit->copy();
}
void Application::on_actionPaste_triggered()
{
ui->textEdit->paste();
clearFormatting(ui->textEdit);
}
void Application::on_textEdit_textChanged()
{
text->setSaveStatus(false);
QString message = "Total length: " +
QString::number(ui->textEdit->toPlainText().length());
ui->statusbar->showMessage(message);
}
void Application::on_clearFormatting_triggered()
{
clearFormatting(ui->textEdit);
}
void Application::highlightText(QTextEdit *textEdit, QColor color,
size_t begin, size_t length)
{
int end = begin + length;
QTextCharFormat format;
format.setBackground(color);
QTextCursor cursor(textEdit->document());
cursor.setPosition(begin, QTextCursor::MoveAnchor);
cursor.setPosition(end, QTextCursor::KeepAnchor);
cursor.setCharFormat(format);
}
void Application::on_btAdvansedStatistics_clicked()
{
ui->textEdit >> *(text);
TextAnalyzer analyzer(*text);
size_t nLetters = analyzer.computeNumberOfLetters();
size_t nWords = analyzer.computeNumberOfWords();
size_t nSentences = analyzer.computeNumberOfSentences();
QString message = "Letters: " + QString::number(nLetters) +
" | Words: " + QString::number(nWords) +
" | Sentences: " + QString::number(nSentences);
ui->statusbar->showMessage(message);
}
void Application::on_getAverageLength_triggered()
{
if(ui->textEdit->toPlainText().isEmpty()){
QMessageBox::warning(this, "No words",
"There are no words in your text");
return;
}
ui->textEdit >> *(text);
TextAnalyzer analyzer(*text);
double averageWordsLength = analyzer.computeAverageWordsLength();
double averageSentencesLength = analyzer.computeAverageSentencesLength();
QMessageBox::information(this, "Information about average lengths",
"The average length of sentences = " +
QString::number(averageSentencesLength) + "\n" +
"The average length of words = " +
QString::number(averageWordsLength));
}
void Application::on_showLongestWords_triggered()
{
if(ui->textEdit->toPlainText().isEmpty()){
QMessageBox::warning(this, "No words",
"There are no words in your text");
return;
}
clearFormatting(ui->textEdit);
ui->textEdit >> *(text);
TextAnalyzer analyzer(*text);
highlightWordsByLength(ui->textEdit, analyzer.getWordsPositions(),
analyzer.computeMaxWordsLength());
}
void Application::on_showShortestWords_triggered()
{
if(ui->textEdit->toPlainText().isEmpty()){
QMessageBox::warning(this, "No words",
"There are no words in your text");
return;
}
clearFormatting(ui->textEdit);
ui->textEdit >> *(text);
TextAnalyzer analyzer(*text);
highlightWordsByLength(ui->textEdit, analyzer.getWordsPositions(),
analyzer.computeMinWordsLength());
}
void Application::highlightWordsByLength(QTextEdit *textEdit,
QVector<QPair<QString, int>> words, int len)
{
words.erase(std::partition(words.begin(), words.end(), [=](QPair<QString, int> str) {
return str.first.length() == len;
}), words.end());
for(auto word: words){
highlightText(textEdit, Qt::yellow, word.second, len);
}
}
void Application::highlightAllOccures(QTextEdit *textEdit, TextAnalyzer analyzer,
QVector<QString> substrings)
{
QVector<int> hues{0, 20, 40, 60, 80, 100, 120,
140, 160, 180, 200, 220, 240,
260, 280, 300, 320, 340};
int ind = 0;
for(auto substring: substrings){
auto positions = analyzer.findAll(substring);
auto color = QColor::fromHsl(hues[ind++ % 18], rand() % 156 + 100, 128);
for(auto position: positions){
highlightText(textEdit, color, position, substring.length());
}
}
}
void Application::on_getLetterFrequency_triggered()
{
ui->textEdit >> *(text);
Frequency frequency(*text);
frequency.exec();
}
void Application::on_showIdSentences_triggered()
{
clearFormatting(ui->textEdit);
if(ui->textEdit->toPlainText().isEmpty()){
QMessageBox::warning(this, "No words",
"There are no words in your text");
return;
}
ui->textEdit >> *(text);
TextAnalyzer analyzer(*text);
auto sentences = analyzer.getIdenticalSentences();
if(sentences.isEmpty()){
QMessageBox::warning(this, "No identical sentences",
"There are no identical sentences in your text");
}
highlightAllOccures(ui->textEdit, analyzer, sentences);
}
void Application::on_repeatNumbers_triggered()
{
clearFormatting(ui->textEdit);
if(ui->textEdit->toPlainText().isEmpty()){
QMessageBox::warning(this, "No words",
"There are no words in your text");
return;
}
if(!ui->textEdit->isReadOnly()){
ui->textEdit >> *(text);
TextAnalyzer analyzer(*text);
Text result = analyzer.generateRepeatNumbers();
ui->textEdit->setHtml(result.text());
ui->textEdit->setReadOnly(true);
ui->repeatNumbers->setText("Hide repeat numbers");
} else {
ui->textEdit << *(text);
ui->textEdit->setReadOnly(false);
ui->repeatNumbers->setText("Show repeat numbers");
}
}
void Application::on_showIdProperNames_triggered()
{
clearFormatting(ui->textEdit);
if(ui->textEdit->toPlainText().isEmpty()){
QMessageBox::warning(this, "No words",
"There are no words in your text");
return;
}
ui->textEdit >> *(text);
TextAnalyzer analyzer(*text);
auto properNames = analyzer.getIdenticalProperNames();
if(properNames.isEmpty()){
QMessageBox::warning(this, "No identical proper names",
"There are no identical proper "
"names in your text");
}
highlightAllOccures(ui->textEdit, analyzer, properNames);
}
void Application::clearFormatting(QTextEdit *textEdit)
{
highlightText(textEdit, Qt::white, 0, ui->textEdit->toPlainText().length());
}
void Application::on_showTask_triggered()
{
QFile file(":/Resourses/task.txt");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
qDebug() << "File is not opened.";
}
QString task = file.readAll();
QMessageBox::information(this, "Course work task", task);
}
void Application::closeEvent(QCloseEvent *event)
{
if(!text->saved()){
QMessageBox warning;
warning.setWindowTitle("Closing application");
warning.setIcon(QMessageBox::Warning);
warning.setText("You forgot to save data.\n"
"Do you want to exit without saving?");
warning.setStandardButtons(QMessageBox::Cancel | QMessageBox::No |
QMessageBox::Yes);
int result = warning.exec();
switch (result) {
case QMessageBox::Yes :
event->accept();
break;
case QMessageBox::No :
on_saveFile_triggered();
event->accept();
break;
case QMessageBox::Cancel :
event->ignore();
break;
}
}
}