File indexing completed on 2024-05-19 04:24:48
0001 /* 0002 SPDX-FileCopyrightText: 2006 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_DOCUMENTRESOURCEMANAGER_H 0009 #define KO_DOCUMENTRESOURCEMANAGER_H 0010 0011 #include <QObject> 0012 0013 #include "kritaflake_export.h" 0014 0015 class KoShape; 0016 class KUndo2Stack; 0017 class KoShapeController; 0018 class KoColor; 0019 class KoUnit; 0020 0021 class QVariant; 0022 class QSizeF; 0023 0024 /** 0025 * The KoResourceManager contains a set of per-canvas <i>or</i> per-document 0026 * properties, like current foreground color, current background 0027 * color and more. All tools belonging to the current canvas are 0028 * notified when a Resource changes (is set). 0029 * 0030 * The properties come from the KoDocumentResourceManager::DocumentResource 0031 * See KoShapeController::resourceManager 0032 * 0033 * The manager can contain all sorts of variable types and there are accessors 0034 * for the most common ones. All variables are always stored inside a QVariant 0035 * instance internally and you can always just use the resource() method to get 0036 * that directly. 0037 * The way to store arbitrary data objects that are stored as pointers you can use 0038 * the following code snippets; 0039 * @code 0040 * QVariant variant; 0041 * variant.setValue<void*>(textShapeData->document()); 0042 * resourceManager->setResource(KoText::CurrentTextDocument, variant); 0043 * // and get it out again. 0044 * QVariant var = resourceManager->resource(KoText::CurrentTextDocument); 0045 * document = static_cast<QTextDocument*>(var.value<void*>()); 0046 * @endcode 0047 */ 0048 class KRITAFLAKE_EXPORT KoDocumentResourceManager : public QObject 0049 { 0050 Q_OBJECT 0051 0052 public: 0053 0054 /** 0055 * This enum holds identifiers to the resources that can be stored in here. 0056 */ 0057 enum DocumentResource { 0058 UndoStack, ///< The document-wide undo stack (KUndo2Stack) 0059 OdfDocument, ///< OBSOLETE The document this canvas shows 0060 GrabSensitivity, ///< The grab sensitivity used for grabbing handles of any kind 0061 MarkerCollection, ///< The collection holding all markers 0062 GlobalShapeController, ///< The KoShapeController for the document 0063 DocumentResolution, ///< Pixels-per-inch resoluton of the document 0064 DocumentRectInPixels, ///< Bounds of the document in pixels 0065 0066 KarbonStart = 1000, ///< Base number for Karbon specific values. 0067 KexiStart = 2000, ///< Base number for Kexi specific values. 0068 FlowStart = 3000, ///< Base number for Flow specific values. 0069 PlanStart = 4000, ///< Base number for Plan specific values. 0070 StageStart = 5000, ///< Base number for Stage specific values. 0071 KritaStart = 6000, ///< Base number for Krita specific values. 0072 SheetsStart = 7000, ///< Base number for Sheets specific values. 0073 WordsStart = 8000, ///< Base number for Words specific values. 0074 KoPageAppStart = 9000, ///< Base number for KoPageApp specific values. 0075 KoTextStart = 10000 ///< Base number for KoText specific values. 0076 }; 0077 0078 0079 /** 0080 * Constructor. 0081 * @param parent the parent QObject, used for memory management. 0082 */ 0083 explicit KoDocumentResourceManager(QObject *parent = 0); 0084 ~KoDocumentResourceManager() override; 0085 0086 /** 0087 * Set a resource of any type. 0088 * @param key the integer key 0089 * @param value the new value for the key. 0090 * @see KoDocumentResourceManager::DocumentResource 0091 */ 0092 void setResource(int key, const QVariant &value); 0093 0094 /** 0095 * Set a resource of type KoColor. 0096 * @param key the integer key 0097 * @param color the new value for the key. 0098 * @see KoDocumentResourceManager::DocumentResource 0099 */ 0100 void setResource(int key, const KoColor &color); 0101 0102 /** 0103 * Set a resource of type KoShape*. 0104 * @param key the integer key 0105 * @param id the new value for the key. 0106 * @see KoDocumentResourceManager::DocumentResource 0107 */ 0108 void setResource(int key, KoShape *shape); 0109 0110 /** 0111 * Set a resource of type KoUnit 0112 * @param key the integer key 0113 * @param id the new value for the key. 0114 * @see KoDocumentResourceManager::DocumentResource 0115 */ 0116 void setResource(int key, const KoUnit &unit); 0117 0118 /** 0119 * Returns a qvariant containing the specified resource or a standard one if the 0120 * specified resource does not exist. 0121 * @param key the key 0122 * @see KoDocumentResourceManager::DocumentResource 0123 */ 0124 QVariant resource(int key) const; 0125 0126 /** 0127 * Return the resource determined by param key as a boolean. 0128 * @param key the identifying key for the resource 0129 * @see KoDocumentResourceManager::DocumentResource 0130 */ 0131 bool boolResource(int key) const; 0132 0133 /** 0134 * Return the resource determined by param key as an integer. 0135 * @param key the identifying key for the resource 0136 * @see KoDocumentResourceManager::DocumentResource 0137 */ 0138 int intResource(int key) const; 0139 0140 /** 0141 * Return the resource determined by param key as a KoColor. 0142 * @param key the identifying key for the resource 0143 * @see KoDocumentResourceManager::DocumentResource 0144 */ 0145 KoColor koColorResource(int key) const; 0146 0147 /** 0148 * Return the resource determined by param key as a pointer to a KoShape. 0149 * @param key the identifying key for the resource 0150 * @see KoDocumentResourceManager::DocumentResource 0151 */ 0152 KoShape *koShapeResource(int key) const; 0153 0154 /** 0155 * Return the resource determined by param key as a QString . 0156 * @param key the identifying key for the resource 0157 * @see KoDocumentResourceManager::DocumentResource 0158 */ 0159 QString stringResource(int key) const; 0160 0161 /** 0162 * Return the resource determined by param key as a QSizeF. 0163 * @param key the identifying key for the resource 0164 * @see KoDocumentResourceManager::DocumentResource 0165 */ 0166 QSizeF sizeResource(int key) const; 0167 0168 /** 0169 * Return the resource determined by param key as a KoUnit. 0170 * @param key the identifying key for the resource 0171 * @see KoDocumentResourceManager::DocumentResource 0172 */ 0173 KoUnit unitResource(int key) const; 0174 0175 /** 0176 * Returns true if there is a resource set with the requested key. 0177 * @param key the identifying key for the resource 0178 * @see KoDocumentResourceManager::DocumentResource 0179 */ 0180 bool hasResource(int key) const; 0181 0182 /** 0183 * Remove the resource with @p key from the provider. 0184 * @param key the key that will be used to remove the resource 0185 * There will be a signal emitted with a variable that will return true on QVariable::isNull(); 0186 * @see KoDocumentResourceManager::DocumentResource 0187 */ 0188 void clearResource(int key); 0189 0190 0191 0192 /** 0193 * Tools that are used to grab handles or similar with the mouse 0194 * should use this value to determine if the mouse is near enough 0195 * @param grabSensitivity the grab sensitivity in pixels 0196 */ 0197 void setGrabSensitivity(int grabSensitivity); 0198 /// Returns the actual grab sensitivity 0199 int grabSensitivity() const; 0200 0201 KUndo2Stack *undoStack() const; 0202 void setUndoStack(KUndo2Stack *undoStack); 0203 0204 qreal documentResolution() const; 0205 QRectF documentRectInPixels() const; 0206 0207 /** 0208 * TODO: remove these methods after legacy ODF text shape is removed. 0209 * New code must use documentResolution() and documentRectInPixels() 0210 * instead. 0211 */ 0212 Q_DECL_DEPRECATED KoShapeController *globalShapeController() const; 0213 Q_DECL_DEPRECATED void setGlobalShapeController(KoShapeController *globalShapeController); 0214 0215 Q_SIGNALS: 0216 /** 0217 * This signal is emitted every time a resource is set that is either 0218 * new or different from the previous set value. 0219 * @param key the identifying key for the resource 0220 * @param value the variants new value. 0221 * @see KoDocumentResourceManager::DocumentResource 0222 */ 0223 void resourceChanged(int key, const QVariant &value); 0224 0225 private: 0226 KoDocumentResourceManager(const KoDocumentResourceManager&); 0227 KoDocumentResourceManager& operator=(const KoDocumentResourceManager&); 0228 0229 class Private; 0230 Private *const d; 0231 }; 0232 0233 #endif