ktimetracker
timekard.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "timekard.h"
00024
00025 #include <cassert>
00026
00027 #include <QDateTime>
00028 #include <QList>
00029 #include <QMap>
00030
00031 #include <KDebug>
00032 #include <KGlobal>
00033 #include <KLocale>
00034
00035 #include <kcal/event.h>
00036
00037 #include "karmstorage.h"
00038 #include "ktimetrackerutility.h"
00039 #include "task.h"
00040 #include "taskview.h"
00041
00042 const int taskWidth = 40;
00043 const int timeWidth = 6;
00044 const int totalTimeWidth = 7;
00045 const int reportWidth = taskWidth + timeWidth;
00046
00047 const QString cr = QString::fromLatin1("\n");
00048
00049 QString TimeKard::totalsAsText(TaskView* taskview, ReportCriteria rc)
00050 {
00051 kDebug(5970) << "Entering function";
00052 QString retval;
00053 QString line;
00054 QString buf;
00055 long sum;
00056 bool justThisTask=!rc.allTasks;
00057
00058 line.fill('-', reportWidth);
00059 line += cr;
00060
00061
00062 retval += i18n("Task Totals") + cr;
00063 retval += KGlobal::locale()->formatDateTime(QDateTime::currentDateTime());
00064 retval += cr + cr;
00065 retval += QString(QString::fromLatin1("%1 %2"))
00066 .arg(i18n("Time"), timeWidth)
00067 .arg(i18n("Task"));
00068 retval += cr;
00069 retval += line;
00070
00071
00072 if (taskview->currentItem())
00073 {
00074 if (justThisTask)
00075 {
00076 if (!rc.sessionTimes) sum = taskview->currentItem()->totalTime();
00077 else sum = taskview->currentItem()->totalSessionTime();
00078 printTask(taskview->currentItem(), retval, 0, rc);
00079 }
00080 else
00081 {
00082 sum = 0;
00083 for ( int i = 0; i < taskview->topLevelItemCount(); ++i ) {
00084 Task *task = static_cast< Task* >( taskview->topLevelItem( i ) );
00085 if (!rc.sessionTimes) sum += task->totalTime();
00086 else sum += task->totalSessionTime();
00087 if ( (task->totalTime() && (!rc.sessionTimes)) || (task->totalSessionTime() && rc.sessionTimes) )
00088 printTask(task, retval, 0, rc);
00089 }
00090 }
00091
00092
00093 buf.fill('-', reportWidth);
00094 retval += QString(QString::fromLatin1("%1")).arg(buf, timeWidth) + cr;
00095 retval += QString(QString::fromLatin1("%1 %2"))
00096 .arg(formatTime(sum),timeWidth)
00097 .arg(i18nc( "total time of all tasks", "Total" ));
00098 }
00099 else
00100 retval += i18n("No tasks.");
00101
00102 return retval;
00103 }
00104
00105
00106 void TimeKard::printTask(Task *task, QString &s, int level, const ReportCriteria &rc)
00107 {
00108 kDebug(5970) << "Entering function";
00109 QString buf;
00110
00111 s += buf.fill(' ', level);
00112 if (!rc.sessionTimes)
00113 {
00114 s += QString(QString::fromLatin1("%1 %2"))
00115 .arg(formatTime(task->totalTime()), timeWidth)
00116 .arg(task->name());
00117 }
00118 else
00119 {
00120 s += QString(QString::fromLatin1("%1 %2"))
00121 .arg(formatTime(task->totalSessionTime()), timeWidth)
00122 .arg(task->name());
00123 }
00124 s += cr;
00125
00126 for ( int i = 0; i < task->childCount(); ++i ) {
00127 Task *subTask = static_cast< Task* >( task->child( i ) );
00128 if ( !rc.sessionTimes )
00129 {
00130 if ( subTask->totalTime() )
00131 printTask(subTask, s, level+1, rc);
00132 }
00133 else
00134 {
00135 if ( subTask->totalSessionTime() )
00136 printTask(subTask, s, level+1, rc);
00137 }
00138 }
00139 }
00140
00141 Week::Week() {}
00142
00143 Week::Week( const QDate &from )
00144 {
00145 _start = from;
00146 }
00147
00148 QDate Week::start() const
00149 {
00150 return _start;
00151 }
00152
00153 QDate Week::end() const
00154 {
00155 return _start.addDays(6);
00156 }
00157
00158 QString Week::name() const
00159 {
00160 return i18n("Week of %1", KGlobal::locale()->formatDate(start()));
00161 }
00162
00163 QList<Week> Week::weeksFromDateRange(const QDate& from, const QDate& to)
00164 {
00165 QDate start;
00166 QList<Week> weeks;
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 start = from.addDays(
00179 -((7 - KGlobal::locale()->weekStartDay() + from.dayOfWeek()) % 7));
00180
00181 for (QDate d = start; d <= to; d = d.addDays(7))
00182 weeks.append(Week(d));
00183
00184 return weeks;
00185 }
00186