1 /* This file is part of the KDE project
2 Copyright (C) 2015 by Adam Pigg (adam@piggz.co.uk)
3 Copyright (C) 2017 Jarosław Staniek <staniek@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19 */
20
21 #include "KReportView.h"
22 #include "KReportPage.h"
23 #include "KReportRenderObjects.h"
24 #include "KReportPreRenderer.h"
25 #include "KReportRendererBase.h"
26 #include "kreport_debug.h"
27
28 #include <QLabel>
29 #include <QBoxLayout>
30 #include <QScrollArea>
31 #include <QLayout>
32 #include <QPainter>
33 #include <QPointer>
34 #include <QPrintDialog>
35 #include <QPrinter>
36 #include <QGraphicsView>
37 #include <QGraphicsScene>
38 #include <QScrollBar>
39
40 //! @internal
41 class Q_DECL_HIDDEN KReportView::Private
42 {
43 public:
Private()44 explicit Private()
45 : reportPage(nullptr)
46 , currentPage(1)
47 , pageCount(0)
48 {}
49
~Private()50 ~Private()
51 {}
52
53 //! Move to page @a page (counted from 1)
moveToPage(int page)54 void moveToPage(int page)
55 {
56 if (page != currentPage && page >= 1 && page <= pageCount) {
57 currentPage = page;
58 reportPage->renderPage(currentPage);
59 }
60 }
61
62 QPointer<ORODocument> reportDocument;
63 QGraphicsView *reportView;
64 QGraphicsScene *reportScene;
65 KReportPage *reportPage;
66
67 int currentPage;
68 int pageCount;
69
70 KReportRendererFactory factory;
71 };
72
73
KReportView(QWidget * parent)74 KReportView::KReportView(QWidget *parent)
75 : QWidget(parent), d(new Private())
76 {
77 setObjectName(QLatin1String("KReportView"));
78
79 d->reportView = new QGraphicsView(this);
80 // page selector should be always visible:
81 d->reportView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
82
83 QVBoxLayout *l = new QVBoxLayout;
84 l->setMargin(0);
85 setLayout(l);
86
87 layout()->addWidget(d->reportView);
88
89 d->reportScene = new QGraphicsScene(this);
90 d->reportScene->setSceneRect(0,0,1000,2000);
91 d->reportView->setScene(d->reportScene);
92
93 d->reportScene->setBackgroundBrush(palette().brush(QPalette::Dark));
94 }
95
~KReportView()96 KReportView::~KReportView()
97 {
98 //kreportDebug();
99 delete d;
100 }
101
moveToFirstPage()102 void KReportView::moveToFirstPage()
103 {
104 d->moveToPage(1);
105 }
106
moveToLastPage()107 void KReportView::moveToLastPage()
108 {
109 d->moveToPage(d->pageCount);
110 }
111
moveToNextPage()112 void KReportView::moveToNextPage()
113 {
114 d->moveToPage(d->currentPage + 1);
115 }
116
moveToPreviousPage()117 void KReportView::moveToPreviousPage()
118 {
119 d->moveToPage(d->currentPage - 1);
120 }
121
moveToPage(int page)122 void KReportView::moveToPage(int page)
123 {
124 d->moveToPage(page);
125 }
126
currentPage() const127 int KReportView::currentPage() const
128 {
129 return d->currentPage;
130 }
131
pageCount() const132 int KReportView::pageCount() const
133 {
134 return d->pageCount;
135 }
136
setDocument(ORODocument * doc)137 void KReportView::setDocument(ORODocument* doc)
138 {
139 d->reportDocument = doc;
140
141 if (d->reportPage) {
142 delete d->reportPage;
143 }
144
145 d->pageCount = doc->pageCount();
146
147 d->reportPage = new KReportPage(this, d->reportDocument);
148 d->reportPage->setObjectName(QLatin1String("KReportPage"));
149
150 d->reportScene->setSceneRect(0,0,d->reportPage->rect().width() + 40, d->reportPage->rect().height() + 40);
151 d->reportScene->addItem(d->reportPage);
152 d->reportPage->setPos(20,20);
153 d->reportView->centerOn(0,0);
154
155 }
156
scrollArea()157 QAbstractScrollArea* KReportView::scrollArea()
158 {
159 return d->reportView;
160 }
161
refreshCurrentPage()162 void KReportView::refreshCurrentPage()
163 {
164 //kreportDebug() << "Refreshing current page" << d->currentPage;
165 if (d->reportPage) {
166 d->reportPage->renderPage(d->currentPage);
167 }
168 }
169
170