karm

print.cpp
1 // #include <iostream>
2 
3 #include <tqdatetime.h>
4 #include <tqpaintdevicemetrics.h>
5 #include <tqpainter.h>
6 
7 #include <tdeglobal.h>
8 #include <tdelocale.h> // i18n
9 
10 #include "karmutility.h" // formatTime()
11 #include "print.h"
12 #include "task.h"
13 #include "taskview.h"
14 
15 const int levelIndent = 10;
16 
17 MyPrinter::MyPrinter(const TaskView *taskView)
18 {
19  _taskView = taskView;
20 }
21 
22 void MyPrinter::print()
23 {
24  // FIXME: make a better caption for the printingdialog
25  if (setup(0L, i18n("Print Times"))) {
26  // setup
27  TQPainter painter(this);
28  TQPaintDeviceMetrics deviceMetrics(this);
29  TQFontMetrics metrics = painter.fontMetrics();
30  pageHeight = deviceMetrics.height();
31  int pageWidth = deviceMetrics.width();
32  xMargin = margins().width();
33  yMargin = margins().height();
34  yoff = yMargin;
35  lineHeight = metrics.height();
36 
37  // Calculate the totals
38  // Note the totals are only calculated at the top most levels, as the
39  // totals are increased together with its children.
40  int totalTotal = 0;
41  int sessionTotal = 0;
42  for (Task* task = _taskView->first_child();
43  task;
44  task = static_cast<Task *>(task->nextSibling())) {
45  totalTotal += task->totalTime();
46  sessionTotal += task->totalSessionTime();
47  }
48 
49  // Calculate the needed width for each of the fields
50  timeWidth = TQMAX(metrics.width(i18n("Total")),
51  metrics.width(formatTime(totalTotal)));
52  sessionTimeWidth = TQMAX(metrics.width(i18n("Session")),
53  metrics.width(formatTime(sessionTotal)));
54 
55  nameFieldWidth = pageWidth - xMargin - timeWidth - sessionTimeWidth - 2*5;
56 
57  int maxReqNameFieldWidth= metrics.width(i18n("Task Name "));
58 
59  for ( Task* task = _taskView->first_child();
60  task;
61  task = static_cast<Task *>(task->nextSibling()))
62  {
63  int width = calculateReqNameWidth(task, metrics, 0);
64  maxReqNameFieldWidth = TQMAX(maxReqNameFieldWidth, width);
65  }
66  nameFieldWidth = TQMIN(nameFieldWidth, maxReqNameFieldWidth);
67 
68  int realPageWidth = nameFieldWidth + timeWidth + sessionTimeWidth + 2*5;
69 
70  // Print the header
71  TQFont origFont, newFont;
72  origFont = painter.font();
73  newFont = origFont;
74  newFont.setPixelSize( static_cast<int>(origFont.pixelSize() * 1.5) );
75  painter.setFont(newFont);
76 
77  int height = metrics.height();
78  TQString now = TDEGlobal::locale()->formatDateTime(TQDateTime::currentDateTime());
79 
80  painter.drawText(xMargin, yoff, pageWidth, height,
81  TQPainter::AlignCenter,
82  i18n("KArm - %1").arg(now));
83 
84  painter.setFont(origFont);
85  yoff += height + 10;
86 
87  // Print the second header.
88  printLine(i18n("Total"), i18n("Session"), i18n("Task Name"), painter, 0);
89 
90  yoff += 4;
91  painter.drawLine(xMargin, yoff, xMargin + realPageWidth, yoff);
92  yoff += 2;
93 
94  // Now print the actual content
95  for ( Task* task = _taskView->first_child();
96  task;
97  task = static_cast<Task *>(task->nextSibling()) )
98  {
99  printTask(task, painter, 0);
100  }
101 
102  yoff += 4;
103  painter.drawLine(xMargin, yoff, xMargin + realPageWidth, yoff);
104  yoff += 2;
105 
106  // Print the Totals
107  printLine( formatTime( totalTotal ),
108  formatTime( sessionTotal ),
109  TQString(), painter, 0);
110  }
111 }
112 
113 int MyPrinter::calculateReqNameWidth( Task* task,
114  TQFontMetrics &metrics,
115  int level)
116 {
117  int width = metrics.width(task->name()) + level * levelIndent;
118 
119  for ( Task* subTask = task->firstChild();
120  subTask;
121  subTask = subTask->nextSibling() ) {
122  int subTaskWidth = calculateReqNameWidth(subTask, metrics, level+1);
123  width = TQMAX(width, subTaskWidth);
124  }
125  return width;
126 }
127 
128 void MyPrinter::printTask(Task *task, TQPainter &painter, int level)
129 {
130  TQString time = formatTime(task->totalTime());
131  TQString sessionTime = formatTime(task->totalSessionTime());
132  TQString name = task->name();
133  printLine(time, sessionTime, name, painter, level);
134 
135  for ( Task* subTask = task->firstChild();
136  subTask;
137  subTask = subTask->nextSibling())
138  {
139  printTask(subTask, painter, level+1);
140  }
141 }
142 
143 void MyPrinter::printLine( TQString total, TQString session, TQString name,
144  TQPainter &painter, int level )
145 {
146  int xoff = xMargin + 10 * level;
147 
148  painter.drawText( xoff, yoff, nameFieldWidth, lineHeight,
149  TQPainter::AlignLeft, name);
150  xoff = xMargin + nameFieldWidth;
151 
152  painter.drawText( xoff, yoff, sessionTimeWidth, lineHeight,
153  TQPainter::AlignRight, session);
154  xoff += sessionTimeWidth+ 5;
155 
156  painter.drawText( xoff, yoff, timeWidth, lineHeight,
157  TQPainter::AlignRight, total);
158  xoff += timeWidth+5;
159 
160  yoff += lineHeight;
161 
162  if (yoff + 2* lineHeight > pageHeight) {
163  newPage();
164  yoff = yMargin;
165  }
166 }
Container and interface for the tasks.
Definition: taskview.h:43
Task * first_child() const
Return the first item in the view, cast to a Task pointer.
Definition: taskview.cpp:172
A class representing a task.
Definition: task.h:42
TQString name() const
returns the name of this task.
Definition: task.h:162
Task * firstChild() const
return parent Task or null in case of TaskView.
Definition: task.h:60