-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.cpp
372 lines (323 loc) · 12.1 KB
/
widget.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
369
370
371
372
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtWidgets>
#include <QDebug>
QT_USE_NAMESPACE
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
readSettings();
QLabel * lbl = new QLabel("<H3>ЧАСТОТНИЙ АНАЛІЗ ТЕКСТУ (тільки українські букви):</H3>");
QGridLayout *bottomLayout = new QGridLayout;
QFrame *separator = new QFrame;
separator->setLineWidth(1);
separator->setMidLineWidth(1);
separator->setFrameShape(QFrame::HLine);
separator->setPalette(QPalette(QColor(0, 0, 0)));
//bottomLayout->addWidget(separator);
QLabel * lblTextObject = new QLabel("&Insert text to FILD:");
insertTextObjectButton = new QPushButton(tr("STAT F&&ILD"));
QLabel * lblFileObject = new QLabel("&File Text for STAT:");
insertFileObjectButton = new QPushButton(tr("OPEN &&FILE"));
QLabel * lblSaveObject = new QLabel("&Save STAT at FILD:");
insertSaveObjectButton = new QPushButton(tr("&&SAVE STAT:"));
bottomLayout->addWidget(lblTextObject,0,0);
bottomLayout->addWidget(insertTextObjectButton,1,0);
lblTextObject->setBuddy(insertTextObjectButton);
lblTextObject->setAlignment(Qt::AlignBottom | Qt::AlignLeft);
lblTextObject->setFrameStyle(QFrame::Box | QFrame::Raised); //(QFrame::Panel | QFrame::Sunken);
bottomLayout->addWidget(lblFileObject,0,1);
bottomLayout->addWidget(insertFileObjectButton,1,1);
lblFileObject->setBuddy(insertFileObjectButton);
lblFileObject->setAlignment(Qt::AlignBottom | Qt::AlignLeft);
lblFileObject->setFrameStyle(QFrame::Box | QFrame::Raised); //(QFrame::Panel | QFrame::Sunken);
bottomLayout->addWidget(lblSaveObject,0,2);
bottomLayout->addWidget(insertSaveObjectButton,1,2);
lblSaveObject->setBuddy(insertSaveObjectButton);
lblSaveObject->setAlignment(Qt::AlignBottom | Qt::AlignLeft);
lblSaveObject->setFrameStyle(QFrame::Box | QFrame::Raised); //(QFrame::Panel | QFrame::Sunken);
textEdit = new QTextEdit;
// textEdit->resize(1100,80);
// textEdit->minimumSize(1100,80);
textEdit->setMinimumHeight(20);
textEdit->setMaximumHeight(120);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(lbl);
mainLayout->addWidget(separator);
mainLayout->addLayout(bottomLayout);
mainLayout->addWidget(textEdit);
connect(insertTextObjectButton, SIGNAL(clicked()),
this, SLOT(insertTextObject()));
connect(insertFileObjectButton, SIGNAL(clicked()),
this, SLOT(insertFileObject()));
connect(insertSaveObjectButton, SIGNAL(clicked()),
this, SLOT(saveStat()));
insertSeries();
mainLayout->addWidget(chartView);
lblStatLabel = new QLabel("...");
mainLayout->addWidget(lblStatLabel);
setLayout(mainLayout);
}
void Widget::addFileDB()
{
// #include <QFileDialog>
QString fileName = QFileDialog::getOpenFileName(this, "Open File", "/", "Text Files (*.txt);;All Files (*.*)");
if (!fileName.isEmpty()) {
// do something with the selected file
}
QFile file(fileName);
file.open(QIODevice::ReadOnly | QIODevice::Text);
for(int i= 0; i < 33; i++) alphabet[i] = 0;
if(file.exists())
{
int i= 1;
textEdit->clear();
textEdit->setPlainText("");
while (!file.atEnd()) {
QString line = file.readLine();
textEdit->insertPlainText(line);
line = line.toUpper();
if (line != "\n")
{
caseLetters(line);
i++;
}
}
}
file.close();
}
void Widget::addTextDB()
{
for(int i= 0; i < 33; i++) alphabet[i] = 0;
QString line = textEdit->toPlainText();
line = line.toUpper();
caseLetters(line);
}
void Widget::insertTextObject()
{
addTextDB();
chart->removeSeries(series); // https://stackoverflow.com/questions/39171173/qt-qcharts-add-and-remove-series
series->clear();
//textEdit
for(int i=0; i<32; i++) {
series->append(i, alphabet[i]);
}
chart->createDefaultAxes();
chart->addSeries(series);
insertStatLabel();
}
void Widget::insertFileObject()
{
addFileDB();
chart->removeSeries(series); // https://stackoverflow.com/questions/39171173/qt-qcharts-add-and-remove-series
series->clear();
//textEdit
for(int i=0; i<32; i++) {
series->append(i, alphabet[i]);
}
chart->createDefaultAxes();
chart->addSeries(series);
insertStatLabel();
}
void Widget::insertSeries()
{
series = new QLineSeries();
// *series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);
chart = new QChart();
chart->legend()->hide();
series->append(0, 10);
series->append(0, 0);
series->append(32, 0);
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle("0-32 відповідає : А Б В Г Ґ Д Е Є Ж З И І Ї Й К Л М Н О П Р С Т У Ф Х Ц Ч Ш Щ ь Ю Я");
chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
}
void Widget::insertStatLabel()
{
//QString strStatLabel = "А\tБ\tВ\tГ\tҐ\tД\tЕ\tЄ\tЖ\tЗ\tИ\tІ\tЇ\tЙ\tК\tЛ\tМ\tН\tО\tП\tР\tС\tТ\tУ\tФ\tХ\tЦ\tЧ\tШ\tЩ\tь\tЮ\tЯ\n";
// QString strStatLabel = "АБВГҐДЕЄЖЗИІЇЙКЛМНОПРСТУФХЦЧШЩьЮЯ\n";
// for(int i=0; i < 32; i++)
// {
// if(!i%10) {strStatLabel += QString::number(alphabet[i]) + "\t";}
// else {strStatLabel += QString::number(alphabet[i]) + "\n";}
// }
QString strStatLabel = "АБВГҐДЕЄЖЗИІЇЙКЛМНОПРСТУФХЦЧШЩьЮЯ\n";
QString strStatLabelSUM = "";
for(int i=0; i < 32; i++)
{
if(((i+1) % 7) == 0 ) {strStatLabelSUM = strStatLabelSUM + strStatLabel[i] + " =\t" + QString::number(alphabet[i]) + "\n";}
else {strStatLabelSUM = strStatLabelSUM + strStatLabel[i] + " =\t" + QString::number(alphabet[i]) + "\t";}
}
lblStatLabel->setText(strStatLabelSUM);
lblStatLabel->setStyleSheet( "font-size: 12pt;");
}
void Widget::saveStat()
{
QString strStatLabel = "АБВГҐДЕЄЖЗИІЇЙКЛМНОПРСТУФХЦЧШЩьЮЯ\n";
QString strStatLabelSUM;
for(int i=0; i < 32; i++) strStatLabelSUM = strStatLabelSUM + strStatLabel[i] + "\t" + QString::number(alphabet[i]) + "\n";
//lblStatLabel->setText(strStatLabel);
//lblStatLabel->setStyleSheet( "font-size: 9pt;");
QString fileName = QFileDialog::getSaveFileName(this, "Save File", "/", "Text Files (*.txt);;All Files (*.*)");
if (!fileName.isEmpty()) { //if(!filename.isNull()){
// save contents to the selected file
QFile f( fileName );
f.open( QIODevice::WriteOnly );
QTextStream out(&f);
//out << textEdit->toPlainText();
out << strStatLabelSUM;
}
}
void Widget::caseLetters(QString caseLettersStr)
{
QString line = caseLettersStr;
for (int j = 0; j < line.size(); j++ ) {
switch (line[j].unicode())
{
case 0x0410: //'А':
alphabet[0]++;
break;
case 0x0411: //'Б':
alphabet[1]++;
break;
case 0x0412: //'В':
alphabet[2]++;
break;
case 0x0413: //'Г':
alphabet[3]++;
break;
case 0x0490: //'Ґ':
alphabet[4]++;
break;
case 0x0414: //'Д':
alphabet[5]++;
break;
case 0x0415: //'Е':
alphabet[6]++;
break;
// case 0x0401: //'Ё':
// alphabet[6]++;
// break;
case 0x0404: //'Є':
alphabet[7]++;
break;
case 0x0416: //'Ж':
alphabet[8]++;
break;
case 0x0417: //'З':
alphabet[9]++;
break;
case 0x0418: //'И':
alphabet[10]++;
break;
case 0x0406: //'І':
alphabet[11]++;;
break;
case 0x0407: //'Ї':
alphabet[12]++;
break;
case 0x0419: //'Й':
alphabet[13]++;
break;
case 0x041A: //'К':
alphabet[14]++;
break;
case 0x041B: //'Л':
alphabet[15]++;
break;
case 0x041C: //'М':
alphabet[16]++;
break;
case 0x041D: //'Н':
alphabet[17]++;
break;
case 0x041E: //'О':
alphabet[18]++;
break;
case 0x041F: //'П':
alphabet[19]++;
break;
case 0x0420: //'Р':
alphabet[20]++;
break;
case 0x0421: //'С':
alphabet[21]++;
break;
case 0x0422: //'Т':
alphabet[22]++;
break;
case 0x0423: //'У':
alphabet[23]++;
break;
case 0x0424: //'Ф':
alphabet[24]++;
break;
case 0x0425: //'Х':
alphabet[25]++;
break;
case 0x0426: //'Ц':
alphabet[26]++;
break;
case 0x0427: //'Ч':
alphabet[27]++;
break;
case 0x0428: //'Ш':
alphabet[28]++;
break;
case 0x0429: //'Щ':
alphabet[29]++;
break;
// case 0x042A: //'Ъ':
// alphabet[27]++;
// break;
// case 0x042B: //'Ы':
// alphabet[28]++;
// break;
case 0x042C: //'Ь':
alphabet[30]++;
break;
// case 0x042D: //'Э':
// alphabet[30]++;
// break;
case 0x042E: //'Ю':
alphabet[31]++;
break;
case 0x042F: //'Я':
alphabet[32]++;
break;
// case 0x040E: //'Ў':
// alphabet[17]++;
// break;
// case 0x040B: //'Ћ':
// alphabet[17]++;
// break;
default:
break;
}
}
}
Widget::~Widget()
{
writeSettings();
}
void Widget::writeSettings()
{
QSettings settings(QSettings::IniFormat, QSettings::UserScope,"text_chast_analiz5", "Capy");
settings.beginGroup("MainWindow");
settings.setValue("size", size());
settings.setValue("pos", pos());
settings.endGroup();
}
void Widget::readSettings()
{
QSettings settings(QSettings::IniFormat, QSettings::UserScope,"text_chast_analiz5", "Capy");
settings.beginGroup("MainWindow");
resize(settings.value("size", QSize(400, 400)).toSize());
move(settings.value("pos", QPoint(200, 200)).toPoint());
settings.endGroup();
}