File indexing completed on 2024-05-12 16:28:23

0001 /*
0002  * This file is part of the KDE project
0003  *
0004  * Copyright (C) 2013 Arjen Hiemstra <ahiemstra@heimr.nl>
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Library General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Library General Public License
0017  * along with this library; see the file COPYING.LIB.  If not, write to
0018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020  *
0021  */
0022 
0023 #include "DocumentImpl.h"
0024 
0025 #include <kactioncollection.h>
0026 
0027 #include <KoCanvasBase.h>
0028 #include <KoToolManager.h>
0029 #include <KoZoomHandler.h>
0030 #include <KoZoomController.h>
0031 
0032 #include "ComponentsKoCanvasController.h"
0033 
0034 using namespace Calligra::Components;
0035 
0036 class DocumentImpl::Private
0037 {
0038 public:
0039     Private()
0040         : type{DocumentType::Unknown}
0041         , canvas{nullptr}
0042         , finder{nullptr}
0043         , canvasController{nullptr}
0044         , zoomController{nullptr}
0045         , document{nullptr}
0046         , readOnly{false}
0047     { }
0048 
0049     DocumentType::Type type;
0050     QGraphicsWidget* canvas;
0051     KoFindBase* finder;
0052     KoCanvasController* canvasController;
0053     KoZoomController* zoomController;
0054     QSize documentSize;
0055     KoDocument* document;
0056     bool readOnly;
0057 };
0058 
0059 DocumentImpl::DocumentImpl(QObject* parent)
0060     : QObject{parent}, d{new Private}
0061 {
0062 
0063 }
0064 
0065 DocumentImpl::~DocumentImpl()
0066 {
0067 
0068 }
0069 
0070 DocumentType::Type DocumentImpl::documentType() const
0071 {
0072     return d->type;
0073 }
0074 
0075 QGraphicsWidget* DocumentImpl::canvas() const
0076 {
0077     return d->canvas;
0078 }
0079 
0080 KoFindBase* DocumentImpl::finder() const
0081 {
0082     return d->finder;
0083 }
0084 
0085 KoCanvasController* DocumentImpl::canvasController() const
0086 {
0087     return d->canvasController;
0088 }
0089 
0090 KoZoomController* DocumentImpl::zoomController() const
0091 {
0092     return d->zoomController;
0093 }
0094 
0095 QSize DocumentImpl::documentSize() const
0096 {
0097     return d->documentSize;
0098 }
0099 
0100 KoDocument* DocumentImpl::koDocument() const
0101 {
0102     return d->document;
0103 }
0104 
0105 void DocumentImpl::setDocumentType(DocumentType::Type type)
0106 {
0107     d->type = type;
0108 }
0109 
0110 void DocumentImpl::setKoDocument(KoDocument* document)
0111 {
0112     d->document = document;
0113 }
0114 
0115 void DocumentImpl::setCanvas(QGraphicsWidget* newCanvas)
0116 {
0117     d->canvas = newCanvas;
0118 }
0119 
0120 void DocumentImpl::setFinder(KoFindBase* newFinder)
0121 {
0122     d->finder = newFinder;
0123 }
0124 
0125 void DocumentImpl::setReadOnly(bool readOnly)
0126 {
0127     d->readOnly = readOnly;
0128 }
0129 
0130 void DocumentImpl::createAndSetCanvasController(KoCanvasBase* canvas)
0131 {
0132     auto controller = new ComponentsKoCanvasController{new KActionCollection{this}};
0133     d->canvasController = controller;
0134     controller->setCanvas(canvas);
0135     if (!d->readOnly) {
0136         KoToolManager::instance()->addController(controller);
0137     }
0138     connect(controller, &ComponentsKoCanvasController::documentSizeChanged, this, &DocumentImpl::setDocumentSize);
0139 }
0140 
0141 void DocumentImpl::createAndSetZoomController(KoCanvasBase* canvas)
0142 {
0143     auto zoomHandler = static_cast<KoZoomHandler*>(canvas->viewConverter());
0144     d->zoomController = new KoZoomController{d->canvasController, zoomHandler, new KActionCollection(this)};
0145 
0146     auto canvasQObject = dynamic_cast<QObject*>(canvas);
0147     connect(d->canvasController->proxyObject, SIGNAL(moveDocumentOffset(QPoint)), canvasQObject, SLOT(setDocumentOffset(QPoint)));
0148     connect(canvasQObject, SIGNAL(canvasUpdated()), this, SIGNAL(requestViewUpdate()));
0149 }
0150 
0151 void DocumentImpl::setDocumentSize(const QSize& size)
0152 {
0153     if(size != d->documentSize) {
0154         d->documentSize = size;
0155         emit documentSizeChanged();
0156     }
0157 }