File indexing completed on 2024-05-12 16:37:10

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2002-2006 David Faure <faure@kde.org>
0003  * Copyright (C) 2005-2009 Thomas Zander <zander@kde.org>
0004  * Copyright (C) 2007 Thorsten Zachmann <zachmann@kde.org>
0005  * Copyright (C) 2008 Pierre Ducroquet <pinaraf@pinaraf.info>
0006  * Copyright (C) 2008 Sebastian Sauer <mail@dipe.org>
0007  * Copyright (C) 2010 Boudewijn Rempt <boud@kogmbh.com>
0008  *
0009  * This library is free software; you can redistribute it and/or
0010  * modify it under the terms of the GNU Library General Public
0011  * License as published by the Free Software Foundation; either
0012  * version 2 of the License, or (at your option) any later version.
0013  *
0014  * This library is distributed in the hope that it will be useful,
0015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0017  * Library General Public License for more details.
0018  *
0019  * You should have received a copy of the GNU Library General Public License
0020  * along with this library; see the file COPYING.LIB.  If not, write to
0021  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0022  * Boston, MA 02110-1301, USA.
0023  */
0024 
0025 #ifndef KWDOCUMENT_H
0026 #define KWDOCUMENT_H
0027 
0028 #include "KWPageManager.h"
0029 #include "KWApplicationConfig.h"
0030 #include "frames/KWFrameLayout.h"
0031 #include "words_export.h"
0032 
0033 #include <KoDocument.h>
0034 #include <KoShapeManager.h>
0035 #include <KoShapeBasedDocumentBase.h>
0036 #include <KoXmlReader.h>
0037 
0038 #include <QObject>
0039 #include <QPainter>
0040 #include <QRect>
0041 #include <QPointer>
0042 
0043 class KWView;
0044 class KWPage;
0045 class KWFrameSet;
0046 class KWFrame;
0047 class KoInlineTextObjectManager;
0048 class KoTextRangeManager;
0049 class KoShapeConfigFactoryBase;
0050 class KoUpdater;
0051 class KoShapeAnchor;
0052 class KoShapeController;
0053 class KoPart;
0054 class KoAnnotationLayoutManager;
0055 class KoDocumentInfoDlg;
0056 
0057 #define WORDS_MIME_TYPE "application/vnd.oasis.opendocument.text"
0058 
0059 /**
0060  * The class that represents a Words document containing content and settings.
0061  */
0062 class WORDS_EXPORT KWDocument : public KoDocument, public KoShapeBasedDocumentBase
0063 {
0064     Q_OBJECT
0065 public:
0066     /**
0067      * Constructor, normally called by the KWFactory::createPartObject()
0068      */
0069     explicit KWDocument(KoPart *part);
0070     ~KWDocument() override;
0071 
0072     // KoShapeBasedDocumentBase interface
0073     /// reimplemented from KoShapeBasedDocumentBase
0074     void addShape(KoShape *shape) override;
0075     /// reimplemented from KoShapeBasedDocumentBase
0076     void removeShape(KoShape *shape) override;
0077     // reimplemented from KoShapeBasedDocumentBase
0078     void shapesRemoved(const QList<KoShape*> &shapes, KUndo2Command *command) override;
0079 
0080     // KoDocument interface
0081     /// reimplemented from KoDocument
0082     QPixmap generatePreview(const QSize& size) override;
0083     /// reimplemented from KoDocument
0084     void paintContent(QPainter&, const QRect&) override;
0085     /// reimplemented from KoDocument
0086     bool loadXML(const KoXmlDocument &doc, KoStore *store) override;
0087     /// reimplemented from KoDocumentBase
0088     bool loadOdf(KoOdfReadStore &odfStore) override;
0089     /// reimplemented from KoDocumentBase
0090     bool saveOdf(SavingContext &documentContext) override;
0091     /// reimplemented from KoDocument
0092     int pageCount() const override {
0093         return pageManager()->pageCount();
0094     }
0095     /// reimplemented from KoDocument
0096     QByteArray nativeFormatMimeType() const override { return WORDS_MIME_TYPE; }
0097     /// reimplemented from KoDocument
0098     QByteArray nativeOasisMimeType() const override { return WORDS_MIME_TYPE; }
0099     /// reimplemented from KoDocument
0100     QStringList extraNativeMimeTypes() const override
0101     {
0102         return QStringList() << "application/vnd.oasis.opendocument.text-master"
0103                              << "application/vnd.oasis.opendocument.text-template";
0104     }
0105 
0106 
0107     bool isMasterDocument() const;
0108     void setIsMasterDocument(bool isMasterDocument);
0109 
0110     // others
0111     KoAnnotationLayoutManager *annotationLayoutManager() const {
0112         return m_annotationManager;
0113     }
0114     /**
0115      * Return the pageManager used in this document.
0116      */
0117     const KWPageManager *pageManager() const {
0118         return &m_pageManager;
0119     }
0120     /**
0121      * Return the pageManager used in this document.
0122      */
0123     Q_SCRIPTABLE KWPageManager *pageManager() {
0124         return &m_pageManager;
0125     }
0126     /**
0127      * Return the frameLayout used in this document.
0128      */
0129     Q_SCRIPTABLE KWFrameLayout *frameLayout() {
0130         return &m_frameLayout;
0131     }
0132 
0133     /**
0134      * Insert a new page after another,
0135      * creating followup frames (but not headers/footers),
0136      * @param afterPageNum the page is inserted after the one specified here
0137      * If afterPageNum is 0, a page is inserted before page 1.
0138      * In all cases, the new page will have the number afterPageNum+1.
0139      * Use appendPage in WP mode, insertPage in DTP mode.
0140      * @param masterPageName the name of the master page to use for this new page.
0141      */
0142     KWPage insertPage(int afterPageNum, const QString &masterPageName = QString());
0143     /**
0144      * Append a new page, creating followup frames (but not headers/footers),
0145      * and return the page number.
0146      * @param masterPageName the name of the master page to use for this new page.
0147      */
0148     KWPage appendPage(const QString &masterPageName = QString());
0149 
0150     /// return the amount of framesets this document holds
0151     int frameSetCount() const {
0152         return m_frameSets.count();
0153     }
0154     /// return a list of all the framesets this document holds
0155     const QList<KWFrameSet*> &frameSets() const {
0156         return m_frameSets;
0157     }
0158     /// return a frameset, or null, by name. @see KWFrameSet::name()
0159     KWFrameSet *frameSetByName(const QString &name);
0160     /// return a suggestion for a copy frameset that does not collide with known ones.
0161     QString suggestFrameSetNameForCopy(const QString& base);
0162     /// return a suggestion for a new frameset name that does not collide with known ones.
0163     QString uniqueFrameSetName(const QString &suggestion);
0164     /// return the main text frameset of the document
0165     KWTextFrameSet *mainFrameSet() const;
0166 
0167     /// return the inlineTextObjectManager for this document.
0168     KoInlineTextObjectManager *inlineTextObjectManager() const;
0169 
0170     /// return the textRangeManager for this document.
0171     KoTextRangeManager *textRangeManager() const;
0172 
0173     KWApplicationConfig &config() {
0174         return m_config;
0175     }
0176     const KWApplicationConfig &config() const {
0177         return m_config;
0178     }
0179 
0180     /// This emits the pageSetupChanged signal which will call KWViewMode::updatePageCache.
0181     void firePageSetupChanged();
0182 
0183     // reimplemented slot from KoDocument
0184     void initEmpty() override;
0185 
0186     bool layoutFinishedAtleastOnce() const { return m_mainFramesetEverFinished; }
0187 
0188     /// request a relayout of auto-generated frames on all pages of this argument style.
0189     void updatePagesForStyle(const KWPageStyle &style);
0190 
0191     /// find the textshape closest to the given shape or return 0
0192     KoShape *findTargetTextShape(KoShape *shape) const;
0193 
0194     KoShapeAnchor *anchorOfShape(KoShape *shape) const;
0195 
0196     KWFrame *frameOfShape(KoShape *shape) const;
0197 
0198     /// returns the document's shapeController. This controller should only be used for deleting shapes.
0199     //TODO: refactor the shapeController so it can be completely per document maybe? Then it can be added to the resourceManager
0200     KoShapeController *shapeController() const { return m_shapeController; }
0201 
0202     KoDocumentInfoDlg* createDocumentInfoDialog(QWidget *parent, KoDocumentInfo *docInfo) const override;
0203 
0204 public Q_SLOTS:
0205     /**
0206      * Relayout the pages or frames within the framesets.
0207      * @param framesets The framesets that should be relayouted. If no framesets are
0208      * provided (empty list) then all framesets and therefore all pages are relayouted.
0209      */
0210     void relayout(QList<KWFrameSet*> framesets = QList<KWFrameSet*>());
0211     /**
0212      * Register a frameset.
0213      * @param frameset The frameset that should be registered. Future operations like
0214      * for example @a relayout() operate on all registered framesets.
0215      */
0216     void addFrameSet(KWFrameSet *frameset);
0217     /**
0218      * Remove frameset from the document stopping it from being saved or displayed.
0219      * Note that the document is normally the one that deletes framesets when the
0220      * document is closed, after removing it the
0221      * caller will have the responsibility to delete it when its no longer of use.
0222      * @param fs the frameset that should be removed from the doc
0223      * \sa addFrameSet()
0224      */
0225     void removeFrameSet(KWFrameSet *fs);
0226 
0227 Q_SIGNALS:
0228     /// signal emitted when a page has been added
0229     void pageSetupChanged();
0230 
0231     /// emitted whenever a shape is added.
0232     void shapeAdded(KoShape *, KoShapeManager::Repaint);
0233 
0234     /// emitted whenever a shape is removed
0235     void shapeRemoved(KoShape *);
0236 
0237     /// emitted wheneve a resources needs to be set on the canvasResourceManager
0238     void resourceChanged(int key, const QVariant &value);
0239 
0240 private Q_SLOTS:
0241     /// Shape maintenance on already registered framesets
0242     void addSequencedShape(KoShape *shape);
0243     void removeSequencedShape(KoShape *shape);
0244     /// Called after the constructor figures out there is an install problem.
0245     void mainTextFrameSetLayoutDone();
0246 
0247     void layoutProgressChanged(int percent);
0248     void layoutFinished();
0249 
0250 protected:
0251     /// reimplemented from KoDocument
0252     void setupOpenFileSubProgress() override;
0253 
0254 private:
0255     friend class KWDLoader;
0256     friend class KWOdfLoader;
0257     friend class KWPagePropertiesCommand;
0258     QString renameFrameSet(const QString &prefix , const QString &base);
0259     /**
0260      * post process loading after either oasis or oldxml loading finished
0261      */
0262     void endOfLoading();
0263     /**
0264      * Called before loading
0265      * It's important to clear out anything that might be in the document already,
0266      * for things like using DBUS to load multiple documents into the same KWDocument,
0267      * or "reload" when words is embedded into konqueror.
0268      */
0269     void clear();
0270 
0271     /**
0272      * emits pageSetupChanged
0273      */
0274     void saveConfig();
0275 
0276 private:
0277     bool m_isMasterDocument;
0278     QList<KWFrameSet*> m_frameSets;
0279     KWPageManager m_pageManager;
0280     KWFrameLayout m_frameLayout;
0281     KWApplicationConfig m_config;
0282     bool m_mainFramesetEverFinished;
0283     QList<KoShapeConfigFactoryBase *> m_panelFactories;
0284     QPointer<KoUpdater> m_layoutProgressUpdater;
0285     KoShapeController *m_shapeController;
0286     QList<KoShape*> m_loadedAnnotationShapes;
0287     KoAnnotationLayoutManager *m_annotationManager;
0288 };
0289 
0290 #endif