-
Notifications
You must be signed in to change notification settings - Fork 2
/
mainwindow.cpp
475 lines (429 loc) · 12.6 KB
/
mainwindow.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QDebug>
#include <QWheelEvent>
#include <QInputDialog>
#include <QScrollBar>
#include <QTextCursor>
#include <QTextBlock>
#include <QStringBuilder>
#include <QDateTime>
#include <QTextStream>
#include <QFile>
MainWindow::MainWindow(QWidget* parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->textEdit->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(scrollTheFile(int)));
connect(ui->textEdit, SIGNAL(goLine(int)), this, SLOT(goLine(int)));
connect(ui->textEdit, SIGNAL(goPage(int)), this, SLOT(goPage(int)));
connect(ui->textEdit, SIGNAL(goToEOF()), this, SLOT(goToEOF()));
connect(ui->textEdit, SIGNAL(cursorPositionChanged()), this, SLOT(cursorPositionChanged()));
currentFileLineNumber = 0;
}
MainWindow::~MainWindow()
{
if (!file)
{
file->close();
delete file;
}
delete ui;
}
void MainWindow::changeEvent(QEvent* e)
{
QMainWindow::changeEvent(e);
switch (e->type())
{
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::on_actionQuit_triggered()
{
QApplication::exit(0);
}
void MainWindow::on_actionAbout_triggered()
{
QMessageBox::about(this, "Big File Editor",
"Opens big files mostly more then GBs <br />for any problem feel free to contact via <a href='http://mascix.com'>my site</a>");
}
void MainWindow::on_actionOpen_triggered()
{
QFontMetrics fm(ui->textEdit->font());
QString tmpFileName = QFileDialog::getOpenFileName(this,
tr("Open File"), "~", tr("Any File (*.*)"));
if (tmpFileName == "~" || tmpFileName == "")
return;
fileName = tmpFileName;
ui->textEdit->clear();
pageSize = this->height() / (fm.height());
file = new QFile(fileName);
qDebug() << "file opened:" << file->open(QIODevice::ReadOnly | QIODevice::Text);
currentFileLineNumber = 0;
QString buffer;
for (int i = 0; i < pageSize; i++)
{
buffer.append(this->readLineFromFile());
buffer.append('\n');
}
ui->textEdit->setWordWrapMode(QTextOption::NoWrap);
ui->textEdit->setPlainText(buffer);
qDebug() << "file:" << fileName;
qDebug() << "lineCount:" << pageSize;
}
void MainWindow::scrollTheFile(int lineIndex)
{
if (!isFileOpened())
return;
if (lineNumberFromWheel > lineIndex)
{
//NOTE:scrolling up
}
else
{
//scrolling down
}
lineNumberFromWheel = lineIndex;
//qDebug()<<"line index:"<<lineIndex;
}
QString MainWindow::readLineFromFile()
{
if (!file->atEnd())
{
QString line = file->readLine();
line.chop(1);
currentFileLineNumber++;
return line;
}
return "";
}
void MainWindow::goLine(int sign)
{
qDebug() << "goLine:" << sign;
if (!isFileOpened())
return;
if (ui->textEdit->textCursor().atEnd())
{
if (sign == 1)
{
//key arrow down
ui->textEdit->moveCursor(QTextCursor::End);
auto chars = this->readLineFromFile();
if (chars.length() > 0)
ui->textEdit->appendPlainText(chars);
}
}
if (sign == -1)
{
//key arrow up
}
}
void MainWindow::goPage(int sign)
{
qDebug() << "goPage:" << sign;
if (!isFileOpened())
return;
if (ui->textEdit->textCursor().atEnd())
if (sign == 1)
{
//page down
for (int i = 0; i < pageSize; i++)
ui->textEdit->appendPlainText(this->readLineFromFile());
}
if (sign == -1)
{
//page up
}
}
void MainWindow::wheelEvent(QWheelEvent* turning)
{
if (!isFileOpened())
return;
if (turning->angleDelta().y() > 0)
{
//mouse wheel up
}
else
{
//mouse wheel down
ui->textEdit->setDisabled(true);
if (ui->textEdit->textCursor().atEnd())
{
for (int i = 0; i < pageSize; i++)
{
QString s = this->readLineFromFile();
if (s.length() > 0)
{
ui->textEdit->appendPlainText(s);
}
}
}
ui->textEdit->setDisabled(false);
qApp->processEvents();
}
//qDebug()<<"pos:"<< file->pos();
}
void MainWindow::on_actionGo_to_line_triggered()
{
if (!isFileOpened())
{
QMessageBox::warning(this, "Open File First", "You need to open a file first.");
return;
}
bool ok;
int gotoLineNumber = QInputDialog::getInt(this, tr("Input Line Number"),
tr("Line Number:"), 25, 0, 100, 1, &ok);
if (ok && gotoLineNumber > ui->textEdit->document()->lineCount())
{
ui->textEdit->setVisible(false);
ui->textEdit->clear();
QString bufferim;
for (int i = ui->textEdit->document()->lineCount(); i < gotoLineNumber; i++)
{
if (!file->atEnd())
{
bufferim.append(file->readLine());
currentFileLineNumber++;
}
if (currentFileLineNumber % 10000 == 0)
{
ui->textEdit->clear();
ui->textEdit->appendPlainText(bufferim);
bufferim.clear();
qApp->processEvents();
}
}
if (bufferim != "")
{
ui->textEdit->appendPlainText(bufferim);
qApp->processEvents();
}
ui->textEdit->setVisible(true);
}
if (ok && gotoLineNumber < ui->textEdit->document()->lineCount())
{
int pos = ui->textEdit->document()->findBlockByLineNumber(gotoLineNumber - 1).position();
QTextCursor cursor = ui->textEdit->textCursor();
cursor.setPosition(pos);
ui->textEdit->setTextCursor(cursor);
qDebug() << "gone:" << gotoLineNumber;
}
}
long MainWindow::findTotalFileLineNumbers()
{
qint64 pozisyon = file->pos();
file->seek(0);
long sayac = 0;
while (!file->atEnd())
{
file->readLine();
sayac++;
if (sayac % 10000)
{
qApp->processEvents();
}
}
file->seek(pozisyon);
return sayac;
}
void MainWindow::goToEOF()
{
if (!isFileOpened())
return;
ui->textEdit->setVisible(false);
long i = 0;
QString bufferim;
while (!file->atEnd())
{
bufferim.append(file->readLine());
i++;
if (i % 10000 == 0)
{
ui->textEdit->clear();
ui->textEdit->appendPlainText(bufferim);
bufferim.clear();
qApp->processEvents();
qDebug() << "date:" << QTime::currentTime() << "file line:" << i;
currentFileLineNumber = i;
}
}
currentFileLineNumber = i;
if (!bufferim.isEmpty())
ui->textEdit->appendPlainText(bufferim);
qDebug() << "file line:" << i;
ui->textEdit->setVisible(true);
}
void MainWindow::cursorPositionChanged()
{
long currentLineInTheEditor = ui->textEdit->textCursor().blockNumber();
ui->statusBar->showMessage("current line in editor:" + QString::number(currentLineInTheEditor) + " in file:" +
QString::number(currentFileLineNumber));
}
void MainWindow::on_actionSave_triggered()
{
if (!isFileOpened())
{
QMessageBox::warning(this, "Open File First", "You need to open a file first.");
return;
}
ui->textEdit->setVisible(false);
QString saveFileName = QFileDialog::getSaveFileName(this, tr("Save File"), fileName + ".out", tr("Any File (*.*)"));
if (saveFileName.isEmpty())
return;
qDebug() << "saveFileName:" << saveFileName << "currentFileLineNumber:" << currentFileLineNumber
<< "ui->textEdit->textCursor().blockNumber()" << ui->textEdit->textCursor().blockNumber();
QString plainTextEditContents = ui->textEdit->toPlainText();
QStringList lines = plainTextEditContents.split("\n");
qDebug() << "lines:" << lines.size();
qint64 pozisyon = file->pos();
if (currentFileLineNumber != lines.size())
{
//NOTE:user is at the end of file and we need to do some preprocess
//for(QTextBlock block = document()->begin(); block.isValid(); block = block.next()) {}
QFile fileSave(saveFileName);
if (!fileSave.open(QIODevice::WriteOnly))
{
QMessageBox::information(this, tr("Unable to open file"), fileSave.errorString());
return;
}
qDebug() << "file opened.";
QTextStream out(&fileSave);
file->seek(0);
long totalLineCount = this->findTotalFileLineNumbers();
ui->statusBar->showMessage("please wait until save done... total lines:" + QString::number(totalLineCount));
int sayac = 0;
while (sayac < currentFileLineNumber - lines.size())
{
out << file->readLine();
sayac++;
if (sayac % 1000)
{
qApp->processEvents();
}
}
foreach(QString line, lines)
{
out << line << '\n';
file->readLine();
sayac++;
if (sayac % 1000)
{
qApp->processEvents();
}
}
while (!file->atEnd())
{
out << file->readLine();
sayac++;
if (sayac % 1000)
{
qApp->processEvents();
}
}
qDebug() << "file closed.";
fileSave.close();
}
else
{
//NOTE:editor and file is sync so we just need to save all editor to the file and add if there is anymore line
//from opened file.
QFile fileSave(saveFileName);
if (!fileSave.open(QIODevice::WriteOnly))
{
QMessageBox::information(this, tr("Unable to open file"), fileSave.errorString());
return;
}
qDebug() << "file opened.";
QTextStream out(&fileSave);
ui->statusBar->showMessage("please wait until save done...");
file->seek(0);
int sayac = 0;
foreach(QString line, lines)
{
out << line << '\n';
file->readLine();
sayac++;
if (sayac % 1000)
{
qApp->processEvents();
}
}
while (!file->atEnd())
{
out << file->readLine();
sayac++;
if (sayac % 1000)
{
qApp->processEvents();
}
}
qDebug() << "file closed.";
fileSave.close();
}
file->seek(pozisyon);
ui->textEdit->setVisible(true);
}
void MainWindow::on_actionSearch_triggered()
{
if (!isFileOpened())
return;
bool ok;
QString searchWord = QInputDialog::getText(this, tr("Input Search Phrase"),
tr("Word:"), QLineEdit::Normal,
"hele", &ok);
if (ok && !searchWord.isEmpty())
{
ui->textEdit->setVisible(false);
file->seek(0);
long i = 0;
QString bufferim;
while (!file->atEnd())
{
bufferim.append(file->readLine());
i++;
if (i % 10000 == 0)
{
ui->textEdit->clear();
ui->textEdit->appendPlainText(bufferim);
bufferim.clear();
qApp->processEvents();
qDebug() << "date:" << QTime::currentTime() << "file line:" << i;
currentFileLineNumber = i;
if (!ui->textEdit->document()->find(searchWord, Qt::CaseInsensitive).isNull())
{
qDebug() << "found the word";
break;
}
}
}
currentFileLineNumber = i;
if (!bufferim.isEmpty())
{
ui->textEdit->appendPlainText(bufferim);
if (!ui->textEdit->document()->find(searchWord, Qt::CaseInsensitive).isNull())
{
qDebug() << "found the word";
}
}
ui->textEdit->setVisible(true);
if (!ui->textEdit->document()->find(searchWord, Qt::CaseInsensitive).isNull())
{
QTextCursor cursor = ui->textEdit->document()->find(searchWord, Qt::CaseInsensitive);
cursor.beginEditBlock();
cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
cursor.endEditBlock();
ui->textEdit->setTextCursor(cursor);
}
}
}
void MainWindow::on_actionGo_to_end_of_file_triggered()
{
goToEOF();
}