Changeset View
Changeset View
Standalone View
Standalone View
kmymoney/plugins/views/reports/core/listtable.cpp
Show All 16 Lines | |||||
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "listtable.h" | 20 | #include "listtable.h" | ||
21 | 21 | | |||
22 | // ---------------------------------------------------------------------------- | 22 | // ---------------------------------------------------------------------------- | ||
23 | // QT Includes | 23 | // QT Includes | ||
24 | 24 | | |||
25 | #include <QDomDocument> | ||||
26 | #include <QDomElement> | ||||
27 | #include <QDomNamedNodeMap> | ||||
25 | #include <QList> | 28 | #include <QList> | ||
26 | #include <QFile> | 29 | #include <QFile> | ||
27 | #include <QTextStream> | 30 | #include <QTextStream> | ||
28 | #include <KLocalizedString> | 31 | #include <KLocalizedString> | ||
29 | 32 | | |||
30 | // ---------------------------------------------------------------------------- | 33 | // ---------------------------------------------------------------------------- | ||
31 | // KDE Includes | 34 | // KDE Includes | ||
32 | // This is just needed for i18n(). Once I figure out how to handle i18n | 35 | // This is just needed for i18n(). Once I figure out how to handle i18n | ||
▲ Show 20 Lines • Show All 429 Lines • ▼ Show 20 Line(s) | 462 | { | |||
462 | 465 | | |||
463 | if (! context.isEmpty()) | 466 | if (! context.isEmpty()) | ||
464 | QTextStream(&g) << context.arg(renderHTML()); | 467 | QTextStream(&g) << context.arg(renderHTML()); | ||
465 | else | 468 | else | ||
466 | QTextStream(&g) << renderHTML(); | 469 | QTextStream(&g) << renderHTML(); | ||
467 | g.close(); | 470 | g.close(); | ||
468 | } | 471 | } | ||
469 | 472 | | |||
473 | bool ListTable::saveToXml(const QString &file) | ||||
474 | { | ||||
475 | QFile out(file); | ||||
476 | if (!out.open(QIODevice::WriteOnly)) | ||||
477 | return false; | ||||
478 | QTextStream stream(&out); | ||||
479 | stream << toXml(); | ||||
480 | return true; | ||||
481 | } | ||||
482 | | ||||
483 | bool ListTable::loadFromXml(const QString &file) | ||||
484 | { | ||||
485 | QFile in(file); | ||||
486 | if (!in.open(QIODevice::ReadOnly)) | ||||
487 | return false; | ||||
488 | | ||||
489 | QDomDocument doc; | ||||
490 | doc.setContent(in.readAll()); | ||||
491 | QDomElement docElem = doc.documentElement(); | ||||
492 | QDomNode n = docElem.firstChild(); | ||||
493 | m_rows.clear(); | ||||
494 | int line = 0; | ||||
495 | while(!n.isNull()) { | ||||
496 | QDomElement e = n.toElement(); // try to convert the node to an element. | ||||
497 | if(!e.isNull()) { | ||||
498 | line++; | ||||
499 | ListTable::TableRow row; | ||||
500 | QDomNamedNodeMap attributes = e.attributes(); | ||||
501 | for (int i = 0; i < attributes.count(); ++i) { | ||||
502 | const QDomAttr& attr = attributes.item(i).toAttr(); | ||||
503 | //qDebug() << line << attr.name() << attr.value(); | ||||
504 | row[attr.name()] = attr.value(); | ||||
habacker: This does not work anymore because the map key type has been changed | |||||
505 | } | ||||
506 | m_rows.append(row); | ||||
507 | } | ||||
508 | n = n.nextSibling(); | ||||
509 | } | ||||
510 | return true; | ||||
511 | } | ||||
512 | | ||||
513 | /** | ||||
514 | * The class KDomElement is a replacement for QDomElement | ||||
515 | * with the possibility to store attributes sorted. | ||||
516 | * | ||||
517 | * Sorted attributes are important for a textual comparison. | ||||
518 | * | ||||
519 | * @author Ralf Habacker <ralf.habacker@freenet.de> | ||||
520 | */ | ||||
521 | class KDomElement { | ||||
522 | public: | ||||
523 | KDomElement(const QString &name = QString()) : m_tag(name) {} | ||||
524 | void setAttribute(const QString &name, const QString &value) | ||||
525 | { | ||||
526 | m_attributes.append(QString("%1=\"%2\"").arg(name, value)); | ||||
527 | } | ||||
528 | | ||||
529 | virtual ~KDomElement() | ||||
530 | { | ||||
531 | } | ||||
532 | | ||||
533 | void appendChild(KDomElement &element) | ||||
534 | { | ||||
535 | m_childs.append(element); | ||||
536 | } | ||||
537 | | ||||
538 | virtual QString toString() const | ||||
539 | { | ||||
540 | if (m_childs.size() > 0) { | ||||
541 | QString s = !m_tag.isEmpty() ? QString("<%1 %2>\n").arg(m_tag, m_attributes.join(" ")) : ""; | ||||
542 | foreach(const KDomElement &child, m_childs) { | ||||
543 | s += child.toString(); | ||||
544 | } | ||||
545 | s += !m_tag.isEmpty() ? QString("</%1>\n").arg(m_tag) : ""; | ||||
546 | return s; | ||||
547 | } else | ||||
548 | return !m_tag.isEmpty() ? QString("<%1 %2 />\n").arg(m_tag, m_attributes.join(" ")) : ""; | ||||
549 | } | ||||
550 | | ||||
551 | protected: | ||||
552 | QString m_tag; | ||||
553 | QStringList m_attributes; | ||||
554 | QList<KDomElement> m_childs; | ||||
555 | }; | ||||
556 | | ||||
557 | /** | ||||
558 | * The class KDomDocument is a simple replacement for QDomDocument. | ||||
559 | * | ||||
560 | * @author Ralf Habacker <ralf.habacker@freenet.de> | ||||
561 | */ | ||||
562 | class KDomDocument : public KDomElement { | ||||
563 | public: | ||||
564 | KDomDocument(const QString &name=QString()) | ||||
565 | : KDomElement(""), | ||||
566 | m_type(name) | ||||
567 | { | ||||
568 | } | ||||
569 | | ||||
570 | virtual ~KDomDocument() | ||||
571 | { | ||||
572 | } | ||||
573 | | ||||
574 | KDomElement createElement(const QString &name) | ||||
575 | { | ||||
576 | return KDomElement(name); | ||||
577 | } | ||||
578 | | ||||
579 | QString toString() const | ||||
580 | { | ||||
581 | QString s = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; | ||||
582 | if (!m_type.isEmpty()) | ||||
583 | s.append(QString("<!DOCTYPE %1>\n").arg(m_type)); | ||||
584 | s.append(KDomElement::toString()); | ||||
585 | return s; | ||||
586 | } | ||||
587 | | ||||
588 | QString m_type; | ||||
589 | }; | ||||
590 | | ||||
591 | QString ListTable::toXml() | ||||
592 | { | ||||
593 | KDomDocument doc; | ||||
594 | KDomElement el = doc.createElement("ListTable"); | ||||
595 | QString name = m_config.name(); | ||||
596 | el.setAttribute("name", name); | ||||
597 | | ||||
598 | foreach(ListTable::TableRow row, m_rows) { | ||||
599 | KDomElement r = doc.createElement("TableRow"); | ||||
600 | QStringList keys = row.keys(); | ||||
habacker: this also does not work. Any hints how to fix this are welcome. | |||||
601 | keys.sort(); | ||||
602 | foreach(const QString &key, keys) { | ||||
603 | r.setAttribute(key, row[key]); | ||||
604 | } | ||||
605 | el.appendChild(r); | ||||
606 | } | ||||
607 | doc.appendChild(el); | ||||
608 | return doc.toString(); | ||||
609 | } | ||||
610 | | ||||
470 | void ListTable::includeInvestmentSubAccounts() | 611 | void ListTable::includeInvestmentSubAccounts() | ||
471 | { | 612 | { | ||
472 | // if we're not in expert mode, we need to make sure | 613 | // if we're not in expert mode, we need to make sure | ||
473 | // that all stock accounts for the selected investment | 614 | // that all stock accounts for the selected investment | ||
474 | // account are also selected. | 615 | // account are also selected. | ||
475 | // In case we get called for a non investment only report we quit | 616 | // In case we get called for a non investment only report we quit | ||
476 | if (KMyMoneySettings::expertMode() || !m_config.isInvestmentsOnly()) { | 617 | if (KMyMoneySettings::expertMode() || !m_config.isInvestmentsOnly()) { | ||
477 | return; | 618 | return; | ||
▲ Show 20 Lines • Show All 266 Lines • Show Last 20 Lines |
This does not work anymore because the map key type has been changed