20#include "kptreportview.h"
24#include "kptcontext.h"
26#include <mreportviewer.h>
27#include <mpagecollection.h>
33#include <kstdaction.h>
34#include <tdetoolbar.h>
35#include <kstandarddirs.h>
37#include <tdemessagebox.h>
38#include <tdeio/netaccess.h>
41#include <kdesktopfile.h>
42#include <tdefiledialog.h>
45#include <tqfileinfo.h>
47#include <tqpopupmenu.h>
50#include <tqstringlist.h>
55class ReportTagsPrivate {
64 summarytasksLevel(
"-1"),
66 milestonesLevel(
"-1"),
67 resourcegroupsLevel(
"-1"),
71 ~ReportTagsPrivate() {}
73 TQString getData(TQString source, TQString tag)
const {
74 if (tag.contains(
"."))
77 return getData(source +
"." + tag);
80 TQString getData(TQString tag)
const {
82 TDELocale *l = TDEGlobal::locale();
83 if (!tag.contains(
'.')) {
85 if (tag ==
"currentdate") {
86 return l->formatDate(TQDate::currentDate(),
true);
90 if (tag.section(
".", 0, 0) ==
"project") {
91 if (tag.section(
".", 1, 1) ==
"name")
92 return (m_project ? m_project->name() : TQString());
93 if (tag.section(
".", 1, 1) ==
"leader")
94 return (m_project ? m_project->leader() : TQString());
96 }
else if (tag.section(
".", 0, 0) ==
"task") {
97 if (tag.section(
".", 1, 1) ==
"name")
98 return (m_task ? m_task->name() : TQString());
99 if (tag.section(
".", 1, 1) ==
"responsible")
100 return (m_task ? m_task->leader() : TQString());
101 else if (tag.section(
".", 1, 1) ==
"wbs")
102 return (m_task ? m_task->wbs() : TQString());
103 else if (tag.section(
".", 1, 1) ==
"start")
104 return (m_task ? l->formatDateTime(m_task->startTime()) : TQString());
105 else if (tag.section(
".", 1, 1) ==
"starttime")
106 return (m_task ? l->formatTime(m_task->startTime().time()) : TQString());
107 else if (tag.section(
".", 1, 1) ==
"startdate")
108 return (m_task ? l->formatDate(m_task->startTime().date(),
true) : TQString());
109 else if (tag.section(
".", 1, 1) ==
"duration") {
110 return (m_task ? m_task->duration().toString(Duration::Format_i18nDayTime) : TQString());
111 }
else if (tag.section(
".", 1, 1) ==
"plannedcost") {
112 return (m_task ? l->formatMoney(m_task->plannedCost()) : TQString());
114 }
else if (tag.section(
".", 0, 0) ==
"resourcegroup") {
115 if (tag.section(
".", 1, 1) ==
"name")
116 return (m_resourcegroup ? m_resourcegroup->name() : TQString());
118 }
else if (tag.section(
".", 0, 0) ==
"resource") {
119 if (tag.section(
".", 1, 1) ==
"name")
120 return (m_resource ? m_resource->name() : TQString());
121 if (tag.section(
".", 1, 1) ==
"type")
122 return (m_resource ? m_resource->typeToString() : TQString());
123 if (tag.section(
".", 1, 1) ==
"email")
124 return (m_resource ? m_resource->email() : TQString());
125 if (tag.section(
".", 1, 1) ==
"availablefrom")
126 return (m_resource ? l->formatDate(m_resource->availableFrom().date(),
true) : TQString());
127 if (tag.section(
".", 1, 1) ==
"availableuntil")
128 return (m_resource ? l->formatDate(m_resource->availableUntil().date(),
true) : TQString());
129 if (tag.section(
".", 1, 1) ==
"units")
130 return (m_resource ? TQString(
"%1%").arg(m_resource->units()) : TQString());
131 if (tag.section(
".", 1, 1) ==
"normalrate")
132 return (m_resource ? l->formatMoney(m_resource->normalRate()) : TQString());
133 if (tag.section(
".", 1, 1) ==
"overtimerate")
134 return (m_resource ? l->formatMoney(m_resource->overtimeRate()) : TQString());
141 ResourceGroup *m_resourcegroup;
142 Resource *m_resource;
144 TQString alltasksLevel;
145 TQStringList alltasksProps;
146 TQString summarytasksLevel;
147 TQStringList summarytasksProps;
149 TQStringList tasksProps;
150 TQString milestonesLevel;
151 TQStringList milestonesProps;
152 TQString resourcegroupsLevel;
153 TQStringList resourcegroupsProps;
154 TQString resourcesLevel;
155 TQStringList resourcesProps;
159class KugarReportViewer :
public Kugar::MReportViewer {
161 KugarReportViewer(TQWidget *parent = 0,
const char *name = 0)
162 : MReportViewer(parent, name)
166 return report ? report->getCurrentIndex() : 0;
169 return report ? report->pageCount() : 0;
173ReportView::ReportView(View *view, TQWidget *parent)
174 : TQSplitter(parent),
179 m_reportList =
new TDEListView(
this);
180 m_reportList->setShadeSortColumn(
false);
181 m_reportList->addColumn(i18n(
"Report Template"));
182 m_reportList->header()->setStretchEnabled(
true, 0);
183 m_reportList->header()->setSortIndicator(0);
185 m_reportview =
new KugarReportViewer(
this);
189 connect(m_reportList, TQ_SIGNAL(clicked(TQListViewItem*)), TQ_SLOT(slotReportListClicked(TQListViewItem*)));
190 connect(m_reportList, TQ_SIGNAL(selectionChanged(TQListViewItem*)), TQ_SLOT(slotReportListSelectionChanged(TQListViewItem*)));
208ReportView::~ReportView() {
213void ReportView::initReportList() {
217 m_reportList->clear();
219 TQStringList reportDesktopFiles = std.findAllResources(
"data",
"kplato/reports/*.desktop",
true,
true);
220 for (TQStringList::iterator it = reportDesktopFiles.begin(); it != reportDesktopFiles.end(); ++it) {
221 KDesktopFile file((*it),
true);
222 TQString name = file.readName();
223 if (!name.isNull()) {
225 TQString url = file.readURL();
227 if (url.left(1) !=
"/" || url.left(6) !=
"file:/") {
228 TQString path = (*it).left((*it).findRev(
'/', -1)+1);
231 m_reportList->insertItem(
new ReportItem(m_reportList, name, url));
237void ReportView::draw(
const TQString &report) {
239 m_reportview->clearReport();
240 m_reportTags =
new ReportTagsPrivate();
241 getTemplateFile(report);
242 m_reportview->setReportTemplate(templateDoc.toString());
244 m_reportview->renderReport();
245 m_reportview->show();
248 enableNavigationBtn();
251void ReportView::setup(KPrinter &printer) {
253 m_reportview->setupPrinter(printer);
256void ReportView::print(KPrinter &printer) {
258 m_reportview->printReport(printer);
262void ReportView::setReportData() {
263 TQString s =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
265 s += setReportDetail();
268 m_reportview->setReportData(s);
271TQString ReportView::setReportDetail() {
274 if (m_reportTags->alltasksLevel !=
"-1") {
276 if (m_reportTags->summarytasksLevel ==
"-1") {
277 m_reportTags->summarytasksLevel = m_reportTags->alltasksLevel;
278 m_reportTags->summarytasksProps = m_reportTags->alltasksProps;
280 if (m_reportTags->tasksLevel ==
"-1") {
281 m_reportTags->tasksLevel = m_reportTags->alltasksLevel;
282 m_reportTags->tasksProps = m_reportTags->alltasksProps;
284 if (m_reportTags->milestonesLevel ==
"-1") {
285 m_reportTags->milestonesLevel = m_reportTags->alltasksLevel;
286 m_reportTags->milestonesProps = m_reportTags->alltasksProps;
288 s+= setTaskChildren(&(mainView()->getProject()));
290 }
else if (m_reportTags->summarytasksLevel ==
"0") {
292 TQPtrListIterator<Node> it(mainView()->getProject().childNodeIterator());
293 for (; it.current(); ++it) {
294 if (it.current()->type() == Node::Type_Summarytask) {
295 s += setTaskDetail(it.current());
297 s+= setTaskChildren(it.current());
301 }
else if (m_reportTags->tasksLevel ==
"0") {
303 TQPtrListIterator<Node> it(mainView()->getProject().childNodeIterator());
304 for (; it.current(); ++it) {
305 if (it.current()->type() == Node::Type_Task) {
306 s += setTaskDetail(it.current());
308 if (it.current()->type() == Node::Type_Summarytask) {
309 s+= setTaskChildren(it.current());
310 if (m_reportTags->summarytasksLevel !=
"-1") {
311 s += setTaskDetail(it.current());
316 }
else if (m_reportTags->milestonesLevel ==
"0") {
318 }
else if (m_reportTags->resourcegroupsLevel ==
"0") {
320 TQPtrListIterator<ResourceGroup> it(mainView()->getProject().resourceGroups());
321 for (; it.current(); ++it) {
322 s += setResourceGroupDetail(it.current());
325 }
else if (m_reportTags->resourcesLevel ==
"0") {
327 TQPtrListIterator<ResourceGroup> it(mainView()->getProject().resourceGroups());
328 for (; it.current(); ++it) {
329 TQPtrListIterator<Resource> rit(it.current()->resources());
330 for (; rit.current(); ++rit) {
331 s += setResourceDetail(rit.current());
339TQString ReportView::setResourceGroupDetail(ResourceGroup *group) {
342 if (m_reportTags->resourcegroupsLevel !=
"-1") {
343 m_reportTags->m_resourcegroup = group;
345 s = setDetail(
"resourcegroup", m_reportTags->resourcegroupsProps, m_reportTags->resourcegroupsLevel);
346 TQPtrListIterator<Resource> rit(group->resources());
347 for (; rit.current(); ++rit) {
348 s += setResourceDetail(rit.current());
354TQString ReportView::setResourceDetail(Resource *res) {
357 if (m_reportTags->resourcesLevel !=
"-1") {
358 m_reportTags->m_resource = res;
360 s = setDetail(
"resource", m_reportTags->resourcesProps, m_reportTags->resourcesLevel);
365TQString ReportView::setTaskChildren(Node *node) {
368 TQPtrListIterator<Node> it(node->childNodeIterator());
369 for (; it.current(); ++it) {
370 s += setTaskDetail(it.current());
371 if (it.current()->type() == Node::Type_Summarytask)
372 s+= setTaskChildren(it.current());
377TQString ReportView::setTaskDetail(Node *node) {
381 TQString level =
"-1";
382 if (node->type() == Node::Type_Task) {
383 props = m_reportTags->tasksProps;
384 level = m_reportTags->tasksLevel;
385 }
else if (node->type() == Node::Type_Summarytask) {
386 props = m_reportTags->summarytasksProps;
387 level = m_reportTags->summarytasksLevel;
388 }
else if (node->type() == Node::Type_Milestone) {
389 props = m_reportTags->milestonesProps;
390 level = m_reportTags->milestonesLevel;
393 m_reportTags->m_task =
static_cast<Task *
>(node);
394 s = setDetail(
"task", props, level);
399TQString ReportView::setDetail(
const TQString & source, TQStringList &properties, TQString &level) {
401 s +=
" level=\"" + level +
"\"";
402 for (
unsigned int i=0; i < properties.count(); ++i) {
404 s +=
" " + properties[i].section(
'=', 0, 0) +
"=";
405 TQString data = m_reportTags->getData(source, properties[i].section(
'=', 1, 1));
408 data = data.replace(
'<',
"<");
409 data = data.replace(
'>',
">");
410 data = data.replace(
'"',
""");
412 s +=
"\"" + data +
"\"";
420void ReportView::openTemplateFile(
const TQString &file) {
421 if (!TQFileInfo(file).isFile()) {
422 KMessageBox::sorry(
this, i18n(
"Cannot find report template file!"),
423 i18n(
"Generate Report"));
428 if (!in.open(IO_ReadOnly)) {
429 KMessageBox::sorry(
this, i18n(
"Cannot open report template file!"),
430 i18n(
"Generate Report"));
435 if ( in.readBlock( buf, 4 ) < 4 )
438 KMessageBox::sorry(
this, i18n(
"Cannot read report template file!"),
439 i18n(
"Generate Report"));
443 if (strncasecmp( buf,
"<?xm", 4 ) == 0) {
452 KoStore* store=KoStore::createStore(file, KoStore::Read);
455 KMessageBox::sorry(
this, i18n(
"Cannot open report template file!"),
456 i18n(
"Generate Report"));
459 bool b = store->open(
"maindoc.xml");
462 KMessageBox::sorry(
this, i18n(
"Cannot find the proper report template file!"),
463 i18n(
"Generate Report"));
467 loadTemplate(*(store->device()));
471void ReportView::loadTemplate(TQIODevice &dev) {
475 if (!templateDoc.setContent( &dev , &errorMsg, &errorLine, &errorColumn)) {
476 TQString msg =
"Parsing template file failed with ";
477 KMessageBox::sorry(
this, msg + errorMsg, i18n(
"Generate Report"));
480 loadTemplate(templateDoc);
483void ReportView::loadTemplate(TQDomDocument &doc) {
486 for (tpl = doc.firstChild(); !tpl.isNull(); tpl = tpl.nextSibling())
487 if (tpl.nodeName() ==
"KugarTemplate")
493 m_reportTags->m_project = &(mainView()->getPart()->getProject());
495 TQDomNodeList children = tpl.childNodes();
496 int childCount = children.length();
498 for(
int j = 0; j < childCount; j++){
499 child = children.item(j);
500 if(child.nodeType() == TQDomNode::ElementNode) {
501 TQDomElement e = child.toElement();
504 if(child.nodeName() ==
"ReportHeader") {
506 }
else if (child.nodeName() ==
"PageHeader") {
508 }
else if(child.nodeName() ==
"DetailHeader") {
510 }
else if(child.nodeName() ==
"Detail") {
512 }
else if(child.nodeName() ==
"DetailFooter") {
514 }
else if(child.nodeName() ==
"PageFooter") {
516 }
else if(child.nodeName() ==
"ReportFooter") {
518 }
else if(child.nodeName() ==
"KPlato") {
525void ReportView::handleHeader(TQDomNode &node) {
527 TQDomNodeList children = node.childNodes();
528 int childCount = children.length();
529 for (
int j = 0; j < childCount; j++) {
530 child = children.item(j);
531 if (child.nodeName() ==
"Label") {
532 TQDomNode n = child.attributes().namedItem(
"Text");
533 TQString s = n.nodeValue();
536 s = i18n(n.nodeValue().latin1());
542 if ( ((i = s.find(
'[', i)) != -1) && ((j = s.find(
']', i+1)) != -1) ) {
543 TQString tag = s.mid(i, j-i+1);
544 TQString data = m_reportTags->getData(tag.mid(1, tag.length()-2));
545 r = r.replace(tag, data);
547 }
while (i != -1 && j != -1);
550 }
else if (child.nodeName() ==
"Field") {
551 TQDomElement e = child.toElement();
552 if (!e.isElement()) {
555 TQString s = e.attribute(
"Field");
556 TQString data = m_reportTags->getData(s);
557 e.setAttribute(
"Text", data);
563TQStringList ReportView::getProperties(TQDomElement &elem) {
565 TQDomNodeList list(elem.childNodes());
566 int childCount = list.length();
567 for (
int j = 0; j < childCount; j++) {
568 TQDomNode child = list.item(j);
569 if (child.nodeName() ==
"Field") {
570 props.append(child.attributes().namedItem(
"Field").nodeValue()+
"="+child.attributes().namedItem(
"Field").nodeValue());
576void ReportView::handleKPlato(TQDomElement &elem) {
577 TQDomNodeList list(elem.childNodes());
578 int childCount = list.length();
579 for (
int j = 0; j < childCount; j++) {
580 TQDomNode child = list.item(j);
581 if (child.nodeName() ==
"Detail") {
582 TQDomElement e = child.toElement();
583 if (!e.isElement()) {
586 TQString source = e.attribute(
"SelectFrom");
587 TQString level = e.attribute(
"Level",
"-1");
589 if (source.isNull() || level ==
"-1")
592 TQStringList list = TQStringList::split(
" ", source);
593 TQStringList::iterator it = list.begin();
594 for (; it != list.end(); ++it) {
596 if ((*it) ==
"alltasks") {
597 m_reportTags->alltasksLevel = level;
599 if ((*it) ==
"summarytasks") {
600 m_reportTags->summarytasksLevel = level;
602 if ((*it) ==
"tasks") {
603 m_reportTags->tasksLevel = level;
605 if ((*it) ==
"milestones") {
606 m_reportTags->milestonesLevel = level;
608 if ((*it) ==
"resourcegroups") {
609 m_reportTags->resourcegroupsLevel = level;
611 if ((*it) ==
"resources") {
612 m_reportTags->resourcesLevel = level;
619void ReportView::handleDetail(TQDomElement &elem) {
621 TQString level = elem.attribute(
"Level",
"-1");
626 if (level == m_reportTags->alltasksLevel) {
627 m_reportTags->alltasksProps = getProperties(elem);
629 if (level == m_reportTags->summarytasksLevel) {
630 m_reportTags->summarytasksProps = getProperties(elem);
632 if (level == m_reportTags->tasksLevel) {
633 m_reportTags->tasksProps = getProperties(elem);
635 if (level == m_reportTags->milestonesLevel) {
636 m_reportTags->milestonesProps = getProperties(elem);
638 if (level == m_reportTags->resourcegroupsLevel) {
639 m_reportTags->resourcegroupsProps = getProperties(elem);
641 if (level == m_reportTags->resourcesLevel) {
642 m_reportTags->resourcesProps = getProperties(elem);
646void ReportView::replaceTags(TQDomNode &node) {
651void ReportView::getTemplateFile(
const TQString &tpl) {
659 KMessageBox::sorry(
this,i18n(
"Malformed template filename: %1").arg(url.prettyURL()));
663 if (TDEIO::NetAccess::download(url,localtpl,
this))
666 KMessageBox::sorry(
this,i18n(
"Unable to download template file: %1").arg(url.prettyURL()));
669 if (!localtpl.isNull())
671 openTemplateFile(localtpl);
673 TDEIO::NetAccess::removeTempFile(localtpl);
677void ReportView::enableNavigationBtn() {
679 emit setFirstPageActionEnabled(m_reportview->currentPage() > 0);
680 emit setNextPageActionEnabled(m_reportview->currentPage() < m_reportview->pageCount()-1);
681 emit setPriorPageActionEnabled(m_reportview->currentPage() > 0);
682 emit setLastPageActionEnabled(m_reportview->currentPage() < m_reportview->pageCount()-1);
684void ReportView::slotFirstPage() {
685 m_reportview->slotFirstPage();
686 enableNavigationBtn();
689void ReportView::slotNextPage() {
690 m_reportview->slotNextPage();
691 enableNavigationBtn();
694void ReportView::slotPrevPage() {
695 m_reportview->slotPrevPage();
696 enableNavigationBtn();
699void ReportView::slotLastPage() {
700 m_reportview->slotLastPage();
701 enableNavigationBtn();
704bool ReportView::setContext(Context::Reportview &context) {
710void ReportView::getContext(Context::Reportview &context)
const {
715void ReportView::slotReportListClicked(TQListViewItem* item) {
716 if (item == m_reportList->selectedItem())
717 slotReportListSelectionChanged(item);
720void ReportView::slotReportListSelectionChanged(TQListViewItem* item) {
721 ReportItem *ri =
dynamic_cast<ReportItem*
>(item);
730#include "kptreportview.moc"