summaryrefslogtreecommitdiffstats
path: root/archiveviewwidget.cpp
blob: 51a9c91cfd0f0f29af325a32607d2d4abef06ac5 (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
/*
  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 <QComboBox>
#include <QLineEdit>
#include <QLineEdit>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QApplication>

#include "archiveviewwidget.h"
#include "archivefileview.h"
#include "moviemodel.h"
#include "listmodel.h"
#include "archiveproxy.h"
#include "editarchiveitemdialog.h"
#include "sizedelegate.h"
#include "archiveddelegate.h"

ArchiveViewWidget::ArchiveViewWidget(MovieModel *model, ListModel *genre, ListModel *actors, QWidget *parent) : QWidget(parent), mMovieModel(model), mGenreModel(genre), mActorsModel(actors){
	//filter bar
	QHBoxLayout *filterLayout = new QHBoxLayout;
	QLabel *l1 = new QLabel(tr("Filter by &genre"));
	mGenre = new QComboBox;
	l1->setBuddy(mGenre);
	mGenre->setModel(mGenreModel);
	connect(mGenre, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(setGenreFilter(const QString &)));
	filterLayout->addWidget(l1);
	filterLayout->addWidget(mGenre);
	QLabel *l2 = new QLabel(tr("Filter by &actor"));
	mActors = new QComboBox;
	mActors->setModel(mActorsModel);
	connect(mActors, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(setActorFilter(const QString &)));
	l2->setBuddy(mActors);
	filterLayout->addWidget(l2);
	filterLayout->addWidget(mActors);
	QLabel *l3 = new QLabel(tr("Filter by &title"));
	mName = new QLineEdit;
	l3->setBuddy(mName);
	filterLayout->addWidget(l3);
	filterLayout->addWidget(mName);
	mFilter = new QPushButton(tr("Filter"));
	connect(mFilter, SIGNAL(clicked()), this, SLOT(setFilter()));
	filterLayout->addWidget(mFilter);
	mClearFilter = new QPushButton(tr("Clear filter"));
	filterLayout->addWidget(mClearFilter);

	//treeview
	mFileView = new ArchiveFileView;
	mFileView->setItemDelegateForColumn(MovieItem::Size, new SizeDelegate(this));
	mFileView->setItemDelegateForColumn(MovieItem::Dvd, new ArchivedDelegate(this));
	mProxy = new ArchiveProxy(this);
	mProxy->setSourceModel(mMovieModel);
	mFileView->setModel(mProxy);
	mFileView->setSortingEnabled(true);
	mFileView->setItemsExpandable(false);
	mFileView->setRootIsDecorated(false);
	connect(mClearFilter, SIGNAL(clicked()), mProxy, SLOT(clearFilter()));

	//main layout
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addLayout(filterLayout);
	mainLayout->addWidget(mFileView);
	
	connect(mFileView->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex)), this, SLOT(rowChanged(const QModelIndex &, const QModelIndex &)));

	setLayout(mainLayout);
}

void ArchiveViewWidget::editFile(){
	QModelIndexList selected = mFileView->selectionModel()->selectedRows();
	if(!selected.isEmpty()){
		QModelIndex idx = selected.at(0);
		QModelIndex real = mProxy->mapToSource(idx);
		mEditDialog->setMovie(real);
		mEditDialog->show();
		mEditDialog->raise();
		mEditDialog->activateWindow();
	}
}

void ArchiveViewWidget::setFilter(){
	QString filter = mName->text().toLower();
	if(filter.isEmpty()){
		return;
	}
	mProxy->setFilter(filter, ArchiveProxy::TitleFilter);
}

void ArchiveViewWidget::setGenreFilter(const QString &filter){
	mProxy->setFilter(filter, ArchiveProxy::GenreFilter);
}

void ArchiveViewWidget::setActorFilter(const QString &filter){
	mProxy->setFilter(filter, ArchiveProxy::ActorFilter);
}

void ArchiveViewWidget::rowChanged(const QModelIndex &current, const QModelIndex & /*prev*/){
	if(current.isValid()){
		QModelIndex idx = current;
		if(current.column() != 0){
			idx = mProxy->index(current.row(), 0, current.parent());
		}
		QString title = QString(tr("%1 - %2")).arg(qApp->applicationName()).arg(idx.data().toString());
		emit windowTitle(title);
	}
}