summaryrefslogtreecommitdiffstats
path: root/actormodel.cpp
blob: 14edcf80517f3f5460cb588519640ec7cea39658 (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
/*
  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 <QIcon>

#include "actormodel.h"

ActorModel::ActorModel() {};

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

QModelIndex ActorModel::index(const QVariant &item) const {
	if(item == QVariant()){
		return QModelIndex();
	}
	QString sItem = item.toString();
	for(int i = 0; i < mItems.size(); ++i){
		if(mItems.at(i) == sItem){
			return createIndex(i, 0, 0);
		}
	}
	return QModelIndex();
}

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

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

bool ActorModel::insertRows(int row, int count, const QModelIndex &){
	beginInsertRows(QModelIndex(), row, row + count - 1);
	for(int i = row; i < (row + count); ++i){
		mItems.insert(i, QString());
	}
	endInsertRows();
	return true;
}

bool ActorModel::removeRows(int row, int count, const QModelIndex &){
	if((row > mItems.size()) || ((row + count) > mItems.size())){
		return false;
	}
	beginRemoveRows(QModelIndex(), row, row + (count - 1));
	for(int i = row; i < (row + count); ++i){
		mItems.removeAt(i);
	}
	endRemoveRows();
	return true;
}

bool ActorModel::setData(const QModelIndex &idx, const QVariant &data, int role){
	if(!idx.isValid() || (idx.column() != 0) || (role != Qt::EditRole)){
		return false;
	}
	mItems[idx.row()] = data.toString();
	qSort(mItems);
	dataChanged(index(0, 0), index((mItems.size() - 1), 0));
	return true;
}

bool ActorModel::addItem(const QVariant &data){
	if(mItems.contains(data.toString())){
		return false;
	}
	bool success = false;
	if(insertRows(0, 1, QModelIndex())){
		QModelIndex idx = index(0, 0);
		if(idx.isValid()){
			success = setData(idx, data, Qt::EditRole);
		}
	}
	return success;
}

bool ActorModel::removeItem(const QVariant &data){
	QModelIndex idx = index(data);
	if(!idx.isValid()){
		return false;
	}
	bool retval = removeRows(idx.row(), 1, QModelIndex());
	if(retval){
		int lidx = mItems.indexOf(data.toString());
		if(lidx != -1){
			mItems.removeAt(lidx);
		}
	}
	return retval;
}

void ActorModel::clear() {
	removeRows(0, mItems.size(), QModelIndex());
	mItems.clear();
}