summaryrefslogtreecommitdiffstats
blob: f322792fbd891c60e93d196d2f5935605dabc39a (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
#include "RosterModel.h"

namespace Swift {

RosterModel::RosterModel() {
}

RosterModel::~RosterModel() {
	delete tree_;
}

void RosterModel::setRoot(QtTreeWidgetItem* root) {
	tree_ = root;
	connect(tree_, SIGNAL(changed()), this, SLOT(handleItemChanged()));
}
							  

void RosterModel::handleItemChanged() {
	//FIXME: This is just a lazy hack to cause the view to refresh until it works.
	// Then I'll replace it with the proper implementation.
	//printf("Changed\n");
	reset();
	emit layoutChanged();
}

int RosterModel::columnCount(const QModelIndex& parent) const {
	Q_UNUSED(parent);
	return 1;
}

QVariant RosterModel::data(const QModelIndex& index, int role) const {
	QtTreeWidgetItem* item = index.isValid() ? static_cast<QtTreeWidgetItem*>(index.internalPointer()) : NULL;
	return item ? item->data(role) : QVariant();
}

QModelIndex RosterModel::index(int row, int column, const QModelIndex& parent) const {
	QtTreeWidgetItem* parentItem = parent.isValid() ? static_cast<QtTreeWidgetItem*>(parent.internalPointer()) : tree_;
	Q_ASSERT(parentItem);
	
	return row < parentItem->rowCount() ? createIndex(row, column, parentItem->getItem(row)) : QModelIndex();
}

QModelIndex RosterModel::parent(const QModelIndex& index) const {
	if (!index.isValid()) {
		return QModelIndex();
	}
	
	QtTreeWidgetItem* item = static_cast<QtTreeWidgetItem*>(index.internalPointer());
	Q_ASSERT(item);

	QtTreeWidgetItem* parentItem = item->getParentItem();
	return parentItem == tree_ ? QModelIndex() : createIndex(parentItem->row(), 0, parentItem);

}

int RosterModel::rowCount(const QModelIndex& parent) const {
	QtTreeWidgetItem* item = parent.isValid() ? static_cast<QtTreeWidgetItem*>(parent.internalPointer()) : tree_;
	Q_ASSERT(item);
	
	return item->rowCount();
}

}