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#if KDE_IS_VERSION(3,3,9)
181 m_reportList->setShadeSortColumn(
false);
183 m_reportList->addColumn(i18n(
"Report Template"));
184 m_reportList->header()->setStretchEnabled(
true, 0);
185 m_reportList->header()->setSortIndicator(0);
187 m_reportview =
new KugarReportViewer(
this);
191 connect(m_reportList, TQ_SIGNAL(clicked(TQListViewItem*)), TQ_SLOT(slotReportListClicked(TQListViewItem*)));
192 connect(m_reportList, TQ_SIGNAL(selectionChanged(TQListViewItem*)), TQ_SLOT(slotReportListSelectionChanged(TQListViewItem*)));
210ReportView::~ReportView() {
215void ReportView::initReportList() {
219 m_reportList->clear();
221 TQStringList reportDesktopFiles = std.findAllResources(
"data",
"kplato/reports/*.desktop",
true,
true);
222 for (TQStringList::iterator it = reportDesktopFiles.begin(); it != reportDesktopFiles.end(); ++it) {
223 KDesktopFile file((*it),
true);
224 TQString name = file.readName();
225 if (!name.isNull()) {
227 TQString url = file.readURL();
229 if (url.left(1) !=
"/" || url.left(6) !=
"file:/") {
230 TQString path = (*it).left((*it).findRev(
'/', -1)+1);
233 m_reportList->insertItem(
new ReportItem(m_reportList, name, url));
239void ReportView::draw(
const TQString &report) {
241 m_reportview->clearReport();
242 m_reportTags =
new ReportTagsPrivate();
243 getTemplateFile(report);
244 m_reportview->setReportTemplate(templateDoc.toString());
246 m_reportview->renderReport();
247 m_reportview->show();
250 enableNavigationBtn();
253void ReportView::setup(KPrinter &printer) {
255 m_reportview->setupPrinter(printer);
258void ReportView::print(KPrinter &printer) {
260 m_reportview->printReport(printer);
264void ReportView::setReportData() {
265 TQString s =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
267 s += setReportDetail();
270 m_reportview->setReportData(s);
273TQString ReportView::setReportDetail() {
276 if (m_reportTags->alltasksLevel !=
"-1") {
278 if (m_reportTags->summarytasksLevel ==
"-1") {
279 m_reportTags->summarytasksLevel = m_reportTags->alltasksLevel;
280 m_reportTags->summarytasksProps = m_reportTags->alltasksProps;
282 if (m_reportTags->tasksLevel ==
"-1") {
283 m_reportTags->tasksLevel = m_reportTags->alltasksLevel;
284 m_reportTags->tasksProps = m_reportTags->alltasksProps;
286 if (m_reportTags->milestonesLevel ==
"-1") {
287 m_reportTags->milestonesLevel = m_reportTags->alltasksLevel;
288 m_reportTags->milestonesProps = m_reportTags->alltasksProps;
290 s+= setTaskChildren(&(mainView()->getProject()));
292 }
else if (m_reportTags->summarytasksLevel ==
"0") {
294 TQPtrListIterator<Node> it(mainView()->getProject().childNodeIterator());
295 for (; it.current(); ++it) {
296 if (it.current()->type() == Node::Type_Summarytask) {
297 s += setTaskDetail(it.current());
299 s+= setTaskChildren(it.current());
303 }
else if (m_reportTags->tasksLevel ==
"0") {
305 TQPtrListIterator<Node> it(mainView()->getProject().childNodeIterator());
306 for (; it.current(); ++it) {
307 if (it.current()->type() == Node::Type_Task) {
308 s += setTaskDetail(it.current());
310 if (it.current()->type() == Node::Type_Summarytask) {
311 s+= setTaskChildren(it.current());
312 if (m_reportTags->summarytasksLevel !=
"-1") {
313 s += setTaskDetail(it.current());
318 }
else if (m_reportTags->milestonesLevel ==
"0") {
320 }
else if (m_reportTags->resourcegroupsLevel ==
"0") {
322 TQPtrListIterator<ResourceGroup> it(mainView()->getProject().resourceGroups());
323 for (; it.current(); ++it) {
324 s += setResourceGroupDetail(it.current());
327 }
else if (m_reportTags->resourcesLevel ==
"0") {
329 TQPtrListIterator<ResourceGroup> it(mainView()->getProject().resourceGroups());
330 for (; it.current(); ++it) {
331 TQPtrListIterator<Resource> rit(it.current()->resources());
332 for (; rit.current(); ++rit) {
333 s += setResourceDetail(rit.current());
341TQString ReportView::setResourceGroupDetail(ResourceGroup *group) {
344 if (m_reportTags->resourcegroupsLevel !=
"-1") {
345 m_reportTags->m_resourcegroup = group;
347 s = setDetail(
"resourcegroup", m_reportTags->resourcegroupsProps, m_reportTags->resourcegroupsLevel);
348 TQPtrListIterator<Resource> rit(group->resources());
349 for (; rit.current(); ++rit) {
350 s += setResourceDetail(rit.current());
356TQString ReportView::setResourceDetail(Resource *res) {
359 if (m_reportTags->resourcesLevel !=
"-1") {
360 m_reportTags->m_resource = res;
362 s = setDetail(
"resource", m_reportTags->resourcesProps, m_reportTags->resourcesLevel);
367TQString ReportView::setTaskChildren(Node *node) {
370 TQPtrListIterator<Node> it(node->childNodeIterator());
371 for (; it.current(); ++it) {
372 s += setTaskDetail(it.current());
373 if (it.current()->type() == Node::Type_Summarytask)
374 s+= setTaskChildren(it.current());
379TQString ReportView::setTaskDetail(Node *node) {
383 TQString level =
"-1";
384 if (node->type() == Node::Type_Task) {
385 props = m_reportTags->tasksProps;
386 level = m_reportTags->tasksLevel;
387 }
else if (node->type() == Node::Type_Summarytask) {
388 props = m_reportTags->summarytasksProps;
389 level = m_reportTags->summarytasksLevel;
390 }
else if (node->type() == Node::Type_Milestone) {
391 props = m_reportTags->milestonesProps;
392 level = m_reportTags->milestonesLevel;
395 m_reportTags->m_task =
static_cast<Task *
>(node);
396 s = setDetail(
"task", props, level);
401TQString ReportView::setDetail(
const TQString & source, TQStringList &properties, TQString &level) {
403 s +=
" level=\"" + level +
"\"";
404 for (
unsigned int i=0; i < properties.count(); ++i) {
406 s +=
" " + properties[i].section(
'=', 0, 0) +
"=";
407 TQString data = m_reportTags->getData(source, properties[i].section(
'=', 1, 1));
410 data = data.replace(
'<',
"<");
411 data = data.replace(
'>',
">");
412 data = data.replace(
'"',
""");
414 s +=
"\"" + data +
"\"";
422void ReportView::openTemplateFile(
const TQString &file) {
423 if (!TQFileInfo(file).isFile()) {
424 KMessageBox::sorry(
this, i18n(
"Cannot find report template file!"),
425 i18n(
"Generate Report"));
430 if (!in.open(IO_ReadOnly)) {
431 KMessageBox::sorry(
this, i18n(
"Cannot open report template file!"),
432 i18n(
"Generate Report"));
437 if ( in.readBlock( buf, 4 ) < 4 )
440 KMessageBox::sorry(
this, i18n(
"Cannot read report template file!"),
441 i18n(
"Generate Report"));
445 if (strncasecmp( buf,
"<?xm", 4 ) == 0) {
454 KoStore* store=KoStore::createStore(file, KoStore::Read);
457 KMessageBox::sorry(
this, i18n(
"Cannot open report template file!"),
458 i18n(
"Generate Report"));
461 bool b = store->open(
"maindoc.xml");
464 KMessageBox::sorry(
this, i18n(
"Cannot find the proper report template file!"),
465 i18n(
"Generate Report"));
469 loadTemplate(*(store->device()));
473void ReportView::loadTemplate(TQIODevice &dev) {
477 if (!templateDoc.setContent( &dev , &errorMsg, &errorLine, &errorColumn)) {
478 TQString msg =
"Parsing template file failed with ";
479 KMessageBox::sorry(
this, msg + errorMsg, i18n(
"Generate Report"));
482 loadTemplate(templateDoc);
485void ReportView::loadTemplate(TQDomDocument &doc) {
488 for (tpl = doc.firstChild(); !tpl.isNull(); tpl = tpl.nextSibling())
489 if (tpl.nodeName() ==
"KugarTemplate")
495 m_reportTags->m_project = &(mainView()->getPart()->getProject());
497 TQDomNodeList children = tpl.childNodes();
498 int childCount = children.length();
500 for(
int j = 0; j < childCount; j++){
501 child = children.item(j);
502 if(child.nodeType() == TQDomNode::ElementNode) {
503 TQDomElement e = child.toElement();
506 if(child.nodeName() ==
"ReportHeader") {
508 }
else if (child.nodeName() ==
"PageHeader") {
510 }
else if(child.nodeName() ==
"DetailHeader") {
512 }
else if(child.nodeName() ==
"Detail") {
514 }
else if(child.nodeName() ==
"DetailFooter") {
516 }
else if(child.nodeName() ==
"PageFooter") {
518 }
else if(child.nodeName() ==
"ReportFooter") {
520 }
else if(child.nodeName() ==
"KPlato") {
527void ReportView::handleHeader(TQDomNode &node) {
529 TQDomNodeList children = node.childNodes();
530 int childCount = children.length();
531 for (
int j = 0; j < childCount; j++) {
532 child = children.item(j);
533 if (child.nodeName() ==
"Label") {
534 TQDomNode n = child.attributes().namedItem(
"Text");
535 TQString s = n.nodeValue();
538 s = i18n(n.nodeValue().latin1());
544 if ( ((i = s.find(
'[', i)) != -1) && ((j = s.find(
']', i+1)) != -1) ) {
545 TQString tag = s.mid(i, j-i+1);
546 TQString data = m_reportTags->getData(tag.mid(1, tag.length()-2));
547 r = r.replace(tag, data);
549 }
while (i != -1 && j != -1);
552 }
else if (child.nodeName() ==
"Field") {
553 TQDomElement e = child.toElement();
554 if (!e.isElement()) {
557 TQString s = e.attribute(
"Field");
558 TQString data = m_reportTags->getData(s);
559 e.setAttribute(
"Text", data);
565TQStringList ReportView::getProperties(TQDomElement &elem) {
567 TQDomNodeList list(elem.childNodes());
568 int childCount = list.length();
569 for (
int j = 0; j < childCount; j++) {
570 TQDomNode child = list.item(j);
571 if (child.nodeName() ==
"Field") {
572 props.append(child.attributes().namedItem(
"Field").nodeValue()+
"="+child.attributes().namedItem(
"Field").nodeValue());
578void ReportView::handleKPlato(TQDomElement &elem) {
579 TQDomNodeList list(elem.childNodes());
580 int childCount = list.length();
581 for (
int j = 0; j < childCount; j++) {
582 TQDomNode child = list.item(j);
583 if (child.nodeName() ==
"Detail") {
584 TQDomElement e = child.toElement();
585 if (!e.isElement()) {
588 TQString source = e.attribute(
"SelectFrom");
589 TQString level = e.attribute(
"Level",
"-1");
591 if (source.isNull() || level ==
"-1")
594 TQStringList list = TQStringList::split(
" ", source);
595 TQStringList::iterator it = list.begin();
596 for (; it != list.end(); ++it) {
598 if ((*it) ==
"alltasks") {
599 m_reportTags->alltasksLevel = level;
601 if ((*it) ==
"summarytasks") {
602 m_reportTags->summarytasksLevel = level;
604 if ((*it) ==
"tasks") {
605 m_reportTags->tasksLevel = level;
607 if ((*it) ==
"milestones") {
608 m_reportTags->milestonesLevel = level;
610 if ((*it) ==
"resourcegroups") {
611 m_reportTags->resourcegroupsLevel = level;
613 if ((*it) ==
"resources") {
614 m_reportTags->resourcesLevel = level;
621void ReportView::handleDetail(TQDomElement &elem) {
623 TQString level = elem.attribute(
"Level",
"-1");
628 if (level == m_reportTags->alltasksLevel) {
629 m_reportTags->alltasksProps = getProperties(elem);
631 if (level == m_reportTags->summarytasksLevel) {
632 m_reportTags->summarytasksProps = getProperties(elem);
634 if (level == m_reportTags->tasksLevel) {
635 m_reportTags->tasksProps = getProperties(elem);
637 if (level == m_reportTags->milestonesLevel) {
638 m_reportTags->milestonesProps = getProperties(elem);
640 if (level == m_reportTags->resourcegroupsLevel) {
641 m_reportTags->resourcegroupsProps = getProperties(elem);
643 if (level == m_reportTags->resourcesLevel) {
644 m_reportTags->resourcesProps = getProperties(elem);
648void ReportView::replaceTags(TQDomNode &node) {
653void ReportView::getTemplateFile(
const TQString &tpl) {
661 KMessageBox::sorry(
this,i18n(
"Malformed template filename: %1").arg(url.prettyURL()));
665 if (TDEIO::NetAccess::download(url,localtpl,
this))
668 KMessageBox::sorry(
this,i18n(
"Unable to download template file: %1").arg(url.prettyURL()));
671 if (!localtpl.isNull())
673 openTemplateFile(localtpl);
675 TDEIO::NetAccess::removeTempFile(localtpl);
679void ReportView::enableNavigationBtn() {
681 emit setFirstPageActionEnabled(m_reportview->currentPage() > 0);
682 emit setNextPageActionEnabled(m_reportview->currentPage() < m_reportview->pageCount()-1);
683 emit setPriorPageActionEnabled(m_reportview->currentPage() > 0);
684 emit setLastPageActionEnabled(m_reportview->currentPage() < m_reportview->pageCount()-1);
686void ReportView::slotFirstPage() {
687 m_reportview->slotFirstPage();
688 enableNavigationBtn();
691void ReportView::slotNextPage() {
692 m_reportview->slotNextPage();
693 enableNavigationBtn();
696void ReportView::slotPrevPage() {
697 m_reportview->slotPrevPage();
698 enableNavigationBtn();
701void ReportView::slotLastPage() {
702 m_reportview->slotLastPage();
703 enableNavigationBtn();
706bool ReportView::setContext(Context::Reportview &context) {
712void ReportView::getContext(Context::Reportview &context)
const {
717void ReportView::slotReportListClicked(TQListViewItem* item) {
718 if (item == m_reportList->selectedItem())
719 slotReportListSelectionChanged(item);
722void ReportView::slotReportListSelectionChanged(TQListViewItem* item) {
723 ReportItem *ri =
dynamic_cast<ReportItem*
>(item);
732#include "kptreportview.moc"