1 /*
2    Copyright (c) 2006 Boudewijn Rempt (boud@valdyas.org)
3    Copyright (C) 2007, 2009, 2010 Thomas Zander <zander@kde.org>
4    Copyright (c) 2008 Carlos Licea <carlos.licea@kdemail.net>
5 
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public
8    License as published by the Free Software Foundation; either
9    version 2 of the License, or (at your option) any later version.
10 
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15 
16    You should have received a copy of the GNU Library General Public License
17    along with this library; see the file COPYING.LIB.  If not, write to
18    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19    Boston, MA 02110-1301, USA.
20  */
21 #ifndef KO_DOCUMENTRESOURCEMANAGER_H
22 #define KO_DOCUMENTRESOURCEMANAGER_H
23 
24 #include <QObject>
25 
26 #include "kritaflake_export.h"
27 
28 class KoShape;
29 class KUndo2Stack;
30 class KoImageCollection;
31 class KoDocumentBase;
32 class KoShapeController;
33 class KoColor;
34 class KoUnit;
35 
36 class QVariant;
37 class QSizeF;
38 
39 /**
40  * The KoResourceManager contains a set of per-canvas <i>or</i> per-document
41  * properties, like current foreground color, current background
42  * color and more. All tools belonging to the current canvas are
43  * notified when a Resource changes (is set).
44  *
45  * The properties come from the KoDocumentResourceManager::DocumentResource
46  * See KoShapeController::resourceManager
47  *
48  * The manager can contain all sorts of variable types and there are accessors
49  * for the most common ones.  All variables are always stored inside a QVariant
50  * instance internally and you can always just use the resource() method to get
51  * that directly.
52  * The way to store arbitrary data objects that are stored as pointers you can use
53  * the following code snippets;
54  * @code
55  *  QVariant variant;
56  *  variant.setValue<void*>(textShapeData->document());
57  *  resourceManager->setResource(KoText::CurrentTextDocument, variant);
58  *  // and get it out again.
59  *  QVariant var = resourceManager->resource(KoText::CurrentTextDocument);
60  *  document = static_cast<QTextDocument*>(var.value<void*>());
61  * @endcode
62  */
63 class KRITAFLAKE_EXPORT KoDocumentResourceManager : public QObject
64 {
65     Q_OBJECT
66 
67 public:
68 
69     /**
70      * This enum holds identifiers to the resources that can be stored in here.
71      */
72 enum DocumentResource {
73     UndoStack,              ///< The document-wide undo stack (KUndo2Stack)
74     ImageCollection,        ///< The KoImageCollection for the document
75     OdfDocument,            ///< The document this canvas shows (KoDocumentBase)
76     HandleRadius,           ///< The handle radius used for drawing handles of any kind
77     GrabSensitivity,        ///< The grab sensitivity used for grabbing handles of any kind
78     MarkerCollection,       ///< The collection holding all markers
79     GlobalShapeController,  ///< The KoShapeController for the document
80     DocumentResolution,     ///< Pixels-per-inch resoluton of the document
81     DocumentRectInPixels,   ///< Bounds of the document in pixels
82 
83     KarbonStart = 1000,      ///< Base number for Karbon specific values.
84     KexiStart = 2000,        ///< Base number for Kexi specific values.
85     FlowStart = 3000,        ///< Base number for Flow specific values.
86     PlanStart = 4000,        ///< Base number for Plan specific values.
87     StageStart = 5000,       ///< Base number for Stage specific values.
88     KritaStart = 6000,       ///< Base number for Krita specific values.
89     SheetsStart = 7000,      ///< Base number for Sheets specific values.
90     WordsStart = 8000,       ///< Base number for Words specific values.
91     KoPageAppStart = 9000,   ///< Base number for KoPageApp specific values.
92     KoTextStart = 10000      ///< Base number for KoText specific values.
93 };
94 
95 
96     /**
97      * Constructor.
98      * @param parent the parent QObject, used for memory management.
99      */
100     explicit KoDocumentResourceManager(QObject *parent = 0);
101     ~KoDocumentResourceManager() override;
102 
103     /**
104      * Set a resource of any type.
105      * @param key the integer key
106      * @param value the new value for the key.
107      * @see  KoDocumentResourceManager::DocumentResource
108      */
109     void setResource(int key, const QVariant &value);
110 
111     /**
112      * Set a resource of type KoColor.
113      * @param key the integer key
114      * @param color the new value for the key.
115      * @see  KoDocumentResourceManager::DocumentResource
116      */
117     void setResource(int key, const KoColor &color);
118 
119     /**
120      * Set a resource of type KoShape*.
121      * @param key the integer key
122      * @param id the new value for the key.
123      * @see  KoDocumentResourceManager::DocumentResource
124      */
125     void setResource(int key, KoShape *shape);
126 
127     /**
128      * Set a resource of type KoUnit
129      * @param key the integer key
130      * @param id the new value for the key.
131      * @see  KoDocumentResourceManager::DocumentResource
132      */
133     void setResource(int key, const KoUnit &unit);
134 
135     /**
136      * Returns a qvariant containing the specified resource or a standard one if the
137      * specified resource does not exist.
138      * @param key the key
139      * @see  KoDocumentResourceManager::DocumentResource
140      */
141     QVariant resource(int key) const;
142 
143     /**
144      * Return the resource determined by param key as a boolean.
145      * @param key the identifying key for the resource
146      * @see  KoDocumentResourceManager::DocumentResource
147      */
148     bool boolResource(int key) const;
149 
150     /**
151      * Return the resource determined by param key as an integer.
152      * @param key the identifying key for the resource
153      * @see  KoDocumentResourceManager::DocumentResource
154      */
155     int intResource(int key) const;
156 
157     /**
158      * Return the resource determined by param key as a KoColor.
159      * @param key the identifying key for the resource
160      * @see  KoDocumentResourceManager::DocumentResource
161      */
162     KoColor koColorResource(int key) const;
163 
164     /**
165      * Return the resource determined by param key as a pointer to a KoShape.
166      * @param key the identifying key for the resource
167      * @see  KoDocumentResourceManager::DocumentResource
168      */
169     KoShape *koShapeResource(int key) const;
170 
171     /**
172      * Return the resource determined by param key as a QString .
173      * @param key the identifying key for the resource
174      * @see  KoDocumentResourceManager::DocumentResource
175      */
176     QString stringResource(int key) const;
177 
178     /**
179      * Return the resource determined by param key as a QSizeF.
180      * @param key the identifying key for the resource
181      * @see  KoDocumentResourceManager::DocumentResource
182      */
183     QSizeF sizeResource(int key) const;
184 
185     /**
186      * Return the resource determined by param key as a KoUnit.
187      * @param key the identifying key for the resource
188      * @see  KoDocumentResourceManager::DocumentResource
189      */
190     KoUnit unitResource(int key) const;
191 
192     /**
193      * Returns true if there is a resource set with the requested key.
194      * @param key the identifying key for the resource
195      * @see  KoDocumentResourceManager::DocumentResource
196      */
197     bool hasResource(int key) const;
198 
199     /**
200      * Remove the resource with @p key from the provider.
201      * @param key the key that will be used to remove the resource
202      * There will be a signal emitted with a variable that will return true on QVariable::isNull();
203      * @see  KoDocumentResourceManager::DocumentResource
204      */
205     void clearResource(int key);
206 
207         /**
208      * Tools that provide a handle for controlling the content that the tool can edit can
209      * use this property to alter the radius that a circular handle should have on screen.
210      * @param handleSize the radius in pixels.
211      */
212     void setHandleRadius(int handleSize);
213     /// Returns the actual handle radius
214     int handleRadius() const;
215 
216     /**
217      * Tools that are used to grab handles or similar with the mouse
218      * should use this value to determine if the mouse is near enough
219      * @param grabSensitivity the grab sensitivity in pixels
220      */
221     void setGrabSensitivity(int grabSensitivity);
222     /// Returns the actual grab sensitivity
223     int grabSensitivity() const;
224 
225     KUndo2Stack *undoStack() const;
226     void setUndoStack(KUndo2Stack *undoStack);
227 
228     KoImageCollection *imageCollection() const;
229     void setImageCollection(KoImageCollection *ic);
230 
231     KoDocumentBase *odfDocument() const;
232     void setOdfDocument(KoDocumentBase *currentDocument);
233 
234     qreal documentResolution() const;
235     QRectF documentRectInPixels() const;
236 
237     /**
238      * TODO: remove these methods after legacy ODF text shape is removed.
239      * New code must use documentResolution() and documentRectInPixels()
240      * instead.
241      */
242     Q_DECL_DEPRECATED KoShapeController *globalShapeController() const;
243     Q_DECL_DEPRECATED void setGlobalShapeController(KoShapeController *globalShapeController);
244 
245 Q_SIGNALS:
246     /**
247      * This signal is emitted every time a resource is set that is either
248      * new or different from the previous set value.
249      * @param key the identifying key for the resource
250      * @param value the variants new value.
251      * @see KoDocumentResourceManager::DocumentResource
252      */
253     void resourceChanged(int key, const QVariant &value);
254 
255 private:
256     KoDocumentResourceManager(const KoDocumentResourceManager&);
257     KoDocumentResourceManager& operator=(const KoDocumentResourceManager&);
258 
259     class Private;
260     Private *const d;
261 };
262 
263 #endif
264