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

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2015-2016 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 #ifndef TIKZ_CORE_ABSTRACT_PROPERTY_H
0020 #define TIKZ_CORE_ABSTRACT_PROPERTY_H
0021 
0022 #include "tikz_export.h"
0023 
0024 #include <QString>
0025 
0026 class QJsonObject;
0027 
0028 namespace tikz {
0029 namespace core {
0030 
0031 class PropertyPrivate;
0032 class Entity;
0033 class PropertyInterface;
0034 
0035 /**
0036  * Base class for properties.
0037  *
0038  * TODO / FIXME:
0039  * - merge PropertyInterface into Entity? --> then toJson/fromJson is much simpler
0040  * - add TikzFormatter classes: "property name=value", only "value",
0041  *   only "double" for double lines, ...
0042  *
0043  * @see Entity
0044  */
0045 class TIKZKITCORE_EXPORT Property
0046 {
0047 public:
0048     /**
0049      * Constructor that creates a property @p propertyName as part of @p entity.
0050      */
0051     explicit Property(const QString & propertyName, PropertyInterface * interface);
0052 
0053     /**
0054      * Destructor.
0055      */
0056     virtual ~Property();
0057 
0058     /**
0059      * Returns the PropertyInterface passed in the constructor.
0060      * This is typically an Entity this property belongs to.
0061      */
0062     PropertyInterface * propertyInterface() const;
0063 
0064     /**
0065      * Returns the property called @p propertyName() from the parent PropertyInterface.
0066      * This is equivalent to calling (with null pointer checks)
0067      * propertyInterface()->parentPropertyInterface()->property(propertyName()).
0068      * A null pointer is returned if no parent interface exists, or if the parent
0069      * interface does not contain a property called @p propertyName().
0070      */
0071     Property * parentProperty() const;
0072 
0073     /**
0074      * Helper function. Overload of parentProperty(), static_cast<>ing to T.
0075      */
0076     template<class T>
0077     T * parentProperty() const
0078     {
0079         return static_cast<T *>(parentProperty());
0080     }
0081 
0082     /**
0083      * Returns the associated Entity, or null, if no Entity was given.
0084      */
0085     Entity * entity() const;
0086 
0087     /**
0088      * Returns the property name of this Property.
0089      * The returned string is used as identifier to query for a certain property.
0090      */
0091     QString propertyName() const;
0092 
0093     /**
0094      * Set the user visible name of this Property to @p name.
0095      * This title is used as display string to the user, e.g. "Line Width"
0096      * or "Fill Color".
0097      */
0098     void setTitle(const QString & name);
0099 
0100     /**
0101      * Returns the user-visible name that was set with setTitle().
0102      */
0103     QString title() const;
0104 
0105     /**
0106      * Returns the type of the property.
0107      * The type is used to cast the property to the respective derived type.
0108      * Additionally, the type helps to serialize and deserialize the Property
0109      */
0110     virtual const char * propertyType() const = 0;
0111 
0112     /**
0113      * Reset the property to its default value.
0114      * @see isSet()
0115      */
0116     virtual void unset();
0117 
0118     /**
0119      * Returns @e true, if this property was set, otherwise false.
0120      */
0121     bool isSet() const;
0122 
0123 public: // load & save
0124     /**
0125      * Load the Property state from the @p json object.
0126      */
0127     void load(const QJsonObject & json);
0128 
0129     /**
0130      * Save the Property state to the @p json object.
0131      */
0132     void save(QJsonObject & json);
0133 
0134 protected:
0135     /**
0136      * Load the payload of the Property state from the @p json object.
0137      * The default implementation is empty.
0138      */
0139     virtual void loadData(const QJsonObject & json);
0140 
0141     /**
0142      * Save the payload of the Property state to the @p json object.
0143      * The default implementation is empty.
0144      */
0145     virtual void saveData(QJsonObject & json);
0146 
0147 public: // change management
0148     /**
0149      * Helper class for signaling changes.
0150      *
0151      * When creating a Transaction instance, the constructor will automatically
0152      * call the PropertyInterface::notifyAboutToChangeProperty(). Similarly,
0153      * either by calling endChange(), or in the destructor, the Transaction
0154      * automatically calls PropertyInterface::notifyPropertyChanged().
0155      *
0156      * This way, the PropertyInterface can track Property changes. For instance,
0157      * this is useful for automatically providing proper undo/redo support.
0158      */
0159     class Transaction
0160     {
0161     public:
0162         /**
0163          * Constructor, calling PropertyInterface::notifyAboutToChangeProperty().
0164          */
0165         Transaction(Property * prop);
0166 
0167         /**
0168          * Destructor, calling PropertyInterface::notifyPropertyChanged().
0169          */
0170         ~Transaction();
0171 
0172         /**
0173          * End the transaction earlier. Will call PropertyInterface::notifyPropertyChanged()
0174          * as well. However, the destructor then will do nothing.
0175          */
0176         void endChange();
0177 
0178     private:
0179         Property * m_property = nullptr;
0180     };
0181 
0182 private:
0183     // pimpl data pointer
0184     PropertyPrivate * const d;
0185 };
0186 
0187 } //namespace core
0188 } //namespace tikz
0189 
0190 #endif // TIKZ_CORE_ABSTRACT_PROPERTY_H
0191 
0192 // kate: indent-width 4; replace-tabs on;