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

0001 /*
0002    SPDX-FileCopyrightText: 2006, 2011 Boudewijn Rempt (boud@valdyas.org)
0003    SPDX-FileCopyrightText: 2007, 2009, 2010 Thomas Zander <zander@kde.org>
0004    SPDX-FileCopyrightText: 2008 Carlos Licea <carlos.licea@kdemail.net>
0005 
0006    SPDX-License-Identifier: LGPL-2.0-or-later
0007  */
0008 #ifndef KO_CANVASRESOURCEMANAGER_H
0009 #define KO_CANVASRESOURCEMANAGER_H
0010 
0011 #include <QObject>
0012 
0013 #include "kritaflake_export.h"
0014 #include "KoDerivedResourceConverter.h"
0015 #include "KoResourceUpdateMediator.h"
0016 #include "KoActiveCanvasResourceDependency.h"
0017 #include <KoCanvasResourcesIds.h>
0018 
0019 template<class T> class QSharedPointer;
0020 class KoCanvasResourcesInterface;
0021 using KoCanvasResourcesInterfaceSP = QSharedPointer<KoCanvasResourcesInterface>;
0022 
0023 class KoShape;
0024 class KoShapeStroke;
0025 class KoColor;
0026 class KoUnit;
0027 
0028 class QVariant;
0029 class QSizeF;
0030 
0031 /**
0032  * The KoCanvasResourceProvider contains a set of per-canvas
0033  * properties, like current foreground color, current background
0034  * color and more. All tools belonging to the current canvas are
0035  * notified when a Resource changes (is set).
0036  *
0037  * The manager can contain all sorts of variable types and there are accessors
0038  * for the most common ones.  All variables are always stored inside a QVariant
0039  * instance internally and you can always just use the resource() method to get
0040  * that directly.
0041  * The way to store arbitrary data objects that are stored as pointers you can use
0042  * the following code snippets;
0043  * @code
0044  *  QVariant variant;
0045  *  variant.setValue<void*>(textShapeData->document());
0046  *  resourceManager->setResource(KoText::CurrentTextDocument, variant);
0047  *  // and get it out again.
0048  *  QVariant var = resourceManager->resource(KoText::CurrentTextDocument);
0049  *  document = static_cast<QTextDocument*>(var.value<void*>());
0050  * @endcode
0051  */
0052 class KRITAFLAKE_EXPORT KoCanvasResourceProvider : public QObject
0053 {
0054     Q_OBJECT
0055 
0056 public:
0057     enum ApplicationSpecial {
0058         NoSpecial = 0,
0059         NoAdvancedText = 1
0060     };
0061 
0062     /**
0063      * Constructor.
0064      * @param parent the parent QObject, used for memory management.
0065      */
0066     explicit KoCanvasResourceProvider(QObject *parent = 0);
0067     ~KoCanvasResourceProvider() override;
0068 
0069 public Q_SLOTS:
0070     /**
0071      * Set a resource of any type.
0072      * @param key the integer key
0073      * @param value the new value for the key.
0074      * @see KoCanvasResource::CanvasResourceId
0075      */
0076     void setResource(int key, const QVariant &value);
0077 
0078     /**
0079      * Set a resource of type KoColor.
0080      * @param key the integer key
0081      * @param color the new value for the key.
0082      * @see KoCanvasResource::CanvasResourceId
0083      */
0084     void setResource(int key, const KoColor &color);
0085 
0086     /**
0087      * Set a resource of type KoShape*.
0088      * @param key the integer key
0089      * @param id the new value for the key.
0090      * @see KoCanvasResource::CanvasResourceId
0091      */
0092     void setResource(int key, KoShape *shape);
0093 
0094     /**
0095      * Set a resource of type KoUnit
0096      * @param key the integer key
0097      * @param id the new value for the key.
0098      * @see KoCanvasResource::CanvasResourceId
0099      */
0100     void setResource(int key, const KoUnit &unit);
0101 
0102 public:
0103     /**
0104      * Returns a qvariant containing the specified resource or a standard one if the
0105      * specified resource does not exist.
0106      * @param key the key
0107      * @see KoCanvasResource::CanvasResourceId
0108      */
0109     QVariant resource(int key) const;
0110 
0111     /**
0112      * Set the foregroundColor resource.
0113      * @param color the new foreground color
0114      */
0115     void setForegroundColor(const KoColor &color);
0116 
0117     /**
0118      * Return the foregroundColor
0119      */
0120     KoColor foregroundColor() const;
0121 
0122     /**
0123      * Set the backgroundColor resource.
0124      * @param color the new background color
0125      */
0126     void setBackgroundColor(const KoColor &color);
0127     /**
0128      * Return the backgroundColor
0129      */
0130     KoColor backgroundColor() const;
0131 
0132     /**
0133      * Return the resource determined by param key as a boolean.
0134      * @param key the identifying key for the resource
0135      * @see KoCanvasResource::CanvasResourceId
0136      */
0137     bool boolResource(int key) const;
0138 
0139     /**
0140      * Return the resource determined by param key as an integer.
0141      * @param key the identifying key for the resource
0142      * @see KoCanvasResource::CanvasResourceId
0143      */
0144     int intResource(int key) const;
0145 
0146     /**
0147      * Return the resource determined by param key as a KoColor.
0148      * @param key the identifying key for the resource
0149      * @see KoCanvasResource::CanvasResourceId
0150      */
0151     KoColor koColorResource(int key) const;
0152 
0153     /**
0154      * Return the resource determined by param key as a pointer to a KoShape.
0155      * @param key the identifying key for the resource
0156      * @see KoCanvasResource::CanvasResourceId
0157      */
0158     KoShape *koShapeResource(int key) const;
0159 
0160     /**
0161      * Return the resource determined by param key as a QString .
0162      * @param key the identifying key for the resource
0163      * @see KoCanvasResource::CanvasResourceId
0164      */
0165     QString stringResource(int key) const;
0166 
0167     /**
0168      * Return the resource determined by param key as a QSizeF.
0169      * @param key the identifying key for the resource
0170      * @see KoCanvasResource::CanvasResourceId
0171      */
0172     QSizeF sizeResource(int key) const;
0173 
0174     /**
0175      * Return the resource determined by param key as a KoUnit.
0176      * @param key the identifying key for the resource
0177      * @see KoCanvasResource::CanvasResourceId
0178      */
0179     KoUnit unitResource(int key) const;
0180 
0181     /**
0182      * Returns true if there is a resource set with the requested key.
0183      * @param key the identifying key for the resource
0184      * @see KoCanvasResource::CanvasResourceId
0185      */
0186     bool hasResource(int key) const;
0187 
0188     /**
0189      * Remove the resource with @p key from the provider.
0190      * @param key the key that will be used to remove the resource
0191      * There will be a signal emitted with a variable that will return true on QVariable::isNull();
0192      * @see KoCanvasResource::CanvasResourceId
0193      */
0194     void clearResource(int key);
0195 
0196     /**
0197      * @see KoResourceManager::addDerivedResourceConverter()
0198      */
0199     void addDerivedResourceConverter(KoDerivedResourceConverterSP converter);
0200 
0201     /**
0202      * @see KoResourceManager::hasDerivedResourceConverter()
0203      */
0204     bool hasDerivedResourceConverter(int key);
0205 
0206     /**
0207      * @see KoResourceManager::removeDerivedResourceConverter()
0208      */
0209     void removeDerivedResourceConverter(int key);
0210 
0211     /**
0212      * @see KoResourceManager::addResourceUpdateMediator
0213      */
0214     void addResourceUpdateMediator(KoResourceUpdateMediatorSP mediator);
0215 
0216     /**
0217      * @see KoResourceManager::hasResourceUpdateMediator
0218      */
0219     bool hasResourceUpdateMediator(int key);
0220 
0221     /**
0222      * @see KoResourceManager::removeResourceUpdateMediator
0223      */
0224     void removeResourceUpdateMediator(int key);
0225 
0226     /**
0227      * @see KoResourceManager::addActiveCanvasResourceDependency
0228      */
0229     void addActiveCanvasResourceDependency(KoActiveCanvasResourceDependencySP dep);
0230 
0231     /**
0232      * @see KoResourceManager::hasActiveCanvasResourceDependency
0233      */
0234     bool hasActiveCanvasResourceDependency(int sourceKey, int targetKey) const;
0235 
0236     /**
0237      * @see KoResourceManager::removeActiveCanvasResourceDependency
0238      */
0239     void removeActiveCanvasResourceDependency(int sourceKey, int targetKey);
0240 
0241     /**
0242      * An interface for accessing this KoCanvasResourceProvider via
0243      * KoCanvasResourcesInterface.
0244      */
0245     KoCanvasResourcesInterfaceSP canvasResourcesInterface() const;
0246 
0247 Q_SIGNALS:
0248     /**
0249      * This signal is emitted every time a resource is set that is either
0250      * new or different from the previous set value.
0251      * @param key the identifying key for the resource
0252      * @param value the variants new value.
0253      * @see KoCanvasResource::CanvasResourceId
0254      */
0255     void canvasResourceChanged(int key, const QVariant &value);
0256 
0257     /**
0258      * This signal is emitted every time a resource is attempted to be
0259      * changed. The this signal is emitted even when the new value of
0260      * the resource is the same as the current value. This method is called
0261      * **before** the actual change has happended at the resource manager.
0262      * @param key the identifying key for the resource
0263      * @param value the variants new value.
0264      * @see KoCanvasResource::CanvasResourceId
0265      */
0266     void canvasResourceChangeAttempted(int key, const QVariant &value);
0267 
0268 private:
0269     KoCanvasResourceProvider(const KoCanvasResourceProvider&);
0270     KoCanvasResourceProvider& operator=(const KoCanvasResourceProvider&);
0271 
0272     class Private;
0273     Private *const d;
0274 };
0275 
0276 #endif