00001 #include "resourcerequirementsmodel.h"
00002 #include <domain/task.h>
00003 #include <domain/tasktype.h>
00004 #include <domain/resourcetype.h>
00005 
00006 #include <limits>
00007 
00008 #include <QDebug>
00009 
00010 using namespace control;
00011 
00012 ResourceRequirementsModel::ResourceRequirementsModel(domain::Task* task) :
00013                                             DomainModel(0), task(task),
00014                                             taskType(task->taskType())
00015 {
00016 }
00017 
00018 ResourceRequirementsModel::ResourceRequirementsModel(domain::TaskType* taskType) :
00019                                              DomainModel(0), task(0),
00020                                              taskType(taskType)
00021 {
00022 }
00023 
00024 int ResourceRequirementsModel::rowCount(const QModelIndex& parent) const
00025 {
00026     if (!parent.isValid())
00027         return taskType->resourceRequirements().size();
00028 
00029     return 0;
00030 }
00031 
00032 int ResourceRequirementsModel::columnCount(const QModelIndex& parent) const
00033 {
00034     if (!parent.isValid())
00035         return LastColumn;
00036 
00037     return 0;
00038 }
00039 
00040 QVariant ResourceRequirementsModel::headerData(int section,
00041                                            Qt::Orientation orientation,
00042                                            int role) const
00043 {
00044     if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
00045     {
00046         switch (section)
00047         {
00048             case Type:       return "Resource type";
00049             case Min:        return "Minimum";
00050             case Max:        return "Maximum";
00051             case NbActive:   return "# active";
00052         }
00053     }
00054 
00055     return QVariant();
00056 }
00057 
00058 QVariant ResourceRequirementsModel::data(const QModelIndex& index, int role) const
00059 {
00060     if (index.parent().isValid())
00061         return QVariant();
00062 
00063     domain::Requirement* req = taskType->resourceRequirements()[index.row()];
00064 
00065     int nbActive;
00066 
00067     if (task != 0)
00068         nbActive = task->nbActiveReservations(static_cast<domain::ResourceType*>(req->type));
00069     else
00070         nbActive = -1;
00071 
00072     if (role == Qt::DisplayRole)
00073     {
00074         switch (index.column())
00075         {
00076             case Type:
00077                 return req->type->name();
00078             case Min:
00079                 return req->min;
00080             case Max:
00081                 if (req->max == std::numeric_limits<unsigned>::max())
00082                     return QChar(0x221E);
00083                 else
00084                     return req->max;
00085             case NbActive:
00086                 return nbActive < 0 ? "/" : QString::number(nbActive);
00087         }
00088     }
00089     else if (role == Qt::ToolTipRole && task != 0)
00090     {
00091         QString toolTip("Requirement of task %1 for a resource of type \"%2\"\n");
00092         if (nbActive >= 0)
00093         {
00094             double perc;
00095             if (req->min == 0)
00096                 perc = 100.0;
00097             else
00098                 perc = qMin(100.0, nbActive / req->min * 100.0);
00099 
00100             toolTip += QString("%1% completed").arg(perc);
00101         }
00102 
00103         return toolTip.arg(task->id()).arg(req->type->name());
00104     }
00105 
00106     return QVariant();
00107 }