summaryrefslogtreecommitdiffstats
path: root/moviemodel.cpp
blob: a4f47dc0f7e3dd870b18fccdf5b0ad774f475e07 (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
/*
  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 <QSqlQuery>

#include "moviemodel.h"

MovieModel::MovieModel(QObject *parent) : QAbstractItemModel(parent) {
	mHeaders << tr("Title") << tr("Filename") << tr("MD5Sum") << tr("Size") << tr("Genre") << tr("Quality") << tr("Archived");
	populate();
}

MovieModel::~MovieModel(){
	qDeleteAll(mItems);
}

QModelIndex MovieModel::index(int row, int column, const QModelIndex &parent) const{
	if((parent != QModelIndex()) || (!hasIndex(row, column, parent)) || (row > mItems.size())){
		return QModelIndex();
	}
	MovieItem *internal = mItems.at(row);
	return createIndex(row, column, internal);
}

QVariant MovieModel::data(const QModelIndex &index, int role) const{
	if(!index.isValid()){
		return QVariant();
	}
	if(role == Qt::DisplayRole){
		MovieItem *item = static_cast<MovieItem*>(index.internalPointer());
		Q_ASSERT(item != 0);
		switch (index.column()){
			case MovieItem::Dvd:
				return QVariant(QString(tr("DVD %1")).arg(QString::number(item->dataAt(MovieItem::Dvd).toInt())));
		}
		return item->dataAt(index.column());
	}
	return QVariant();
}

Qt::ItemFlags MovieModel::flags(const QModelIndex &index) const{
	if(!index.isValid()){
		return 0;
	}
	return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QVariant MovieModel::headerData(int section, Qt::Orientation o, int role) const{
	if((o == Qt::Horizontal) && (role == Qt::DisplayRole)){
		if(section < mHeaders.size()){
			return mHeaders.at(section);
		}
	}
	return QVariant();
}

bool MovieModel::insertRows(int row, int count, const QModelIndex &){
	if(row > rowCount(QModelIndex())){
		return false;
	}
	Q_ASSERT(count == 1);
	beginInsertRows(QModelIndex(), row, row);
	MovieItem *newItem = new MovieItem(-1, 0);
	mItems.insert(row, newItem);
	endInsertRows();
	return true;
}

bool MovieModel::removeRows(int row, int count, const QModelIndex &){
	if(row >= rowCount(QModelIndex())){
		return false;
	}
	beginRemoveRows(QModelIndex(), row, row + count - 1);
	for(int i = row; i < (row + count); ++i){
		MovieItem *item = mItems.at(i);
		delete item;
		mItems.removeAt(i);
	}
	endRemoveRows();
	return true;
}

bool MovieModel::setDataAt(const QModelIndex &index, const QList<QVariant> &data, qint32 genre, const QList<QVariant> &actors, const QList<QVariant> &covers){
	if(!index.isValid() || (data.size() > MovieItem::NumRows)){
		return false;
	}
	MovieItem *item = static_cast<MovieItem*>(index.internalPointer());
	Q_ASSERT(item != 0);
	if(item->id() != -1){
		qWarning("ID not as expected: item->id() == %d", item->id());
		return false;
	}
	return true;
}

void MovieModel::populate(){
	QSqlQuery movieQuery("SELECT imovid FROM movies");
	movieQuery.exec();
	while(movieQuery.next()){
		int id = movieQuery.value(0).toInt();
		MovieItem *item = new MovieItem(id);
		mItems << item;
	}
}