-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
263 lines (202 loc) · 6.29 KB
/
mainwindow.cpp
File metadata and controls
263 lines (202 loc) · 6.29 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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <string>
#include <experimental/filesystem>
#include <map>
#include <algorithm>
#include <QTableWidget>
#include <QProgressBar>
#include<QDebug>
namespace fs = std::experimental::filesystem;
using namespace std;
ifstream file;
string path ;/* = "C:/Users/Aly EL-kady/Desktop/questions";*/
class BST {
struct node {
string word;
vector<string> filename;
node* left;
node* right;
};
node* root;
node* root2 ;
node* insert(string myword , string myfilename, node* t)
{
if (t == NULL)
{
t = new node;
t->word = myword;
t->filename.push_back(myfilename);
t->left = t->right = NULL;
}
else if (myword < t->word)
t->left = insert(myword, myfilename, t->left);
else if (myword > t->word)
t->right = insert(myword, myfilename, t->right);
else if (myword == t->word)
t->filename.push_back(myfilename);
return t;
}
node* find(node* t, string re_word) {
if (t == NULL)
return NULL;
else if (re_word < t->word)
return find(t->left, re_word);
else if (re_word > t->word)
return find(t->right, re_word);
else
return t;
}
void inorder(node* t) {
if (t == NULL)
return;
inorder(t->left);
cout << t->word << " " << endl;
for (int i = 0; i < t->filename.size(); i++)
cout << t->filename[i] << " " << endl;
inorder(t->right);
}
public:
BST() {
root = NULL;
}
void insert(string x , string filename) {
root = insert(x, filename, root);
}
void display() {
inorder(root);
cout << endl;
}
vector <string> search(string x) {
root2 = find(root, x);
//root = find(root, x);
vector<string> r = { "NOT-FOUND" };
if(root2!= NULL)
return root2->filename;
else return r;
}
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->textBrowser->setVisible(false);
ui->progressBar->setVisible(false);
ui->progressBar->setMinimum(0);
ui->progressBar->setMaximum(100);
ui->lineEdit_path->setVisible(true);
ui->label_2->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
string keyWord;
BST tree;
float counter = 1;
//C:\Users\Aly EL-kady\Desktop\aly - > 8files
//C:\Users\Aly EL-kady\Desktop\questions - > 100k
//C:\Users\Aly EL-kady\Desktop\dir - > 10k
void MainWindow::on_pushButton_clicked()
{
vector<string> Final;
for (int i = 0 ; i < Final.size(); i++)
Final.erase(Final.begin());
vector <string> words;
vector <string> filenames;
keyWord = ui->lineEdit->text().toStdString();
for (int i = 0; i < keyWord.size(); i++)
{
keyWord[i]=tolower(keyWord[i]);
}
Final = tree.search(keyWord); //returns the vector
for (int i = 0 ; i < Final.size(); i++){
int row = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(row);
ui->tableWidget->setItem(row , 0 , new QTableWidgetItem(QString::fromStdString(Final[i])));
// qApp->processEvents();
}
ui->tableWidget->sortItems(0 , Qt::SortOrder::AscendingOrder);
}
void MainWindow::on_tableWidget_itemDoubleClicked(QTableWidgetItem *item)
{
ui->textBrowser->setVisible(true);
auto textt = ui->tableWidget->item(ui->tableWidget->currentRow(),0);
QString text = textt->text();
int comp;
for (const auto& entry : fs::directory_iterator(path))
{
string temp_path = entry.path().string();
char c = '\\';
int index = temp_path.find_last_of(c);
string filename = temp_path.substr(index + 1);
comp = text.toStdString().compare(filename);
if(comp==0)
{
QFile files(QString::fromStdString(temp_path));
if(!files.open(QIODevice::ReadOnly))
{
QMessageBox::information(0,"Info",files.errorString());
}
QTextStream in (&files);
ui->textBrowser->setText(in.readAll());
break;
}
}
}
void MainWindow::on_lineEdit_editingFinished()
{
ui->tableWidget->setRowCount(0);
ui->tableWidget->clearContents();
ui->textBrowser->hide();
ui->progressBar->setVisible(true);
}
void MainWindow::on_lineEdit_path_editingFinished()
{
ui->progressBar->setVisible(true);
string word;
path = ui->lineEdit_path->text().toStdString();
for (int i = 0 ; i < path.length(); i++){
if (path[i] == '\\' )
{
char c = '//';
path[i]=c;
}
}
float numFiles=0;
for (const auto& entry : fs::directory_iterator(path)) {
numFiles++;
}
for (const auto& entry : fs::directory_iterator(path)) {
file.open(entry.path().string());
string temp_path = entry.path().string();
char c = '\\';
int index = temp_path.find_last_of(c);
string filename = temp_path.substr(index + 1);
while (file >> word) {
for (int i = 0; i < word.size(); i++)
{
word[i]=tolower(word[i]);
}
/////////////tree-building/////////////////
tree.insert(word , filename);
}
float d,h;
d = 100/numFiles;
h = counter*d;
ui->progressBar->setValue(h);
counter++;
qApp->processEvents();
file.close();
}
int x = 0;
//QMessageBox :: information (this , "this" , QString::number(numFiles));
}