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

#include <qdebug>

namespace Swift {
EventModel::EventModel() {
	
}

EventModel::~EventModel() {
	foreach (QtEvent* event, activeEvents_) {
		delete event;
	}
	foreach (QtEvent* event, inactiveEvents_) {
		delete event;
	}
}

QVariant EventModel::data(const QModelIndex& index, int role) const {
	if (!index.isValid()) {
		return QVariant();
	}
	int row = index.row();
	QtEvent* item = index.row() < activeEvents_.size() ? activeEvents_[row] : inactiveEvents_[row - activeEvents_.size()];
	QVariant result = item ? item->data(role) : QVariant();
	qDebug() << "Asked for data of " << index << ", " << role << " returning " << result;
	return result;
}

int EventModel::rowCount(const QModelIndex& parent) const {
	/* Invalid parent = root, valid parent = child, and we're a list not a tree.*/
	int count = parent.isValid() ? 0 : activeEvents_.size() + inactiveEvents_.size();
	return count;
}

void EventModel::addEvent(boost::shared_ptr<Event> event, bool active) {
	qDebug() << " Adding Event";
	if (active) {
		activeEvents_.push_front(new QtEvent(event, active));
		emit dataChanged(createIndex(0, 0), createIndex(1, 0));
	} else {
		inactiveEvents_.push_front(new QtEvent(event, active));
		emit dataChanged(createIndex(activeEvents_.size() -1, 0), createIndex(activeEvents_.size(), 0));
		if (inactiveEvents_.size() > 50) {
			removeEvent(inactiveEvents_[20]->getEvent());
		}
	}
	emit layoutChanged();
}

void EventModel::removeEvent(boost::shared_ptr<Event> event) {
	for (int i = inactiveEvents_.size() - 1; i >= 0; i--) {
		if (event == inactiveEvents_[i]->getEvent()) {
			inactiveEvents_.removeAt(i);
			emit dataChanged(createIndex(activeEvents_.size() + i - 1, 0), createIndex(activeEvents_.size() + i - 1, 0));
			return;
		}
	}

	for (int i = 0; i < activeEvents_.size(); i++) {
		if (event == activeEvents_[i]->getEvent()) {
			activeEvents_.removeAt(i);
			emit dataChanged(createIndex(i - 1, 0), createIndex(i - 1, 0));
			return;
		}
	}

}

}