File indexing completed on 2024-05-12 12:58:01

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