00001 #include "taskmodel.h" 00002 00003 #include <domain/task.h> 00004 #include <domain/user.h> 00005 #include <domain/project.h> 00006 #include <domain/taskstates.h> 00007 #include <domain/taskmanagerdata.h> 00008 #include <domain/schedule.h> 00009 #include <domain/tasktype.h> 00010 #include <tools/functions.h> 00011 #include <QStringList> 00012 00013 using namespace control; 00014 00015 TaskModel::TaskModel(domain::TaskManagerData* data, 00016 QList<const domain::Task*> shownTasks) : DomainModel(data), 00017 taskList(shownTasks), 00018 showAll(false) 00019 { 00020 Q_FOREACH (const domain::Task* task, taskList) 00021 { 00022 connect(data->task(task), SIGNAL(dataChanged()), 00023 this, SLOT(emitDataChange())); 00024 } 00025 } 00026 00027 TaskModel::TaskModel(domain::TaskManagerData* data) : DomainModel(data), 00028 showAll(true) 00029 { 00030 connect(data, SIGNAL(tasksChanged()), this, SLOT(emitDataChange())); 00031 } 00032 00033 int TaskModel::columnCount(const QModelIndex& parent) const 00034 { 00035 if (!parent.isValid()) 00036 return LastColumn; 00037 00038 return 0; 00039 } 00040 00041 int TaskModel::rowCount(const QModelIndex& parent) const 00042 { 00043 if (!parent.isValid()) 00044 return tasks().size(); 00045 00046 return 0; 00047 } 00048 00049 QVariant TaskModel::headerData(int section, Qt::Orientation orientation, 00050 int role) const 00051 { 00052 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) 00053 { 00054 switch (section) 00055 { 00056 case Id: return "ID"; 00057 case Type: return "Type"; 00058 case Status: return "Status"; 00059 case User: return "User"; 00060 case Project: return "Project"; 00061 case StartTime: return "Start time"; 00062 case Deadline: return "Deadline"; 00063 case Duration: return "Duration"; 00064 case Fields: return "Fields"; 00065 00066 default: return QVariant(); 00067 } 00068 } 00069 00070 return QVariant(); 00071 } 00072 00073 QVariant TaskModel::data(const QModelIndex& index, int role) const 00074 { 00075 const domain::Task* task = taskForIndex(index); 00076 00077 if (role == Qt::DisplayRole) 00078 { 00079 switch (index.column()) 00080 { 00081 case Id: return task->id(); 00082 case Type: return task->taskType()->name(); 00083 case Status: return task->stateString(); 00084 case User: return task->user()->name(); 00085 case Project: return projectString(task); 00086 case StartTime: return task->schedule().startDate(); 00087 case Deadline: return task->schedule().dueDate(); 00088 case Duration: return task->schedule().duration().toString(); 00089 case Fields: return fieldsString(task).join(", "); 00090 00091 default: return QVariant(); 00092 } 00093 } 00094 else if (role == Qt::ToolTipRole) 00095 { 00096 QString toolTip("Task %1\nHas %2 sub task(s)\nFields:\n - %3"); 00097 return toolTip.arg(task->id()).arg(task->subTasks().size()) 00098 .arg(fieldsString(task).join("\n - ")); 00099 } 00100 else 00101 return QVariant(); 00102 } 00103 00104 const domain::Task* TaskModel::taskForIndex(const QModelIndex& index) const 00105 { 00106 return tasks()[index.row()]; 00107 } 00108 00109 QModelIndex TaskModel::indexForTask(const domain::Task* task) const 00110 { 00111 if (!tasks().contains(task)) 00112 return QModelIndex(); 00113 00114 return index(tasks().indexOf(task), 0); 00115 } 00116 00117 QList<const domain::Task*> TaskModel::tasks() const 00118 { 00119 return showAll ? tools::makeConstList(domainData->tasks()) : taskList; 00120 } 00121 00122 QString TaskModel::projectString(const domain::Task* task) const 00123 { 00124 return task->project() == 0 ? "/" : task->project()->description(); 00125 } 00126 00127 QStringList TaskModel::fieldsString(const domain::Task* task) const 00128 { 00129 QStringList ret; 00130 00131 Q_FOREACH (QString id, task->fields().keys()) 00132 { 00133 QString name = task->taskType()->fieldById(id).name; 00134 QVariant value = task->field(id); 00135 ret << QString("%1: %2").arg(name).arg(value.toString()); 00136 } 00137 00138 if (ret.isEmpty()) 00139 ret << "/"; 00140 00141 return ret; 00142 } 00143 00144 00145 00146 00147 00148 00149 00150 00151 00152 00153 00154 00155 00156 00157 00158 00159 00160 00161 00162 00163 00164 00165 00166