File indexing completed on 2024-04-21 14:55:58

0001 /*
0002     kstringvalidator.h
0003 
0004     Copyright (c) 2001 Marc Mutz <mutz@kde.org>
0005 
0006     This library is free software; you can redistribute it and/or
0007     modify it under the terms of the GNU Library General Public
0008     License as published by the Free Software Foundation; version 2.0
0009     of the License.
0010 
0011     This library is distributed in the hope that it will be useful,
0012     but WITHOUT ANY WARRANTY; without even the implied warranty of
0013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014     Library General Public License for more details.
0015 
0016     You should have received a copy of the GNU Library General Public
0017     License along with this library; if not, write to the Free
0018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0019     02110-1301 USA
0020 */
0021 
0022 #ifndef KSTRINGVALIDATOR_H
0023 #define KSTRINGVALIDATOR_H
0024 
0025 #include <kdelibs4support_export.h>
0026 
0027 #include <QStringList>
0028 #include <QValidator>
0029 
0030 /**
0031  * @deprecated since 5.0
0032  * @short A QValidator to (dis)allow certain strings
0033  *
0034  * This validator allows you to accept only certain or to accept all
0035  * but certain strings.
0036  *
0037  * When used in rejecting mode, accepts only strings not in the
0038  * stringlist. This mode is the default and comes in handy when asking
0039  * the user for a name of some listed entity. Set the list of already
0040  * used names to prevent the user from entering duplicate names.
0041  *
0042  * When used in non-rejecting mode, accepts only strings that appear
0043  * in the stringlist. Use with care! From a user's point of view this
0044  * mode is hard to grasp.
0045  *
0046  * This validator can also fix strings. In rejecting mode, a number
0047  * will be appended to the string until it is Acceptable. E.g. if
0048  * "foo" and "foo 1" are in the stringlist, then fixup will change
0049  * "foo" to "foo 2", provided "foo 2" isn't in the list of forbidden
0050  * strings.
0051  *
0052  * In accepting mode, when the input starts with an Acceptable
0053  * substring, truncates to the longest Acceptable string. When the
0054  * input is the start of an Acceptable string, completes to the
0055  * shortest Acceptable string.
0056  *
0057  * NOTE: fixup isn't yet implemented.
0058  *
0059  * @author Marc Mutz <mutz@kde.org>
0060  **/
0061 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KStringListValidator : public QValidator
0062 {
0063     Q_OBJECT
0064     Q_PROPERTY(QStringList stringList READ stringList WRITE setStringList)
0065     Q_PROPERTY(bool rejecting READ isRejecting WRITE setRejecting)
0066     Q_PROPERTY(bool fixupEnabled READ isFixupEnabled WRITE setFixupEnabled)
0067 
0068 public:
0069     /**
0070      * Creates a new string validator.
0071      *
0072      * @param list         The list of strings to (dis)allow.
0073      * @param rejecting    Selects the validator's mode
0074      *                     (rejecting: true; accepting: false)
0075      * @param fixupEnabled Selects whether to fix strings or not.
0076      * @param parent Passed to lower level constructor.
0077      *
0078      **/
0079     KDELIBS4SUPPORT_DEPRECATED explicit KStringListValidator(const QStringList &list = QStringList(),
0080                                   bool rejecting = true, bool fixupEnabled = false,
0081                                   QObject *parent = nullptr);
0082 
0083     /**
0084      * Destroys the string validator.
0085      */
0086     ~KStringListValidator() override;
0087 
0088     /**
0089      * Sets whether the string validator is in rejecting mode or not.
0090      * If in rejecting mode, the strings from @see stringList are not
0091      * allowed to appear in the validation string.
0092      */
0093     void setRejecting(bool rejecting);
0094 
0095     /**
0096      * Returns whether the string validator is in rejecting mode.
0097      */
0098     bool isRejecting() const;
0099 
0100     /**
0101      * Sets the fixup flag. If enabled, wrong input is corrected
0102      * automatically.
0103      */
0104     void setFixupEnabled(bool fixupEnabled);
0105 
0106     /**
0107      * Returns whether the fixup flag is set.
0108      */
0109     bool isFixupEnabled() const;
0110 
0111     /**
0112      * Sets the @param list of string which is used as black or
0113      * white list, depending on the rejecting mode (@see isRejecting()).
0114      */
0115     void setStringList(const QStringList &list);
0116 
0117     /**
0118      * Returns the string list of the validator.
0119      */
0120     QStringList stringList() const;
0121 
0122     /**
0123      * Reimplemented from @see QValidator.
0124      */
0125     State validate(QString &input, int &pos) const override;
0126 
0127     /**
0128      * Reimplemented from @see QValidator.
0129      */
0130     void fixup(QString &input) const override;
0131 
0132 private:
0133     class Private;
0134     Private *const d;
0135 };
0136 
0137 #endif // KSTRINGVALIDATOR_H