00001
00002
00003 #include <qdatetime.h>
00004 #include <qpaintdevicemetrics.h>
00005 #include <qpainter.h>
00006
00007 #include <kglobal.h>
00008 #include <klocale.h>
00009
00010 #include "karmutility.h"
00011 #include "print.h"
00012 #include "task.h"
00013 #include "taskview.h"
00014
00015 const int levelIndent = 10;
00016
00017 MyPrinter::MyPrinter(const TaskView *taskView)
00018 {
00019 _taskView = taskView;
00020 }
00021
00022 void MyPrinter::print()
00023 {
00024
00025 if (setup(0L, i18n("Print Times"))) {
00026
00027 QPainter painter(this);
00028 QPaintDeviceMetrics deviceMetrics(this);
00029 QFontMetrics metrics = painter.fontMetrics();
00030 pageHeight = deviceMetrics.height();
00031 int pageWidth = deviceMetrics.width();
00032 xMargin = margins().width();
00033 yMargin = margins().height();
00034 yoff = yMargin;
00035 lineHeight = metrics.height();
00036
00037
00038
00039
00040 int totalTotal = 0;
00041 int sessionTotal = 0;
00042 for (Task* task = _taskView->first_child();
00043 task;
00044 task = static_cast<Task *>(task->nextSibling())) {
00045 totalTotal += task->totalTime();
00046 sessionTotal += task->totalSessionTime();
00047 }
00048
00049
00050 timeWidth = QMAX(metrics.width(i18n("Total")),
00051 metrics.width(formatTime(totalTotal)));
00052 sessionTimeWidth = QMAX(metrics.width(i18n("Session")),
00053 metrics.width(formatTime(sessionTotal)));
00054
00055 nameFieldWidth = pageWidth - xMargin - timeWidth - sessionTimeWidth - 2*5;
00056
00057 int maxReqNameFieldWidth= metrics.width(i18n("Task Name "));
00058
00059 for ( Task* task = _taskView->first_child();
00060 task;
00061 task = static_cast<Task *>(task->nextSibling()))
00062 {
00063 int width = calculateReqNameWidth(task, metrics, 0);
00064 maxReqNameFieldWidth = QMAX(maxReqNameFieldWidth, width);
00065 }
00066 nameFieldWidth = QMIN(nameFieldWidth, maxReqNameFieldWidth);
00067
00068 int realPageWidth = nameFieldWidth + timeWidth + sessionTimeWidth + 2*5;
00069
00070
00071 QFont origFont, newFont;
00072 origFont = painter.font();
00073 newFont = origFont;
00074 newFont.setPixelSize( static_cast<int>(origFont.pixelSize() * 1.5) );
00075 painter.setFont(newFont);
00076
00077 int height = metrics.height();
00078 QString now = KGlobal::locale()->formatDateTime(QDateTime::currentDateTime());
00079
00080 painter.drawText(xMargin, yoff, pageWidth, height,
00081 QPainter::AlignCenter,
00082 i18n("KArm - %1").arg(now));
00083
00084 painter.setFont(origFont);
00085 yoff += height + 10;
00086
00087
00088 printLine(i18n("Total"), i18n("Session"), i18n("Task Name"), painter, 0);
00089
00090 yoff += 4;
00091 painter.drawLine(xMargin, yoff, xMargin + realPageWidth, yoff);
00092 yoff += 2;
00093
00094
00095 for ( Task* task = _taskView->first_child();
00096 task;
00097 task = static_cast<Task *>(task->nextSibling()) )
00098 {
00099 printTask(task, painter, 0);
00100 }
00101
00102 yoff += 4;
00103 painter.drawLine(xMargin, yoff, xMargin + realPageWidth, yoff);
00104 yoff += 2;
00105
00106
00107 printLine( formatTime( totalTotal ),
00108 formatTime( sessionTotal ),
00109 QString(), painter, 0);
00110 }
00111 }
00112
00113 int MyPrinter::calculateReqNameWidth( Task* task,
00114 QFontMetrics &metrics,
00115 int level)
00116 {
00117 int width = metrics.width(task->name()) + level * levelIndent;
00118
00119 for ( Task* subTask = task->firstChild();
00120 subTask;
00121 subTask = subTask->nextSibling() ) {
00122 int subTaskWidth = calculateReqNameWidth(subTask, metrics, level+1);
00123 width = QMAX(width, subTaskWidth);
00124 }
00125 return width;
00126 }
00127
00128 void MyPrinter::printTask(Task *task, QPainter &painter, int level)
00129 {
00130 QString time = formatTime(task->totalTime());
00131 QString sessionTime = formatTime(task->totalSessionTime());
00132 QString name = task->name();
00133 printLine(time, sessionTime, name, painter, level);
00134
00135 for ( Task* subTask = task->firstChild();
00136 subTask;
00137 subTask = subTask->nextSibling())
00138 {
00139 printTask(subTask, painter, level+1);
00140 }
00141 }
00142
00143 void MyPrinter::printLine( QString total, QString session, QString name,
00144 QPainter &painter, int level )
00145 {
00146 int xoff = xMargin + 10 * level;
00147
00148 painter.drawText( xoff, yoff, nameFieldWidth, lineHeight,
00149 QPainter::AlignLeft, name);
00150 xoff = xMargin + nameFieldWidth;
00151
00152 painter.drawText( xoff, yoff, sessionTimeWidth, lineHeight,
00153 QPainter::AlignRight, session);
00154 xoff += sessionTimeWidth+ 5;
00155
00156 painter.drawText( xoff, yoff, timeWidth, lineHeight,
00157 QPainter::AlignRight, total);
00158 xoff += timeWidth+5;
00159
00160 yoff += lineHeight;
00161
00162 if (yoff + 2* lineHeight > pageHeight) {
00163 newPage();
00164 yoff = yMargin;
00165 }
00166 }