-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow2.cpp
More file actions
192 lines (163 loc) · 5.96 KB
/
mainwindow2.cpp
File metadata and controls
192 lines (163 loc) · 5.96 KB
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
#include "mainwindow2.h"
#include "ui_MainWindow1.h"
#include "finderOfStrings.h"
#include "trigram_process.h"
typedef std::pair<QString, std::vector<std::pair<int, int>>> myPair;
subFind::subFind(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::mainwindow1) {
ui->setupUi(this);
ui->buttonFind->setEnabled(true);
ui->buttonStop->setEnabled(false);
ui->progressBar->setValue(0);
ui->progressBarPreprocess->setValue(0);
ui->label->clear();
connect(ui->buttonSelectDir, &QPushButton::clicked, this, &subFind::select_directory);
connect(ui->buttonFind, &QPushButton::clicked, this, &subFind::start_find);
connect(ui->buttonStop, &QPushButton::clicked, this, &subFind::interruption);
connect(ui->pushButtonStopPreprocess, &QPushButton::clicked, this, &subFind::interruptionTrig);
qRegisterMetaType<myPair>("myPair");
qRegisterMetaType<fileTrigram>("fileTrigram");
fsWatcher = new QFileSystemWatcher(this);
connect(fsWatcher, SIGNAL(fileChanged(QString)), this, SLOT(changed(QString)));
}
subFind::~subFind() {
interruption();
interruptionTrig();
}
void subFind::change(QString path) {
ui->label->clear();
ui->label->setText(path + " was changed");
int index = 0;
for (int i = 0; i < files.size(); i++) {
if (files[i].file == path) {
index = i;
break;
}
}
files[index].trigrams.clear();
finderTrig::addTrigrams(files[index]);
}
void subFind::changed(QString path) {
if(thread && thread->isRunning()){
toChange.push_back(path);
} else {
change(path);
}
}
void subFind::select_directory() {
QString dir = QFileDialog::getExistingDirectory(this, "Select Directory for Scanning",
QString(),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
setWindowTitle(QString("Directory Content - %1").arg(dir));
curDir = dir;
ui->label->clear();
startPreprocess();
}
void subFind::startPreprocess() {
files.clear();
ui->buttonFind->setEnabled(false);
ui->label->clear();
ui->label->setText("Preprocessing");
ui->pushButtonStopPreprocess->setEnabled(true);
ui->progressBarPreprocess->setValue(0);
threadTrig = new QThread;
auto *worker = new finderTrig(curDir);
worker->moveToThread(threadTrig);
connect(threadTrig, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(finished()), threadTrig, SLOT(quit()));
connect(worker, SIGNAL(addFileTrigrams(fileTrigram)), this, SLOT(addFileTrigramsToFiles(fileTrigram)));
connect(worker, SIGNAL(finished()), this, SLOT(finishThings()));
connect(worker, SIGNAL(addToWatcher(QString)), this, SLOT(addToFSWatcher(QString)));
connect(worker, SIGNAL(increaseBarTrig()), this, SLOT(increaseBar()));
connect(worker, SIGNAL(setBar(int)), this, SLOT(set_max_index_bar(int)));
threadTrig->start();
}
void subFind::set_max_index_bar(int max) {
ui->progressBarPreprocess->setMaximum(max);
}
void subFind::addToFSWatcher(QString name) {
fsWatcher->addPath(name);
}
void subFind::finishThings() {
ui->buttonFind->setEnabled(true);
ui->label->clear();
ui->label->setText("Preprocessing is finished");
ui->pushButtonStopPreprocess->setEnabled(false);
}
void subFind::addFileTrigramsToFiles(fileTrigram add) {
files.push_back(add);
}
void subFind::interruption() {
if(thread) {
thread->requestInterruption();
ui->label->setText("Stopped");
ui->buttonStop->setEnabled(false);
}
}
void subFind::interruptionTrig() {
if(threadTrig) {
threadTrig->requestInterruption();
ui->label->setText("Stopped");
ui->pushButtonStopPreprocess->setEnabled(false);
}
}
void subFind::interruptionStart() {
if(thread) {
thread->requestInterruption();
}
}
void subFind::start_find() {
interruptionStart();
ui->treeWidget->clear();
ui->progressBar->setValue(0);
ui->buttonStop->setEnabled(true);
std::string sub = ui->lineEditSubString->text().toStdString();
ui->progressBar->setMaximum(static_cast<int>(files.size()));
thread = new QThread;
auto *worker = new finderSub(curDir, sub, files);
worker->moveToThread(thread);
connect(thread, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
connect(worker, SIGNAL(addToTree(myPair)),
this, SLOT(addToTreeUI(myPair)));
connect(worker, SIGNAL(finished()), this, SLOT(doFinishThings()));
connect(worker, SIGNAL(updateProgressBar()), this, SLOT(updBar()));
time = std::clock();
thread->start();
}
void subFind::updBar() {
ui->progressBar->setValue(ui->progressBar->value() + 1);
}
void subFind::increaseBar() {
ui->progressBarPreprocess->setValue(ui->progressBarPreprocess->value() + 1);
}
void subFind::doFinishThings() {
time = std::clock() - time;
if (ui->treeWidget->topLevelItemCount() == 0) {
auto *item = new QTreeWidgetItem(ui->treeWidget);
item->setText(0, QString("Not Found Substring"));
}
ui->label->clear();
ui->label->setText(QString::number(time / CLOCKS_PER_SEC) + QString(" sec)"));
ui->buttonStop->setEnabled(false);
for(auto i = static_cast<int>(toChange.size() - 1); i >= 0; i--) {
change(toChange[i]);
}
toChange.clear();
}
void subFind::addToTreeUI(std::pair<QString, std::vector<std::pair<int, int>>> add) {
auto *item = new QTreeWidgetItem(ui->treeWidget);
QString temp = add.first.mid(curDir.length() + 1, add.first.length() - curDir.length());
temp += " founded: ";
item->setText(0, temp);
for (auto &j : add.second) {
auto *itemchild = new QTreeWidgetItem(item);
QString tempi = "";
tempi += "in ";
tempi += QString::number(j.first);
tempi += " line ";
tempi += QString::number(j.second) + " times";
itemchild->setText(0, tempi);
}
}