1 /*
2     This file is part of the Okteta Kasten module, made within the KDE community.
3 
4     SPDX-FileCopyrightText: 2006, 2009, 2011 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8 
9 #ifndef KASTEN_BYTEARRAYVALIDATOR_HPP
10 #define KASTEN_BYTEARRAYVALIDATOR_HPP
11 
12 // Okteta core
13 #include <Okteta/OktetaCore>
14 // Qt
15 #include <QValidator>
16 
17 namespace Okteta {
18 class ValueCodec;
19 class CharCodec;
20 
21 class ByteArrayValidator : public QValidator
22 {
23     Q_OBJECT
24 
25 public:
26     // matching Okteta::ValueCoding
27     enum Coding
28     {
29         InvalidCoding = -1,
30         HexadecimalCoding = 0,
31         DecimalCoding = 1,
32         OctalCoding = 2,
33         BinaryCoding = 3,
34         CharCoding = 4,
35         Utf8Coding = 5
36     };
37 
38 public:
39     explicit ByteArrayValidator(QObject* parent = nullptr,
40                                 Coding codecId = CharCoding,
41                                 int charCodecId = LocalEncoding);
42     ByteArrayValidator() = delete;
43 
44     ~ByteArrayValidator() override;
45 
46 public: // QValidator API
47     QValidator::State validate(QString& input, int& pos) const override;
48 
49 public:
50     /// Sets one of the value codecs or the current char codec.
51     void setCodec(Coding codecId);
52     /// Sets the char codec to use. Does not change the current codec.
53     void setCharCodec(const QString& charCodecName);
54     /// Sets the maximal length of the edited bytearray to @p maxLength.
55     /// If @p maxLength is negative, the behaviour is undefined. Default is @c 32767.
56     void setMaxLength(int maxLength);
57     /// Sets the maximal length of the edited bytearray to @p minLength.
58     /// If @p minLength is negative, the behaviour is undefined. Default is @c 0.
59     void setMinLength(int minLength);
60 
61 public:
62     int maxLength() const;
63     int minLength() const;
64 
65 public:
66     QByteArray toByteArray(const QString& string) const;
67     QString toString(const QByteArray& byteArray) const;
68 
69 private:
70     /**
71      * Returns a string that is at least as long as @p destLen number of characters,
72      * by adding zeroes to the left as necessary.
73      *
74      * e.g. zeroExtend( "32", 3 ) => "032"
75      */
76 //     QString zeroExtend( const QString &src, int destLen ) const;
77 
78     Coding mCodecId = InvalidCoding;
79     ValueCodec* mValueCodec = nullptr;
80     CharCodec* mCharCodec;
81     int mMaxLength = 32767;
82     int mMinLength = 0;
83 };
84 
maxLength() const85 inline int ByteArrayValidator::maxLength() const { return mMaxLength; }
minLength() const86 inline int ByteArrayValidator::minLength() const { return mMinLength; }
87 
88 }
89 
90 #endif
91