krop's commit fixes my problem in a better way, reverting
[kdepim.git] / ktimetracker / timekard.cpp
blobe53856eb312adf809ac41c715ac71474d213c9a1
1 /*
2 * Copyright (C) 2003 by Mark Bucciarelli <mark@hubcapconsutling.com>
3 * 2007 the ktimetracker developers
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the
17 * Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor
19 * Boston, MA 02110-1301 USA.
23 #include "timekard.h"
25 #include <cassert>
27 #include <QDateTime>
28 #include <QList>
29 #include <QMap>
31 #include <KDebug>
32 #include <KGlobal>
33 #include <KLocale>
35 #include <kcal/event.h>
37 #include "karmstorage.h"
38 #include "ktimetrackerutility.h" // formatTime()
39 #include "task.h"
40 #include "taskview.h"
42 const int taskWidth = 40;
43 const int timeWidth = 6;
44 const int totalTimeWidth = 7;
45 const int reportWidth = taskWidth + timeWidth;
47 const QString cr = QString::fromLatin1("\n");
49 QString TimeKard::totalsAsText(TaskView* taskview, ReportCriteria rc)
51 kDebug(5970) << "Entering function";
52 QString retval;
53 QString line;
54 QString buf;
55 long sum;
56 bool justThisTask=!rc.allTasks;
58 line.fill('-', reportWidth);
59 line += cr;
61 // header
62 retval += i18n("Task Totals") + cr;
63 retval += KGlobal::locale()->formatDateTime(QDateTime::currentDateTime());
64 retval += cr + cr;
65 retval += QString(QString::fromLatin1("%1 %2"))
66 .arg(i18n("Time"), timeWidth)
67 .arg(i18n("Task"));
68 retval += cr;
69 retval += line;
71 // tasks
72 if (taskview->currentItem())
74 if (justThisTask)
76 if (!rc.sessionTimes) sum = taskview->currentItem()->totalTime();
77 else sum = taskview->currentItem()->totalSessionTime();
78 printTask(taskview->currentItem(), retval, 0, rc);
80 else // print all tasks
82 sum = 0;
83 for ( int i = 0; i < taskview->topLevelItemCount(); ++i ) {
84 Task *task = static_cast< Task* >( taskview->topLevelItem( i ) );
85 if (!rc.sessionTimes) sum += task->totalTime();
86 else sum += task->totalSessionTime();
87 if ( (task->totalTime() && (!rc.sessionTimes)) || (task->totalSessionTime() && rc.sessionTimes) )
88 printTask(task, retval, 0, rc);
92 // total
93 buf.fill('-', reportWidth);
94 retval += QString(QString::fromLatin1("%1")).arg(buf, timeWidth) + cr;
95 retval += QString(QString::fromLatin1("%1 %2"))
96 .arg(formatTime(sum),timeWidth)
97 .arg(i18nc( "total time of all tasks", "Total" ));
99 else
100 retval += i18n("No tasks.");
102 return retval;
105 // Print out "<indent for level> <task total> <task>", for task and subtasks. Used by totalsAsText.
106 void TimeKard::printTask(Task *task, QString &s, int level, const ReportCriteria &rc)
108 kDebug(5970) << "Entering function";
109 QString buf;
111 s += buf.fill(' ', level);
112 if (!rc.sessionTimes)
114 s += QString(QString::fromLatin1("%1 %2"))
115 .arg(formatTime(task->totalTime()), timeWidth)
116 .arg(task->name());
118 else // print session times
120 s += QString(QString::fromLatin1("%1 %2"))
121 .arg(formatTime(task->totalSessionTime()), timeWidth)
122 .arg(task->name());
124 s += cr;
126 for ( int i = 0; i < task->childCount(); ++i ) {
127 Task *subTask = static_cast< Task* >( task->child( i ) );
128 if ( !rc.sessionTimes )
130 if ( subTask->totalTime() ) // to avoid 00:00 entries
131 printTask(subTask, s, level+1, rc);
133 else
135 if ( subTask->totalSessionTime() ) // to avoid 00:00 entries
136 printTask(subTask, s, level+1, rc);
141 Week::Week() {}
143 Week::Week( const QDate &from )
145 _start = from;
148 QDate Week::start() const
150 return _start;
153 QDate Week::end() const
155 return _start.addDays(6);
158 QString Week::name() const
160 return i18n("Week of %1", KGlobal::locale()->formatDate(start()));
163 QList<Week> Week::weeksFromDateRange(const QDate& from, const QDate& to)
165 QDate start;
166 QList<Week> weeks;
168 // The QDate weekNumber() method always puts monday as the first day of the
169 // week.
171 // Not that it matters here, but week 1 always includes the first Thursday
172 // of the year. For example, January 1, 2000 was a Saturday, so
173 // QDate(2000,1,1).weekNumber() returns 52.
175 // Since report always shows a full week, we generate a full week of dates,
176 // even if from and to are the same date. The week starts on the day
177 // that is set in the locale settings.
178 start = from.addDays(
179 -((7 - KGlobal::locale()->weekStartDay() + from.dayOfWeek()) % 7));
181 for (QDate d = start; d <= to; d = d.addDays(7))
182 weeks.append(Week(d));
184 return weeks;