summaryrefslogtreecommitdiffstats
path: root/actorcountmodel.cpp
blob: ac82f972efd63158506f0500ad88b085ec29e003 (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
/*
  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 <QIcon>

#include "actorcountmodel.h"

ActorCountModel::ActorCountModel(QObject *parent) : QAbstractItemModel(parent) {
	QSqlQuery query("SELECT iactorid, tactorname FROM actor ORDER BY tactorname ASC");
	QSqlQuery countQuery;
	countQuery.prepare("SELECT COUNT(*) FROM movieactormap WHERE iactorid = :id");
	while(query.next()){
		QVariant *name = new QVariant(query.value(1));
		countQuery.bindValue(":id", query.value(0));
		countQuery.exec();
		QVariant *count;
		while(countQuery.next()){
			count = new QVariant(countQuery.value(0));
		}
		QList<QVariant*> l;
		l << name << count;
		mItems.append(l);
	}
	mHeaderData << tr("Model name") << tr("Movies");
}

ActorCountModel::~ActorCountModel() {
	foreach(QList<QVariant*> l, mItems){
		qDeleteAll(l);
	}
}

QModelIndex ActorCountModel::index(int row, int column, const QModelIndex &parent) const {
	if((column > 1) || (row >= mItems.size()) || (row < 0) || (parent != QModelIndex())){
		return QModelIndex();
	}
	return createIndex(row, column, 0);
}

int ActorCountModel::rowCount(const QModelIndex &) const {
	return mItems.size();
}

int ActorCountModel::columnCount(const QModelIndex &) const {
	return 2;
}

QVariant ActorCountModel::data(const QModelIndex &idx, int role) const {
	if(!idx.isValid() || (idx.column() > 1) || (idx.row() > mItems.size())){
		return QVariant();
	}
	switch (role) {
		case Qt::DisplayRole:
			return *(mItems.at(idx.row()).at(idx.column()));
			break;
		case Qt::DecorationRole:
			if(idx.column() == 0){
				return QIcon(":/dildo.png");
			}
			break;
	}
	return QVariant();
}

QVariant ActorCountModel::headerData(int section, Qt::Orientation o, int role) const {
	if((role != Qt::DisplayRole) || (o != Qt::Horizontal) || (section > 1)){
		return QVariant();
	}
	return mHeaderData.at(section);
}