/*
* Copyright (C) 2010 Ixonos Plc.
*
* This file is part of fosdem-schedule.
*
* fosdem-schedule 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.
*
* fosdem-schedule is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* fosdem-schedule. If not, see .
*/
#include "eventmodel.h"
#include
#include
#include
const QString EventModel::COMMA_SEPARATOR = ", ";
EventModel::EventModel()
{ }
void EventModel::createTimeGroups()
{
mGroups.clear();
mParents.clear();
if (mEvents.empty())
{
return;
}
const int timeSpan = 5400;
QTime startTime = mEvents.first().start().time();
mGroups << EventModel::Group(QString("%1 - %2").arg(startTime.toString("HH:mm"),
startTime.addSecs(timeSpan).toString("HH:mm")), 0);
QTime nextGroupTime = mEvents.first().start().time().addSecs(timeSpan);
for (int i=0; i::iterator event = mEvents.begin();
int i = 0;
while (event != mEvents.end())
{
roomId = event->roomId();
if (nextRoomId != roomId)
{
mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
mGroups << EventModel::Group(Room::retrieveRoomName(roomId), i);
nextRoomId = roomId;
}
mParents[event->id()] = mGroups.count() - 1;
event++;
i++;
}
mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
}
QVariant EventModel::data(const QModelIndex& index, int role) const
{
if (index.isValid() && role == Qt::DisplayRole)
{
if (index.internalId() == 0)
{
return mGroups.at(index.row()).mTitle;
}
else //event data
{
return static_cast(index.internalPointer())->id();
}
}
return QVariant();
}
QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
{
// TODO: add checks for out of range rows
if (!parent.isValid())
{
return createIndex(row, column, 0);
}
else if (parent.internalId() == 0)
{
const Group& group = mGroups.at(parent.row());
Event* event = const_cast(&mEvents.at(row + group.mFirstEventIndex));
return createIndex(row, column, reinterpret_cast(event));
}
else
{
return QModelIndex();
}
}
QModelIndex EventModel::parent(const QModelIndex & index) const
{
if (index.isValid())
{
if (index.internalId() == 0)
{
return QModelIndex();
}
Event * event = static_cast(index.internalPointer());
return createIndex(mParents[event->id()], 0, 0);
}
return QModelIndex();
}
int EventModel::columnCount(const QModelIndex & parent) const
{
Q_UNUSED(parent);
return 1;
}
int EventModel::rowCount (const QModelIndex & parent) const
{
if (!parent.isValid())
{
return mGroups.count();
}
if (parent.internalId() == 0)
{
return mGroups.at(parent.row()).mChildCount;
}
return 0;
}
void EventModel::clearModel()
{
qDebug() << __PRETTY_FUNCTION__ << this << mEvents.count();
mGroups.clear();
mEvents.clear();
mParents.clear();
reset();
}
void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
{
clearModel();
// check for existence of the conference in the DB
if(Conference::getAll().count())
{
qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
}
createTimeGroups();
}
void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
{
clearModel();
// check for existence of the conference in the DB
if(Conference::getAll().count())
{
qDebug() << "Loading FAV Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
}
createTimeGroups();
}
int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId)
{
clearModel();
// check for existence of the conference in the DB
if(Conference::getAll().count())
{
qDebug() << "Loading search result Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
try{
mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
}
catch( OrmException &e ){
qDebug() << "Event::getSearchResultByDate failed: " << e.text();
}
catch(...){
qDebug() << "Event::getSearchResultByDate failed";
}
}
createTimeGroups();
return mEvents.count();
}
void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId)
{
clearModel();
if (Conference::getAll().count())
{
qDebug() << "Loading Conference Data (by Track): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start");
}
createTrackGroups();
}
void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId)
{
clearModel();
if (Conference::getAll().count())
{
qDebug() << "Loading Conference Data (by Room): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
}
createRoomGroups();
}
void EventModel::loadNowEvents(int aConferenceId)
{
clearModel();
// check for existence of the conference in the DB
if(Conference::getAll().count())
{
qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] scheduled NOW";
mEvents = Event::nowEvents(aConferenceId, "start");
}
createTimeGroups();
}
void EventModel::loadConflictEvents(int aEventId, int aConferenceId)
{
clearModel();
// check for existence of the conference in the DB
if(Conference::getAll().count())
{
qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] in conflict with " << aEventId;
mEvents = Event::conflictEvents(aEventId, aConferenceId);
}
createTimeGroups();
}
void EventModel::updateModel(int aEventId)
{
for(int i=0; i(eventIndex.internalPointer())->id() == aEventId)
{
emit(dataChanged(groupIndex,groupIndex));
emit(dataChanged(eventIndex,eventIndex));
}
}
}
}