print.cpp
00001 // #include <iostream> 00002 00003 #include <tqdatetime.h> 00004 #include <tqpaintdevicemetrics.h> 00005 #include <tqpainter.h> 00006 00007 #include <kglobal.h> 00008 #include <klocale.h> // i18n 00009 00010 #include "karmutility.h" // formatTime() 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 // FIXME: make a better caption for the printingdialog 00025 if (setup(0L, i18n("Print Times"))) { 00026 // setup 00027 TQPainter painter(this); 00028 TQPaintDeviceMetrics deviceMetrics(this); 00029 TQFontMetrics 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 // Calculate the totals 00038 // Note the totals are only calculated at the top most levels, as the 00039 // totals are increased together with its children. 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 // Calculate the needed width for each of the fields 00050 timeWidth = TQMAX(metrics.width(i18n("Total")), 00051 metrics.width(formatTime(totalTotal))); 00052 sessionTimeWidth = TQMAX(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 = TQMAX(maxReqNameFieldWidth, width); 00065 } 00066 nameFieldWidth = TQMIN(nameFieldWidth, maxReqNameFieldWidth); 00067 00068 int realPageWidth = nameFieldWidth + timeWidth + sessionTimeWidth + 2*5; 00069 00070 // Print the header 00071 TQFont 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 TQString now = KGlobal::locale()->formatDateTime(TQDateTime::currentDateTime()); 00079 00080 painter.drawText(xMargin, yoff, pageWidth, height, 00081 TQPainter::AlignCenter, 00082 i18n("KArm - %1").arg(now)); 00083 00084 painter.setFont(origFont); 00085 yoff += height + 10; 00086 00087 // Print the second header. 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 // Now print the actual content 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 // Print the Totals 00107 printLine( formatTime( totalTotal ), 00108 formatTime( sessionTotal ), 00109 TQString(), painter, 0); 00110 } 00111 } 00112 00113 int MyPrinter::calculateReqNameWidth( Task* task, 00114 TQFontMetrics &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 = TQMAX(width, subTaskWidth); 00124 } 00125 return width; 00126 } 00127 00128 void MyPrinter::printTask(Task *task, TQPainter &painter, int level) 00129 { 00130 TQString time = formatTime(task->totalTime()); 00131 TQString sessionTime = formatTime(task->totalSessionTime()); 00132 TQString 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( TQString total, TQString session, TQString name, 00144 TQPainter &painter, int level ) 00145 { 00146 int xoff = xMargin + 10 * level; 00147 00148 painter.drawText( xoff, yoff, nameFieldWidth, lineHeight, 00149 TQPainter::AlignLeft, name); 00150 xoff = xMargin + nameFieldWidth; 00151 00152 painter.drawText( xoff, yoff, sessionTimeWidth, lineHeight, 00153 TQPainter::AlignRight, session); 00154 xoff += sessionTimeWidth+ 5; 00155 00156 painter.drawText( xoff, yoff, timeWidth, lineHeight, 00157 TQPainter::AlignRight, total); 00158 xoff += timeWidth+5; 00159 00160 yoff += lineHeight; 00161 00162 if (yoff + 2* lineHeight > pageHeight) { 00163 newPage(); 00164 yoff = yMargin; 00165 } 00166 }