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

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     HandleRadius,           ///< The handle radius used for drawing handles of any kind
0061     GrabSensitivity,        ///< The grab sensitivity used for grabbing handles of any kind
0062     MarkerCollection,       ///< The collection holding all markers
0063     GlobalShapeController,  ///< The KoShapeController for the document
0064     DocumentResolution,     ///< Pixels-per-inch resoluton of the document
0065     DocumentRectInPixels,   ///< Bounds of the document in pixels
0066 
0067     KarbonStart = 1000,      ///< Base number for Karbon specific values.
0068     KexiStart = 2000,        ///< Base number for Kexi specific values.
0069     FlowStart = 3000,        ///< Base number for Flow specific values.
0070     PlanStart = 4000,        ///< Base number for Plan specific values.
0071     StageStart = 5000,       ///< Base number for Stage specific values.
0072     KritaStart = 6000,       ///< Base number for Krita specific values.
0073     SheetsStart = 7000,      ///< Base number for Sheets specific values.
0074     WordsStart = 8000,       ///< Base number for Words specific values.
0075     KoPageAppStart = 9000,   ///< Base number for KoPageApp specific values.
0076     KoTextStart = 10000      ///< Base number for KoText specific values.
0077 };
0078 
0079 
0080     /**
0081      * Constructor.
0082      * @param parent the parent QObject, used for memory management.
0083      */
0084     explicit KoDocumentResourceManager(QObject *parent = 0);
0085     ~KoDocumentResourceManager() override;
0086 
0087     /**
0088      * Set a resource of any type.
0089      * @param key the integer key
0090      * @param value the new value for the key.
0091      * @see  KoDocumentResourceManager::DocumentResource
0092      */
0093     void setResource(int key, const QVariant &value);
0094 
0095     /**
0096      * Set a resource of type KoColor.
0097      * @param key the integer key
0098      * @param color the new value for the key.
0099      * @see  KoDocumentResourceManager::DocumentResource
0100      */
0101     void setResource(int key, const KoColor &color);
0102 
0103     /**
0104      * Set a resource of type KoShape*.
0105      * @param key the integer key
0106      * @param id the new value for the key.
0107      * @see  KoDocumentResourceManager::DocumentResource
0108      */
0109     void setResource(int key, KoShape *shape);
0110 
0111     /**
0112      * Set a resource of type KoUnit
0113      * @param key the integer key
0114      * @param id the new value for the key.
0115      * @see  KoDocumentResourceManager::DocumentResource
0116      */
0117     void setResource(int key, const KoUnit &unit);
0118 
0119     /**
0120      * Returns a qvariant containing the specified resource or a standard one if the
0121      * specified resource does not exist.
0122      * @param key the key
0123      * @see  KoDocumentResourceManager::DocumentResource
0124      */
0125     QVariant resource(int key) const;
0126 
0127     /**
0128      * Return the resource determined by param key as a boolean.
0129      * @param key the identifying key for the resource
0130      * @see  KoDocumentResourceManager::DocumentResource
0131      */
0132     bool boolResource(int key) const;
0133 
0134     /**
0135      * Return the resource determined by param key as an integer.
0136      * @param key the identifying key for the resource
0137      * @see  KoDocumentResourceManager::DocumentResource
0138      */
0139     int intResource(int key) const;
0140 
0141     /**
0142      * Return the resource determined by param key as a KoColor.
0143      * @param key the identifying key for the resource
0144      * @see  KoDocumentResourceManager::DocumentResource
0145      */
0146     KoColor koColorResource(int key) const;
0147 
0148     /**
0149      * Return the resource determined by param key as a pointer to a KoShape.
0150      * @param key the identifying key for the resource
0151      * @see  KoDocumentResourceManager::DocumentResource
0152      */
0153     KoShape *koShapeResource(int key) const;
0154 
0155     /**
0156      * Return the resource determined by param key as a QString .
0157      * @param key the identifying key for the resource
0158      * @see  KoDocumentResourceManager::DocumentResource
0159      */
0160     QString stringResource(int key) const;
0161 
0162     /**
0163      * Return the resource determined by param key as a QSizeF.
0164      * @param key the identifying key for the resource
0165      * @see  KoDocumentResourceManager::DocumentResource
0166      */
0167     QSizeF sizeResource(int key) const;
0168 
0169     /**
0170      * Return the resource determined by param key as a KoUnit.
0171      * @param key the identifying key for the resource
0172      * @see  KoDocumentResourceManager::DocumentResource
0173      */
0174     KoUnit unitResource(int key) const;
0175 
0176     /**
0177      * Returns true if there is a resource set with the requested key.
0178      * @param key the identifying key for the resource
0179      * @see  KoDocumentResourceManager::DocumentResource
0180      */
0181     bool hasResource(int key) const;
0182 
0183     /**
0184      * Remove the resource with @p key from the provider.
0185      * @param key the key that will be used to remove the resource
0186      * There will be a signal emitted with a variable that will return true on QVariable::isNull();
0187      * @see  KoDocumentResourceManager::DocumentResource
0188      */
0189     void clearResource(int key);
0190 
0191         /**
0192      * Tools that provide a handle for controlling the content that the tool can edit can
0193      * use this property to alter the radius that a circular handle should have on screen.
0194      * @param handleSize the radius in pixels.
0195      */
0196     void setHandleRadius(int handleSize);
0197     /// Returns the actual handle radius
0198     int handleRadius() const;
0199 
0200     /**
0201      * Tools that are used to grab handles or similar with the mouse
0202      * should use this value to determine if the mouse is near enough
0203      * @param grabSensitivity the grab sensitivity in pixels
0204      */
0205     void setGrabSensitivity(int grabSensitivity);
0206     /// Returns the actual grab sensitivity
0207     int grabSensitivity() const;
0208 
0209     KUndo2Stack *undoStack() const;
0210     void setUndoStack(KUndo2Stack *undoStack);
0211 
0212     qreal documentResolution() const;
0213     QRectF documentRectInPixels() const;
0214 
0215     /**
0216      * TODO: remove these methods after legacy ODF text shape is removed.
0217      * New code must use documentResolution() and documentRectInPixels()
0218      * instead.
0219      */
0220     Q_DECL_DEPRECATED KoShapeController *globalShapeController() const;
0221     Q_DECL_DEPRECATED void setGlobalShapeController(KoShapeController *globalShapeController);
0222 
0223 Q_SIGNALS:
0224     /**
0225      * This signal is emitted every time a resource is set that is either
0226      * new or different from the previous set value.
0227      * @param key the identifying key for the resource
0228      * @param value the variants new value.
0229      * @see KoDocumentResourceManager::DocumentResource
0230      */
0231     void resourceChanged(int key, const QVariant &value);
0232 
0233 private:
0234     KoDocumentResourceManager(const KoDocumentResourceManager&);
0235     KoDocumentResourceManager& operator=(const KoDocumentResourceManager&);
0236 
0237     class Private;
0238     Private *const d;
0239 };
0240 
0241 #endif