You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
7.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 Wayne Stambaugh, stambaughw@gmail.com
  5. * Copyright (C) 2016-2017 KiCad Developers, see change_log.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file sch_validators.cpp
  26. * @brief Implementation of control validators for schematic dialogs.
  27. */
  28. #include <wx/combo.h>
  29. #include <sch_connection.h>
  30. #include <sch_validators.h>
  31. #include <template_fieldnames.h>
  32. SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR( bool aIsLibEditor, int aFieldId, wxString* aValue ) :
  33. wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
  34. {
  35. m_fieldId = aFieldId;
  36. m_isLibEditor = aIsLibEditor;
  37. // Fields cannot contain carriage returns, line feeds, or tabs.
  38. wxString excludes( "\r\n\t" );
  39. // The reference field cannot contain spaces.
  40. if( aFieldId == REFERENCE )
  41. excludes += " ";
  42. else if( aFieldId == VALUE && m_isLibEditor )
  43. excludes += " :/\\";
  44. long style = GetStyle();
  45. // The reference and value fields cannot be empty.
  46. if( aFieldId == REFERENCE || aFieldId == VALUE || aFieldId == FIELD_NAME )
  47. style |= wxFILTER_EMPTY;
  48. SetStyle( style );
  49. SetCharExcludes( excludes );
  50. }
  51. SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR( const SCH_FIELD_VALIDATOR& aValidator ) :
  52. wxTextValidator( aValidator )
  53. {
  54. m_fieldId = aValidator.m_fieldId;
  55. m_isLibEditor = aValidator.m_isLibEditor;
  56. }
  57. bool SCH_FIELD_VALIDATOR::Validate( wxWindow *aParent )
  58. {
  59. // If window is disabled, simply return
  60. if( !m_validatorWindow->IsEnabled() )
  61. return true;
  62. wxTextEntry * const text = GetTextEntry();
  63. if( !text )
  64. return false;
  65. wxString val( text->GetValue() );
  66. wxString tmp = val.Clone(); // For trailing and leading white space tests.
  67. wxString fieldName;
  68. switch( m_fieldId )
  69. {
  70. case FIELD_NAME: fieldName = _( "field name" ); break;
  71. case REFERENCE: fieldName = _( "reference field" ); break;
  72. case VALUE: fieldName = _( "value field" ); break;
  73. case FOOTPRINT: fieldName = _( "footprint field" ); break;
  74. case DATASHEET: fieldName = _( "datasheet field" ); break;
  75. default: fieldName = _( "user defined field" ); break;
  76. };
  77. wxString msg;
  78. // We can only do some kinds of validation once the input is complete, so
  79. // check for them here:
  80. if( HasFlag( wxFILTER_EMPTY ) && val.empty() )
  81. msg.Printf( _( "The %s cannot be empty." ), fieldName );
  82. else if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) )
  83. {
  84. wxArrayString whiteSpace;
  85. bool spaceIllegal = ( m_fieldId == REFERENCE ) ||
  86. ( m_fieldId == VALUE && m_isLibEditor );
  87. if( val.Find( '\r' ) != wxNOT_FOUND )
  88. whiteSpace.Add( _( "carriage return" ) );
  89. if( val.Find( '\n' ) != wxNOT_FOUND )
  90. whiteSpace.Add( _( "line feed" ) );
  91. if( val.Find( '\t' ) != wxNOT_FOUND )
  92. whiteSpace.Add( _( "tab" ) );
  93. if( spaceIllegal && (val.Find( ' ' ) != wxNOT_FOUND) )
  94. whiteSpace.Add( _( "space" ) );
  95. wxString badChars;
  96. if( whiteSpace.size() == 1 )
  97. badChars = whiteSpace[0];
  98. else if( whiteSpace.size() == 2 )
  99. badChars.Printf( _( "%s or %s" ), whiteSpace[0], whiteSpace[1] );
  100. else if( whiteSpace.size() == 3 )
  101. badChars.Printf( _( "%s, %s, or %s" ), whiteSpace[0], whiteSpace[1], whiteSpace[2] );
  102. else if( whiteSpace.size() == 4 )
  103. badChars.Printf( _( "%s, %s, %s, or %s" ),
  104. whiteSpace[0], whiteSpace[1], whiteSpace[2], whiteSpace[3] );
  105. else
  106. wxCHECK_MSG( false, true, wxT( "Invalid illegal character in field validator." ) );
  107. msg.Printf( _( "The %s cannot contain %s characters." ), fieldName, badChars );
  108. }
  109. if ( !msg.empty() )
  110. {
  111. m_validatorWindow->SetFocus();
  112. wxMessageBox( msg, _( "Field Validation Error" ), wxOK | wxICON_EXCLAMATION, aParent );
  113. return false;
  114. }
  115. return true;
  116. }
  117. SCH_NETNAME_VALIDATOR::SCH_NETNAME_VALIDATOR( wxString *aVal ) :
  118. wxValidator()
  119. {
  120. }
  121. SCH_NETNAME_VALIDATOR::SCH_NETNAME_VALIDATOR( const SCH_NETNAME_VALIDATOR& aValidator )
  122. {
  123. }
  124. wxTextEntry *SCH_NETNAME_VALIDATOR::GetTextEntry()
  125. {
  126. #if wxUSE_TEXTCTRL
  127. if( wxDynamicCast( m_validatorWindow, wxTextCtrl ) )
  128. return static_cast<wxTextCtrl*>( m_validatorWindow );
  129. #endif
  130. #if wxUSE_COMBOBOX
  131. if( wxDynamicCast( m_validatorWindow, wxComboBox ) )
  132. return static_cast<wxComboBox*>( m_validatorWindow );
  133. #endif
  134. #if wxUSE_COMBOCTRL
  135. if( wxDynamicCast( m_validatorWindow, wxComboCtrl ) )
  136. return static_cast<wxComboCtrl*>( m_validatorWindow );
  137. #endif
  138. wxFAIL_MSG( "SCH_NETNAME_VALIDATOR can only be used with wxTextCtrl, wxComboBox, or wxComboCtrl" );
  139. return nullptr;
  140. }
  141. bool SCH_NETNAME_VALIDATOR::Validate( wxWindow *aParent )
  142. {
  143. // If window is disabled, simply return
  144. if ( !m_validatorWindow->IsEnabled() )
  145. return true;
  146. wxTextEntry * const text = GetTextEntry();
  147. if ( !text )
  148. return false;
  149. const wxString& errormsg = IsValid( text->GetValue() );
  150. if( !errormsg.empty() )
  151. {
  152. m_validatorWindow->SetFocus();
  153. wxMessageBox( errormsg, _( "Invalid signal name" ), wxOK | wxICON_EXCLAMATION, aParent );
  154. return false;
  155. }
  156. return true;
  157. }
  158. wxString SCH_NETNAME_VALIDATOR::IsValid( const wxString& str ) const
  159. {
  160. if( SCH_CONNECTION::IsBusGroupLabel( str ) )
  161. return wxString();
  162. if( str.Contains( '{' ) || str.Contains( '}' ) )
  163. return _( "Signal name contains '{' or '}' but is not a valid group bus name" );
  164. if( ( str.Contains( '[' ) || str.Contains( ']' ) ) &&
  165. !SCH_CONNECTION::IsBusVectorLabel( str ) )
  166. return _( "Signal name contains '[' or ']' but is not a valid vector bus name" );
  167. if( str.Contains( '\r' ) || str.Contains( '\n' ) )
  168. return _( "Signal names cannot contain CR or LF characters" );
  169. if( str.Contains( ' ' ) || str.Contains( '\t' ) )
  170. return _( "Signal names cannot contain spaces" );
  171. return wxString();
  172. }