summaryrefslogtreecommitdiffstats
path: root/archiveitemeditdialog.cpp
blob: 90aa1dfed74e5a6715a488ea80b1e41c7617bb3f (plain)
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
/*
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version
  2 of the License, or (at your option) any later version.
*/

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QTabWidget>

#include <algorithm>

#include "archiveitemeditdialog.h"
#include "archiveiteminfoedit.h"
#include "archiveitemcoveredit.h"
#include "moviemodelsingleton.h"
#include "moviemodel.h"
#include "movieitem.h"
#include "fileinfomodel.h"
#include "listmodelsingleton.h"
#include "listmodel.h"
#include "coveritem.h"
#include "helper.h"

ArchiveItemEditDialog::ArchiveItemEditDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f), mMovieId(-1){
	//Init dialog
	QVBoxLayout *mainLayout = new QVBoxLayout;

	//Tab widget
	mTab = new QTabWidget;
	mInfoEdit = new ArchiveItemInfoEdit;
	mTab->addTab(mInfoEdit, tr("General"));
	mCoverEdit = new ArchiveItemCoverEdit;
	mTab->addTab(mCoverEdit, tr("Covers"));
	mainLayout->addWidget(mTab);

	//Button layout
	QHBoxLayout *buttonLayout = new QHBoxLayout;
	mCancel = new QPushButton(tr("Cancel"));
	connect(mCancel, SIGNAL(clicked()), this, SLOT(reject()));
	mUpdate = new QPushButton(tr("Update"));
	connect(mUpdate, SIGNAL(clicked()), this, SLOT(accept()));
	buttonLayout->addStretch();
	buttonLayout->addWidget(mCancel);
	buttonLayout->addWidget(mUpdate);
	mainLayout->addLayout(buttonLayout);

	setLayout(mainLayout);
}

void ArchiveItemEditDialog::setMovie(const QModelIndex &idx){
	if(!idx.isValid()){
		return;
	}

	//Numbers and title setup
	mInfoEdit->setTitle(idx.data(MovieModel::TitleBaseRole).toString());
	mInfoEdit->setDvd(idx.data(MovieModel::DvdRole).toInt());
	mInfoEdit->setSeries(idx.data(MovieModel::SeriesNoRole).toInt());
	mInfoEdit->setPart(idx.data(MovieModel::PartNoRole).toInt());
	mInfoEdit->setGenre(idx.data(MovieModel::GenreRole).toString());
	mInfoEdit->setQuality(idx.data(MovieModel::QualityRole).toInt());

	//File and actor info
	mInfoEdit->setup(idx);

	//Set covers
	mCoverEdit->setCovers(idx.data(MovieModel::CoverRole).toList());

	//Store ID
	mMovieId = idx.data(MovieModel::IdRole).toInt();

	//Window Title
	QString title = QString(tr("Edit %1")).arg(idx.data().toString());
	setWindowTitle(title);
}

void ArchiveItemEditDialog::accept(){
	//Get Models
	ListModel *actorModel = ListModelSingleton::instance()->model("actor");
	ListModel *genreModel = ListModelSingleton::instance()->model("genre");
	MovieModel *movieModel = MovieModelSingleton::instance();

	//Update actors
	QStringList actors = mInfoEdit->actors();
	QList<QVariant> actorIds;
	foreach(QString a, actors){
		QModelIndex idx = actorModel->index(a);
		if(idx.isValid()){
			actorIds << idx.data(ListModel::IdRole);
		}
	}
	movieModel->setActors(mMovieId, actorIds);

	//Update genre
	QString genre = mInfoEdit->genre();
	QVariant genreId = genreModel->index(genre).data(ListModel::IdRole);
	QModelIndex movieModelIndex = movieModel->index(mMovieId, MovieItem::Genre);
	movieModel->setDataAt(movieModelIndex, genreId);

	//Update quality
	movieModelIndex = movieModel->index(mMovieId, MovieItem::Quality);
	movieModel->setDataAt(movieModelIndex, mInfoEdit->quality());

	//Update dvdno
	movieModelIndex = movieModel->index(mMovieId, MovieItem::Dvd);
	movieModel->setDataAt(movieModelIndex, mInfoEdit->dvd());

	//Update seriesno
	movieModelIndex = movieModel->index(mMovieId, MovieItem::SeriesNo);
	movieModel->setDataAt(movieModelIndex, mInfoEdit->series());

	//Update partno
	movieModelIndex = movieModel->index(mMovieId, MovieItem::PartNo);
	movieModel->setDataAt(movieModelIndex, mInfoEdit->part());

	//Update title
	movieModelIndex = movieModel->index(mMovieId, MovieItem::Title);
	if(!mInfoEdit->title().isEmpty()){
		movieModel->setDataAt(movieModelIndex, mInfoEdit->title());
	}

	//Update covers
	updateCovers();

	QDialog::accept();
}

void ArchiveItemEditDialog::updateCovers(){
	//Get model and covers
	MovieModel *movieModel = MovieModelSingleton::instance();
	QModelIndex movieIdx = movieModel->index(mMovieId);
	QList<QVariant> currentCovers = movieIdx.data(MovieModel::CoverRole).toList();
	QList<QVariant> newCovers = mCoverEdit->covers();

	//Front
	CoverItem curFront = cover(currentCovers, "front");
	CoverItem newFront = cover(newCovers, "front");
	updateCover(curFront, newFront);

	//Back
	CoverItem curBack = cover(currentCovers, "back");
	CoverItem newBack = cover(newCovers, "back");
	updateCover(curBack, newBack);

	//General
	CoverItem curGeneral = cover(currentCovers, "general");
	CoverItem newGeneral = cover(newCovers, "general");
	updateCover(curGeneral, newGeneral);

	//Update database
	QList<CoverItem> coverList;
	foreach(QVariant c, newCovers){
		coverList << c.value<CoverItem>();
	}
	movieModel->setCovers(mMovieId, coverList);
}

void ArchiveItemEditDialog::updateCover(const CoverItem &oldCover, const CoverItem &newCover){
	if(oldCover == newCover){
		return;
	}
	if(oldCover != CoverItem()){
		Helper::removeFromArchive(oldCover.fileName(), oldCover.md5());
	}
	if(newCover != CoverItem()){
		Helper::moveToArchive(newCover.fullPath(), newCover.md5());
	}
}

CoverItem ArchiveItemEditDialog::cover(const QList<QVariant> &coverList, const QString &type) const{
	QList<QVariant>::const_iterator it = std::find_if(coverList.constBegin(), coverList.constEnd(), std::bind2nd(CoverItem::findType(), type));
	CoverItem retval = CoverItem();
	if(it != coverList.constEnd()){
		retval = it->value<CoverItem>();
	}
	return retval;
}