KiCad PCB EDA Suite
validators.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2013 Wayne Stambaugh <stambaughw@gmail.com>
5  * Copyright (C) 2004-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  * Copyright (C) 2018 CERN
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
31 #ifndef VALIDATORS_H
32 #define VALIDATORS_H
33 
34 #include <wx/valtext.h>
35 #include <wx/grid.h>
36 #include <wx/regex.h>
37 
38 #include <lib_id.h>
39 
44 class GRID_CELL_TEXT_EDITOR : public wxGridCellTextEditor
45 {
46 public:
48 
49  virtual void SetValidator( const wxValidator& validator ) override;
50  virtual void StartingKey( wxKeyEvent& event ) override;
51 
52 protected:
53  wxScopedPtr<wxValidator> m_validator;
54 };
55 
56 
63 class FOOTPRINT_NAME_VALIDATOR : public wxTextValidator
64 {
65 public:
66  FOOTPRINT_NAME_VALIDATOR( wxString* aValue = nullptr );
67 };
68 
69 
76 class FILE_NAME_WITH_PATH_CHAR_VALIDATOR : public wxTextValidator
77 {
78 public:
79  FILE_NAME_WITH_PATH_CHAR_VALIDATOR( wxString* aValue = nullptr );
80 };
81 
82 
91 class ENV_VAR_NAME_VALIDATOR : public wxTextValidator
92 {
93 public:
94  ENV_VAR_NAME_VALIDATOR( wxString* aValue = nullptr );
96 
97  virtual ~ENV_VAR_NAME_VALIDATOR();
98 
99  // Make a clone of this validator (or return nullptr) - currently necessary
100  // if you're passing a reference to a validator.
101  virtual wxObject *Clone() const override
102  {
103  return new ENV_VAR_NAME_VALIDATOR( *this );
104  }
105 
106  void OnChar( wxKeyEvent& event );
107 
108  void OnTextChanged( wxCommandEvent& event );
109 };
110 
111 
116 class REGEX_VALIDATOR : public wxTextValidator
117 {
118 public:
123  REGEX_VALIDATOR( const wxString& aRegEx, wxString* aValue = nullptr )
124  : wxTextValidator( wxFILTER_NONE, aValue )
125  {
126  compileRegEx( aRegEx, wxRE_DEFAULT );
127  }
128 
134  REGEX_VALIDATOR( const wxString& aRegEx, int aFlags, wxString* aValue = nullptr )
135  : wxTextValidator( wxFILTER_NONE, aValue )
136  {
137  compileRegEx( aRegEx, aFlags );
138  }
139 
140  REGEX_VALIDATOR( const REGEX_VALIDATOR& aOther ) : wxTextValidator( aOther )
141  {
142  compileRegEx( aOther.m_regExString, aOther.m_regExFlags );
143  }
144 
145  virtual wxObject* Clone() const override
146  {
147  return new REGEX_VALIDATOR( *this );
148  }
149 
150  bool Validate( wxWindow* aParent ) override;
151 
152 protected:
154  void compileRegEx( const wxString& aRegEx, int aFlags );
155 
157  wxString m_regExString;
158 
161 
163  wxRegEx m_regEx;
164 };
165 
172 class LIB_ID_VALIDATOR : public wxTextValidator
173 {
174 public:
179  LIB_ID_VALIDATOR( wxString* aValue = nullptr ) :
180  wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
181  {
182  SetCharExcludes( "\r\n\t" );
183  }
184 
185  virtual wxObject* Clone() const override
186  {
187  return new LIB_ID_VALIDATOR( *this );
188  }
189 
190  bool Validate( wxWindow* aParent ) override;
191 };
192 
193 
194 class NETNAME_VALIDATOR : public wxTextValidator
195 {
196 public:
197  NETNAME_VALIDATOR( wxString *aVal = nullptr );
198 
199  NETNAME_VALIDATOR( bool aAllowSpaces );
200 
201  NETNAME_VALIDATOR( const NETNAME_VALIDATOR& aValidator );
202 
203  virtual wxObject* Clone() const override { return new NETNAME_VALIDATOR( *this ); }
204 
205  virtual bool TransferToWindow() override { return true; }
206 
207  virtual bool TransferFromWindow() override { return true; }
208 
209  virtual bool Validate( wxWindow *aParent ) override;
210 
211 protected:
212  // returns the error message if the contents of 'val' are invalid
213  wxString IsValid( const wxString& aVal ) const override;
214 
215 private:
217 };
218 
219 
220 namespace KIUI
221 {
235 void ValidatorTransferToWindowWithoutEvents( wxValidator& aValidator );
236 
237 } // namespace KIUI
238 
239 #endif // #ifndef VALIDATORS_H
wxString m_regExString
Original compilation flags (for copy constructor)
Definition: validators.h:157
wxRegEx m_regEx
Definition: validators.h:163
void compileRegEx(const wxString &aRegEx, int aFlags)
< Compiles and stores a regular expression
Definition: validators.cpp:258
REGEX_VALIDATOR(const wxString &aRegEx, int aFlags, wxString *aValue=nullptr)
Definition: validators.h:134
void ValidatorTransferToWindowWithoutEvents(wxValidator &aValidator)
Call a text validator's TransferDataToWindow method without firing a text change event.
Definition: validators.cpp:376
wxScopedPtr< wxValidator > m_validator
Definition: validators.h:53
bool Validate(wxWindow *aParent) override
Definition: validators.cpp:220
virtual wxObject * Clone() const override
Definition: validators.h:185
virtual wxObject * Clone() const override
Definition: validators.h:203
This class provides a custom wxValidator object for limiting the allowable characters when defining f...
Definition: validators.h:76
virtual void StartingKey(wxKeyEvent &event) override
Definition: validators.cpp:55
This class works around a bug in wxGrid where the first keystroke doesn't get sent through the valida...
Definition: validators.h:44
void OnTextChanged(wxCommandEvent &event)
Definition: validators.cpp:201
This class provides a custom wxValidator object for limiting the allowable characters when defining f...
Definition: validators.h:63
ENV_VAR_NAME_VALIDATOR(wxString *aValue=nullptr)
Definition: validators.cpp:108
virtual wxObject * Clone() const override
Definition: validators.h:145
virtual bool TransferToWindow() override
Definition: validators.h:205
virtual bool TransferFromWindow() override
Definition: validators.h:207
Custom validator that verifies that a string defines a valid LIB_ID.
Definition: validators.h:172
LIB_ID_VALIDATOR(wxString *aValue=nullptr)
Definition: validators.h:179
virtual void SetValidator(const wxValidator &validator) override
Definition: validators.cpp:46
virtual wxObject * Clone() const override
Definition: validators.h:101
This class provides a custom wxValidator object for limiting the allowable characters when defining a...
Definition: validators.h:91
FILE_NAME_WITH_PATH_CHAR_VALIDATOR(wxString *aValue=nullptr)
Definition: validators.cpp:82
REGEX_VALIDATOR(const REGEX_VALIDATOR &aOther)
Definition: validators.h:140
int m_regExFlags
Compiled regex.
Definition: validators.h:160
REGEX_VALIDATOR(const wxString &aRegEx, wxString *aValue=nullptr)
Definition: validators.h:123
NETNAME_VALIDATOR(wxString *aVal=nullptr)
Definition: validators.cpp:319
wxString IsValid(const wxString &aVal) const override
Definition: validators.cpp:364
virtual bool Validate(wxWindow *aParent) override
Definition: validators.cpp:340
bool Validate(wxWindow *aParent) override
Definition: validators.cpp:271
void OnChar(wxKeyEvent &event)
Definition: validators.cpp:130
Custom validator that checks verifies that a string exactly matches a regular expression.
Definition: validators.h:116
FOOTPRINT_NAME_VALIDATOR(wxString *aValue=nullptr)
Definition: validators.cpp:71
virtual ~ENV_VAR_NAME_VALIDATOR()
Definition: validators.cpp:124