File indexing completed on 2024-05-12 16:35:53

0001 /* This file is part of the KDE project
0002    Copyright 2008 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
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 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 "TablePageManager.h"
0021 
0022 #include "TableShape.h"
0023 
0024 #include <Sheet.h>
0025 
0026 #include <KoShapeContainer.h>
0027 #include <KoTextDocumentLayout.h>
0028 #include <KoTextShapeData.h>
0029 
0030 #include <QList>
0031 
0032 using namespace Calligra::Sheets;
0033 
0034 class TablePageManager::Private
0035 {
0036 public:
0037     TableShape* master;
0038     QList<TableShape*> pages;
0039 };
0040 
0041 TablePageManager::TablePageManager(TableShape* master)
0042         : PageManager(master->sheet())
0043         , d(new Private)
0044 {
0045     d->master = master;
0046 }
0047 
0048 TablePageManager::~TablePageManager()
0049 {
0050     delete d;
0051 }
0052 
0053 QSizeF TablePageManager::size(int page) const
0054 {
0055     if (page < 1 || page > d->pages.count() || !d->pages[page - 1]->KoShape::parent())
0056         return QSizeF();
0057     return (page == 1) ? d->master->size() : d->pages[page - 1]->KoShape::parent()->size();
0058 }
0059 
0060 void TablePageManager::clearPages()
0061 {
0062     qDeleteAll(d->pages);
0063     d->pages.clear();
0064 }
0065 
0066 void TablePageManager::insertPage(int page)
0067 {
0068     if (page <= 1 || page > d->pages.count()) {
0069         return;
0070     }
0071     TableShape* const shape = static_cast<TableShape*>(d->pages[page - 1]);
0072     const QRect cellRange = this->cellRange(page);
0073     shape->setVisibleCellRange(cellRange);
0074     shape->KoShape::setSize(shape->sheet()->cellCoordinatesToDocument(cellRange).size());
0075 }
0076 
0077 void TablePageManager::preparePage(int /*page*/)
0078 {
0079 #if 0
0080     // The first page is the master page, which always exists.
0081     if (page == 1) {
0082         return;
0083     }
0084     KoTextShapeData* const data = static_cast<KoTextShapeData*>(d->master->KoShape::parent()->userData());
0085     if (!data) {
0086         // not embedded in a text shape
0087         return;
0088     }
0089     Q_CHECK_PTR(data);
0090     QTextDocument* const document = data->document();
0091     Q_CHECK_PTR(document);
0092     KoTextDocumentLayout* const layout = qobject_cast<KoTextDocumentLayout*>(document->documentLayout());
0093     Q_CHECK_PTR(layout);
0094     const QList<KoShape*> textShapes = layout->shapes();
0095     const int masterIndex = textShapes.indexOf(d->master);
0096     if (masterIndex < 0)
0097         return;  // huh?
0098     KoShapeContainer* const textShape = dynamic_cast<KoShapeContainer*>(textShapes.value(masterIndex + page - 1));
0099     if (textShape) {
0100         TableShape* const shape = new TableShape(d->master->columns(), d->master->rows());
0101         const TableShape* predecessor = d->pages[page - 1];
0102         shape->setPosition(predecessor->position() + QPointF(0.0, predecessor->size().height()));
0103         d->pages.append(shape);
0104         textShape->addShape(shape);
0105     }
0106 #endif
0107 }