1 /*
2    Copyright (c) 2006, 2011 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_CANVASRESOURCEMANAGER_H
22 #define KO_CANVASRESOURCEMANAGER_H
23 
24 #include <QObject>
25 
26 #include "kritaflake_export.h"
27 #include "KoDerivedResourceConverter.h"
28 #include "KoResourceUpdateMediator.h"
29 
30 class KoShape;
31 class KoShapeStroke;
32 class KoColor;
33 class KoUnit;
34 
35 class QVariant;
36 class QSizeF;
37 
38 /**
39  * The KoCanvasResourceProvider contains a set of per-canvas
40  * properties, like current foreground color, current background
41  * color and more. All tools belonging to the current canvas are
42  * notified when a Resource changes (is set).
43  *
44  * The manager can contain all sorts of variable types and there are accessors
45  * for the most common ones.  All variables are always stored inside a QVariant
46  * instance internally and you can always just use the resource() method to get
47  * that directly.
48  * The way to store arbitrary data objects that are stored as pointers you can use
49  * the following code snippets;
50  * @code
51  *  QVariant variant;
52  *  variant.setValue<void*>(textShapeData->document());
53  *  resourceManager->setResource(KoText::CurrentTextDocument, variant);
54  *  // and get it out again.
55  *  QVariant var = resourceManager->resource(KoText::CurrentTextDocument);
56  *  document = static_cast<QTextDocument*>(var.value<void*>());
57  * @endcode
58  */
59 class KRITAFLAKE_EXPORT KoCanvasResourceProvider : public QObject
60 {
61     Q_OBJECT
62 
63 public:
64 
65     /**
66      * This enum holds identifiers to the resources that can be stored in here.
67      */
68     enum CanvasResource {
69         ForegroundColor,    ///< The active foreground color selected for this canvas.
70         BackgroundColor,    ///< The active background color selected for this canvas.
71         PageSize,           ///< The size of the (current) page in postscript points.
72         Unit,               ///< The unit of this canvas
73         CurrentPage,        ///< The current page number
74         ActiveStyleType,    ///< the actual active style type see KoFlake::StyleType for valid values
75         ActiveRange,        ///< The area where the rulers should show white
76         ShowTextShapeOutlines,     ///< Paint of text shape outlines ?
77         ShowFormattingCharacters,  ///< Paint of formatting characters ?
78         ShowTableBorders,  ///< Paint of table borders (when not really there) ?
79         ShowSectionBounds, ///< Paint of sections bounds ?
80         ShowInlineObjectVisualization, ///< paint a different  background for inline objects
81         ApplicationSpeciality, ///< Special features and limitations of the application
82         KarbonStart = 1000,      ///< Base number for Karbon specific values.
83         KexiStart = 2000,        ///< Base number for Kexi specific values.
84         FlowStart = 3000,        ///< Base number for Flow specific values.
85         PlanStart = 4000,        ///< Base number for Plan specific values.
86         StageStart = 5000,       ///< Base number for Stage specific values.
87         KritaStart = 6000,       ///< Base number for Krita specific values.
88         SheetsStart = 7000,      ///< Base number for Sheets specific values.
89         WordsStart = 8000,       ///< Base number for Words specific values.
90         KoPageAppStart = 9000    ///< Base number for KoPageApp specific values.
91     };
92 
93     enum ApplicationSpecial {
94         NoSpecial = 0,
95         NoAdvancedText = 1
96     };
97 
98     /**
99      * Constructor.
100      * @param parent the parent QObject, used for memory management.
101      */
102     explicit KoCanvasResourceProvider(QObject *parent = 0);
103     ~KoCanvasResourceProvider() override;
104 
105 public Q_SLOTS:
106     /**
107      * Set a resource of any type.
108      * @param key the integer key
109      * @param value the new value for the key.
110      * @see KoCanvasResourceProvider::CanvasResource
111      */
112     void setResource(int key, const QVariant &value);
113 
114     /**
115      * Set a resource of type KoColor.
116      * @param key the integer key
117      * @param color the new value for the key.
118      * @see KoCanvasResourceProvider::CanvasResource
119      */
120     void setResource(int key, const KoColor &color);
121 
122     /**
123      * Set a resource of type KoShape*.
124      * @param key the integer key
125      * @param id the new value for the key.
126      * @see KoCanvasResourceProvider::CanvasResource
127      */
128     void setResource(int key, KoShape *shape);
129 
130     /**
131      * Set a resource of type KoUnit
132      * @param key the integer key
133      * @param id the new value for the key.
134      * @see KoCanvasResourceProvider::CanvasResource
135      */
136     void setResource(int key, const KoUnit &unit);
137 
138 public:
139     /**
140      * Returns a qvariant containing the specified resource or a standard one if the
141      * specified resource does not exist.
142      * @param key the key
143      * @see KoCanvasResourceProvider::CanvasResource
144      */
145     QVariant resource(int key) const;
146 
147     /**
148      * Set the foregroundColor resource.
149      * @param color the new foreground color
150      */
151     void setForegroundColor(const KoColor &color);
152 
153     /**
154      * Return the foregroundColor
155      */
156     KoColor foregroundColor() const;
157 
158     /**
159      * Set the backgroundColor resource.
160      * @param color the new background color
161      */
162     void setBackgroundColor(const KoColor &color);
163     /**
164      * Return the backgroundColor
165      */
166     KoColor backgroundColor() const;
167 
168     /**
169      * Return the resource determined by param key as a boolean.
170      * @param key the identifying key for the resource
171      * @see KoCanvasResourceProvider::CanvasResource
172      */
173     bool boolResource(int key) const;
174 
175     /**
176      * Return the resource determined by param key as an integer.
177      * @param key the identifying key for the resource
178      * @see KoCanvasResourceProvider::CanvasResource
179      */
180     int intResource(int key) const;
181 
182     /**
183      * Return the resource determined by param key as a KoColor.
184      * @param key the identifying key for the resource
185      * @see KoCanvasResourceProvider::CanvasResource
186      */
187     KoColor koColorResource(int key) const;
188 
189     /**
190      * Return the resource determined by param key as a pointer to a KoShape.
191      * @param key the identifying key for the resource
192      * @see KoCanvasResourceProvider::CanvasResource
193      */
194     KoShape *koShapeResource(int key) const;
195 
196     /**
197      * Return the resource determined by param key as a QString .
198      * @param key the identifying key for the resource
199      * @see KoCanvasResourceProvider::CanvasResource
200      */
201     QString stringResource(int key) const;
202 
203     /**
204      * Return the resource determined by param key as a QSizeF.
205      * @param key the identifying key for the resource
206      * @see KoCanvasResourceProvider::CanvasResource
207      */
208     QSizeF sizeResource(int key) const;
209 
210     /**
211      * Return the resource determined by param key as a KoUnit.
212      * @param key the identifying key for the resource
213      * @see KoCanvasResourceProvider::CanvasResource
214      */
215     KoUnit unitResource(int key) const;
216 
217     /**
218      * Returns true if there is a resource set with the requested key.
219      * @param key the identifying key for the resource
220      * @see KoCanvasResourceProvider::CanvasResource
221      */
222     bool hasResource(int key) const;
223 
224     /**
225      * Remove the resource with @p key from the provider.
226      * @param key the key that will be used to remove the resource
227      * There will be a signal emitted with a variable that will return true on QVariable::isNull();
228      * @see KoCanvasResourceProvider::CanvasResource
229      */
230     void clearResource(int key);
231 
232     /**
233      * @see KoReosurceManager::addDerivedResourceConverter()
234      */
235     void addDerivedResourceConverter(KoDerivedResourceConverterSP converter);
236 
237     /**
238      * @see KoReosurceManager::hasDerivedResourceConverter()
239      */
240     bool hasDerivedResourceConverter(int key);
241 
242     /**
243      * @see KoReosurceManager::removeDerivedResourceConverter()
244      */
245     void removeDerivedResourceConverter(int key);
246 
247     /**
248      * @see KoReosurceManager::addResourceUpdateMediator
249      */
250     void addResourceUpdateMediator(KoResourceUpdateMediatorSP mediator);
251 
252     /**
253      * @see KoReosurceManager::hasResourceUpdateMediator
254      */
255     bool hasResourceUpdateMediator(int key);
256 
257     /**
258 
259      * @see KoReosurceManager::removeResourceUpdateMediator
260      */
261     void removeResourceUpdateMediator(int key);
262 
263 Q_SIGNALS:
264     /**
265      * This signal is emitted every time a resource is set that is either
266      * new or different from the previous set value.
267      * @param key the identifying key for the resource
268      * @param value the variants new value.
269      * @see KoCanvasResourceProvider::CanvasResource
270      */
271     void canvasResourceChanged(int key, const QVariant &value);
272 
273     /**
274      * This signal is emitted every time a resource is attempted to be
275      * changed. The this signal is emitted even when the new value of
276      * the resource is the same as the current value. This method is called
277      * **before** the actual change has happended at the resource manager.
278      * @param key the identifying key for the resource
279      * @param value the variants new value.
280      * @see KoCanvasResourceProvider::CanvasResource
281      */
282     void canvasResourceChangeAttempted(int key, const QVariant &value);
283 
284 private:
285     KoCanvasResourceProvider(const KoCanvasResourceProvider&);
286     KoCanvasResourceProvider& operator=(const KoCanvasResourceProvider&);
287 
288     class Private;
289     Private *const d;
290 };
291 
292 #endif
293