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
|
/*
* Copyright (c) 2010 Kevin Smith
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include "EventModel.h"
#include <QtDebug>
namespace Swift {
EventModel::EventModel() {
}
EventModel::~EventModel() {
foreach (QtEvent* event, activeEvents_) {
delete event;
}
foreach (QtEvent* event, inactiveEvents_) {
delete event;
}
}
QtEvent* EventModel::getItem(int row) const {
return row < activeEvents_.size() ? activeEvents_[row] : inactiveEvents_[row - activeEvents_.size()];
}
int EventModel::getNewEventCount() {
return activeEvents_.size();
}
QVariant EventModel::data(const QModelIndex& index, int role) const {
if (!index.isValid()) {
return QVariant();
}
QtEvent* item = getItem(index.row());
QVariant result = item ? item->data(role) : QVariant();
return result;
}
/*
* We only reimplement this to get the pointers inside the indices.
*/
QModelIndex EventModel::index(int row, int column, const QModelIndex & parent) const {
if (!hasIndex(row, column, parent) || parent.isValid()) {
return QModelIndex();
}
return row < rowCount() ? createIndex(row, column, getItem(row)) : QModelIndex();
}
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<StanzaEvent> event, bool active) {
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<StanzaEvent> 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;
}
}
}
}
|