File indexing completed on 2024-04-21 04:41:48

0001 /* This file is part of the KDE project
0002    Copyright (C) 2015 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2.1 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "window.h"
0021 #include "DesignerWidget.h"
0022 #include "KReportExampleDataSource.h"
0023 
0024 #include <KReportRenderObjects>
0025 #include <KReportPluginManager>
0026 #include <KReportRendererBase>
0027 
0028 #include <QApplication>
0029 #include <QDebug>
0030 #include <QFile>
0031 #include <QMenu>
0032 #include <QMenuBar>
0033 #include <QSettings>
0034 #include <QSplitter>
0035 
0036 QDebug operator<<(QDebug dbg, const QDomNode& node)
0037 {
0038   QString s;
0039   QTextStream str(&s, QIODevice::WriteOnly);
0040   node.save(str, 2);
0041   dbg << qPrintable(s);
0042   return dbg;
0043 }
0044 
0045 Window::Window(QWidget *parent, Qt::WindowFlags flags)
0046     : QMainWindow(parent, flags)
0047 {
0048     QSplitter *centralWidget = new QSplitter(Qt::Vertical, this);
0049     m_designerWidget = new ReportDesignerWidget(centralWidget);
0050     m_designerWidget->createMainToolBar(this);
0051     m_designerWidget->createItemsToolBar(this);
0052     m_designerWidget->createPropertyEditorDockWidget(this, Qt::RightDockWidgetArea);
0053     connect(m_designerWidget, &ReportDesignerWidget::designChanged,
0054             this, &Window::showDesign);
0055 
0056     createMenus();
0057 
0058     m_reportView = new KReportView(centralWidget);
0059     setCentralWidget(centralWidget);
0060     centralWidget->setSizes(QList<int>() << height() / 2 << height() / 2);
0061 
0062 #if 0
0063     if (loadDocument()) {
0064         m_preRenderer = new KReportPreRenderer(m_document.documentElement());
0065         if (!m_preRenderer->isValid()) {
0066             return;
0067         }
0068     }
0069 #endif
0070 
0071     KReportPluginManager* manager = KReportPluginManager::self();
0072     //! @todo
0073     Q_UNUSED(manager);
0074 
0075     // restore visual settings
0076     QSettings settings;
0077     restoreGeometry(settings.value("MainWindow/geometry").toByteArray());
0078         restoreState(settings.value("MainWindow/windowState").toByteArray());
0079 
0080     showDesign(m_designerWidget->document());
0081 }
0082 
0083 Window::~Window()
0084 {
0085 }
0086 
0087 void Window::closeEvent(QCloseEvent *event)
0088 {
0089     // store visual settings
0090     QSettings settings;
0091     settings.setValue("MainWindow/geometry", saveGeometry());
0092     settings.setValue("MainWindow/windowState", saveState());
0093     QMainWindow::closeEvent(event);
0094 }
0095 
0096 bool Window::loadDocument()
0097 {
0098     //qDebug() << KREPORTEXAMPLE_DATA_DIR;
0099     QFile file(QLatin1String(KREPORTEXAMPLE_DATA_DIR) + "/report.xml");
0100     if (!file.open(QIODevice::ReadOnly)) {
0101         return false;
0102     }
0103     QString errorMsg;
0104     int errorLine;
0105     int errorColumn;
0106     if (!m_document.setContent(&file,
0107                                &errorMsg, &errorLine, &errorColumn))
0108     {
0109         qWarning() << "Error reading XML from" << file.fileName()
0110                    << "Message:" << errorMsg
0111                    << "Line:" << errorLine
0112                    << "Column:" << errorColumn;
0113         return false;
0114     }
0115     return true;
0116 }
0117 
0118 void Window::createMenus()
0119 {
0120     m_fileMenu = menuBar()->addMenu(tr("&File"));
0121     m_exitAction = new QAction(tr("E&xit"), this);
0122     m_exitAction->setShortcuts(QKeySequence::Quit);
0123     connect(m_exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
0124     m_fileMenu->addAction(m_exitAction);
0125 }
0126 
0127 void Window::showDesign(const QDomElement &design)
0128 {
0129     //qDebug() << design;
0130     KReportPreRenderer preRenderer(design);
0131     if (!preRenderer.isValid()) {
0132         return;
0133     }
0134 
0135     auto dataSource = new KReportExampleDataSource();
0136     preRenderer.setDataSource(dataSource);
0137     preRenderer.setScriptSource(dataSource);
0138     preRenderer.setName("example_report");
0139 
0140     if (preRenderer.generateDocument()) {
0141         m_reportView->setDocument(preRenderer.document());
0142         m_reportView->moveToFirstPage();
0143     }
0144 }