KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_content_panel_base.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_RE_CLASSES_H_
25#define DRC_RE_CLASSES_H_
26
27#include <wx/arrstr.h>
28#include <wx/bitmap.h>
29#include <wx/chartype.h>
30#include <wx/statbmp.h>
31#include <wx/tokenzr.h>
32
33#include <bitmaps.h>
34#include <confirm.h>
35#include <eda_text.h>
38#include <grid_tricks.h>
39#include <kidialog.h>
40#include <pgm_base.h>
42#include <template_fieldnames.h>
43#include <string_utils.h>
44
45#include <algorithm>
46#include <vector>
47
49
51{
52public:
54
56
57 virtual bool ValidateInputs( int* aErrorCount, std::string* aValidationMessage ) = 0;
58
59 virtual wxString GenerateRule( const RULE_GENERATION_CONTEXT& aContext ) = 0;
60
61 wxStaticBitmap* GetConstraintImage(wxPanel* aParent, BITMAPS aBitMap)
62 {
63 return new wxStaticBitmap( aParent, wxID_ANY, KiBitmapBundle( aBitMap ),
64 wxDefaultPosition, wxSize( -1, 250 ), 0 );
65 }
66
67protected:
68 static wxString sanitizeRuleName( const wxString& aRuleName )
69 {
70 if( aRuleName.IsEmpty() )
71 return wxString( wxS( "rule" ) );
72
73 wxString result;
74 result.reserve( aRuleName.length() );
75
76 for( wxUniChar c : aRuleName )
77 {
78 if( wxIsspace( c ) )
79 {
80 result.append( '_' );
81 }
82 else if( wxIsalnum( c ) || c == '_' || c == '-' || c == '.' )
83 {
84 result.append( c );
85 }
86 else
87 {
88 result.append( '_' );
89 }
90 }
91
92 // Avoid names starting with a digit which S-expression parsers treat specially.
93 if( !result.empty() && wxIsdigit( *result.begin() ) )
94 result.Prepend( wxS( "r_" ) );
95
96 return result;
97 }
98
99 static wxString escapeCondition( const wxString& aCondition )
100 {
101 return EscapeString( aCondition, CTX_QUOTED_STR );
102 }
103
104 static wxString trimTrailingZeros( const wxString& aValue )
105 {
106 wxString result( aValue );
107
108 if( !result.Contains( '.' ) )
109 return result;
110
111 while( result.Length() > 1 && result.Last() == '0' )
112 result.Truncate( result.Length() - 1 );
113
114 if( result.Last() == '.' )
115 result.Truncate( result.Length() - 1 );
116
117 return result;
118 }
119
120 static wxString formatDouble( double aValue, int aPrecision = 6 )
121 {
122 wxString formatted = wxString::Format( wxS( "%.*f" ), aPrecision, aValue );
123 return trimTrailingZeros( formatted );
124 }
125
126 wxString buildRule( const RULE_GENERATION_CONTEXT& aContext,
127 const std::vector<wxString>& aConstraintClauses ) const
128 {
129 wxString rule;
130 rule << wxS( "(rule " ) << sanitizeRuleName( aContext.ruleName ) << wxS( "\n" );
131
132 if( !aContext.comment.IsEmpty() )
133 {
134 wxArrayString lines = wxSplit( aContext.comment, '\n', '\0' );
135
136 for( const wxString& line : lines )
137 {
138 if( line.IsEmpty() )
139 continue;
140
141 rule << wxS( "\t# " ) << line << wxS( "\n" );
142 }
143 }
144
145 if( !aContext.layerClause.IsEmpty() )
146 rule << wxS( "\t" ) << aContext.layerClause << wxS( "\n" );
147
148 for( const wxString& clause : aConstraintClauses )
149 {
150 if( clause.IsEmpty() )
151 continue;
152
153 rule << wxS( "\t" ) << clause << wxS( "\n" );
154 }
155
156 if( !aContext.conditionExpression.IsEmpty() )
157 {
158 rule << wxS( "\t(condition \"" ) << escapeCondition( aContext.conditionExpression )
159 << wxS( "\")\n" );
160 }
161
162 rule << wxS( ")\n" );
163
164 return rule;
165 }
166};
167
168#endif // DRC_RE_CLASSES_H_
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
BITMAPS
A list of all bitmap identifiers.
static wxString escapeCondition(const wxString &aCondition)
virtual bool ValidateInputs(int *aErrorCount, std::string *aValidationMessage)=0
wxString buildRule(const RULE_GENERATION_CONTEXT &aContext, const std::vector< wxString > &aConstraintClauses) const
virtual wxString GenerateRule(const RULE_GENERATION_CONTEXT &aContext)=0
static wxString trimTrailingZeros(const wxString &aValue)
virtual ~DRC_RULE_EDITOR_CONTENT_PANEL_BASE()=default
static wxString formatDouble(double aValue, int aPrecision=6)
static wxString sanitizeRuleName(const wxString &aRuleName)
wxStaticBitmap * GetConstraintImage(wxPanel *aParent, BITMAPS aBitMap)
This file is part of the common library.
see class PGM_BASE
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_QUOTED_STR
wxString result
Test unit parsing edge cases and error handling.