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 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 
35 #include <general.h>
36 #include <class_library.h>
37 #include <dialog_helpers.h>
38 
39 // Used in DisplayListComponentsInLib: this is a callback function for EDA_LIST_DIALOG
40 // to display keywords and description of a component
41 static void DisplayCmpDocAndKeywords( wxString& aName, void* aData )
42 {
43  PART_LIBS* libs = (PART_LIBS*) aData;
44 
45  wxASSERT( libs );
46 
47  LIB_ID id( wxEmptyString, aName );
48  LIB_ALIAS* part = libs->FindLibraryAlias( id );
49 
50  if( !part )
51  return;
52 
53  aName = wxT( "Description: " ) + part->GetDescription();
54  aName += wxT( "\nKey Words: " ) + part->GetKeyWords();
55 }
56 
57 
59 {
60  PROJECT& prj = Prj();
61 
62  if( PART_LIBS* libs = prj.SchLibs() )
63  {
64  if( !libs->GetLibraryCount() )
65  {
66  DisplayError( this, _( "No component libraries are loaded." ) );
67  return NULL;
68  }
69 
70  wxArrayString headers;
71 
72  headers.Add( wxT( "Library" ) );
73 
74  wxArrayString libNamesList = libs->GetLibraryNames();
75 
76  std::vector<wxArrayString> itemsToDisplay;
77 
78  // Conversion from wxArrayString to vector of ArrayString
79  for( unsigned i = 0; i < libNamesList.GetCount(); i++ )
80  {
81  wxArrayString item;
82 
83  item.Add( libNamesList[i] );
84 
85  itemsToDisplay.push_back( item );
86  }
87 
88  wxString old_lib_name = prj.GetRString( PROJECT::SCH_LIB_SELECT );
89 
90  EDA_LIST_DIALOG dlg( this, _( "Select Library" ), headers, itemsToDisplay, old_lib_name );
91 
92  if( dlg.ShowModal() != wxID_OK )
93  return NULL;
94 
95  wxString libname = dlg.GetTextSelection();
96 
97  if( !libname )
98  return NULL;
99 
100  PART_LIB* lib = libs->FindLibrary( libname );
101 
102  if( lib )
103  prj.SetRString( PROJECT::SCH_LIB_SELECT, libname );
104 
105  return lib;
106  }
107 
108  return NULL;
109 }
110 
111 
112 
114  wxString& aBuffer, wxString& aPreviousChoice )
115 {
116  wxArrayString nameList;
117 
118  if( aLibrary == NULL )
119  aLibrary = SelectLibraryFromList();
120 
121  if( aLibrary == NULL )
122  return false;
123 
124  aLibrary->GetAliasNames( nameList );
125 
126  wxArrayString headers;
127  headers.Add( wxT("Component") );
128  headers.Add( wxT("Library") );
129  std::vector<wxArrayString> itemsToDisplay;
130 
131  // Conversion from wxArrayString to vector of ArrayString
132  for( unsigned i = 0; i < nameList.GetCount(); i++ )
133  {
134  wxArrayString item;
135  item.Add( nameList[i] );
136  item.Add( aLibrary->GetLogicalName() );
137  itemsToDisplay.push_back( item );
138  }
139 
140  EDA_LIST_DIALOG dlg( this, _( "Select Component" ), headers, itemsToDisplay,
141  aPreviousChoice, DisplayCmpDocAndKeywords, Prj().SchLibs() );
142 
143  if( dlg.ShowModal() != wxID_OK )
144  return false;
145 
146  aBuffer = dlg.GetTextSelection();
147 
148  return true;
149 }
150 
151 
152 bool SCH_BASE_FRAME::SelectPartNameToLoad( PART_LIB* aLibrary, wxString& aBufName )
153 {
154  int ii;
155  static wxString previousCmpName;
156 
157  ii = DisplayListComponentsInLib( aLibrary, aBufName, previousCmpName );
158 
159  if( ii <= 0 || aBufName.IsEmpty() )
160  return false;
161 
162  previousCmpName = aBufName;
163  return true;
164 }
bool DisplayListComponentsInLib(PART_LIB *aLibrary, wxString &aBuffer, wxString &aPreviousChoice)
Function DisplayComponentsNamesInLib Select a component from the list of components in a library...
Definition: selpart.cpp:113
Part library alias object definition.
void GetAliasNames(wxArrayString &aNames)
Load a string array with the names of all the entries in this library.
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
This file is part of the common library.
const wxString GetLogicalName()
Function GetLogicalName returns the logical name of the library.
class EDA_LIST_DIALOG
Class LIB_ID.
Definition: lib_id.h:56
LIB_ALIAS * FindLibraryAlias(const LIB_ID &aLibId, const wxString &aLibraryName=wxEmptyString)
Function FindLibraryEntry searches all libraries in the list for an entry.
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
static void DisplayCmpDocAndKeywords(wxString &aName, void *aData)
Definition: selpart.cpp:41
wxString GetDescription() const
Helper dialog and control classes.
PART_LIB * SelectLibraryFromList()
Function SelectLibraryFromList displays a list of current loaded libraries, and allows the user to se...
Definition: selpart.cpp:58
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:162
Class PART_LIBS is a collection of PART_LIBs.
bool SelectPartNameToLoad(PART_LIB *aLibrary, wxString &aBufName)
Function SelectPartNameToLoad Select a part name from the list of components (parts) found in a libra...
Definition: selpart.cpp:152
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:177
see class PGM_BASE
Definition for part library class.
Class PART_LIB is used to load, save, search, and otherwise manipulate part library files...
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