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

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_RESOURCEMANAGER_P_H
0009 #define KO_RESOURCEMANAGER_P_H
0010 
0011 #include <QObject>
0012 #include <QSizeF>
0013 #include <QHash>
0014 
0015 #include "kritaflake_export.h"
0016 #include <KoColor.h>
0017 #include <KoUnit.h>
0018 #include "KoDerivedResourceConverter.h"
0019 #include "KoResourceUpdateMediator.h"
0020 #include "KoActiveCanvasResourceDependency.h"
0021 
0022 
0023 class KoShape;
0024 class QVariant;
0025 
0026 /**
0027  * @brief The KoResourceManager class provides access to the currently
0028  * active resources for a given canvas. It has nearly zilch to do with
0029  * the system that provides resources like brushes or palettes to the
0030  * application.
0031  */
0032 class KRITAFLAKE_EXPORT KoResourceManager : public QObject
0033 {
0034     Q_OBJECT
0035 public:
0036 
0037     KoResourceManager() {}
0038 
0039     /**
0040      * Set a resource of any type.
0041      * @param key the integer key
0042      * @param value the new value for the key.
0043      * @see KoCanvasResource::CanvasResourceId KoDocumentResourceManager::DocumentResource
0044      */
0045     void setResource(int key, const QVariant &value);
0046 
0047     /**
0048      * Set a resource of type KoColor.
0049      * @param key the integer key
0050      * @param color the new value for the key.
0051      * @see KoCanvasResource::CanvasResourceId KoDocumentResourceManager::DocumentResource
0052      */
0053     void setResource(int key, const KoColor &color);
0054 
0055     /**
0056      * Set a resource of type KoShape*.
0057      * @param key the integer key
0058      * @param shape the new shape for the key.
0059      * @see KoCanvasResource::CanvasResourceId KoDocumentResourceManager::DocumentResource
0060      */
0061     void setResource(int key, KoShape *shape);
0062 
0063     /**
0064      * Set a resource of type KoUnit
0065      * @param key the integer key
0066      * @param unit the unit for the key.
0067      * @see KoCanvasResource::CanvasResourceId KoDocumentResourceManager::DocumentResource
0068      */
0069     void setResource(int key, const KoUnit &unit);
0070 
0071     /**
0072      * Returns a qvariant containing the specified resource or a standard one if the
0073      * specified resource does not exist.
0074      * @param key the key
0075      * @see KoCanvasResource::CanvasResourceId KoDocumentResourceManager::DocumentResource
0076      */
0077     QVariant resource(int key) const;
0078 
0079     /**
0080      * Return the resource determined by param key as a boolean.
0081      * @param key the identifying key for the resource
0082      * @see KoCanvasResource::CanvasResourceId KoDocumentResourceManager::DocumentResource
0083      */
0084     bool boolResource(int key) const;
0085 
0086     /**
0087      * Return the resource determined by param key as an integer.
0088      * @param key the identifying key for the resource
0089      * @see KoCanvasResource::CanvasResourceId KoDocumentResourceManager::DocumentResource
0090      */
0091     int intResource(int key) const;
0092 
0093     /**
0094      * Return the resource determined by param key as a KoColor.
0095      * @param key the identifying key for the resource
0096      * @see KoCanvasResource::CanvasResourceId KoDocumentResourceManager::DocumentResource
0097      */
0098     KoColor koColorResource(int key) const;
0099 
0100     /**
0101      * Return the resource determined by param key as a pointer to a KoShape.
0102      * @param key the identifying key for the resource
0103      * @see KoCanvasResource::CanvasResourceId KoDocumentResourceManager::DocumentResource
0104      */
0105     KoShape *koShapeResource(int key) const;
0106 
0107     /**
0108      * Return the resource determined by param key as a QString .
0109      * @param key the identifying key for the resource
0110      * @see KoCanvasResource::CanvasResourceId KoDocumentResourceManager::DocumentResource
0111      */
0112     QString stringResource(int key) const;
0113 
0114     /**
0115      * Return the resource determined by param key as a QSizeF.
0116      * @param key the identifying key for the resource
0117      * @see KoCanvasResource::CanvasResourceId KoDocumentResourceManager::DocumentResource
0118      */
0119     QSizeF sizeResource(int key) const;
0120 
0121     /**
0122      * Return the resource determined by param key as a KoUnit.
0123      * @param key the identifying key for the resource
0124      * @see KoCanvasResource::CanvasResourceId KoDocumentResourceManager::DocumentResource
0125      */
0126     KoUnit unitResource(int key) const;
0127 
0128     /**
0129      * Returns true if there is a resource set with the requested key.
0130      * @param key the identifying key for the resource
0131      * @see KoCanvasResource::CanvasResourceId KoDocumentResourceManager::DocumentResource
0132      */
0133     bool hasResource(int key) const;
0134 
0135     /**
0136      * Remove the resource with @p key from the provider.
0137      * @param key the key that will be used to remove the resource
0138      * @see KoCanvasResource::CanvasResourceId KoDocumentResourceManager::DocumentResource
0139      */
0140     void clearResource(int key);
0141 
0142     /**
0143      * Some of the resources may be "derived" from the others. For
0144      * example opacity, composite op and erase mode properties are
0145      * contained inside a paintop preset, so we need not create a
0146      * separate resource for them. Instead we created a derived resource,
0147      * that loads/saves values from/to another resource, but has its own
0148      * "resource changed" signal (via a different key).
0149      *
0150      * When a parent resource changes, the resource manager emits
0151      * update signals for all its derived resources.
0152      */
0153     void addDerivedResourceConverter(KoDerivedResourceConverterSP converter);
0154 
0155     /**
0156      * @return true if the resource with \p key is a derived resource
0157      *         (has a converter installed)
0158      *
0159      * @see addDerivedResourceConverter()
0160      */
0161     bool hasDerivedResourceConverter(int key);
0162 
0163     /**
0164      * Removes a derived resource converter. If you rty to add a
0165      * resource with \p key it will be treated as a usual resource.
0166      *
0167      * @see addDerivedResourceConverter()
0168      */
0169     void removeDerivedResourceConverter(int key);
0170 
0171     /**
0172      * Some resources can "mutate", that is their value doesn't change
0173      * (a pointer), whereas the contents changes. But we should still
0174      * notify all the derived resources subscribers that the resource
0175      * has changed. For that purpose we use a special mediator class
0176      * that connects the resource (which is not a QObject at all) and
0177      * the resource manager controls that connection.
0178      */
0179     void addResourceUpdateMediator(KoResourceUpdateMediatorSP mediator);
0180 
0181     /**
0182      * \see addResourceUpdateMediator()
0183      */
0184     bool hasResourceUpdateMediator(int key);
0185 
0186     /**
0187      * \see addResourceUpdateMediator()
0188      */
0189     void removeResourceUpdateMediator(int key);
0190 
0191     /**
0192      * Add a dependency object that will link two active resources. When the
0193      * target of the dependency changes, the source will get a notification
0194      * about the change.
0195      */
0196     void addActiveCanvasResourceDependency(KoActiveCanvasResourceDependencySP dep);
0197 
0198     /**
0199      * @return true if specified dependency exists
0200      *
0201      * \see addActiveCanvasResourceDependency
0202      */
0203     bool hasActiveCanvasResourceDependency(int sourceKey, int targetKey) const;
0204 
0205     /**
0206      * Remove specified dependency
0207      *
0208      * \see addActiveCanvasResourceDependency
0209      */
0210     void removeActiveCanvasResourceDependency(int sourceKey, int targetKey);
0211 
0212 Q_SIGNALS:
0213     void resourceChanged(int key, const QVariant &value);
0214     void resourceChangeAttempted(int key, const QVariant &value);
0215 
0216 private:
0217     void notifyResourceChanged(int key, const QVariant &value);
0218     void notifyDerivedResourcesChanged(int key, const QVariant &value);
0219 
0220     void notifyResourceChangeAttempted(int key, const QVariant &value);
0221     void notifyDerivedResourcesChangeAttempted(int key, const QVariant &value);
0222 
0223     void notifyDependenciesAboutTargetChange(int targetKey, const QVariant &value);
0224 
0225 private Q_SLOTS:
0226     void slotResourceInternalsChanged(int key);
0227 
0228 private:
0229     KoResourceManager(const KoResourceManager&);
0230     KoResourceManager& operator=(const KoResourceManager&);
0231 
0232     QHash<int, QVariant> m_resources;
0233 
0234     QHash<int, KoDerivedResourceConverterSP> m_derivedResources;
0235     QMultiHash<int, KoDerivedResourceConverterSP> m_derivedFromSource;
0236 
0237     QMultiHash<int, KoActiveCanvasResourceDependencySP> m_dependencyFromSource;
0238     QMultiHash<int, KoActiveCanvasResourceDependencySP> m_dependencyFromTarget;
0239 
0240     QHash<int, KoResourceUpdateMediatorSP> m_updateMediators;
0241 };
0242 
0243 #endif
0244