-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
281 lines (210 loc) · 8.94 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setupHistoryTableView();
setupCurrentFilesTableWidget();
setAcceptDrops(true);
connect(this, SIGNAL(databaseChanged()), this, SLOT(refreshHistoryTableView()));
// ui->statusBar->showMessage("This is the status bar");
}
void MainWindow::setupHistoryTableView()
{
//adding layout to historyDockWidget to enable it resizes. Adding this in Qt Designer won't make resizing happen!
QGridLayout*vertLayout = new QGridLayout(this);
vertLayout->addWidget(ui->historyTableView);
ui->historyDockWidgetContents->setLayout(vertLayout);
historyTableViewModel= new QSqlQueryModel(this);
historyTableViewModel->setQuery("SELECT id, fileName, dateTime, compressionRatio FROM History");
historyTableViewModel->setHeaderData(0, Qt::Horizontal, QString("Id"));
historyTableViewModel->setHeaderData(1, Qt::Horizontal, QString("File"));
historyTableViewModel->setHeaderData(2, Qt::Horizontal, QString("Date/Time"));
historyTableViewModel->setHeaderData(3, Qt::Horizontal, QString("Compression Ratio"));
historyTableViewProxyModel=new QSortFilterProxyModel(this);
// historyTableViewProxyModel->setDynamicSortFilter(true);
historyTableViewProxyModel->setSourceModel(historyTableViewModel);
ui->historyTableView->setModel(historyTableViewProxyModel);
ui->historyTableView->setSortingEnabled(true);
ui->historyTableView->setColumnHidden(0, true); //hides the id column
ui->historyTableView->verticalHeader()->setHidden(true);
}
void MainWindow::setupCurrentFilesTableWidget()
{
ui->currentFilesTableWidget->setColumnCount(5);
ui->currentFilesTableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("File"));
ui->currentFilesTableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("Size (bytes)"));
ui->currentFilesTableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("Progress"));
ui->currentFilesTableWidget->setHorizontalHeaderItem(3, new QTableWidgetItem("Compressed Size"));
ui->currentFilesTableWidget->setHorizontalHeaderItem(4, new QTableWidgetItem("Ratio"));
}
MainWindow::~MainWindow()
{
delete ui;
}
//opens a file browser for user to choose which file to compress, and add the chosen file names and size to
//tableview containing all to-be-compressed files
void MainWindow::on_action_Load_triggered()
{
QList<QUrl> selectedFileUrls = fileDialog.getOpenFileUrls(this, "Load file");
extractFileInsights(selectedFileUrls);}
//these two methods "on_action_History_toggled" and "on_historyDockWidget_visibilityChanged" are like observables
//there might be an existing function that handles visibility changes
//toggles whether to show or hide History dock widget
void MainWindow::on_action_History_toggled(bool arg1)
{
if (arg1) { ui->historyDockWidget->show(); }
else { ui->historyDockWidget->hide(); }
qDebug() << QString(arg1);
}
void MainWindow::on_historyDockWidget_visibilityChanged(bool visible)
{
ui->action_History->setChecked(visible);
}
void MainWindow::on_action_Stats_toggled(bool arg1)
{
if (arg1) { ui->statsDockWidget->show(); }
else { ui->statsDockWidget->hide(); }
}
void MainWindow::on_statsDockWidget_visibilityChanged(bool visible)
{
ui->action_Stats->setChecked(visible);
}
void MainWindow::on_historyTableView_clicked(const QModelIndex &index)
{
if (index.isValid())
{
int rowIndex = index.row();
ui->historyTableView->selectRow(rowIndex);
int id = index.sibling(rowIndex, 0).data().toInt();
populateStatsDockWidget(id);
qDebug() << id;
QString cellText = index.data().toString();
qDebug() << cellText;
}
else { qCritical() << "The index is invalid"; }
}
//change Stats dock's labels to show the stats associated with this compressed file
void MainWindow::populateStatsDockWidget(const int& id) const
{
HistoryItem historyItem = DbManager::getDbManager().getHistoryItem(id);
ui->fileName_lbl->setText("<b>File Name: </b>" + historyItem.fileName);
ui->date_time_lbl->setText("<b>Datetime: </b>" + historyItem.dateTime);
ui->originalSize_lbl->setText("<b>Original Size (b): </b>" + QString::number(historyItem.originalSize));
ui->compressedSize_lbl->setText("<b>Compressed Size (b): </b>" + QString::number(historyItem.compressedSize));
ui->executionTime_lbl->setText("<b>Execution Time (ms): </b>" + QString::number(historyItem.executionTime));
ui->compressionRatio_lbl->setText("<b>Compression Ratio: </b>" + QString::number(historyItem.compressionRatio) + "%");
ui->notes_lbl->setText(historyItem.notes);
}
void MainWindow::displayFileData(const QString& fileName, const double& fileSize)
{
ui->currentFilesTableWidget->insertRow(ui->currentFilesTableWidget->rowCount());
int lastRowIndex = ui->currentFilesTableWidget->rowCount() - 1;
ui->currentFilesTableWidget->setItem(lastRowIndex, 0, new QTableWidgetItem(fileName));
ui->currentFilesTableWidget->setItem(lastRowIndex, 1, new QTableWidgetItem(QString::number(fileSize)));
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
event->accept();
}
//user can drag-drop file to add file to the "to be compressed" queue
void MainWindow::dropEvent(QDropEvent *event)
{
const QMimeData* mimeData = event->mimeData();
// check for our needed mime type, here a file or a list of files
if (mimeData->hasUrls())
{
QList<QUrl> urls = mimeData->urls();
extractFileInsights(urls);
}
}
void MainWindow::extractFileInsights(const QList<QUrl> &urls)
{
foreach (QUrl url, urls)
{
QString filePath = getFilePath(url);
QString fileName = getFileName(filePath);
double fileSize = getFileSize(filePath);
filePaths.append(filePath);
displayFileData(fileName, fileSize);
}
}
QString MainWindow::getFileName(const QString& filePath)
{
QFile file(filePath);
Q_ASSERT( file.exists()); //since the user selected the file, it must exist!
QFileInfo fileInfo(file.fileName()); //us
QString fileName = fileInfo.fileName();
qDebug() << fileName;
return fileName;
}
double MainWindow::getFileSize(const QString &filePath)
{
QFile file(filePath);
qDebug() << file.size();
return file.size();
}
QString MainWindow::getFilePath(const QUrl& url)
{
return url.toString().right(url.toString().count() - 8);
}
void MainWindow::on_currentFilesTableWidget_clicked(const QModelIndex &index)
{
if (index.isValid())
{
int rowIndex = index.row();
ui->currentFilesTableWidget->selectRow(rowIndex);
}
}
//asks if user is sure to delete history and then clears the model.
//Might consider using Observer pattern later when compression algos implemented and appending new entries
void MainWindow::on_actionClear_History_triggered()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this,
"Clear History", "Are you sure you want to clear history of previously compressed files?",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes)
{
DbManager::getDbManager().clearHistoryTable();
emit databaseChanged();
}
}
//intended to trigger compressing the files. For now, just insert a record into database
void MainWindow::on_actionCompressIt_triggered()
{
//always Huffman encode for now
while(!filePaths.isEmpty())
{
QString filePath = filePaths.dequeue();
QElapsedTimer timer;
timer.start();
compressionController.compress(filePath, AlgorithmEnum::Huffman);
int compressionTime = timer.elapsed(); //compression time is in ms
QString filePathBin = filePath; //gest the binary file url
filePathBin.chop(3);
filePathBin += "bin";
QString fileName = getFileName(filePath);
double originalSize = getFileSize(filePath);
double compressedSize = getFileSize(filePathBin);
double compressionRatio = (compressedSize / originalSize) * 100; //as percentage
QString dateTime = QDateTime::currentDateTime().toString();
DbManager::getDbManager().addHistoryItem(fileName, dateTime, compressionRatio, originalSize, compressedSize, compressionTime, "");
emit databaseChanged();
}
Q_ASSERT(filePaths.isEmpty()); //the queue should be empty after compressing
ui->currentFilesTableWidget->clearContents();
}
void MainWindow::refreshHistoryTableView()
{
qDebug() << "refreshHistoryTableView called";
historyTableViewModel->setQuery("SELECT id, fileName, dateTime, compressionRatio FROM History");
}
void MainWindow::on_actionDecompress_triggered()
{
QUrl selectedBinaryFileUrl = fileDialog.getOpenFileUrl(this, "Load file", QUrl(), "Binary Files (*.bin)");
QString filePath = getFilePath(selectedBinaryFileUrl);
compressionController.decompress(filePath);
}