KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_rule_editor_utils.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) 2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#ifndef DRC_RULE_EDITOR_UTILS_H_
25#define DRC_RULE_EDITOR_UTILS_H_
26
27#include <wx/wx.h>
28#include <wx/object.h>
29
30#include <string>
31#include <optional>
32#include <memory>
33#include <vector>
34
37
38class BOARD;
39class DRC_RULE;
40
41
43{
44public:
45 using RuleConverter = std::function<std::shared_ptr<DRC_RE_BASE_CONSTRAINT_DATA>( const std::shared_ptr<DRC_RULE>& )>;
46
47 static void RegisterRuleConverter( RuleConverter aConverter );
48
49 static bool IsBoolInputType( const DRC_RULE_EDITOR_CONSTRAINT_NAME& aConstraintType );
50
51 static bool IsNumericInputType( const DRC_RULE_EDITOR_CONSTRAINT_NAME& aConstraintType );
52
53 static std::string FormatErrorMessage( const int& aErrorCount, const std::string aErrorMessage );
54
55 static std::vector<std::shared_ptr<DRC_RE_BASE_CONSTRAINT_DATA>> ParseRules( const wxString& aRules );
56
57 static bool SaveRules( const wxString& aFilename,
58 const std::vector<std::shared_ptr<DRC_RE_BASE_CONSTRAINT_DATA>>& aRules,
59 const BOARD* aBoard );
60
69 static wxString GetConstraintCode( DRC_RULE_EDITOR_CONSTRAINT_NAME aConstraintType );
70
79 static std::optional<DRC_RULE_EDITOR_CONSTRAINT_NAME> GetConstraintTypeFromCode( const wxString& aCode );
80
88
96 static bool ConstraintFromKicadDrc( const wxString& aCode, DRC_RE_BASE_CONSTRAINT_DATA* aData );
97
109 static bool ValidateNumericCtrl( wxTextCtrl* aTextCtrl, std::string aLabel, bool aCanBeZero, int* aErrorCount,
110 std::string* aValidationMessage );
111
123 static bool ValidateIntegerCtrl( wxTextCtrl* aTextCtrl, std::string aLabel, bool aCanBeZero, int* aErrorCount,
124 std::string* aValidationMessage );
125
136 static bool ValidateComboCtrl( wxComboBox* aComboBox, std::string aLabel, int* aErrorCount,
137 std::string* aValidationMessage );
138
151 static bool ValidateMinMaxCtrl( wxTextCtrl* aMinTextCtrl, wxTextCtrl* aMaxTextCtrl, std::string aMinLabel,
152 std::string aMaxLabel, int* aErrorCount, std::string* aValidationMessage );
153
171 static bool ValidateMinPreferredMaxCtrl( wxTextCtrl* aMinTextCtrl, wxTextCtrl* aPreferredTextCtrl,
172 wxTextCtrl* aMaxTextCtrl, std::string aMinLabel,
173 std::string aPreferredLabel, std::string aMaxLabel, int* aErrorCount,
174 std::string* aValidationMessage );
175
186 static bool ValidateCheckBoxCtrls( const std::vector<wxCheckBox*>& aCheckboxes, std::string aLabel,
187 int* aErrorCount, std::string* aValidationMessage );
188
189 // ==================== Pure Validators (No GUI Dependencies) ====================
190 // These can be used in headless tests and by constraint data classes.
191
201 static bool ValidateNumericValue( double aValue, bool aCanBeZero, const std::string& aLabel,
202 VALIDATION_RESULT* aResult );
203
214 static bool ValidateNumericString( const std::string& aValueStr, bool aCanBeZero, bool aIntegerOnly,
215 const std::string& aLabel, VALIDATION_RESULT* aResult );
216
227 static bool ValidateMinMax( double aMin, double aMax, const std::string& aMinLabel,
228 const std::string& aMaxLabel, VALIDATION_RESULT* aResult );
229
242 static bool ValidateMinPreferredMax( double aMin, double aPreferred, double aMax,
243 const std::string& aMinLabel, const std::string& aPrefLabel,
244 const std::string& aMaxLabel, VALIDATION_RESULT* aResult );
245
254 static bool ValidateAtLeastOneSelected( const std::vector<bool>& aSelected, const std::string& aLabel,
255 VALIDATION_RESULT* aResult );
256
265 static bool ValidateSelection( int aSelectionIndex, const std::string& aLabel,
266 VALIDATION_RESULT* aResult );
267};
268
269#endif // DRC_RULE_EDITOR_UTILS_H_
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
static bool ValidateMinPreferredMax(double aMin, double aPreferred, double aMax, const std::string &aMinLabel, const std::string &aPrefLabel, const std::string &aMaxLabel, VALIDATION_RESULT *aResult)
Validates that min <= preferred <= max.
static bool ValidateMinMaxCtrl(wxTextCtrl *aMinTextCtrl, wxTextCtrl *aMaxTextCtrl, std::string aMinLabel, std::string aMaxLabel, int *aErrorCount, std::string *aValidationMessage)
Validates the minimum and maximum value controls, ensuring that the minimum value is not greater than...
static bool ValidateMinMax(double aMin, double aMax, const std::string &aMinLabel, const std::string &aMaxLabel, VALIDATION_RESULT *aResult)
Validates that min <= max.
static bool ValidateNumericValue(double aValue, bool aCanBeZero, const std::string &aLabel, VALIDATION_RESULT *aResult)
Validates a numeric value.
static bool IsNumericInputType(const DRC_RULE_EDITOR_CONSTRAINT_NAME &aConstraintType)
static wxString ConstraintToKicadDrc(DRC_RULE_EDITOR_CONSTRAINT_NAME aType)
Convert a constraint type into the keyword used in a .kicad_drc file.
static bool ValidateComboCtrl(wxComboBox *aComboBox, std::string aLabel, int *aErrorCount, std::string *aValidationMessage)
Validates a combo box control, ensuring that a selection has been made.
static wxString GetConstraintCode(DRC_RULE_EDITOR_CONSTRAINT_NAME aConstraintType)
Translate a rule tree node type into the keyword used by the rules file for that constraint.
static bool ConstraintFromKicadDrc(const wxString &aCode, DRC_RE_BASE_CONSTRAINT_DATA *aData)
Populate a constraint data object using a keyword from a .kicad_drc file.
static bool ValidateSelection(int aSelectionIndex, const std::string &aLabel, VALIDATION_RESULT *aResult)
Validates that a selection has been made (index >= 0).
static bool ValidateNumericString(const std::string &aValueStr, bool aCanBeZero, bool aIntegerOnly, const std::string &aLabel, VALIDATION_RESULT *aResult)
Validates that a string represents a valid numeric value.
static std::optional< DRC_RULE_EDITOR_CONSTRAINT_NAME > GetConstraintTypeFromCode(const wxString &aCode)
Resolve a constraint keyword from a rules file into the corresponding rule tree enumeration value.
static bool SaveRules(const wxString &aFilename, const std::vector< std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > > &aRules, const BOARD *aBoard)
static void RegisterRuleConverter(RuleConverter aConverter)
static bool ValidateIntegerCtrl(wxTextCtrl *aTextCtrl, std::string aLabel, bool aCanBeZero, int *aErrorCount, std::string *aValidationMessage)
Validates an integer input control, ensuring the value is a valid integer, non-empty,...
static bool ValidateNumericCtrl(wxTextCtrl *aTextCtrl, std::string aLabel, bool aCanBeZero, int *aErrorCount, std::string *aValidationMessage)
Validates a numeric input control, checking if the value is valid, non-empty, and greater than zero.
std::function< std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA >(const std::shared_ptr< DRC_RULE > &)> RuleConverter
static bool ValidateCheckBoxCtrls(const std::vector< wxCheckBox * > &aCheckboxes, std::string aLabel, int *aErrorCount, std::string *aValidationMessage)
Validates a list of checkboxes, ensuring that at least one option is selected.
static std::string FormatErrorMessage(const int &aErrorCount, const std::string aErrorMessage)
static bool ValidateAtLeastOneSelected(const std::vector< bool > &aSelected, const std::string &aLabel, VALIDATION_RESULT *aResult)
Validates that at least one option is selected.
static bool ValidateMinPreferredMaxCtrl(wxTextCtrl *aMinTextCtrl, wxTextCtrl *aPreferredTextCtrl, wxTextCtrl *aMaxTextCtrl, std::string aMinLabel, std::string aPreferredLabel, std::string aMaxLabel, int *aErrorCount, std::string *aValidationMessage)
Validates the minimum, preferred, and maximum value controls, ensuring that:
static bool IsBoolInputType(const DRC_RULE_EDITOR_CONSTRAINT_NAME &aConstraintType)
static std::vector< std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > > ParseRules(const wxString &aRules)
DRC_RULE_EDITOR_CONSTRAINT_NAME
Result of a validation operation.