File: window.cpp

package info (click to toggle)
kreport 3.2.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,032 kB
  • sloc: cpp: 23,245; python: 779; xml: 197; sh: 108; makefile: 9
file content (142 lines) | stat: -rw-r--r-- 4,275 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* This file is part of the KDE project
   Copyright (C) 2015 Jarosław Staniek <staniek@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "window.h"
#include "DesignerWidget.h"
#include "KReportExampleDataSource.h"

#include <KReportRenderObjects>
#include <KReportPluginManager>
#include <KReportRendererBase>

#include <QApplication>
#include <QDebug>
#include <QFile>
#include <QMenu>
#include <QMenuBar>
#include <QSettings>
#include <QSplitter>

QDebug operator<<(QDebug dbg, const QDomNode& node)
{
  QString s;
  QTextStream str(&s, QIODevice::WriteOnly);
  node.save(str, 2);
  dbg << qPrintable(s);
  return dbg;
}

Window::Window(QWidget *parent, Qt::WindowFlags flags)
    : QMainWindow(parent, flags)
{
    QSplitter *centralWidget = new QSplitter(Qt::Vertical, this);
    m_designerWidget = new ReportDesignerWidget(centralWidget);
    m_designerWidget->createMainToolBar(this);
    m_designerWidget->createItemsToolBar(this);
    m_designerWidget->createPropertyEditorDockWidget(this, Qt::RightDockWidgetArea);
    connect(m_designerWidget, &ReportDesignerWidget::designChanged,
            this, &Window::showDesign);

    createMenus();

    m_reportView = new KReportView(centralWidget);
    setCentralWidget(centralWidget);
    centralWidget->setSizes(QList<int>() << height() / 2 << height() / 2);

#if 0
    if (loadDocument()) {
        m_preRenderer = new KReportPreRenderer(m_document.documentElement());
        if (!m_preRenderer->isValid()) {
            return;
        }
    }
#endif

    KReportPluginManager* manager = KReportPluginManager::self();
    //! @todo
    Q_UNUSED(manager);

    // restore visual settings
    QSettings settings;
    restoreGeometry(settings.value("MainWindow/geometry").toByteArray());
        restoreState(settings.value("MainWindow/windowState").toByteArray());

    showDesign(m_designerWidget->document());
}

Window::~Window()
{
}

void Window::closeEvent(QCloseEvent *event)
{
    // store visual settings
    QSettings settings;
    settings.setValue("MainWindow/geometry", saveGeometry());
    settings.setValue("MainWindow/windowState", saveState());
    QMainWindow::closeEvent(event);
}

bool Window::loadDocument()
{
    //qDebug() << KREPORTEXAMPLE_DATA_DIR;
    QFile file(QLatin1String(KREPORTEXAMPLE_DATA_DIR) + "/report.xml");
    if (!file.open(QIODevice::ReadOnly)) {
        return false;
    }
    QString errorMsg;
    int errorLine;
    int errorColumn;
    if (!m_document.setContent(&file,
                               &errorMsg, &errorLine, &errorColumn))
    {
        qWarning() << "Error reading XML from" << file.fileName()
                   << "Message:" << errorMsg
                   << "Line:" << errorLine
                   << "Column:" << errorColumn;
        return false;
    }
    return true;
}

void Window::createMenus()
{
    m_fileMenu = menuBar()->addMenu(tr("&File"));
    m_exitAction = new QAction(tr("E&xit"), this);
    m_exitAction->setShortcuts(QKeySequence::Quit);
    connect(m_exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    m_fileMenu->addAction(m_exitAction);
}

void Window::showDesign(const QDomElement &design)
{
    //qDebug() << design;
    KReportPreRenderer preRenderer(design);
    if (!preRenderer.isValid()) {
        return;
    }

    preRenderer.setDataSource(new KReportExampleDataSource());
    preRenderer.setName("example_report");

    if (preRenderer.generateDocument()) {
        m_reportView->setDocument(preRenderer.document());
        m_reportView->moveToFirstPage();
    }
}