summaryrefslogtreecommitdiffstats
path: root/seriestreewidget.cpp
blob: 9b45dcdbcedb5815a87710667f81e16f347c9b3b (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
/*
  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 "seriestreewidget.h"
#include "smubermodel.h"
#include "smtreemodel.h"
#include "smubermodelsingleton.h"
#include "seriestreemodel.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 = SmUberModelSingleton::instance()->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)));

	//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::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);
		mExpandedItems << s;
	}
	int sortOrder = s.value("archive/sortorder", Qt::DescendingOrder).toInt();
	mView->sortByColumn(0, static_cast<Qt::SortOrder>(sortOrder));
}

void SeriesTreeWidget::writeSettings(){
	QSettings s;
	s.setValue("archive/expanded", mExpandedItems);
	s.setValue("archive/sortorder", mProxy->sortOrder());
}

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);
	}
}

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);
}