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.

186 lines
5.0 KiB

15 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2015-2017 KiCad Developers, see CHANGELOG.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 selpart.cpp
  26. */
  27. #include <fctsys.h>
  28. #include <gr_basic.h>
  29. #include <confirm.h>
  30. #include <pgm_base.h>
  31. #include <sch_base_frame.h>
  32. #include <symbol_lib_table.h>
  33. #include <general.h>
  34. #include <class_library.h>
  35. #include <dialog_helpers.h>
  36. static void DisplayCmpDocAndKeywords( wxString& aSelection, void* aData )
  37. {
  38. SYMBOL_LIB_TABLE* libs = (SYMBOL_LIB_TABLE*) aData;
  39. wxASSERT( libs );
  40. LIB_ID id;
  41. if( id.Parse( aSelection ) != -1 )
  42. {
  43. aSelection = _( "Invalid symbol library identifier!" );
  44. return;
  45. }
  46. LIB_ALIAS* part = nullptr;
  47. try
  48. {
  49. part = libs->LoadSymbol( id );
  50. }
  51. catch( const IO_ERROR& )
  52. {
  53. aSelection.Printf( _( "Error occurred loading symbol \"%s\" from library \"%s\"." ),
  54. id.GetLibItemName().wx_str(), id.GetLibNickname().wx_str() );
  55. return;
  56. }
  57. if( !part )
  58. return;
  59. aSelection = _( "Description:" ) + " " + part->GetDescription() + "\n";
  60. aSelection += _( "Key words:" ) + " " + part->GetKeyWords();
  61. }
  62. wxString SCH_BASE_FRAME::SelectLibraryFromList()
  63. {
  64. PROJECT& prj = Prj();
  65. if( prj.SchSymbolLibTable()->IsEmpty() )
  66. {
  67. DisplayError( this, _( "No symbol libraries are loaded." ) );
  68. return wxEmptyString;
  69. }
  70. wxArrayString headers;
  71. headers.Add( _( "Library" ) );
  72. std::vector< wxArrayString > itemsToDisplay;
  73. std::vector< wxString > libNicknames = prj.SchSymbolLibTable()->GetLogicalLibs();
  74. // Conversion from wxArrayString to vector of ArrayString
  75. for( const auto& name : libNicknames )
  76. {
  77. wxArrayString item;
  78. item.Add( name );
  79. itemsToDisplay.push_back( item );
  80. }
  81. wxString old_lib_name = prj.GetRString( PROJECT::SCH_LIB_SELECT );
  82. EDA_LIST_DIALOG dlg( this, _( "Select Symbol Library" ), headers, itemsToDisplay,
  83. old_lib_name );
  84. if( dlg.ShowModal() != wxID_OK )
  85. return wxEmptyString;
  86. wxString libname = dlg.GetTextSelection();
  87. if( !libname.empty() )
  88. {
  89. if( prj.SchSymbolLibTable()->HasLibrary( libname ) )
  90. prj.SetRString( PROJECT::SCH_LIB_SELECT, libname );
  91. else
  92. libname = wxEmptyString;
  93. }
  94. return libname;
  95. }
  96. bool SCH_BASE_FRAME::DisplayListComponentsInLib( wxString& aLibrary, wxString& aBuffer,
  97. wxString& aPreviousChoice )
  98. {
  99. wxArrayString nameList;
  100. if( !aLibrary )
  101. aLibrary = SelectLibraryFromList();
  102. if( !aLibrary )
  103. return false;
  104. try
  105. {
  106. Prj().SchSymbolLibTable()->EnumerateSymbolLib( aLibrary, nameList );
  107. }
  108. catch( const IO_ERROR& ioe )
  109. {
  110. wxString msg;
  111. msg.Printf( _( "Error occurred loading symbol library \"%s\"." ), aLibrary );
  112. DisplayErrorMessage( this, msg, ioe.What() );
  113. return false;
  114. }
  115. wxArrayString headers;
  116. headers.Add( _( "Library:Symbol" ) );
  117. std::vector<wxArrayString> itemsToDisplay;
  118. // Conversion from wxArrayString to vector of ArrayString
  119. for( unsigned i = 0; i < nameList.GetCount(); i++ )
  120. {
  121. LIB_ID id;
  122. wxArrayString item;
  123. id.SetLibItemName( nameList[i], false );
  124. id.SetLibNickname( aLibrary );
  125. item.Add( id.Format() );
  126. itemsToDisplay.push_back( item );
  127. }
  128. EDA_LIST_DIALOG dlg( this, _( "Select Symbol" ), headers, itemsToDisplay, aPreviousChoice,
  129. DisplayCmpDocAndKeywords, Prj().SchSymbolLibTable() );
  130. if( dlg.ShowModal() != wxID_OK )
  131. return false;
  132. aBuffer = dlg.GetTextSelection();
  133. return true;
  134. }
  135. bool SCH_BASE_FRAME::SelectPartNameToLoad( wxString& aLibrary, wxString& aBufName )
  136. {
  137. static wxString previousCmpName;
  138. if( !DisplayListComponentsInLib( aLibrary, aBufName, previousCmpName ) || aBufName.empty() )
  139. return false;
  140. previousCmpName = aBufName;
  141. return true;
  142. }