File indexing completed on 2024-05-12 04:35:03

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2013-2015 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #ifndef TIKZ_DOCUMENT_H
0021 #define TIKZ_DOCUMENT_H
0022 
0023 #include "Entity.h"
0024 #include "tikz_export.h"
0025 #include "tikz.h"
0026 #include "Path.h"
0027 #include "MetaPos.h"
0028 
0029 #include <QVector>
0030 
0031 class QAbstractItemModel;
0032 class QUrl;
0033 
0034 namespace tikz {
0035 namespace core {
0036 
0037 class DocumentPrivate;
0038 class Style;
0039 class Node;
0040 class UndoItem;
0041 class Visitor;
0042 
0043 class TIKZKITCORE_EXPORT Document : public Entity
0044 {
0045     Q_OBJECT
0046     Q_PROPERTY(tikz::Unit preferredUnit READ preferredUnit WRITE setPreferredUnit NOTIFY preferredUnitChanged)
0047     Q_PROPERTY(QString documentName READ documentName)
0048     Q_PROPERTY(Style* style READ style)
0049 
0050     public:
0051         /**
0052          * Default constructor.
0053          */
0054         explicit Document(QObject * parent = nullptr);
0055 
0056         /**
0057          * Destructor
0058          */
0059         virtual ~Document();
0060 
0061     //
0062     // Reimplemented from Entity
0063     //
0064     public:
0065         /**
0066          * Returns the EntityType Document.
0067          */
0068         tikz::EntityType entityType() const override;
0069 
0070     //
0071     // visitor pattern
0072     //
0073     public:
0074         /**
0075          * Visitor pattern.
0076          * Visits all elements of the document.
0077          */
0078         bool accept(Visitor & visitor);
0079 
0080     //
0081     // file loading
0082     //
0083     public Q_SLOTS:
0084         /**
0085          * Clear all contents of the document and reset the associated file.
0086          * @warning This functions is called in the Document destructor.
0087          *          So never make it virtual!
0088          */
0089         void close();
0090 
0091         /**
0092          * Load the tikz document from @p url.
0093          */
0094         bool load(const QUrl & url);
0095 
0096         /**
0097          * Reload the current Document.
0098          * Reloading a document works only if a file was loaded before.
0099          */
0100         bool reload();
0101 
0102         /**
0103          * Save the tikz document to the file opened with load().
0104          */
0105         bool save();
0106 
0107         /**
0108          * Save the tikz document to @p file in json notation.
0109          */
0110         bool saveAs(const QUrl & file);
0111 
0112     public:
0113         /**
0114          * Get the current url of this file.
0115          */
0116         QUrl url() const;
0117 
0118         /**
0119          * Get the Document's filename
0120          */
0121         QString documentName() const;
0122 
0123         /**
0124          * Check whether this Document is completely empty.
0125          * The return value is @e true, if no modifications have been made
0126          * and the Document is empty without any contents loaded from a file.
0127          */
0128         bool isEmptyBuffer() const;
0129 
0130         /**
0131          * Export the picture to TikZ.
0132          */
0133         QString tikzCode();
0134 
0135     //
0136     // signals
0137     // NOTE: See also ConfigObject::changed()
0138     //
0139     Q_SIGNALS:
0140         /**
0141          * This signal is emitted whenever the modified state changed.
0142          */
0143         void modifiedChanged();
0144 
0145         /**
0146          * This signal is emitted right before all its Node%s and Path%s
0147          * are deleted. Make sure to connect to this signal to cleanup
0148          * all references to Nodes and Paths of this Document to avoid
0149          * danling pointers.
0150          */
0151         void aboutToClear();
0152 
0153         /**
0154          * This signal is emitted whenever this Document's name changes.
0155          */
0156         void documentNameChanged(tikz::core::Document * doc);
0157 
0158     //
0159     // Undo / redo management
0160     //
0161     public:
0162         /**
0163          * Get the undo stack of this document.
0164          */
0165         void addUndoItem(tikz::core::UndoItem * undoItem);
0166 
0167         /**
0168          * Begin undo group @p name.
0169          * Each beginTransaction() must have a matching finishTransaction().
0170          * The calls may be nested.
0171          */
0172         void beginTransaction(const QString & name);
0173 
0174         /**
0175          * Cancel the currently pending transaction. All changes are reverted
0176          * and the undo / redo stack remains unchanged.
0177          */
0178         void cancelTransaction();
0179 
0180         /**
0181          * Finish currently running transaction.
0182          */
0183         void finishTransaction();
0184 
0185         /**
0186          * Check whether a transaction is currently running.
0187          */
0188         bool transactionRunning() const;
0189 
0190         /**
0191          * If @p active is @e true, modifying Nodes, Paths or the Document
0192          * directly changes the data without creating an undo item.
0193          * For instance, all undo items themselves call this function with
0194          * @p active set to @e true.
0195          *
0196          * @return the old active state is returned
0197          */
0198         bool setUndoActive(bool active);
0199 
0200         /**
0201          * Check whether undo tracking is active.
0202          */
0203         bool undoActive() const;
0204 
0205         /**
0206          * Check whether the document is in a modified state.
0207          */
0208         bool isModified() const;
0209 
0210         /**
0211          * Returns whether undo can currently be invoked or not.
0212          */
0213         bool undoAvailable() const;
0214 
0215         /**
0216          * Returns whether redo can currently be invoked or not.
0217          */
0218         bool redoAvailable() const;
0219 
0220         /**
0221          * Returns the history model.
0222          */
0223         QAbstractItemModel * historyModel() const;
0224 
0225     public Q_SLOTS:
0226         /**
0227          * Undo one undo group.
0228          * Calling undo() only has an effect if undoAvailable() is @e true.
0229          * @see undoAvailable(), undoAvailableChanged()
0230          */
0231         void undo();
0232 
0233         /**
0234          * Redo one redo group.
0235          * Calling redo() only has an effect if redoAvailable() is @e true.
0236          * @see redoAvailable(), redoAvailableChanged()
0237          */
0238         void redo();
0239 
0240     Q_SIGNALS:
0241         /**
0242          * This signal is emitted whenever undoAvailable() changes.
0243          * @see undoAvailable()
0244          */
0245         bool undoAvailableChanged(bool available) const;
0246 
0247         /**
0248          * This signal is emitted whenever redoAvailable() changes.
0249          * @see redoAvailable()
0250          */
0251         bool redoAvailableChanged(bool available) const;
0252 
0253     //
0254     // convenience functions
0255     //
0256     public:
0257         /**
0258          * Returns the position for @p pos in tikz::Pos coordinates.
0259          */
0260         virtual tikz::Pos scenePos(const MetaPos & pos) const;
0261 
0262         /**
0263          * Sets the preferred Unit to @p unit.
0264          * The preferred Unit is typically used by the graphical scene when
0265          * moving nodes and paths.
0266          * @see preferredUnit(), referredUnitChanged()
0267          */
0268         void setPreferredUnit(tikz::Unit unit);
0269 
0270         /**
0271          * Returns the currently preferred unit.
0272          * @see setPreferredUnit(), referredUnitChanged()
0273          */
0274         tikz::Unit preferredUnit() const;
0275 
0276     Q_SIGNALS:
0277         /**
0278          * This signal is emitted whenever the preferred unit changed.
0279          * @see setPreferredUnit(), preferredUnit()
0280          */
0281         void preferredUnitChanged(tikz::Unit unit);
0282 
0283     //
0284     // Node, Path and style management
0285     //
0286     public:
0287         /**
0288          * Get the global document style.
0289          * This is the global document style. All other styles fall back to
0290          * this style, if a property is not set.
0291          */
0292         Style * style() const;
0293 
0294         /**
0295          * Get the list of nodes of the tikz document.
0296          */
0297         QVector<Uid> nodes() const;
0298 
0299         /**
0300          * Get the list of paths of the tikz document.
0301          */
0302         QVector<Uid> paths() const;
0303 
0304         /**
0305          * Returns the Entity for the Uid @p uid.
0306          * A null pointer is returned if the Entity does not exist, or if the
0307          * Uid::document() pointer does not equal this Document.
0308          */
0309         Entity * entity(const tikz::core::Uid & uid) const;
0310 
0311         /**
0312          * Get list of all entities.
0313          */
0314         QVector<Uid> entities() const;
0315 
0316     //
0317     // Node and path creation
0318     //
0319     public:
0320         /**
0321          * Creates a new style associated with this document.
0322          * If the style is not needed anymore, delete it with deleteEntity().
0323          */
0324         Entity * createEntity(tikz::EntityType type);
0325 
0326         /**
0327          * Templated helper function for creating entities.
0328          */
0329         template<typename T>
0330         T * createEntity(tikz::EntityType type)
0331         {
0332             return qobject_cast<T*>(createEntity(type));
0333         }
0334 
0335         /**
0336          * Helper function to create a Node.
0337          */
0338         Node * createNode();
0339 
0340         /**
0341          * Remove @p entity from the document by deleting the entity object.
0342          * Afterwards, the pointer is invalid.
0343          * @param entity entity to delete
0344          */
0345         void deleteEntity(Entity * entity);
0346 
0347         /**
0348          * Helper function to create a Path.
0349          */
0350         Path * createPath();
0351 
0352     //
0353     // internal: Undo / redo items manipulate with ID
0354     //
0355     protected:
0356         /**
0357          * Create a new entity of @p type associated with this document with @p uid.
0358          */
0359         virtual Entity * createEntity(const Uid & uid, EntityType type);
0360 
0361         /**
0362          * Delete entity @p uid associated with this document.
0363          */
0364         virtual void deleteEntity(const Uid & uid);
0365 
0366         /**
0367          * Create a new path associated with this document with @p uid.
0368          */
0369         virtual Path * createPath(PathType type, const Uid & uid);
0370 
0371     //
0372     // data pointer
0373     //
0374     private:
0375         DocumentPrivate * const d;
0376 
0377     //
0378     // friends
0379     //
0380     protected:
0381         // visitors
0382         friend class DeserializeVisitor;
0383 
0384         // uddo/redo system
0385         friend class UndoCreateEntity;
0386         friend class UndoDeleteEntity;
0387         friend class UndoCreatePath;
0388         friend class UndoDeletePath;
0389 };
0390 
0391 }
0392 }
0393 
0394 #endif // TIKZ_DOCUMENT_H
0395 
0396 // kate: indent-width 4; replace-tabs on;