File indexing completed on 2024-05-19 04:24:47
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 * Tools that provide a handle for controlling the content that the tool can edit can 0183 * use this property to alter the radius that a circular handle should have on screen. 0184 * @param handleSize the radius in pixels. 0185 */ 0186 void setHandleRadius(int handleSize); 0187 /// Returns the actual handle radius 0188 int handleRadius() const; 0189 0190 /** 0191 * @brief setDecorationThickness 0192 * Set the minimum decoration thickness. 0193 * @param decorationThickness the minimum decoration thickness in pixels. 0194 */ 0195 void setDecorationThickness(int decorationThickness); 0196 0197 /** 0198 * @brief decorationThickness 0199 * @return the minimum decoration thickness in pixels. 0200 */ 0201 int decorationThickness() const; 0202 0203 /** 0204 * Returns true if there is a resource set with the requested key. 0205 * @param key the identifying key for the resource 0206 * @see KoCanvasResource::CanvasResourceId 0207 */ 0208 bool hasResource(int key) const; 0209 0210 /** 0211 * Remove the resource with @p key from the provider. 0212 * @param key the key that will be used to remove the resource 0213 * There will be a signal emitted with a variable that will return true on QVariable::isNull(); 0214 * @see KoCanvasResource::CanvasResourceId 0215 */ 0216 void clearResource(int key); 0217 0218 /** 0219 * @see KoResourceManager::addDerivedResourceConverter() 0220 */ 0221 void addDerivedResourceConverter(KoDerivedResourceConverterSP converter); 0222 0223 /** 0224 * @see KoResourceManager::hasDerivedResourceConverter() 0225 */ 0226 bool hasDerivedResourceConverter(int key); 0227 0228 /** 0229 * @see KoResourceManager::removeDerivedResourceConverter() 0230 */ 0231 void removeDerivedResourceConverter(int key); 0232 0233 /** 0234 * @see KoResourceManager::addResourceUpdateMediator 0235 */ 0236 void addResourceUpdateMediator(KoResourceUpdateMediatorSP mediator); 0237 0238 /** 0239 * @see KoResourceManager::hasResourceUpdateMediator 0240 */ 0241 bool hasResourceUpdateMediator(int key); 0242 0243 /** 0244 * @see KoResourceManager::removeResourceUpdateMediator 0245 */ 0246 void removeResourceUpdateMediator(int key); 0247 0248 /** 0249 * @see KoResourceManager::addActiveCanvasResourceDependency 0250 */ 0251 void addActiveCanvasResourceDependency(KoActiveCanvasResourceDependencySP dep); 0252 0253 /** 0254 * @see KoResourceManager::hasActiveCanvasResourceDependency 0255 */ 0256 bool hasActiveCanvasResourceDependency(int sourceKey, int targetKey) const; 0257 0258 /** 0259 * @see KoResourceManager::removeActiveCanvasResourceDependency 0260 */ 0261 void removeActiveCanvasResourceDependency(int sourceKey, int targetKey); 0262 0263 /** 0264 * An interface for accessing this KoCanvasResourceProvider via 0265 * KoCanvasResourcesInterface. 0266 */ 0267 KoCanvasResourcesInterfaceSP canvasResourcesInterface() const; 0268 0269 Q_SIGNALS: 0270 /** 0271 * This signal is emitted every time a resource is set that is either 0272 * new or different from the previous set value. 0273 * @param key the identifying key for the resource 0274 * @param value the variants new value. 0275 * @see KoCanvasResource::CanvasResourceId 0276 */ 0277 void canvasResourceChanged(int key, const QVariant &value); 0278 0279 /** 0280 * This signal is emitted every time a resource is attempted to be 0281 * changed. The this signal is emitted even when the new value of 0282 * the resource is the same as the current value. This method is called 0283 * **before** the actual change has happened at the resource manager. 0284 * @param key the identifying key for the resource 0285 * @param value the variants new value. 0286 * @see KoCanvasResource::CanvasResourceId 0287 */ 0288 void canvasResourceChangeAttempted(int key, const QVariant &value); 0289 0290 private: 0291 KoCanvasResourceProvider(const KoCanvasResourceProvider&); 0292 KoCanvasResourceProvider& operator=(const KoCanvasResourceProvider&); 0293 0294 class Private; 0295 Private *const d; 0296 }; 0297 0298 #endif