This repository has been archived by the owner on Jun 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
126 lines (104 loc) · 4.06 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLayout>
#include <QMessageBox>
#include <QSqlQuery>
#include "TextCompare/textcompare.h"
#include "DatabaseSettings/DatabaseConnection.h"
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
// QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins
DbConnection.Database = QSqlDatabase::addDatabase("QPSQL");
ui->setupUi(this);
dialog = new DatabaseSettingsDialog();
statsWindow = new Stats();
connect(ui->compareButton, &QPushButton::pressed, this, &MainWindow::Compare);
connect(ui->openDatabaseSettings, &QAction::triggered, this, &MainWindow::OpenDBSettingsWindow);
connect(ui->openStats, &QAction::triggered, this, &MainWindow::OpenStatsWindow);
connect(ui->saveButton, &QPushButton::pressed, this, &MainWindow::SaveComparison);
connect(ui->firstOpenFile, &QPushButton::pressed, this, &MainWindow::OpenFile1);
connect(ui->secondOpenFile, &QPushButton::pressed, this, &MainWindow::OpenFile2);
}
void MainWindow::Compare()
{
ui->firstView->ResetCharFormat();
QStringList firstVeiwLines = ui->firstView->toPlainText().split("\n", QString::SplitBehavior::KeepEmptyParts);
QStringList secondViewLines = ui->secondView->toPlainText().split("\n", QString::SplitBehavior::KeepEmptyParts);
for (int i = 0; i < firstVeiwLines.count(); i++)
firstVeiwLines[i] = TextCompare::RightTrimm(firstVeiwLines[i]);
for (int i = 0; i < secondViewLines.count(); i++)
secondViewLines[i] = TextCompare::RightTrimm(secondViewLines[i]);
diffs = TextCompare::FindDifferentLines(firstVeiwLines, secondViewLines);
QColor lineColor = QColor(Qt::blue).lighter(175);
QTextCharFormat format;
format.setProperty(QTextFormat::FullWidthSelection, true);
format.setBackground(lineColor);
for (int line : diffs.first)
ui->firstView->HighlightLine(line, format);
for (int line : diffs.second)
ui->secondView->HighlightLine(line, format);
}
bool MainWindow::SaveComparison()
{
QMessageBox msgBox;
if (!DbConnection.Database.isOpen())
{
msgBox.setText("Нет подключения к базе данных");
msgBox.exec();
return false;
}
if (ui->firstFilename->text() == "" || ui->secondFilename->text() == "")
{
msgBox.setText("Укажите имена файлов.");
msgBox.exec();
return false;
}
QSqlQuery sqlQuerry;
sqlQuerry.prepare(
"INSERT INTO diff_table (\"firstFilename\", \"firstFileChangesCount\", \"secondFilename\", \"secondFileChangesCount\", \"date\", \"firstFileContent\", \"secondFileContent\")\n"
"VALUES (?, ?, ?, ?, CURRENT_DATE, ?, ?);"
);
Compare();
sqlQuerry.addBindValue(ui->firstFilename->text());
sqlQuerry.addBindValue(diffs.first.count());
sqlQuerry.addBindValue(ui->secondFilename->text());
sqlQuerry.addBindValue(diffs.second.count());
sqlQuerry.addBindValue(ui->firstView->toPlainText());
sqlQuerry.addBindValue(ui->secondView->toPlainText());
sqlQuerry.exec();
return true;
}
QString MainWindow::readFile(const QString &filePath)
{
QFile file(filePath);
file.open(QIODevice::ReadOnly);
QTextStream fileTextStream(&file);
return fileTextStream.readAll();
}
void MainWindow::OpenFile1()
{
ui->firstFilename->setText(QFileDialog::getOpenFileName(this));
ui->firstView->setPlainText(readFile(ui->firstFilename->text()));
}
void MainWindow::OpenFile2()
{
ui->secondFilename->setText(QFileDialog::getOpenFileName(this));
ui->secondView->setPlainText(readFile(ui->secondFilename->text()));
}
void MainWindow::OpenDBSettingsWindow()
{
dialog->show();
}
void MainWindow::OpenStatsWindow()
{
statsWindow->show();
}
MainWindow::~MainWindow()
{
delete statsWindow;
delete dialog;
delete ui; // Все дочерние виджеты будут удалены автоматически
}