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.

141 lines
4.9 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 <sch_validators.h>
  29. #include <template_fieldnames.h>
  30. SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR( bool aIsCmplibEditor,
  31. int aFieldId, wxString* aValue ) :
  32. wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
  33. {
  34. m_fieldId = aFieldId;
  35. m_isLibEditor = aIsCmplibEditor;
  36. // Fields cannot contain carriage returns, line feeds, or tabs.
  37. wxString excludes( "\r\n\t" );
  38. // The reference field cannot contain spaces.
  39. if( aFieldId == REFERENCE )
  40. excludes += " ";
  41. else if( aFieldId == VALUE && m_isLibEditor )
  42. excludes += " :/\\";
  43. long style = GetStyle();
  44. // The reference and value fields cannot be empty.
  45. if( aFieldId == REFERENCE || aFieldId == VALUE )
  46. style |= wxFILTER_EMPTY;
  47. SetStyle( style );
  48. SetCharExcludes( excludes );
  49. }
  50. SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR( const SCH_FIELD_VALIDATOR& aValidator ) :
  51. wxTextValidator( aValidator )
  52. {
  53. m_fieldId = aValidator.m_fieldId;
  54. m_isLibEditor = aValidator.m_isLibEditor;
  55. }
  56. bool SCH_FIELD_VALIDATOR::Validate( wxWindow *aParent )
  57. {
  58. // If window is disabled, simply return
  59. if( !m_validatorWindow->IsEnabled() )
  60. return true;
  61. wxTextEntry * const text = GetTextEntry();
  62. if( !text )
  63. return false;
  64. wxString val( text->GetValue() );
  65. wxString tmp = val.Clone(); // For trailing and leading white space tests.
  66. wxString fieldName;
  67. switch( m_fieldId )
  68. {
  69. case REFERENCE: fieldName = _( "reference designator" ); break;
  70. case VALUE: fieldName = _( "value" ); break;
  71. case FOOTPRINT: fieldName = _( "footprint" ); break;
  72. case DATASHEET: fieldName = _( "data sheet" ); break;
  73. default: fieldName = _( "user defined" ); break;
  74. };
  75. wxString errorMsg;
  76. // We can only do some kinds of validation once the input is complete, so
  77. // check for them here:
  78. if( HasFlag( wxFILTER_EMPTY ) && val.empty() )
  79. errorMsg.Printf( _( "The %s field cannot be empty." ), fieldName );
  80. else if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) )
  81. {
  82. wxArrayString whiteSpace;
  83. bool spaceIllegal = ( m_fieldId == REFERENCE ) ||
  84. ( m_fieldId == VALUE && m_isLibEditor );
  85. if( val.Find( '\r' ) != wxNOT_FOUND )
  86. whiteSpace.Add( _( "carriage return" ) );
  87. if( val.Find( '\n' ) != wxNOT_FOUND )
  88. whiteSpace.Add( _( "line feed" ) );
  89. if( val.Find( '\t' ) != wxNOT_FOUND )
  90. whiteSpace.Add( _( "tab" ) );
  91. if( spaceIllegal && (val.Find( ' ' ) != wxNOT_FOUND) )
  92. whiteSpace.Add( _( "space" ) );
  93. wxString badChars;
  94. if( whiteSpace.size() == 1 )
  95. badChars = whiteSpace[0];
  96. else if( whiteSpace.size() == 2 )
  97. badChars.Printf( _( "%s or %s" ), whiteSpace[0], whiteSpace[1] );
  98. else if( whiteSpace.size() == 3 )
  99. badChars.Printf( _( "%s, %s, or %s" ), whiteSpace[0], whiteSpace[1], whiteSpace[2] );
  100. else if( whiteSpace.size() == 4 )
  101. badChars.Printf( _( "%s, %s, %s, or %s" ),
  102. whiteSpace[0], whiteSpace[1], whiteSpace[2], whiteSpace[3] );
  103. else
  104. wxCHECK_MSG( false, true, wxT( "Invalid illegal character in field validator." ) );
  105. errorMsg.Printf( _( "The %s field cannot contain %s characters." ), fieldName, badChars );
  106. }
  107. if ( !errorMsg.empty() )
  108. {
  109. m_validatorWindow->SetFocus();
  110. wxMessageBox( errorMsg, _( "Field Validation Error" ),
  111. wxOK | wxICON_EXCLAMATION, aParent );
  112. return false;
  113. }
  114. return true;
  115. }