KiCad PCB EDA Suite
selpart.cpp
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) 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 
29 #include <fctsys.h>
30 #include <gr_basic.h>
31 #include <confirm.h>
32 #include <pgm_base.h>
33 #include <sch_base_frame.h>
34 #include <symbol_lib_table.h>
35 
36 #include <general.h>
37 #include <class_library.h>
38 #include <dialog_helpers.h>
39 
40 
41 static void DisplayCmpDocAndKeywords( wxString& aSelection, void* aData )
42 {
43  SYMBOL_LIB_TABLE* libs = (SYMBOL_LIB_TABLE*) aData;
44 
45  wxASSERT( libs );
46 
47  LIB_ID id;
48 
49  if( id.Parse( aSelection ) != -1 )
50  {
51  aSelection = _( "Invalid symbol library indentifier!" );
52  return;
53  }
54 
55  LIB_ALIAS* part = nullptr;
56 
57  try
58  {
59  part = libs->LoadSymbol( id );
60  }
61  catch( const IO_ERROR& ioe )
62  {
63  aSelection.Printf( _( "Error occurred loading symbol '%s' from library '%s'." ),
64  id.GetLibItemName().wx_str(), id.GetLibNickname().wx_str() );
65  return;
66  }
67 
68  if( !part )
69  return;
70 
71  aSelection = _( "Description: " ) + part->GetDescription() + "\n";
72  aSelection += _( "Key Words: " ) + part->GetKeyWords();
73 }
74 
75 
77 {
78  PROJECT& prj = Prj();
79 
80  if( prj.SchSymbolLibTable()->IsEmpty() )
81  {
82  DisplayError( this, _( "No symbol libraries are loaded." ) );
83  return wxEmptyString;
84  }
85 
86  wxArrayString headers;
87 
88  headers.Add( _( "Library" ) );
89 
90  std::vector< wxArrayString > itemsToDisplay;
91  std::vector< wxString > libNicknames = prj.SchSymbolLibTable()->GetLogicalLibs();
92 
93  // Conversion from wxArrayString to vector of ArrayString
94  for( auto name : libNicknames )
95  {
96  wxArrayString item;
97 
98  item.Add( name );
99  itemsToDisplay.push_back( item );
100  }
101 
102  wxString old_lib_name = prj.GetRString( PROJECT::SCH_LIB_SELECT );
103 
104  EDA_LIST_DIALOG dlg( this, _( "Select Symbol Library" ), headers, itemsToDisplay,
105  old_lib_name );
106 
107  if( dlg.ShowModal() != wxID_OK )
108  return wxEmptyString;
109 
110  wxString libname = dlg.GetTextSelection();
111 
112  if( !libname.empty() )
113  {
114  if( prj.SchSymbolLibTable()->HasLibrary( libname ) )
115  prj.SetRString( PROJECT::SCH_LIB_SELECT, libname );
116  else
117  libname = wxEmptyString;
118  }
119 
120  return libname;
121 }
122 
123 
124 
125 bool SCH_BASE_FRAME::DisplayListComponentsInLib( wxString& aLibrary, wxString& aBuffer,
126  wxString& aPreviousChoice )
127 {
128  wxArrayString nameList;
129 
130  if( !aLibrary )
131  aLibrary = SelectLibraryFromList();
132 
133  if( !aLibrary )
134  return false;
135 
136  try
137  {
138  Prj().SchSymbolLibTable()->EnumerateSymbolLib( aLibrary, nameList );
139  }
140  catch( const IO_ERROR& ioe )
141  {
142  wxString msg;
143 
144  msg.Printf( _( "Error occurred loading symbol library '%s'." ), aLibrary );
145  DisplayErrorMessage( this, msg, ioe.What() );
146  return false;
147  }
148 
149  wxArrayString headers;
150  headers.Add( _( "Library:Symbol" ) );
151 
152  std::vector<wxArrayString> itemsToDisplay;
153 
154  // Conversion from wxArrayString to vector of ArrayString
155  for( unsigned i = 0; i < nameList.GetCount(); i++ )
156  {
157  LIB_ID id;
158  wxArrayString item;
159  id.SetLibItemName( nameList[i], false );
160  id.SetLibNickname( aLibrary );
161  item.Add( id.Format() );
162  itemsToDisplay.push_back( item );
163  }
164 
165  EDA_LIST_DIALOG dlg( this, _( "Select Symbol" ), headers, itemsToDisplay, aPreviousChoice,
166  DisplayCmpDocAndKeywords, Prj().SchSymbolLibTable() );
167 
168  if( dlg.ShowModal() != wxID_OK )
169  return false;
170 
171  aBuffer = dlg.GetTextSelection();
172 
173  return true;
174 }
175 
176 
177 bool SCH_BASE_FRAME::SelectPartNameToLoad( wxString& aLibrary, wxString& aBufName )
178 {
179  static wxString previousCmpName;
180 
181  if( !DisplayListComponentsInLib( aLibrary, aBufName, previousCmpName ) || aBufName.empty() )
182  return false;
183 
184  previousCmpName = aBufName;
185  return true;
186 }
LIB_ALIAS * LoadSymbol(const wxString &aNickname, const wxString &aAliasName)
Load a LIB_ALIAS having aAliasName from the library given by aNickname.
Part library alias object definition.
wxString GetTextSelection(int aColumn=0)
Function GetTextSelection return the selected text from aColumn in the wxListCtrl in the dialog...
Definition: displlst.cpp:137
Class PROJECT holds project specific data.
Definition: project.h:56
wxString SelectLibraryFromList()
Dispaly a list of loaded libraries in the symbol library and allows the user to select a library...
Definition: selpart.cpp:76
This file is part of the common library.
static void DisplayCmpDocAndKeywords(wxString &aSelection, void *aData)
Definition: selpart.cpp:41
class EDA_LIST_DIALOG
bool DisplayListComponentsInLib(wxString &aLibrary, wxString &aBuffer, wxString &aPreviousChoice)
Select a component from the list of components in a library.
Definition: selpart.cpp:125
Class LIB_ID.
Definition: lib_id.h:56
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:85
wxString GetDescription() const
Helper dialog and control classes.
bool SelectPartNameToLoad(wxString &aLibrary, wxString &aBufName)
Function SelectPartNameToLoad Select a part name from the list of components (parts) found in a libra...
Definition: selpart.cpp:177
int SetLibItemName(const UTF8 &aLibItemName, bool aTestForRev=true)
Function SetLibItemName.
Definition: lib_id.cpp:232
VTBL_ENTRY void SetRString(RSTRING_T aStringId, const wxString &aString)
Function SetRString stores a "retained string", which is any session and project specific string iden...
Definition: project.cpp:173
VTBL_ENTRY const wxString & GetRString(RSTRING_T aStringId)
Function GetRString returns a "retained string", which is any session and project specific string ide...
Definition: project.cpp:188
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
see class PGM_BASE
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
const char * name
Definition for part library class.
wxString GetKeyWords() const
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:71
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47