File indexing completed on 2024-04-28 16:13:35

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 "Document.h"
0024 
0025 #include <QDebug>
0026 #include <QUrl>
0027 #include <QSizeF>
0028 #include <QPoint>
0029 
0030 #include <KoCanvasBase.h>
0031 #include <KoCanvasController.h>
0032 #include <KoDocument.h>
0033 #include <KoSelection.h>
0034 #include <KoShapeManager.h>
0035 #include <KoTextEditor.h>
0036 
0037 #include "impl/TextDocumentImpl.h"
0038 #include "impl/SpreadsheetImpl.h"
0039 #include "impl/PresentationImpl.h"
0040 
0041 using namespace Calligra::Components;
0042 
0043 class Document::Private
0044 {
0045 public:
0046     Private(Document* qq) : q{qq}, impl{nullptr}, status{DocumentStatus::Unloaded}, readOnly{false}
0047     { }
0048 
0049     void updateImpl();
0050 
0051     Document* q;
0052 
0053     QUrl source;
0054     DocumentImpl* impl;
0055     DocumentStatus::Status status;
0056     bool readOnly;
0057 };
0058 
0059 Document::Document(QObject* parent)
0060     : QObject{parent}, d{new Private{this}}
0061 {
0062 
0063 }
0064 
0065 Document::~Document()
0066 {
0067     delete d;
0068 }
0069 
0070 QUrl Document::source() const
0071 {
0072     return d->source;
0073 }
0074 
0075 void Document::setSource(const QUrl& value)
0076 {
0077     if(value != d->source) {
0078         d->source = value;
0079         emit sourceChanged();
0080 
0081         d->status = DocumentStatus::Loading;
0082         emit statusChanged();
0083 
0084         d->updateImpl();
0085         emit documentTypeChanged();
0086 
0087         if(d->impl) {
0088             d->impl->setReadOnly(d->readOnly);
0089             if(d->impl->load(d->source)) {
0090                 d->status = DocumentStatus::Loaded;
0091                 connect(d->impl->canvasController()->canvas()->shapeManager(), SIGNAL(selectionChanged()), SIGNAL(textEditorChanged()));
0092             } else {
0093                 d->status = DocumentStatus::Failed;
0094             }
0095         } else {
0096             d->status = DocumentStatus::Unloaded;
0097         }
0098 
0099         emit indexCountChanged();
0100         emit statusChanged();
0101     }
0102 }
0103 
0104 bool Document::readOnly() const
0105 {
0106     return d->readOnly;
0107 }
0108 
0109 void Document::setReadOnly(bool readOnly)
0110 {
0111     if (d->readOnly != readOnly) {
0112         d->readOnly = readOnly;
0113 
0114         emit readOnlyChanged();
0115     }
0116 }
0117 
0118 DocumentType::Type Document::documentType() const
0119 {
0120     if(d->impl) {
0121         return d->impl->documentType();
0122     }
0123 
0124     return DocumentType::Unknown;
0125 }
0126 
0127 DocumentStatus::Status Document::status() const
0128 {
0129     return d->status;
0130 }
0131 
0132 QSize Document::documentSize() const
0133 {
0134     if(d->impl) {
0135         return d->impl->documentSize();
0136     }
0137 
0138     return QSize{};
0139 }
0140 
0141 int Document::currentIndex() const
0142 {
0143     if(d->impl) {
0144         return d->impl->currentIndex();
0145     }
0146 
0147     return -1;
0148 }
0149 
0150 void Document::setCurrentIndex(int newValue)
0151 {
0152     if(d->impl) {
0153         d->impl->setCurrentIndex(newValue);
0154     }
0155 }
0156 
0157 int Document::indexCount() const
0158 {
0159     if(d->impl) {
0160         return d->impl->indexCount();
0161     }
0162 
0163     return 0;
0164 }
0165 
0166 KoFindBase* Document::finder() const
0167 {
0168     if(d->impl) {
0169         return d->impl->finder();
0170     }
0171 
0172     return nullptr;
0173 }
0174 
0175 QGraphicsWidget* Document::canvas() const
0176 {
0177     if(d->impl) {
0178         return d->impl->canvas();
0179     }
0180 
0181     return nullptr;
0182 }
0183 
0184 KoCanvasController* Document::canvasController() const
0185 {
0186     if(d->impl) {
0187         return d->impl->canvasController();
0188     }
0189 
0190     return nullptr;
0191 }
0192 
0193 KoZoomController* Document::zoomController() const
0194 {
0195     if(d->impl) {
0196         return d->impl->zoomController();
0197     }
0198 
0199     return nullptr;
0200 }
0201 
0202 QObject* Document::part() const
0203 {
0204     return d->impl->part();
0205 }
0206 
0207 QObject* Document::document() const
0208 {
0209     return koDocument();
0210 }
0211 
0212 KoDocument* Document::koDocument() const
0213 {
0214     if(d->impl) {
0215         return d->impl->koDocument();
0216     }
0217 
0218     return nullptr;
0219 }
0220 
0221 QUrl Document::urlAtPoint(const QPoint& point)
0222 {
0223     if(d->impl)
0224         return d->impl->urlAtPoint(point);
0225     return QUrl();
0226 }
0227 
0228 QObject * Document::textEditor()
0229 {
0230     if (d->impl && d->impl->canvasController()) {
0231         return KoTextEditor::getTextEditorFromCanvas(d->impl->canvasController()->canvas());
0232     }
0233     return 0;
0234 }
0235 
0236 void Document::deselectEverything()
0237 {
0238     KoTextEditor* editor = KoTextEditor::getTextEditorFromCanvas(d->impl->canvasController()->canvas());
0239     if (editor) {
0240         editor->clearSelection();
0241     }
0242     d->impl->canvasController()->canvas()->shapeManager()->selection()->deselectAll();
0243     emit requestViewUpdate();
0244 }
0245 
0246 void Document::Private::updateImpl()
0247 {
0248     delete impl;
0249     impl = nullptr;
0250 
0251     auto type = Global::documentType(source);
0252     switch(type) {
0253     case DocumentType::TextDocument:
0254         impl = new TextDocumentImpl{q};
0255         break;
0256     case DocumentType::Spreadsheet:
0257         impl = new SpreadsheetImpl{q};
0258         break;
0259     case DocumentType::Presentation:
0260         impl = new PresentationImpl{q};
0261         break;
0262     default:
0263         break;
0264     }
0265 
0266     if(impl) {
0267         connect(impl, &DocumentImpl::documentSizeChanged, q, &Document::documentSizeChanged);
0268         connect(impl, &DocumentImpl::currentIndexChanged, q, &Document::currentIndexChanged);
0269         connect(impl, &DocumentImpl::requestViewUpdate, q, &Document::requestViewUpdate);
0270     }
0271     emit q->documentChanged();
0272 }
0273 
0274 #include "moc_Document.cpp"