File indexing completed on 2025-03-09 03:57:05

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2010-05-01
0007  * Description : an abstract rule class
0008  *
0009  * SPDX-FileCopyrightText: 2009-2012 by Andi Clemens <andi dot clemens at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #ifndef DIGIKAM_RULE_H
0016 #define DIGIKAM_RULE_H
0017 
0018 // Local includes
0019 
0020 #include "parseresults.h"
0021 #include "parsesettings.h"
0022 #include "token.h"
0023 
0024 class QAction;
0025 class QMenu;
0026 class QPushButton;
0027 class QString;
0028 
0029 namespace Digikam
0030 {
0031 
0032 class Rule : public QObject
0033 {
0034     Q_OBJECT
0035 
0036 public:
0037 
0038     enum IconType
0039     {
0040         Action = 0,
0041         Dialog
0042     };
0043 
0044 public:
0045 
0046     explicit Rule(const QString& name);
0047     Rule(const QString& name, const QString& icon);
0048     ~Rule() override;
0049 
0050     /**
0051      * TODO: This is probably not needed anymore. Find out.
0052      * returns the currently assigned regExp object. Note that it is returned as a const ref, meaning
0053      * that if you use it in your custom parse operation, the main parse method has already searched for the pattern
0054      * and filled in the results of this search, so that you can use QRegularExpressionMatch::captured() immediately,
0055      * you don't have to search on your own.
0056      *
0057      * For example when implementing the Option::parseOperation() method, get the regExp object with
0058      *
0059      *      const QRegularExpression& reg = regExp();
0060      *
0061      * and immediately fetch possible matches with
0062      *
0063      *      const QString& param1 = reg.captured(1);
0064      *
0065      * @see Option
0066      * @see Modifier
0067      *
0068      * @return a const ref to the assigned regexp object
0069      */
0070     QRegularExpression& regExp() const;
0071 
0072     QString description() const;
0073     QPixmap icon(Rule::IconType type = Rule::Action) const;
0074 
0075     /**
0076      * @return a list of all registered tokens
0077      */
0078     TokenList& tokens() const;
0079 
0080     /**
0081      * Register a button in the parent object. By calling this method, a new button for the parser
0082      * object will be created and all necessary connections will be setup.
0083      *
0084      * @param  parent the parent object the button will be registered for
0085      * @return a pointer to the newly created button
0086      */
0087     QPushButton* registerButton(QWidget* parent);
0088 
0089     /**
0090      * Register a menu action in the parent object. By calling this method, a new action for the parser
0091      * object will be created and all necessary connections will be setup.
0092      *
0093      * @param  parent the parent object the action will be registered for
0094      * @return a pointer to the newly created action
0095      */
0096     QAction* registerMenu(QMenu* parent);
0097 
0098     /**
0099      * Returns true if a token menu is used.
0100      */
0101     bool useTokenMenu() const;
0102 
0103     /**
0104      * Checks the validity of the parse object
0105      * @return true if valid
0106      */
0107     bool isValid() const;
0108 
0109     /**
0110      * Resets the parser to its initial state
0111      */
0112     virtual void reset();
0113 
0114     /**
0115      * Escape the token characters to make them work in regular expressions
0116      *
0117      * @param token the token to be escaped
0118      * @return A token with escaped characters. This token can then be used in a regular expression
0119      */
0120     static QString escapeToken(const QString& token);
0121 
0122     ParseResults parse(ParseSettings& settings);
0123 
0124 Q_SIGNALS:
0125 
0126     void signalTokenTriggered(const QString&);
0127 
0128 protected:
0129 
0130     /**
0131      * TODO: describe me
0132      * @param settings contains settings
0133      * @param match result of the regular expression match done in Option::parse()
0134      * @return
0135      */
0136     virtual QString parseOperation(ParseSettings& settings,
0137                                    const QRegularExpressionMatch& match) = 0;
0138 
0139     /**
0140      * add a token to the parser, every parser should at least assign one token object
0141      * @param id the token id string (used for parsing)
0142      * @param description the description of the token (used for example in the tooltip)
0143      * @param actionName [optional] the name of the token action (only used when the token menu is displayed)
0144      * @return
0145      */
0146     bool addToken(const QString& id, const QString& description, const QString& actionName = QString());
0147 
0148     void setRegExp(const QRegularExpression& regExp);
0149     void setDescription(const QString& desc);
0150     void setIcon(const QString& pixmap);
0151 
0152     /**
0153      * If multiple tokens have been assigned to a rule, a menu will be created.
0154      * If you want to display a menu for every defined token, set this method to 'true' and
0155      * re-implement the @see slotTokenTriggered method.
0156      * @param value boolean parameter to set token menu usage
0157      */
0158     void setUseTokenMenu(bool value);
0159 
0160 protected Q_SLOTS:
0161 
0162     virtual void slotTokenTriggered(const QString&);
0163 
0164 private:
0165 
0166     QPushButton* createButton(const QString& name, const QIcon& icon);
0167 
0168 private:
0169 
0170     // Disable
0171     Rule(QObject*)               = delete;
0172     Rule(const Rule&)            = delete;
0173     Rule& operator=(const Rule&) = delete;
0174 
0175 private:
0176 
0177     class Private;
0178     Private* const d;
0179 };
0180 
0181 typedef QList<Rule*> RulesList;
0182 
0183 } // namespace Digikam
0184 
0185 #endif // DIGIKAM_RULE_H