File indexing completed on 2024-05-26 05:10:43

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 /** @file
0007  * This file is Skrooge plugin for bank management.
0008  *
0009  * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include "skgreportboardwidget.h"
0012 
0013 #include "skgdocumentbank.h"
0014 #include "skgmainpanel.h"
0015 #include "skgreportpluginwidget.h"
0016 #include "skgtraces.h"
0017 
0018 SKGReportBoardWidget::SKGReportBoardWidget(QWidget* iParent, SKGDocumentBank* iDocument)
0019     : SKGBoardWidget(iParent, iDocument, i18nc("Dashboard widget title", "Report"), true)
0020 {
0021     SKGTRACEINFUNC(10)
0022 
0023     // This must be done at the beginning
0024     this->setMinimumSize(200, 200);
0025 
0026     // Create menu
0027     setContextMenuPolicy(Qt::ActionsContextMenu);
0028 
0029     QStringList overlayopen;
0030     overlayopen.push_back(QStringLiteral("quickopen"));
0031     auto open = new QAction(SKGServices::fromTheme(QStringLiteral("view-statistics"), overlayopen), i18nc("Verb", "Open report…"), this);
0032     connect(open, &QAction::triggered, this, &SKGReportBoardWidget::onOpen);
0033     addAction(open);
0034 
0035     m_graph = new SKGReportPluginWidget(iParent, iDocument, true);
0036     setMainWidget(m_graph);
0037 
0038     // Refresh
0039     connect(getDocument(), &SKGDocument::tableModified, this, &SKGReportBoardWidget::dataModified, Qt::QueuedConnection);
0040 }
0041 
0042 SKGReportBoardWidget::~SKGReportBoardWidget()
0043 {
0044     SKGTRACEINFUNC(10)
0045     m_graph = nullptr;
0046 }
0047 
0048 QString SKGReportBoardWidget::getState()
0049 {
0050     QDomDocument doc(QStringLiteral("SKGML"));
0051     doc.setContent(SKGBoardWidget::getState());
0052     QDomElement root = doc.documentElement();
0053 
0054     if (m_graph != nullptr) {
0055         root.setAttribute(QStringLiteral("graph"), m_graph->getState());
0056     }
0057     return doc.toString();
0058 }
0059 
0060 void SKGReportBoardWidget::setState(const QString& iState)
0061 {
0062     SKGBoardWidget::setState(iState);
0063 
0064     QDomDocument doc(QStringLiteral("SKGML"));
0065     if (doc.setContent(iState)) {
0066         QDomElement root = doc.documentElement();
0067 
0068         QString title = root.attribute(QStringLiteral("title"));
0069         if (!title.isEmpty()) {
0070             setMainTitle(title);
0071         }
0072 
0073 
0074         QString graphS = root.attribute(QStringLiteral("graph"));
0075         if (m_graph != nullptr) {
0076             if (graphS.isEmpty()) {
0077                 m_graph->setState(iState);
0078             } else {
0079                 m_graph->setState(graphS);
0080             }
0081         }
0082     }
0083 
0084     dataModified(QLatin1String(""), 0);
0085 }
0086 
0087 void SKGReportBoardWidget::dataModified(const QString& iTableName, int iIdTransaction)
0088 {
0089     SKGTRACEINFUNC(10)
0090     Q_UNUSED(iIdTransaction)
0091     if (iTableName == QStringLiteral("operation") || iTableName.isEmpty()) {
0092         bool exist = false;
0093         getDocument()->existObjects(QStringLiteral("account"), QLatin1String(""), exist);
0094         if (parentWidget() != nullptr) {
0095             setVisible(exist);
0096         }
0097     }
0098 }
0099 
0100 void SKGReportBoardWidget::onOpen()
0101 {
0102     QDomDocument doc(QStringLiteral("SKGML"));
0103     QString graphS;
0104     if (doc.setContent(getState())) {
0105         QDomElement root = doc.documentElement();
0106         graphS = root.attribute(QStringLiteral("graph"));
0107 
0108         QDomDocument doc2(QStringLiteral("SKGML"));
0109         if (doc2.setContent(graphS)) {
0110             QDomElement root2 = doc2.documentElement();
0111 
0112             QString currentPage = root2.attribute(QStringLiteral("currentPage"));
0113             if (SKGServices::stringToInt(currentPage) < -1) {
0114                 root2.setAttribute(QStringLiteral("currentPage"), QStringLiteral("-1"));
0115                 graphS = doc2.toString();
0116             }
0117         }
0118     }
0119     SKGMainPanel::getMainPanel()->openPage(SKGMainPanel::getMainPanel()->getPluginByName(QStringLiteral("Skrooge report plugin")), -1, graphS);
0120 }
0121 
0122