1 /* This file is part of the KDE project
2 Copyright 2008 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20 #include "TablePageManager.h"
21
22 #include "TableShape.h"
23
24 #include <Sheet.h>
25
26 #include <KoShapeContainer.h>
27 #include <KoTextDocumentLayout.h>
28 #include <KoTextShapeData.h>
29
30 #include <QList>
31
32 using namespace Calligra::Sheets;
33
34 class TablePageManager::Private
35 {
36 public:
37 TableShape* master;
38 QList<TableShape*> pages;
39 };
40
TablePageManager(TableShape * master)41 TablePageManager::TablePageManager(TableShape* master)
42 : PageManager(master->sheet())
43 , d(new Private)
44 {
45 d->master = master;
46 }
47
~TablePageManager()48 TablePageManager::~TablePageManager()
49 {
50 delete d;
51 }
52
size(int page) const53 QSizeF TablePageManager::size(int page) const
54 {
55 if (page < 1 || page > d->pages.count() || !d->pages[page - 1]->KoShape::parent())
56 return QSizeF();
57 return (page == 1) ? d->master->size() : d->pages[page - 1]->KoShape::parent()->size();
58 }
59
clearPages()60 void TablePageManager::clearPages()
61 {
62 qDeleteAll(d->pages);
63 d->pages.clear();
64 }
65
insertPage(int page)66 void TablePageManager::insertPage(int page)
67 {
68 if (page <= 1 || page > d->pages.count()) {
69 return;
70 }
71 TableShape* const shape = static_cast<TableShape*>(d->pages[page - 1]);
72 const QRect cellRange = this->cellRange(page);
73 shape->setVisibleCellRange(cellRange);
74 shape->KoShape::setSize(shape->sheet()->cellCoordinatesToDocument(cellRange).size());
75 }
76
preparePage(int)77 void TablePageManager::preparePage(int /*page*/)
78 {
79 #if 0
80 // The first page is the master page, which always exists.
81 if (page == 1) {
82 return;
83 }
84 KoTextShapeData* const data = static_cast<KoTextShapeData*>(d->master->KoShape::parent()->userData());
85 if (!data) {
86 // not embedded in a text shape
87 return;
88 }
89 Q_CHECK_PTR(data);
90 QTextDocument* const document = data->document();
91 Q_CHECK_PTR(document);
92 KoTextDocumentLayout* const layout = qobject_cast<KoTextDocumentLayout*>(document->documentLayout());
93 Q_CHECK_PTR(layout);
94 const QList<KoShape*> textShapes = layout->shapes();
95 const int masterIndex = textShapes.indexOf(d->master);
96 if (masterIndex < 0)
97 return; // huh?
98 KoShapeContainer* const textShape = dynamic_cast<KoShapeContainer*>(textShapes.value(masterIndex + page - 1));
99 if (textShape) {
100 TableShape* const shape = new TableShape(d->master->columns(), d->master->rows());
101 const TableShape* predecessor = d->pages[page - 1];
102 shape->setPosition(predecessor->position() + QPointF(0.0, predecessor->size().height()));
103 d->pages.append(shape);
104 textShape->addShape(shape);
105 }
106 #endif
107 }
108