File indexing completed on 2024-11-17 04:49:33
0001 /* 0002 * Copyright (C) 2003 by Mark Bucciarelli <mark@hubcapconsutling.com> 0003 * Copyright (C) 2019 Alexander Potashev <aspotashev@gmail.com> 0004 * 0005 * This program is free software; you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation; either version 2 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License along 0016 * with this program; if not, write to the 0017 * Free Software Foundation, Inc. 0018 * 51 Franklin Street, Fifth Floor 0019 * Boston, MA 02110-1301 USA. 0020 * 0021 */ 0022 0023 #include "totalsastext.h" 0024 0025 #include <QDateTime> 0026 0027 #include <KLocalizedString> 0028 0029 #include "ktimetrackerutility.h" // formatTime() 0030 #include "model/task.h" 0031 #include "model/tasksmodel.h" 0032 0033 static const int taskWidth = 40; 0034 static const int timeWidth = 6; 0035 static const int reportWidth = taskWidth + timeWidth; 0036 0037 static const auto cr = QStringLiteral("\n"); 0038 0039 // Print out "<indent for level> <task total> <task>", for task and subtasks. 0040 // Used by totalsAsText. 0041 static void printTask(Task *task, QString &s, int level, const ReportCriteria &rc) 0042 { 0043 QString buf; 0044 0045 s += buf.fill(QChar::fromLatin1(' '), level); 0046 if (!rc.sessionTimes) { 0047 s += QString(QString::fromLatin1("%1 %2")) 0048 .arg(formatTime(static_cast<double>(task->totalTime()), rc.decimalMinutes), timeWidth) 0049 .arg(task->name()); 0050 } else { 0051 // print session times 0052 s += QString(QString::fromLatin1("%1 %2")) 0053 .arg(formatTime(static_cast<double>(task->totalSessionTime()), rc.decimalMinutes), timeWidth) 0054 .arg(task->name()); 0055 } 0056 s += cr; 0057 0058 for (int i = 0; i < task->childCount(); ++i) { 0059 Task *subTask = dynamic_cast<Task *>(task->child(i)); 0060 if (!rc.sessionTimes) { 0061 if (subTask->totalTime()) { // to avoid 00:00 entries 0062 printTask(subTask, s, level + 1, rc); 0063 } 0064 } else { 0065 if (subTask->totalSessionTime()) { // to avoid 00:00 entries 0066 printTask(subTask, s, level + 1, rc); 0067 } 0068 } 0069 } 0070 } 0071 0072 QString totalsAsText(TasksModel *model, Task *currentItem, const ReportCriteria &rc) 0073 { 0074 QString retval; 0075 QString line; 0076 QString buf; 0077 int64_t sum; 0078 bool justThisTask = !rc.allTasks; 0079 0080 line.fill(QChar::fromLatin1('-'), reportWidth); 0081 line += cr; 0082 0083 // header 0084 retval += i18n("Task Totals") + cr; 0085 retval += QLocale().toString(QDateTime::currentDateTime()); 0086 retval += cr + cr; 0087 retval += QString(QString::fromLatin1("%1 %2")).arg(i18n("Time"), timeWidth).arg(i18n("Task")); 0088 retval += cr; 0089 retval += line; 0090 0091 // tasks 0092 if (currentItem) { 0093 if (justThisTask) { 0094 if (!rc.sessionTimes) { 0095 sum = currentItem->totalTime(); 0096 } else { 0097 sum = currentItem->totalSessionTime(); 0098 } 0099 0100 printTask(currentItem, retval, 0, rc); 0101 } else { // print all tasks 0102 sum = 0; 0103 for (int i = 0; i < model->topLevelItemCount(); ++i) { 0104 Task *task = dynamic_cast<Task *>(model->topLevelItem(i)); 0105 if (!rc.sessionTimes) { 0106 sum += task->totalTime(); 0107 } else { 0108 sum += task->totalSessionTime(); 0109 } 0110 if ((task->totalTime() && !rc.sessionTimes) || (task->totalSessionTime() && rc.sessionTimes)) { 0111 printTask(task, retval, 0, rc); 0112 } 0113 } 0114 } 0115 // total 0116 buf.fill(QChar::fromLatin1('-'), reportWidth); 0117 retval += QString(QString::fromLatin1("%1")).arg(buf, timeWidth) + cr; 0118 retval += QString(QString::fromLatin1("%1 %2")) 0119 .arg(formatTime(sum, rc.decimalMinutes), timeWidth) 0120 .arg(i18nc("total time of all tasks", "Total")); 0121 } else { 0122 retval += i18n("No tasks."); 0123 } 0124 0125 return retval; 0126 }