File indexing completed on 2024-05-12 15:56:48

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2006 Boudewijn Rempt (boud@valdyas.org)
0003  * SPDX-FileCopyrightText: 2006-2007 Thomas Zander <zander@kde.org>
0004  * SPDX-FileCopyrightText: 2008 Thorsten Zachmann <zachmann@kde.org>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.0-or-later
0007  */
0008 
0009 #ifndef KOSHAPEFACTORYBASE_H
0010 #define KOSHAPEFACTORYBASE_H
0011 
0012 #include <QObject>
0013 #include <QString>
0014 #include <QList>
0015 #include <QDomDocument>
0016 
0017 #include "kritaflake_export.h"
0018 
0019 class KoShape;
0020 class KoProperties;
0021 class KoShapeConfigWidgetBase;
0022 class KoShapeLoadingContext;
0023 class KoDocumentResourceManager;
0024 class QStringList;
0025 
0026 #define SHAPETEMPLATE_MIMETYPE "application/x-flake-shapetemplate"
0027 #define SHAPEID_MIMETYPE "application/x-flake-shapeId"
0028 
0029 /**
0030  * Contains a KoProperties object that describes the settings of a
0031  * particular variant of a shape object, together with a name, a description
0032  * and an icon for use in the user interface.
0033  */
0034 struct KRITAFLAKE_EXPORT KoShapeTemplate {
0035     KoShapeTemplate() {
0036         properties = 0;
0037     }
0038     QString id;         ///< The id of the shape
0039     QString templateId;         ///< The id of this particular template - only has to be unique with the shape
0040     QString name;       ///< The name to be shown for this template
0041     QString family;       ///< The family of the shape (possible values are: "funny","arrow")
0042     QString toolTip;    ///< The tooltip text for the template
0043     QString iconName;       ///< Icon name
0044     /**
0045      * The properties which, when passed to the KoShapeFactoryBase::createShape() method
0046      * result in the shape this template represents.
0047      */
0048     const KoProperties *properties;
0049 };
0050 
0051 /**
0052  * A factory for KoShape objects.
0053  * The baseclass for all shape plugins. Each plugin that ships a KoShape should also
0054  * ship a factory. That factory will extend this class and set variable data like
0055  * a toolTip and icon in the constructor of that extending class.
0056  *
0057  * An example usage would be:
0058 @code
0059 class MyShapeFactory : public KoShapeFactoryBase {
0060 public:
0061     MyShapeFactory()
0062         : KoShapeFactoryBase("MyShape", i18n("My Shape")) {
0063         setToolTip(i18n("A nice shape"));
0064     }
0065     ~MyShapeFactory() {}
0066     // more methods here
0067 };
0068 @endcode
0069 
0070  * After you created the factory you should create a plugin that can announce the factory to the
0071  * KoShapeRegistry.  See the KoPluginLoader as well.
0072  */
0073 class KRITAFLAKE_EXPORT KoShapeFactoryBase : public QObject
0074 {
0075     Q_OBJECT
0076 public:
0077 
0078     /**
0079      * Create the new factory
0080      * @param id a string that will be used internally for referencing the shape
0081      * @param name the user visible name of the shape this factory creates.
0082      */
0083     KoShapeFactoryBase(const QString &id, const QString &name, const QString &deferredPluginName = QString());
0084     ~KoShapeFactoryBase() override;
0085 
0086     /**
0087      * Create a list of option panels to show on creating a new shape.
0088      * The shape type this factory creates may have general or specific setting panels
0089      * that will be shown after inserting a new shape.
0090      * The first item in the list will be shown as the first tab in the list of panels,
0091      * behind all app specific panels.
0092      * This is a separate list as set by setOptionPanels() and fetched by panelFactories()
0093      */
0094     virtual QList<KoShapeConfigWidgetBase*> createShapeOptionPanels() {
0095         return QList<KoShapeConfigWidgetBase*>();
0096     }
0097 
0098     /**
0099      * return the id for the shape this factory creates.
0100      * @return the id for the shape this factory creates.
0101      */
0102     QString id() const;
0103     /**
0104      * Return all the templates this factory knows about.
0105      * Each template shows a different way to create a shape this factory is specialized in.
0106      */
0107     QList<KoShapeTemplate> templates() const;
0108     /**
0109      * return a translated tooltip Text for a selector of shapes
0110      * @return a translated tooltip Text
0111      */
0112     QString toolTip() const;
0113     /**
0114      * return the basename of the icon for a selector of shapes
0115      * @return the basename of the icon for a selector of shapes
0116      */
0117     QString iconName() const;
0118     /**
0119      * return the user visible (and translated) name to be seen by the user.
0120      * @return the user visible (and translated) name to be seen by the user.
0121      */
0122     QString name() const;
0123     /**
0124      * return the non-visible name of the family the default shape belongs to.
0125      * @return the family name.
0126      */
0127     QString family() const;
0128     /// lower prio means the shape is more generic and will be checked later
0129     int loadingPriority() const;
0130 
0131     /**
0132      * The list of namespaces to the supported elements the factory supports.
0133      */
0134     QList<QPair<QString, QStringList> > odfElements() const;
0135 
0136     /// returns true if this shapeFactory is able to load the ODF type
0137     /// started at argument element. ('draw:line' / 'draw:frame' / etc)
0138     virtual bool supports(const QDomElement &element, KoShapeLoadingContext &context) const = 0;
0139 
0140     /**
0141      * The hidden boolean requests if the shape should be hidden in the
0142      * shape selector or shown with all its templates.
0143      * The default is false
0144      * @see setHidden()
0145      */
0146     bool hidden() const;
0147 
0148     /**
0149      * This method is called whenever there is a new document resource
0150      * manager that is created. The factory may reimplement this in
0151      * order to get existing resources or put factory specific resources in.
0152      * In case the factory creates new resources it is advised to parent
0153      * them to the manager (which is a QObject) for memory management
0154      * purposes.
0155      *
0156      * FIXME: this method is only used by Tables. We should refactor so
0157      * it is no longer necessary.
0158      *
0159      * NOTE: this actually is also used somehow to create the imagecollection
0160      *        for the picture shape?
0161      *
0162      * NOTE: we store the documentmanagers in a list, and remove them
0163      * from the list on delete.
0164      *
0165      * @param manager the new manager
0166      */
0167     virtual void newDocumentResourceManager(KoDocumentResourceManager *manager) const;
0168 
0169     /**
0170      * This method should be implemented by factories to create a shape that the user
0171      * gets when doing a base insert. For example from a script.  The created shape
0172      * should have its values set to good defaults that the user can then adjust further if
0173      * needed.  Including the KoShape:setShapeId(), with the Id from this factory
0174      * The default shape position is not relevant, it will be moved by the caller.
0175      * @param documentResources the resources manager that has all the document wide
0176      *      resources which can be used to create the object.
0177      * @return a new shape
0178      * @see createShape() newDocumentResourceManager()
0179      */
0180     virtual KoShape *createDefaultShape(KoDocumentResourceManager *documentResources = 0) const;
0181 
0182     /**
0183      * This method should be implemented by factories to create a shape based on a set of
0184      * properties that are specifically made for this shape-type.
0185      * This method should also set this factories shapeId on the shape using KoShape::setShapeId()
0186      * The default implementation just ignores 'params' and calls createDefaultShape()
0187      * @return a new shape
0188      * @param params the parameters to use when creating the shape
0189      * @param documentResources the resources manager that has all the document wide
0190      *      resources which can be used to create the object.
0191      * @see createDefaultShape() newDocumentResourceManager() addTemplate()
0192      * @see KoShapeTemplate::properties
0193      */
0194     virtual KoShape *createShape(const KoProperties *params, KoDocumentResourceManager *documentResources = 0) const;
0195 
0196 protected:
0197 
0198     /**
0199      * Add a template with the properties of a specific type of shape this factory can generate
0200      * using the createShape() method. The factory will take ownership of the properties object
0201      * to which the member @p properties of @p params points to and destroy it only in its own destructor.
0202      * @param params the new template this factory knows to produce
0203      */
0204     void addTemplate(const KoShapeTemplate &params);
0205 
0206     /**
0207      * Set the tooltip to be used for a selector of shapes
0208      * @param tooltip the tooltip
0209      */
0210     void setToolTip(const QString &tooltip);
0211 
0212     /**
0213      * Set an icon to be used in a selector of shapes
0214      * @param iconName the basename (without extension) of the icon
0215      */
0216     void setIconName(const char *iconName);
0217 
0218     /**
0219      * Set the family name of the default shape
0220      * @param family the family name of the default shape this factory creates.
0221      *   for example "funny", "arrows", "geometrics". Use "" for default
0222      */
0223     void setFamily(const QString &family);
0224 
0225     /**
0226      * Set the loading priority for this icon; higher priority means
0227      * the shape is more specific which means it will be earlier in
0228      * the queue to try loading a particular odf element.
0229      */
0230     void setLoadingPriority(int priority);
0231 
0232     /**
0233      * Set the namespace and element tags used for quick checking whether this shapefactory
0234      * is able to create a shape from xml identified by this element
0235      * name.
0236      *
0237      * @param nameSpace the ODF name space (like
0238      * urn:oasis:names:tc:opendocument:xmlns:text:1.0,
0239      * take it from KoXmlNS.h)
0240      * @param elementNames the name of the element itself, like "path"
0241      *
0242      */
0243     void setXmlElementNames(const QString &nameSpace, const QStringList &elementNames);
0244 
0245     /**
0246      * Set the namespaces and according element tags used for quick checking whether this shapefactory
0247      * is able to create a shape from xml identified by this element
0248      * name.
0249      *
0250      * @param elementNamesList containing a list of namespace (like
0251      * urn:oasis:names:tc:opendocument:xmlns:text:1.0,
0252      * take it from KoXmlNS.h) to a list of elementName of the element itself, like "path"
0253      */
0254     void setXmlElements(const QList<QPair<QString, QStringList> > &elementNamesList);
0255 
0256     /**
0257      * The hidden boolean requests if the shape should be hidden in the
0258      * shape selector or shown with all its templates.
0259      * The default is false
0260      * @see hidden()
0261      */
0262     void setHidden(bool hidden);
0263 
0264 private:
0265 
0266     void getDeferredPlugin();
0267 
0268 private Q_SLOTS:
0269 
0270     /// called whenever a document KoDocumentResourceManager is deleted
0271     void pruneDocumentResourceManager(QObject *);
0272 
0273 private:
0274 
0275     class Private;
0276     Private * const d;
0277 };
0278 
0279 #endif