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

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2004 Boudewijn Rempt <boud@valdyas.org>
0003  * SPDX-FileCopyrightText: 2006 Thomas Zander <zander@kde.org>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #ifndef KO_TOOL_FACTORY_H
0009 #define KO_TOOL_FACTORY_H
0010 
0011 #include "kritaflake_export.h"
0012 
0013 #include <QString>
0014 #include <QList>
0015 #include <QObject>
0016 
0017 class KoCanvasBase;
0018 class KoToolBase;
0019 class QKeySequence;
0020 class KisKActionCollection;
0021 class QAction;
0022 
0023 /**
0024  * Each tool has a "section" which it uses to be grouped in the toolbox.
0025  */
0026 namespace ToolBoxSection {
0027     static const QString Main {"main"};                   ///< Tools that only work on vector shapes
0028     static const QString Shape {"0 Krita/Shape"};         ///< Freehand and shapes like ellipses and lines
0029     static const QString Transform {"2 Krita/Transform"}; ///< Tools that transform the layer
0030     static const QString Fill {"3 Krita/Fill"};           ///< Tools that fill parts of the canvas
0031     static const QString View {"4 Krita/View"};           ///< Assistance tools: guides, reference, etc.
0032     static const QString Select {"5 Krita/Select"};       ///< Tools that select pixels
0033     static const QString Navigation {"navigation"};       ///< Tools that affect the canvas: pan, zoom, etc.
0034 }
0035 
0036 /**
0037  * A factory for KoToolBase objects.
0038  *
0039  * The baseclass for all tool plugins. Each plugin that ships a KoToolBase should also
0040  * ship a factory. That factory will extend this class and set variable data like
0041  * a toolTip and icon in the constructor of that extending class.
0042  *
0043  * An example usage would be:<pre>
0044  * class MyToolFactory : public KoToolFactoryBase {
0045  * public:
0046  *   MyToolFactory(const QStringList&)
0047  *       : KoToolFactoryBase("MyTool") {
0048  *       setToolTip(i18n("Create object"));
0049  *       setSection("main");
0050  *       setPriority(5);
0051  *   }
0052  *   ~MyToolFactory() {}
0053  *   KoToolBase *createTool(KoCanvasBase *canvas);
0054  * };
0055  * K_PLUGIN_FACTORY_WITH_JSON((MyToolFactoryFactory, "mytool.json", registerPlugin<MyToolFactory>();)
0056 </pre>
0057 
0058  */
0059 class KRITAFLAKE_EXPORT KoToolFactoryBase : public QObject
0060 {
0061 
0062     Q_OBJECT
0063 
0064 public:
0065     /**
0066      * Create the new factory
0067      * @param id a string that will be used internally for referencing the tool
0068      */
0069     explicit KoToolFactoryBase(const QString &id);
0070     virtual ~KoToolFactoryBase();
0071 
0072     /**
0073      * Create the actions for this tool. Actions are unique per window, not per
0074      * tool instance; tool instances are unique per view/canvas.
0075      */
0076     QList<QAction *> createActions(KisKActionCollection *actionCollection);
0077 
0078     /**
0079      * Instantiate a new tool
0080      * @param canvas the canvas that the new tool will work on. Should be passed
0081      *    to the constructor of the tool.
0082      * @return a new KoToolBase instance, or zero if the tool doesn't want to show up.
0083      */
0084     virtual KoToolBase *createTool(KoCanvasBase *canvas) = 0;
0085 
0086     /**
0087      * return the id for the tool this factory creates.
0088      * @return the id for the tool this factory creates.
0089      */
0090     QString id() const;
0091     /**
0092      * Returns The priority of this tool in its section in the toolbox
0093      * @return The priority of this tool.
0094      */
0095     int priority() const;
0096     /**
0097      * returns the section used to group tools in the toolbox
0098      * @return the section
0099      */
0100     QString section() const;
0101     /**
0102      * return a translated tooltip Text
0103      * @return a translated tooltip Text
0104      */
0105     QString toolTip() const;
0106     /**
0107      * return the basename of the icon for this tool
0108      * @return the basename of the icon for this tool
0109      */
0110     QString iconName() const;
0111 
0112     /**
0113      * Return the id of the shape we can process.
0114      * This is the shape Id the tool we create is associated with.  So a TextTool for a TextShape.
0115      * @see KoShapeFactoryBase::shapeId()
0116      * @see setActivationShapeId()
0117      * @return the id of a shape, or an empty string for all shapes.
0118      */
0119     QString activationShapeId() const;
0120 
0121     /**
0122      * Return the default keyboard shortcut for activation of this tool (if
0123      * the shape this tool belongs to is active).
0124      *
0125      * See KoToolManager for use.
0126      *
0127      * @return the shortcut
0128      */
0129     QKeySequence shortcut() const;
0130 
0131 protected:
0132 
0133     /**
0134      * Set the default shortcut for activation of this tool.
0135      */
0136     void setShortcut(const QKeySequence & shortcut);
0137 
0138     /**
0139      * Set the tooltip to be used for this tool
0140      * @param tooltip the tooltip
0141      */
0142     void setToolTip(const QString &tooltip);
0143 
0144     /**
0145      * Set the section used to group tools in the toolbox
0146      * @param section the section
0147      */
0148     void setSection(const QString &section);
0149 
0150     /**
0151      * Set an icon to be used in the toolBox.
0152      * @param iconName the basename (without extension) of the icon
0153      */
0154     void setIconName(const char *iconName);
0155     void setIconName(const QString &iconName);
0156 
0157     /**
0158      * Set the priority of this tool, as it is shown in the toolBox; lower number means
0159      * it will be show more to the front of the list.
0160      * @param newPriority the priority
0161      */
0162     void setPriority(int newPriority);
0163 
0164     /**
0165      * Set the id of the shape we can process.
0166      * This is the Id, as passed to the constructor of a KoShapeFactoryBase, that the tool
0167      * we create is associated with. This means that if a KoTextShape is selected, then
0168      * all tools that have its id set here will be added to the dynamic part of the toolbox.
0169      * @param activationShapeId the Id of the shape
0170      * @see activationShapeId()
0171      */
0172     void setActivationShapeId(const QString &activationShapeId);
0173 
0174     /**
0175      * @brief createActionsImpl should be reimplemented if the tool needs any actions.
0176      * The actions should have a valid objectName().
0177      *
0178      * @return the list of actions this tool wishes to be available.
0179      */
0180     virtual QList<QAction *> createActionsImpl();
0181 
0182 private Q_SLOTS:
0183 
0184     void activateTool();
0185 
0186 private:
0187     class Private;
0188     Private * const d;
0189 };
0190 
0191 #endif