File indexing completed on 2024-05-05 16:08:23

0001 /* This file is part of the KDE project
0002    Copyright (C) 1998, 1999, 2000 Torben Weis <weis@kde.org>
0003    Copyright (C) 2001 David Faure <faure@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #ifndef KDATATOOL_H
0022 #define KDATATOOL_H
0023 
0024 #include <kdelibs4support_export.h>
0025 #include <QObject>
0026 #include <QString>
0027 
0028 #include <QAction>
0029 
0030 #include <kservice.h>
0031 
0032 class KDataTool;
0033 class QPixmap;
0034 class QStringList;
0035 class KActionCollection;
0036 
0037 // If you're only looking at implementing a data-tool, skip directly to the last
0038 // class definition, KDataTool.
0039 
0040 /**
0041  * This is a convenience class for KService. You can use it if you have
0042  * a KService describing a KDataTool. In this case the KDataToolInfo class
0043  * is more convenient to work with.
0044  *
0045  * Especially useful is the method createTool which creates the datatool
0046  * described by the service.
0047  * @see KDataTool
0048  */
0049 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KDataToolInfo
0050 {
0051 public:
0052     /**
0053      * Create an invalid KDataToolInfo.
0054      */
0055     KDataToolInfo();
0056     /**
0057      * Create a valid KDataToolInfo.
0058      * @param service the corresponding service
0059      * @param componentName the name of the component to use
0060      */
0061     KDataToolInfo(const KService::Ptr &service, const QString &componentName);
0062     /**
0063      * Destructor
0064      */
0065     ~KDataToolInfo();
0066     /**
0067      * Copy constructor.
0068      */
0069     KDataToolInfo(const KDataToolInfo &info);
0070     /**
0071      * Assignment operator.
0072      */
0073     KDataToolInfo &operator= (const KDataToolInfo &info);
0074 
0075     /**
0076      * Returns the data type that the DataTool can accept.
0077      * @return the C++ data type that this DataTool accepts.
0078      *         For example "QString" or "QImage" or something more
0079      *         complicated.
0080      */
0081     QString dataType() const;
0082     /**
0083      * Returns a list of mime type that will be accepted by the DataTool.
0084      * The mimetypes are only used if the dataType can be used to store
0085      * different mimetypes. For example in a "QString" you could save "text/plain"
0086      * or "text/html" or "text/xml".
0087      *
0088      * @return the mime types accepted by this DataTool. For example
0089      *         "image/gif" or "text/plain". In some cases the dataType
0090      *         determines the accepted type of data perfectly. In this cases
0091      *         this list may be empty.
0092      */
0093     QStringList mimeTypes() const;
0094 
0095     /**
0096      * Checks whether the DataTool is read-only.
0097      * @return true if the DataTool does not modify the data passed to it by KDataTool::run.
0098      */
0099     bool isReadOnly() const;
0100 
0101     /**
0102      * Returns the icon name for this DataTool.
0103      * @return the name of the icon for the DataTool
0104      */
0105     QString iconName() const;
0106     /**
0107      * Returns a list of strings that you can put in a QPopupMenu item, for example to
0108      * offer the DataTools services to the user. The returned value
0109      * is usually something like "Spell checking", "Shrink Image", "Rotate Image"
0110      * or something like that.
0111      * This list comes from the Comment field of the tool's desktop file
0112      * (so that it can be translated).
0113      *
0114      * Each of the strings returned corresponds to a string in the list returned by
0115      * commands.
0116      *
0117      * @return a list of strings that you can put in a QPopupMenu item
0118      */
0119     QStringList userCommands() const;
0120     /**
0121      * Returns the list of commands the DataTool can execute. The application
0122      * passes the command to the KDataTool::run method.
0123      *
0124      * This list comes from the Commands field of the tool's desktop file.
0125      *
0126      * Each of the strings returned corresponds to a string in the list returned by
0127      * userCommands.
0128      * @return the list of commands the DataTool can execute, suitable for
0129      *         the KDataTool::run method.
0130      */
0131     QStringList commands() const;
0132 
0133     /**
0134      * Creates the data tool described by this KDataToolInfo.
0135      * @param parent the parent of the QObject (or 0 for parent-less KDataTools)
0136      * @return a pointer to the created data tool or 0 on error.
0137      */
0138     KDataTool *createTool(QObject *parent = nullptr) const;
0139 
0140     /**
0141      * The KDataToolInfo's service that is represented by this class.
0142      * @return the service
0143      */
0144     KService::Ptr service() const;
0145 
0146     /**
0147      * The instance of the service.
0148      * @return the component name
0149      */
0150     QString componentName() const;
0151 
0152     /**
0153      * A DataToolInfo may be invalid if the KService passed to its constructor does
0154      * not feature the service type "KDataTool".
0155      * @return true if valid, false otherwise
0156      */
0157     bool isValid() const;
0158 
0159     /**
0160      * Queries the KServiceTypeTrader about installed KDataTool implementations.
0161      * @param datatype a type that the application can 'export' to the tools (e.g. QString)
0162      * @param mimetype the mimetype of the data (e.g. text/plain)
0163      * @param componentName the application (or the part)'s instance (to check if a tool is excluded from this part,
0164      * and also used if the tool wants to read its configuration in the app's config file).
0165      * @return the list of results
0166      */
0167     static QList<KDataToolInfo> query(const QString &datatype, const QString &mimetype, const QString &componentName);
0168 
0169 private:
0170     class KDataToolInfoPrivate;
0171     KDataToolInfoPrivate *const d;
0172 };
0173 
0174 /**
0175  * This class helps applications implement support for KDataTool.
0176  * The steps to follow are simple:
0177  * @li query for the available tools using KDataToolInfo::query
0178  * @li pass the result to KDataToolAction::dataToolActionList (with a slot)
0179  * @li plug the resulting actions, either using KXMLGUIClient::plugActionList, or by hand.
0180  *
0181  * The slot defined for step 2 is called when the action is activated, and
0182  * that's where the tool should be created and run.
0183  */
0184 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KDataToolAction : public QAction
0185 {
0186     Q_OBJECT
0187 public:
0188     /**
0189      * Constructs a new KDataToolAction.
0190      *
0191      * @param text The text that will be displayed
0192      * @param info The corresponding KDataToolInfo
0193      * @param command The command of the action
0194      * @param parent This action's parent
0195      * @param name The name of the action
0196      */
0197     KDataToolAction(const QString &text, const KDataToolInfo &info, const QString &command, QObject *parent);
0198 
0199     /**
0200      * Destructor
0201      */
0202     ~KDataToolAction() override;
0203 
0204     /**
0205      * Creates a list of actions from a list of information about data-tools.
0206      * The slot must have a signature corresponding to the toolActivated signal.
0207      *
0208      * Note that it's the caller's responsibility to delete the actions when they're not needed anymore.
0209      * @param tools the list of data tool descriptions
0210      * @param receiver the receiver for toolActivated() signals
0211      * @param slot the slot that will receive the toolActivated() signals
0212      * @param parent the parent action collection for the actions to be created
0213      * @return the KActions
0214      */
0215     static QList<QAction *> dataToolActionList(const QList<KDataToolInfo> &tools, const QObject *receiver, const char *slot, KActionCollection *parent);
0216 
0217 Q_SIGNALS:
0218     /**
0219      * Emitted when a tool has been activated.
0220      * @param info a description of the activated tools
0221      * @param command the command for the tool
0222      */
0223     void toolActivated(const KDataToolInfo &info, const QString &command);
0224 
0225 protected:
0226     virtual void slotActivated();
0227 
0228 private:
0229     class KDataToolActionPrivate;
0230     KDataToolActionPrivate *const d;
0231 
0232 };
0233 
0234 /**
0235  * A generic tool that processes data.
0236  *
0237  * A data-tool is a "plugin" for an application, that acts (reads/modifies)
0238  * on a portion of the data present in the document (e.g. a text document,
0239  * a single word or paragraph, a KSpread cell, an image, etc.)
0240  *
0241  * The application has some generic code for presenting the tools in a popupmenu
0242  * @see KDataToolAction, and for activating a tool, passing it the data
0243  * (and possibly getting modified data from it).
0244  */
0245 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KDataTool : public QObject
0246 {
0247     Q_OBJECT
0248 public:
0249     /**
0250      * Constructor
0251      * The data-tool is only created when a menu-item, that relates to it, is activated.
0252      * @param parent the parent of the QObject (or 0 for parent-less KDataTools)
0253      */
0254     KDataTool(QObject *parent = nullptr);
0255 
0256     /**
0257      * Destructor
0258      */
0259     ~KDataTool() override;
0260 
0261     /**
0262      * @internal. Do not use under any circumstance (including bad weather).
0263      */
0264     void setComponentName(const QString &componentName);
0265 
0266     /**
0267      * Returns the instance of the part that created this tool.
0268      * Usually used if the tool wants to read its configuration in the app's config file.
0269      * @return the instance of the part that created this tool.
0270      */
0271     QString componentName() const;
0272 
0273     /**
0274      * Interface for 'running' this tool.
0275      * This is the method that the data-tool must implement.
0276      *
0277      * @param command is the command that was selected (see KDataToolInfo::commands())
0278      * @param data the data provided by the application, on which to run the tool.
0279      *             The application is responsible for setting that data before running the tool,
0280      *             and for getting it back and updating itself with it, after the tool ran.
0281      * @param datatype defines the type of @p data.
0282      * @param mimetype defines the mimetype of the data (for instance datatype may be
0283      *                 QString, but the mimetype can be text/plain, text/html etc.)
0284      * @return true if successful, false otherwise
0285      */
0286     virtual bool run(const QString &command, void *data, const QString &datatype, const QString &mimetype) = 0;
0287 
0288 private:
0289     class KDataToolPrivate;
0290     KDataToolPrivate *const d;
0291 };
0292 
0293 #endif