summaryrefslogtreecommitdiffstats
path: root/filestreewidget.cpp
blob: 7af3f6f65ca838312ec327a8e9e25bcc85c2ec15 (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
/*
  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 <QDebug>

#include "filestreewidget.h"
#include "smmodelsingleton.h"
#include "filestreemodel.h"
#include "seriestreemodel.h"

FilesTreeWidget::FilesTreeWidget(QWidget *parent) : QWidget(parent), mSelectedSize(0){
	//the view
	mView = new FilesTreeView;
	mView->setSelectionMode(QAbstractItemView::ExtendedSelection);
	mModel = static_cast<FilesTreeModel*>(SmModelSingleton::instance()->model("FilesModel"));
	mProxy = new FilesTreeSortModel(this);
	mProxy->setSourceModel(mModel);
	mView->setModel(mProxy);
	mView->setSortingEnabled(true);
	QItemSelectionModel *selModel = mView->selectionModel();
	connect(selModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(fileSelectionChanged(QModelIndex,QModelIndex)));
	connect(selModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(fileSelectionChanged(QItemSelection,QItemSelection)));

	//layout
	QHBoxLayout *mainLayout = new QHBoxLayout;
	mainLayout->addWidget(mView);
	setLayout(mainLayout);

	//series model
	mSeriesModel = static_cast<SeriesTreeModel*>(SmModelSingleton::instance()->model("SeriesModel"));
}

void FilesTreeWidget::fileSelectionChanged(const QModelIndex &current, const QModelIndex &previous){
	Q_UNUSED(previous);
	int seriesPartId = current.data(FilesTreeModel::SeriesPartIdRole).toInt();
	int seriesId = mSeriesModel->seriesIdByPartId(seriesPartId);
	int filePart = current.data(FilesTreeModel::PartNoRole).toInt();
	QModelIndex seriesIdx = mSeriesModel->findValue(seriesId, QModelIndex(), SeriesTreeModel::SeriesId);
	if(seriesIdx.isValid()){
		QModelIndex seriesPartIdx = mSeriesModel->findValue(seriesPartId, seriesIdx, SeriesTreeModel::SeriesPartId);
		QString seriesNumber = QString::number(seriesPartIdx.data(SeriesTreeModel::SeriesPartRole).toInt());
		QString msg;
		if(filePart != 0){
			msg = QString(tr("%1 %2 (%3)")).arg(seriesIdx.data(SeriesTreeModel::NameRole).toString()).arg(seriesNumber).arg(filePart);
		}else{
			msg = QString(tr("%1 %2")).arg(seriesIdx.data(SeriesTreeModel::NameRole).toString()).arg(seriesNumber);
		}
		emit statusMessage(msg);
	}
}

void FilesTreeWidget::fileSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected){
	QModelIndexList sel = selected.indexes();
	QModelIndexList desel = deselected.indexes();
	foreach(QModelIndex i, sel){
		if(i.column() == 0){
			mSelectedSize += i.data(FilesTreeModel::SizeRole).toLongLong();
		}
	}
	foreach(QModelIndex i, desel){
		if(i.column() == 0){
			mSelectedSize -= i.data(FilesTreeModel::SizeRole).toLongLong();
		}
	}
	emit sizeChanged(mSelectedSize);
}

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

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

bool FilesTreeSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const{
	if(left.parent() == QModelIndex()){
		return false;
	}
	if(left.column() == FilesTreeModel::SizeDisplay){
		return left.data(FilesTreeModel::SizeRole).toLongLong() < right.data(FilesTreeModel::SizeRole).toLongLong();
	}
	if(left.column() == FilesTreeModel::DvdNoRole){
		return left.data(FilesTreeModel::DvdNoRole).toInt() < right.data(FilesTreeModel::DvdNoRole).toInt();
	}
	return QSortFilterProxyModel::lessThan(left, right);
}