-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
321 lines (225 loc) · 8.32 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
\
#include "simpledialog.h"
#include "diskwriter.h"
#include "movieform.h"
#include "newmoviedialog.h"
#include "moviedownloader.h"
#include "movie.h"
#include "messagebox.h"
#include <QtXml>
#include <QMessageBox>
#include <QFileDialog>
#include <QProgressBar>
#define FILEOPEN_XML_TYPE "XML files (*.xml)"
#define NEW_COLLECTION "New Collection.xml"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
try
{
collectionFile = "example.xml";
loadMovieList(collectionFile);
loadMovieListWidget(movieList);
ui->movieListWidget->setCurrentItem(ui->movieListWidget->item(0));
}
catch(char *error)
{
QMessageBox::information(this,"Error",error);
}
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::on_actionNew_Window_triggered()
{
NewMovieDialog dialog(this);
if(dialog.exec()) {
movie mov = dialog.getMovie();
movieList.push_back(mov);
addMovieToListWidget(mov);
}
}
void MainWindow::loadMovieList(QString filename)
{
movieList.clear();
QDomDocument document;
QFile file(filename);
//qDebug() << file.fileName();
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
MessageBox::ErrorMessageBox("Error","Could not load \"" + filename + "\"");
return ;
}
else {
if(!document.setContent(&file)) {
MessageBox::ErrorMessageBox("Error","Could not parse \"" + filename + "\" as XML");
return ;
}
file.close();
}
QDomElement root = document.firstChildElement();
QDomNodeList movies = root.elementsByTagName("movie");
for(int i=0;i<movies.count();i++) {
QDomNode movienode = movies.at(i);
if(movienode.isElement()) {
movie mov(movienode.toElement());
movieList.push_back(mov);
}
}
}
void MainWindow::loadMovieListWidget(const QList<movie>& movieList)
{
ui->movieListWidget->clear();
ui->movieListWidget->setIconSize(QSize(50,40));
for(int i=0;i<movieList.count();i++) {
addMovieToListWidget(movieList[i]);
}
}
void MainWindow::addMovieToListWidget(const movie& mov) {
QListWidgetItem *item = new QListWidgetItem(mov.getName());
item->setSizeHint(QSize(200,64));
QIcon icon(QPixmap(mov.getPosterPath()));
item->setIcon(icon);
ui->movieListWidget->addItem(item);
}
int MainWindow::getIndexByName(const QString& movieName) {
for(int i=0;i<movieList.size();i++)
if(movieList[i].getName() == movieName)
return i;
throw "Don't find any movie with name " + movieName + " !";
}
movie MainWindow::getMovieByName(const QString& movieName) {
foreach(movie mov,movieList)
if(mov.getName() == movieName)
return mov;
throw "Don't find any movie with name " + movieName + " !";
}
void MainWindow::on_movieListWidget_itemDoubleClicked(QListWidgetItem *item) {
movieform *movForm = new movieform(this);
movForm->setMovie(getMovieByName(item->text()));
movForm->show();
}
void MainWindow::on_actionToolbar_changed() {
if(ui->actionToolbar->isChecked())
ui->mainToolBar->setVisible(true);
else
ui->mainToolBar->setVisible(false);
}
void MainWindow::on_actionThumbnails_changed() {
if(ui->actionThumbnails->isChecked()) {
ui->movieListWidget->setIconSize(QSize(50,40));
}
else {
ui->movieListWidget->setIconSize(QSize(0,0));
}
}
void MainWindow::on_actionExit_triggered() {
this->close();
}
void MainWindow::on_actionSave_Collection_triggered() {
diskwriter::XMLCollectionWriter(collectionFile,movieList);
}
void MainWindow::on_actionDelete_Movie_triggered() {
for(int i=0;i<ui->movieListWidget->count();i++)
if(ui->movieListWidget->item(i)->isSelected()) {
for(int j=0;j<movieList.count();j++)
if(ui->movieListWidget->item(i)->text() == movieList[j].getName()) {
movieList.removeAt(j);
break ;
}
ui->movieListWidget->takeItem(i);
}
}
void MainWindow::on_actionOpen_Collection_triggered() {
QFileDialog dialog;
dialog.setFileMode(QFileDialog::ExistingFile);
dialog.setNameFilter("XML files (*.xml)");
if(dialog.exec()) {
collectionFile = dialog.selectedFiles()[0];
loadMovieList(collectionFile);
loadMovieListWidget(movieList);
}
}
void MainWindow::on_actionNew_Collection_triggered() {
QString fileName = QFileDialog::getSaveFileName(this,"New collection",NEW_COLLECTION,FILEOPEN_XML_TYPE);
if(fileName != NULL) {
movieList.clear();
loadMovieListWidget(movieList);
diskwriter::XMLCollectionWriter(fileName,movieList);
collectionFile = fileName;
}
}
void MainWindow::on_actionSave_As_triggered() {
QString fileName = QFileDialog::getSaveFileName(this,"Save file as",NEW_COLLECTION,FILEOPEN_XML_TYPE);
if(fileName != NULL) {
collectionFile = fileName;
diskwriter::XMLCollectionWriter(collectionFile,movieList);
}
}
void MainWindow::on_actionUpdate_Movie_triggered() {
//QProgressBar *progressBar = new QProgressBar(ui->statusBar);
//progressBar->setMaximumSize(170,19);
//ui->statusBar->addWidget(progressBar);
QString selectedMovieName = ui->movieListWidget->selectedItems().at(0)->text();
try {
int selectedIndex = getIndexByName(selectedMovieName);
if(getMovieByName(selectedMovieName).getrtid().size() == 0) {
MessageBox::ErrorMessageBox("Error", "Could not update " + selectedMovieName + " without a specific ID");
return ;
}
movieDownloader *movDownloader = new movieDownloader(movieList.at(selectedIndex).getrtid());
QThread *thread = new QThread();
movDownloader->moveToThread(thread);
QObject::connect(movDownloader, SIGNAL(finished(movie*)), this, SLOT(updateMovie(movie*)));
QObject::connect(thread, SIGNAL(started()), movDownloader, SLOT(run()), Qt::DirectConnection);
QObject::connect(movDownloader, SIGNAL(finished()),thread, SLOT(quit()), Qt::DirectConnection);
QObject::connect(movDownloader, SIGNAL(progress(QString)), this, SLOT(progressUpdate(QString)));
thread->start();
//ui->statusBar->showMessage("Downloading...");
}
catch(...) {
}
}
void MainWindow::updateMovie(movie* mov) {
ui->statusBar->clearMessage();
ui->statusBar->showMessage("Finished update: " + mov->getOriginalName());
QIcon icon(QPixmap(mov->getPosterPath()));
for(int i=0;i<movieList.size();i++)
if(movieList.at(i).getrtid() == mov->getrtid()) {
movieList[i].updateDBInformations(mov);
ui->movieListWidget->item(i)->setIcon(icon);
}
}
void MainWindow::progressUpdate(QString progressInformations) {
ui->statusBar->clearMessage();
ui->statusBar->showMessage("Progress: " + progressInformations);
}
void MainWindow::on_actionEdit_Movie_triggered() {
QString selectedMovieName = ui->movieListWidget->selectedItems().at(0)->text();
int selectedIndex = getIndexByName(selectedMovieName);
NewMovieDialog dialog(this);
dialog.setMovie(movieList[selectedIndex]);
if(dialog.exec()) {
movie editedMovie = dialog.getMovie();
//Some garbage collection if the movie ID is modifier, in
//conclusion the movie poster is changed too
//This part give some bugs, but for the moment this is the
//best idea for the feature. It will be change in the future
if(movieList[selectedIndex].getrtid() != editedMovie.getrtid()) {
QString lastMoveID = movieList[selectedIndex].getrtid();
if(QFile::exists(DEFAULT_POSTER_FOLDER + lastMoveID + DEFAULT_POSTER_TYPE) == true)
QFile::remove(DEFAULT_POSTER_FOLDER + lastMoveID + DEFAULT_POSTER_TYPE);
}
if(movieList[selectedIndex].getName() != editedMovie.getName()) {
for(int i=0;i<ui->movieListWidget->count();i++)
if(ui->movieListWidget->item(i)->text() == selectedMovieName) {
ui->movieListWidget->item(i)->setText(dialog.getMovie().getName());
break ;
}
}
movieList[selectedIndex] = editedMovie;
}
}