summaryrefslogtreecommitdiffstats
path: root/seriestreewidget.cpp
blob: 7e60c53c63d9860d8452308bad9ba2a48980e45b (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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
  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 <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QSortFilterProxyModel>
#include <QRegExp>
#include <QContextMenuEvent>
#include <QMenu>
#include <QAction>
#include <QMessageBox>
#include <QSettings>
#include <QFileDialog>
#include <QSettings>
#include <QFileInfo>
#include <QComboBox>
#include <QFile>

#include "seriestreewidget.h"
#include "smtreemodel.h"
#include "smglobals.h"
#include "seriestreemodel.h"
#include "filestreemodel.h"
#include "helper.h"

SeriesTreeWidget::SeriesTreeWidget(QWidget *parent) : QWidget(parent){
	//filter bar
	QLabel *l1 = new QLabel(tr("&Filter:"));
	mFilterEdit = new QLineEdit;
	l1->setBuddy(mFilterEdit);
	mFilter = new QPushButton(tr("Filter"));
	QHBoxLayout *filterLayout = new QHBoxLayout;
	connect(mFilter, SIGNAL(clicked()), this, SLOT(filter()));
	connect(mFilterEdit, SIGNAL(returnPressed()), this, SLOT(filter()));
	filterLayout->addWidget(l1);
	filterLayout->addWidget(mFilterEdit);
	filterLayout->addWidget(mFilter);

	//the view
	mView = new SeriesTreeView;
	mProxy = new SeriesTreeSortModel(this);
	mModel = static_cast<SeriesTreeModel*>(SmGlobals::instance()->model("SeriesModel"));
	mProxy->setSourceModel(mModel);
	mView->setModel(mProxy);
	mView->setSortingEnabled(true);
	for(int i = 1; i < 5 ;++i){
		mView->setColumnHidden(i, true);
	}
	mView->resizeColumnToContents(0);
	mView->setSelectionMode(QAbstractItemView::ExtendedSelection);
	connect(mModel, SIGNAL(needResort()), this, SLOT(resort()));
	connect(mView, SIGNAL(expanded(QModelIndex)), this, SLOT(itemExpanded(QModelIndex)));
	connect(mView, SIGNAL(collapsed(QModelIndex)), this, SLOT(itemCollaped(QModelIndex)));

	//cover dialog
	mCoverDialog = new AddCoverDialog(this);

	//layout
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addLayout(filterLayout);
	mainLayout->addWidget(mView);
	setLayout(mainLayout);

}

void SeriesTreeWidget::newSeries(){
	QList<QVariant> data;
	data << tr("<New series>") << QVariant() << QVariant() << QVariant() << SeriesTreeModel::NewSeries;
	if(mModel->addRow(data, QModelIndex())){
		QModelIndex newRow = mModel->index(mModel->rowCount(QModelIndex()) - 1, 0, QModelIndex());
		if(newRow.isValid()){
			QModelIndex proxyIndex = mProxy->mapFromSource(newRow);
			mView->selectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
			mView->edit(proxyIndex);
		}
	}
}

void SeriesTreeWidget::seriesAdded(QString seriesName, int seriesPart, bool resort){
	if(resort){
		mProxy->invalidate();
	}
	QModelIndex destIdx = mModel->findValue(seriesName);
	if(destIdx.isValid()){
		QModelIndex seriesPartIdx = mModel->findValue(seriesPart, destIdx, SeriesTreeModel::SeriesPart);
		if(seriesPartIdx.isValid()){
			destIdx = seriesPartIdx;
		}
	}
	if(destIdx.isValid()){
		QModelIndex proxyIndex = mProxy->mapFromSource(destIdx);
		mView->selectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
	}
}

void SeriesTreeWidget::deleteFromSeries(){
	QModelIndexList selected = mView->selectionModel()->selectedRows();
	if(selected.isEmpty()){
		return;
	}
	QList<QPersistentModelIndex> series;
	QList<QPersistentModelIndex> parts;
	foreach(QModelIndex s, selected){
		QModelIndex real = mProxy->mapToSource(s);
		if(real.data(SeriesTreeModel::TypeRole).toInt() == SeriesTreeModel::Series || real.data(SeriesTreeModel::TypeRole).toInt() == SeriesTreeModel::NewSeries){
			series << QPersistentModelIndex(real);
		}
		if(real.data(SeriesTreeModel::TypeRole).toInt() == SeriesTreeModel::Part){
			parts << QPersistentModelIndex(real);
		}
	}
	QList<QPersistentModelIndex> removeParts;
	foreach(QPersistentModelIndex s, parts){
		if(series.contains(s.parent())){
			removeParts << s;
		}
	}
	foreach(QPersistentModelIndex s, removeParts){
		parts.removeAll(s);
	}
	QFileInfoList files;
	foreach(QPersistentModelIndex s, series){
		files.append(mModel->findFiles(s));
	}

	QString message;
	if(!files.isEmpty()){
		message = QString(tr("<p>This operation will delete %1 file(s) permanently:</p>")).arg(files.count());
		message.append(tr("<ul>"));
		int ctr = 0;
		foreach(QFileInfo f, files){
			++ctr;
			QString li = QString(tr("<li>%1</li>")).arg(f.absoluteFilePath());
			message.append(li);
			if(ctr == 20){
				QString elide = QString(tr("<li>(%1 more files)</li>")).arg(files.count() - ctr);
				message.append(elide);
				break;
			}
		}
		message.append(tr("</ul>"));
		message.append(tr("<p>Continue?</p>"));
	}else{
		message = QString(tr("<p>This operation will delete no files. Continue?</p>"));
	}
	int retval = QMessageBox::critical(this, tr("Delete Series"), message, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
	if(retval == QMessageBox::Yes){
		foreach(QPersistentModelIndex s, series){
			mModel->deleteFromSeries(s);
		}
		foreach(QPersistentModelIndex s, parts){
			mModel->deleteFromSeries(s);
		}
	}
}

void SeriesTreeWidget::readSettings(){
	QSettings s;
	QStringList expanded = s.value("archive/expanded").toStringList();
	foreach(QString s, expanded){
		QModelIndex idx = mProxy->mapFromSource(mModel->findValue(s));
		mView->expand(idx);
	}
	int sortOrder = s.value("archive/sortorder", Qt::DescendingOrder).toInt();
	mView->sortByColumn(0, static_cast<Qt::SortOrder>(sortOrder));
	QString selectedSeries = s.value("archive/selectedseries").toString();
	QModelIndex seriesIdx = mModel->findValue(selectedSeries);
	if(seriesIdx.isValid()){
		QModelIndex real = mProxy->mapFromSource(seriesIdx);
		mView->selectionModel()->setCurrentIndex(real, QItemSelectionModel::SelectCurrent);
	}
}

void SeriesTreeWidget::writeSettings(){
	QSettings s;
	s.setValue("archive/expanded", mExpandedItems);
	s.setValue("archive/sortorder", mProxy->sortOrder());
	QString selected = mView->selectionModel()->currentIndex().data(SeriesTreeModel::NameRole).toString();
	s.setValue("archive/selectedseries", selected);
}

void SeriesTreeWidget::expandCurrent(){
	QModelIndexList selected = mView->selectionModel()->selectedRows();
	if(selected.isEmpty()){
		return;
	}
	QModelIndex idx = selected.at(0);
	mView->expand(idx);
}

void SeriesTreeWidget::filter(){
	mProxy->setFilterRegExp(mFilterEdit->text());
}

void SeriesTreeWidget::resort(){
	mView->sortByColumn(0, mProxy->sortOrder());
	mView->scrollTo(mView->selectionModel()->currentIndex(), QAbstractItemView::PositionAtCenter);
}

void SeriesTreeWidget::itemExpanded(const QModelIndex &what){
	mExpandedItems << what.data(SeriesTreeModel::NameRole).toString();
}

void SeriesTreeWidget::itemCollaped(const QModelIndex &what){
	QString itemName = what.data(SeriesTreeModel::NameRole).toString();
	if(mExpandedItems.contains(itemName)){
		mExpandedItems.removeAll(itemName);
	}
}

void SeriesTreeWidget::addCover(){
	QModelIndexList selected = mView->selectionModel()->selectedRows();
	if(selected.isEmpty()){
		return;
	}
	if(selected.count() > 1){
		QMessageBox::critical(this, tr("Error"), tr("Please don't select more than one item when adding covers."));
		return;
	}
	QModelIndex real = mProxy->mapToSource(selected.at(0));
	if(real.data(SeriesTreeModel::TypeRole).toInt() != SeriesTreeModel::Part){
		QMessageBox::critical(this, tr("Error"), tr("Please select a series part when adding covers."));
		return;
	}
	int retval = mCoverDialog->exec();
	if(retval == QDialog::Accepted){
		QString fileName = mCoverDialog->file();
		QFileInfo fi(fileName);
		if(fi.exists()){
			FilesTreeModel *filesModel = static_cast<FilesTreeModel*>(SmGlobals::instance()->model("FilesModel"));
			int seriesPartId = real.data(SeriesTreeModel::SeriesPartIdRole).toInt();
			int type = mCoverDialog->fileType();
			if(filesModel->addFile(fileName, type, 0, -1, seriesPartId, -1)){
				QString md5sum = Helper::md5Sum(fileName);
				QString targetFile = Helper::createArchivePath(fileName, md5sum);
				QFile::rename(fileName, targetFile);
				filesModel->setIds(QList<int>() << seriesPartId);
				emit filesReload();
			}
		}
	}
}

SeriesTreeView::SeriesTreeView(QWidget *parent) : QTreeView(parent) {}

void SeriesTreeView::contextMenuEvent(QContextMenuEvent *e){
	QMenu contextMenu(this);
	foreach(QAction *a, actions()){
		contextMenu.addAction(a);
	}
	contextMenu.exec(e->globalPos());
}

SeriesTreeSortModel::SeriesTreeSortModel(QObject *parent) : QSortFilterProxyModel(parent) {}

bool SeriesTreeSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const{
	if(left.data(SeriesTreeModel::TypeRole).toInt() == SeriesTreeModel::Part && right.data(SeriesTreeModel::TypeRole).toInt() == SeriesTreeModel::Part){
		return left.data(SeriesTreeModel::SeriesPartRole).toInt() < right.data(SeriesTreeModel::SeriesPartRole).toInt();
	}
	return QSortFilterProxyModel::lessThan(left, right);
}

AddCoverDialog::AddCoverDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f){
	//File selection
	QLabel *l1 = new QLabel(tr("Select file"));
	mFile = new QLineEdit;
	QPushButton *mSelectFile = new QPushButton(tr("Browse..."));
	connect(mSelectFile, SIGNAL(clicked()), this, SLOT(selectFile()));
	QHBoxLayout *fileLayout = new QHBoxLayout;
	fileLayout->addWidget(l1);
	fileLayout->addWidget(mFile);
	fileLayout->addWidget(mSelectFile);

	//file type
	mFilesModel = static_cast<FilesTreeModel*>(SmGlobals::instance()->model("FilesModel"));
	QHash<int, QString> coverTypes = mFilesModel->coverTypes();
	mFileType = new QComboBox;
	QStringList types;
	foreach(QString t, coverTypes.values()){
		types << t;
	}
	connect(mFileType, SIGNAL(currentIndexChanged(QString)), this, SLOT(typeChanged(QString)));
	mFileType->addItems(types);
	QHBoxLayout *typesLayout = new QHBoxLayout;
	typesLayout->setAlignment(Qt::AlignCenter);
	typesLayout->addWidget(mFileType);

	//buttons
	mOk = new QPushButton(tr("Ok"));
	connect(mOk, SIGNAL(clicked()), this, SLOT(accept()));
	mCancel = new QPushButton(tr("Cancel"));
	connect(mCancel, SIGNAL(clicked()), this, SLOT(reject()));
	QHBoxLayout *buttonLayout = new QHBoxLayout;
	buttonLayout->addStretch();
	buttonLayout->addWidget(mOk);
	buttonLayout->addWidget(mCancel);

	//main layout
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addLayout(fileLayout);
	mainLayout->addLayout(typesLayout);
	mainLayout->addLayout(buttonLayout);
	setLayout(mainLayout);
}

const QString AddCoverDialog::file() const {
	return mFile->text();
}

void AddCoverDialog::selectFile(){
	QString startDir = mLastOpenedDir;
	if(startDir.isEmpty()){
		QSettings s;
		startDir = s.value("paths/selecteddir").toString();
	}
	QString file = QFileDialog::getOpenFileName(this, tr("Select cover"), startDir);
	mFile->setText(file);
	QFileInfo fi(file);
	mLastOpenedDir = fi.absolutePath();
}

void AddCoverDialog::typeChanged(const QString &type){
	QHash<int, QString>::const_iterator it = mFilesModel->coverTypes().constBegin();
	while(it != mFilesModel->coverTypes().constEnd()){
		if(it.value() == type){
			mCurrentType = it.key();
			return;
		}
		++it;
	}
}