00001 #include "invitationmodel.h"
00002
00003 #include <domain/taskmanagerdata.h>
00004 #include <domain/invitation.h>
00005 #include <domain/task.h>
00006 #include <domain/user.h>
00007 #include <domain/usertype.h>
00008 #include <tools/functions.h>
00009
00010 using namespace control;
00011
00012 InvitationModel::InvitationModel(domain::TaskManagerData* data, int states)
00013 : DomainModel(data) , taskToShow(0), userToShow(0), shownStates(states)
00014 {
00015 connect(data, SIGNAL(invitationsChanged()), this, SLOT(emitDataChange()));
00016 }
00017
00018 InvitationModel::InvitationModel(domain::TaskManagerData* data,
00019 const domain::Task* task, int states): DomainModel(data), taskToShow(task),
00020 userToShow(0), shownStates(states)
00021 {
00022 connect(data, SIGNAL(invitationsChanged()), this, SLOT(emitDataChange()));
00023 }
00024
00025 InvitationModel::InvitationModel(domain::TaskManagerData* data, const domain::User* user,
00026 int states): DomainModel(data), taskToShow(0), userToShow(user),
00027 shownStates(states)
00028 {
00029 connect(data, SIGNAL(invitationsChanged()), this, SLOT(emitDataChange()));
00030 }
00031
00032 int InvitationModel::columnCount(const QModelIndex& parent) const
00033 {
00034 if (!parent.isValid())
00035 return LastColumn;
00036
00037 return 0;
00038
00039 }
00040
00041 int InvitationModel::rowCount(const QModelIndex& parent) const
00042 {
00043 if (!parent.isValid())
00044 return invitations().size();
00045
00046 return 0;
00047 }
00048
00049 QVariant InvitationModel::headerData(int section, Qt::Orientation orientation, int role) const
00050 {
00051 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
00052 {
00053 switch (section)
00054 {
00055 case Task: return "Task";
00056 case InvitedUser: return "Invited user";
00057 case UserType: return "User type";
00058 case Status: return "Status";
00059
00060 default: return QVariant();
00061 }
00062 }
00063
00064 return QVariant();
00065 }
00066
00067 QVariant InvitationModel::data(const QModelIndex& index, int role) const
00068 {
00069 const domain::Invitation* invitation = invitations()[index.row()];
00070
00071 if (role == Qt::DisplayRole)
00072 {
00073 switch (index.column())
00074 {
00075 case Task: return invitation->task()->id();
00076 case InvitedUser: return invitation->user()->name();
00077 case UserType: return invitation->user()->userType()->name();
00078 case Status: return invitation->stateString();
00079
00080 default: return QVariant();
00081 }
00082 }
00083 else if (role == Qt::ToolTipRole)
00084 {
00085 QString toolTip = "Invitation for Task %1\nFor User %2\nHas state %3";
00086
00087 return toolTip.arg(invitation->task()->id())
00088 .arg(invitation->user()->name())
00089 .arg(invitation->stateString());
00090 }
00091 else
00092 return QVariant();
00093 }
00094
00095 const domain::Invitation* InvitationModel::invitationForIndex(const QModelIndex& index) const
00096 {
00097 return invitations()[index.row()];
00098 }
00099
00100 QModelIndex InvitationModel::indexForInvitation(const domain::Invitation* invitation) const
00101 {
00102 if (!invitations().contains(invitation))
00103 return QModelIndex();
00104
00105 return index(invitations().indexOf(invitation), 0);
00106 }
00107
00108 QList<const domain::Invitation*> control::InvitationModel::invitations() const
00109 {
00110 QList<const domain::Invitation*> ret, possibleInvitation;
00111
00112 if (taskToShow == 0 && userToShow == 0)
00113 possibleInvitation = tools::makeConstList(domainData->invitations());
00114 else if (taskToShow != 0)
00115 possibleInvitation = taskToShow->invitations();
00116 else
00117 possibleInvitation = userToShow->invitations();
00118
00119 Q_FOREACH (const domain::Invitation* invitation, possibleInvitation)
00120 {
00121 if (invitation->state() & shownStates)
00122 ret << invitation;
00123 }
00124
00125 return ret;
00126 }