File: validators.h

package info (click to toggle)
kicad 9.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 769,124 kB
  • sloc: cpp: 960,330; ansic: 121,001; xml: 66,428; python: 18,382; sh: 1,010; awk: 301; asm: 292; makefile: 227; javascript: 167; perl: 10
file content (248 lines) | stat: -rw-r--r-- 7,596 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 Wayne Stambaugh <stambaughw@gmail.com>
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 * Copyright (C) 2018 CERN
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

/**
 * @file validators.h
 * @brief Custom text control validator definitions.
 */

#ifndef VALIDATORS_H
#define VALIDATORS_H

#include <memory>

#include <wx/valtext.h>
#include <wx/grid.h>
#include <wx/regex.h>

#include <lib_id.h>


#define FIELD_NAME  -1
#define FIELD_VALUE -2

#define SHEETNAME_V      100    // We can't use SHEETNAME and SHEETFILENAME because they
#define SHEETFILENAME_V  101    //   overlap with REFERENCE_FIELD and VALUE_FIELD
#define SHEETUSERFIELD_V 102

#define LABELUSERFIELD_V 200


/**
 * Provide a custom wxValidator object for limiting the allowable characters when
 * defining footprint names.
 *
 * Since the introduction of the PRETTY footprint library format, footprint names cannot have
 * any characters that would prevent file creation on any platform.  The characters \/:*?|"<>
 * are illegal and filtered by the validator.
 */
class FOOTPRINT_NAME_VALIDATOR : public wxTextValidator
{
public:
    FOOTPRINT_NAME_VALIDATOR( wxString* aValue = nullptr );
};


/**
 * Provide a custom wxValidator object for limiting the allowable characters when
 * defining file names with path, for instance in schematic sheet file names.
 *
 * The characters *?|"<> are illegal and filtered by the validator,
 * but /\: are valid (\ and : only on Windows.)
 */
class FILE_NAME_WITH_PATH_CHAR_VALIDATOR : public wxTextValidator
{
public:
    FILE_NAME_WITH_PATH_CHAR_VALIDATOR( wxString* aValue = nullptr );
};


/**
 * Provide a custom wxValidator object for limiting the allowable characters when defining an
 * environment variable name in a text edit control.
 *
 * Only uppercase, numbers, and underscore (_) characters are valid and the first character of
 * the name cannot start with a number.  This is according to IEEE Std 1003.1-2001.  Even though
 * most systems support other characters, these characters guarantee compatibility for
 * all shells.
 */
class ENV_VAR_NAME_VALIDATOR : public wxTextValidator
{
public:
    ENV_VAR_NAME_VALIDATOR( wxString* aValue = nullptr );
    ENV_VAR_NAME_VALIDATOR( const ENV_VAR_NAME_VALIDATOR& val );

    virtual ~ENV_VAR_NAME_VALIDATOR();

    // Make a clone of this validator (or return nullptr) - currently necessary
    // if you're passing a reference to a validator.
    virtual wxObject *Clone() const override
    {
        return new ENV_VAR_NAME_VALIDATOR( *this );
    }

    void OnChar( wxKeyEvent& event );

    void OnTextChanged( wxCommandEvent& event );
};


/**
 * Custom validator that checks verifies that a string *exactly* matches a regular expression.
 */
class REGEX_VALIDATOR : public wxTextValidator
{
public:
    /**
     * @param aRegEx is a regular expression to validate strings.
     * @param aValue is a pointer to a wxString containing the value to validate.
     */
    REGEX_VALIDATOR( const wxString& aRegEx, wxString* aValue = nullptr )
        : wxTextValidator( wxFILTER_NONE, aValue )
    {
        compileRegEx( aRegEx, wxRE_DEFAULT );
    }

    /**
     * @param aRegEx is a regular expression to validate strings.
     * @param aFlags are compilation flags (normally wxRE_DEFAULT).
     * @param aValue is a pointer to a wxString containing the value to validate.
     */
    REGEX_VALIDATOR( const wxString& aRegEx, int aFlags, wxString* aValue = nullptr )
        : wxTextValidator( wxFILTER_NONE, aValue )
    {
        compileRegEx( aRegEx, aFlags );
    }

    REGEX_VALIDATOR( const REGEX_VALIDATOR& aOther ) : wxTextValidator( aOther )
    {
        compileRegEx( aOther.m_regExString, aOther.m_regExFlags );
    }

    virtual wxObject* Clone() const override
    {
        return new REGEX_VALIDATOR( *this );
    }

    bool Validate( wxWindow* aParent ) override;

    const wxString& GetRegEx() const
    {
        return m_regExString;
    }

protected:
    /// Compiles and stores a regular expression.
    void compileRegEx( const wxString& aRegEx, int aFlags );

    /// Original regular expression (for copy constructor).
    wxString m_regExString;

    /// Original compilation flags (for copy constructor).
    int m_regExFlags;

    /// Compiled regular expression.
    wxRegEx m_regEx;
};

class NETNAME_VALIDATOR : public wxTextValidator
{
public:
    NETNAME_VALIDATOR( wxString* aVal = nullptr );

    NETNAME_VALIDATOR( bool aAllowSpaces );

    NETNAME_VALIDATOR( const NETNAME_VALIDATOR& aValidator );

    virtual wxObject* Clone() const override { return new NETNAME_VALIDATOR( *this ); }

    virtual bool TransferToWindow() override { return true; }

    virtual bool TransferFromWindow() override { return true; }

    virtual bool Validate( wxWindow *aParent ) override;

protected:
    /// @return the error message if the contents of @a aVal are invalid.
    wxString IsValid( const wxString& aVal ) const override;

private:
    bool m_allowSpaces;
};


namespace KIUI
{
/**
 * Call a text validator's TransferDataToWindow method without firing
 * a text change event.
 *
 * This is useful when you want to keep a validator in sync with other data,
 * but the act of changing it should not trigger other updates. It is the
 * validator equivalent of ChangeValue() compared to SetValue().
 *
 * This function blocks all events, but the same technique can be used to
 * selectively block events.
 *
 * @param aValidator the validator to update the control of
 */
void ValidatorTransferToWindowWithoutEvents( wxValidator& aValidator );

} // namespace KIUI


/**
 * A text control validator used for validating the text allowed in fields.
 *
 * - The reference field does not accept spaces.
 * - The value field does not accept spaces in the symbol library editor because in symbol
 *   libraries, the value field is the symbol name in the library.
 */
class FIELD_VALIDATOR : public wxTextValidator
{
public:
    FIELD_VALIDATOR( int aFieldId, wxString* aValue = nullptr );

    FIELD_VALIDATOR( const FIELD_VALIDATOR& aValidator );

    virtual wxObject* Clone() const override { return new FIELD_VALIDATOR( *this ); }

    /**
     * Override the default Validate() function provided by wxTextValidator to provide
     * better error messages.
     *
     * @param aParent is the parent window of the error message dialog.
     * @return true if the text in the control is valid otherwise false.
     */
    virtual bool Validate( wxWindow* aParent ) override;

    bool DoValidate( const wxString& aValue, wxWindow* aParent );

private:
    int m_fieldId;
};


#endif  // #ifndef VALIDATORS_H