00001 #include "userrequirementsmodel.h" 00002 #include <domain/task.h> 00003 #include <domain/tasktype.h> 00004 #include <domain/usertype.h> 00005 00006 #include <limits> 00007 00008 #include <QDebug> 00009 00010 using namespace control; 00011 00012 UserRequirementsModel::UserRequirementsModel(domain::Task* task) : 00013 DomainModel(0), task(task), 00014 taskType(task->taskType()) 00015 { 00016 } 00017 00018 UserRequirementsModel::UserRequirementsModel(domain::TaskType* taskType) : 00019 DomainModel(0), task(0), 00020 taskType(taskType) 00021 { 00022 } 00023 00024 int UserRequirementsModel::rowCount(const QModelIndex& parent) const 00025 { 00026 if (!parent.isValid()) 00027 return taskType->userRequirements().size(); 00028 00029 return 0; 00030 } 00031 00032 int UserRequirementsModel::columnCount(const QModelIndex& parent) const 00033 { 00034 if (!parent.isValid()) 00035 return LastColumn; 00036 00037 return 0; 00038 } 00039 00040 QVariant UserRequirementsModel::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 "User type"; 00049 case Min: return "Minimum"; 00050 case Max: return "Maximum"; 00051 case NbAccepted: return "# accepted"; 00052 } 00053 } 00054 00055 return QVariant(); 00056 } 00057 00058 QVariant UserRequirementsModel::data(const QModelIndex& index, int role) const 00059 { 00060 if (index.parent().isValid()) 00061 return QVariant(); 00062 00063 domain::Requirement* req = taskType->userRequirements()[index.row()]; 00064 00065 int nbAccepted; 00066 00067 if (task != 0) 00068 nbAccepted = task->nbAcceptedInvitations(static_cast<domain::UserType*>(req->type)); 00069 else 00070 nbAccepted = -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 NbAccepted: 00086 return nbAccepted < 0 ? "/" : QString::number(nbAccepted); 00087 } 00088 } 00089 else if (role == Qt::ToolTipRole && task != 0) 00090 { 00091 QString toolTip("Requirement of task %1 for a user of type \"%2\"\n"); 00092 if (nbAccepted >= 0) 00093 { 00094 double perc; 00095 if (req->min == 0) 00096 perc = 100.0; 00097 else 00098 perc = qMin(100.0, nbAccepted / 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 }