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
|
/*
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 <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QComboBox>
#include <QLabel>
#include <QModelIndex>
#include <QApplication>
#include <QFileDialog>
#include <QFileInfo>
#include <QSettings>
#include <algorithm>
#include "coverarchiveeditor.h"
#include "moviemodel.h"
#include "coveritem.h"
#include "helper.h"
CoverArchiveEditor::CoverArchiveEditor(MovieModel *model, QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f), mModel(model){
QGridLayout *grid = new QGridLayout;
//front cover
QLabel *l1 = new QLabel(tr("Set &front cover"));
mFront = new QComboBox;
l1->setBuddy(mFront);
grid->addWidget(l1, 0, 0);
grid->addWidget(mFront, 0, 1);
//back cover
QLabel *l2 = new QLabel(tr("Set &back cover"));
mBack = new QComboBox;
l2->setBuddy(mBack);
grid->addWidget(l2, 1, 0);
grid->addWidget(mBack, 1, 1);
//general cover
QLabel *l3 = new QLabel(tr("Set &general cover"));
mCovers = new QComboBox;
l3->setBuddy(mCovers);
grid->addWidget(l3, 2, 0);
grid->addWidget(mCovers, 2, 1);
//buttons
QHBoxLayout *buttonLayout = new QHBoxLayout;
mAddCover = new QPushButton(tr("Add cover..."));
connect(mAddCover, SIGNAL(clicked()), this, SLOT(addCover()));
mUpdate = new QPushButton(tr("Update"));
connect(mUpdate, SIGNAL(clicked()), this, SLOT(accept()));
mClose = new QPushButton(tr("Close"));
connect(mClose, SIGNAL(clicked()), this, SLOT(reject()));
buttonLayout->addStretch();
buttonLayout->addWidget(mAddCover);
buttonLayout->addWidget(mUpdate);
buttonLayout->addWidget(mClose);
//main layout
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(grid);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);
}
void CoverArchiveEditor::setMovie(const QModelIndex &movie){
mCoverList = mModel->data(movie, MovieModel::CoverRole).toList();
mMovId = mModel->data(movie, MovieModel::IdRole).toInt();
QString title = QString(tr("%1 - Cover edit for %2")).arg(qApp->applicationName()).arg(movie.data().toString());
setWindowTitle(title);
QStringList files("-");
foreach(QVariant c, mCoverList){
CoverItem item = c.value<CoverItem>();
files << item.fileName();
}
mFront->addItems(files);
QList<QVariant>::const_iterator it;
int idx(-1);
it = std::find_if(mCoverList.constBegin(), mCoverList.constEnd(), std::bind2nd(CoverItem::findType(), "front"));
QString fn;
if(it != mCoverList.constEnd()){
fn = it->value<CoverItem>().fileName();
idx = mFront->findText(fn);
if(idx != -1){
mFront->setCurrentIndex(idx);
}
}
mBack->addItems(files);
it = std::find_if(mCoverList.constBegin(), mCoverList.constEnd(), std::bind2nd(CoverItem::findType(), "back"));
if(it != mCoverList.constEnd()){
fn = it->value<CoverItem>().fileName();
idx = mBack->findText(fn);
if(idx != -1){
mBack->setCurrentIndex(idx);
}
}
mCovers->addItems(files);
it = std::find_if(mCoverList.constBegin(), mCoverList.constEnd(), std::bind2nd(CoverItem::findType(), "general"));
if(it != mCoverList.constEnd()){
fn = it->value<CoverItem>().fileName();
idx = mCovers->findText(fn);
if(idx != -1){
mCovers->setCurrentIndex(idx);
}
}
}
void CoverArchiveEditor::accept(){
QList<CoverItem> items;
QString fn;
CoverItem item;
fn = mFront->currentText();
item = realItem(fn, "front");
if(item != CoverItem()){
items << item;
}
fn = mBack->currentText();
item = realItem(fn, "back");
if(item != CoverItem()){
items << item;
}
fn = mCovers->currentText();
item = realItem(fn, "general");
if(item != CoverItem()){
items << item;
}
mModel->setCovers(mMovId, items);
}
void CoverArchiveEditor::addCover(){
QSettings s;
QString startDir = s.value("ui/selectstartup").toString();
QStringList files = QFileDialog::getOpenFileNames(this, "Select covers to add...", startDir);
QStringList addItems;
foreach(QString f, files){
mAddedCovers << f;
QFileInfo info(f);
addItems << info.fileName();
}
mFront->addItems(addItems);
mBack->addItems(addItems);
mCovers->addItems(addItems);
}
CoverItem CoverArchiveEditor::coverItem(const QString &path, const QString &type) const{
QString md5 = Helper::md5Sum(path);
QString newPath = Helper::moveToArchive(path, md5);
return CoverItem(newPath, type, md5);
}
CoverItem CoverArchiveEditor::realItem(const QString &filename, const QString &type) const{
QList<QVariant>::const_iterator initCit = std::find_if(mCoverList.constBegin(), mCoverList.constEnd(), std::bind2nd(CoverItem::findFilename(), filename));
//got it, have to convert it to the right type
if(initCit != mCoverList.constEnd()){
CoverItem i = initCit->value<CoverItem>();
i.setType(type);
return i;
//an added item
}else{
QList<QString>::const_iterator ait = std::find_if(mAddedCovers.constBegin(), mAddedCovers.constEnd(), std::bind2nd(Helper::StringListContains(), filename));
//maybe an unset item
if(ait == mAddedCovers.constEnd()){
QList<QVariant>::const_iterator oit = std::find_if(mCoverList.constBegin(), mCoverList.constEnd(), std::bind2nd(CoverItem::findType(), type));
if(oit != mCoverList.constEnd()){
CoverItem oldItem = oit->value<CoverItem>();
//remove it from fs
Helper::removeFromArchive(oldItem.fileName(), oldItem.md5());
}
return CoverItem();
}
CoverItem i = coverItem((*ait), type);
return i;
}
return CoverItem();
}
|