summaryrefslogtreecommitdiffstats
blob: eb7fe2092f2f854a728c88b03b9c0f597baeb593 (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
/*
 * Copyright (c) 2010 Kevin Smith
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include "Swift/QtUI/MUCSearch/MUCSearchModel.h"

namespace Swift {

MUCSearchModel::MUCSearchModel() {
}

void MUCSearchModel::clear() {
	emit layoutAboutToBeChanged();
	services_.clear();
	emit layoutChanged();
}

void MUCSearchModel::addService(MUCSearchServiceItem* service) {
	emit layoutAboutToBeChanged();
	services_.push_back(service);
	emit layoutChanged();
}

int MUCSearchModel::columnCount(const QModelIndex& /*parent*/) const {
	return 1;
}

QVariant MUCSearchModel::data(const QModelIndex& index, int role) const {
	return index.isValid() ? static_cast<MUCSearchItem*>(index.internalPointer())->data(role) : QVariant();
}

QModelIndex MUCSearchModel::index(int row, int column, const QModelIndex & parent) const {
	if (!hasIndex(row, column, parent)) {
		return QModelIndex();
	}
	
	if (parent.isValid()) {
		MUCSearchServiceItem* parentItem = static_cast<MUCSearchServiceItem*>(parent.internalPointer());
		return row < parentItem->rowCount() ? createIndex(row, column, parentItem->getItem(row)) : QModelIndex();
		
	} else {
		return row < services_.size() ? createIndex(row, column, services_[row]) : QModelIndex();
	}


}

QModelIndex MUCSearchModel::parent(const QModelIndex& index) const {
	if (!index.isValid()) {
		return QModelIndex();
	}
	MUCSearchItem* item = static_cast<MUCSearchItem*>(index.internalPointer());
	if (!item) {
		return QModelIndex();
	}
	if (dynamic_cast<MUCSearchServiceItem*>(item)) {
		return QModelIndex();
	}
	MUCSearchServiceItem* parent = dynamic_cast<MUCSearchRoomItem*>(item)->getParent();
	int row = services_.indexOf(parent);
	return parent ? createIndex(row, 1, parent) : QModelIndex();
}

int MUCSearchModel::rowCount(const QModelIndex& parentIndex) const {
	if (!parentIndex.isValid()) {
		return services_.size();	
	} 
	if (dynamic_cast<MUCSearchServiceItem*>(static_cast<MUCSearchItem*>(parentIndex.internalPointer()))) {
		return services_[parentIndex.row()]->rowCount();
	} else {
		return 0;
	}
}

}