KiCad PCB EDA Suite
viewlibs.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 AUTHORS.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 <kiway.h>
31 #include <gr_basic.h>
32 #include <pgm_base.h>
33 #include <class_drawpanel.h>
34 #include <confirm.h>
35 #include <eda_doc.h>
36 
37 #include <class_sch_screen.h>
38 #include <general.h>
39 #include <viewlib_frame.h>
40 #include <eeschema_id.h>
41 #include <class_library.h>
42 #include <dialog_helpers.h>
44 #include <cmp_tree_model_adapter.h>
45 #include <symbol_lib_table.h>
46 
47 void LIB_VIEW_FRAME::OnSelectSymbol( wxCommandEvent& aEvent )
48 {
49  wxString dialogTitle;
50  SYMBOL_LIB_TABLE* libs = Prj().SchSymbolLibTable();
51 
52  // Container doing search-as-you-type.
53  auto adapter( CMP_TREE_MODEL_ADAPTER::Create( libs ) );
54 
55  const auto libNicknames = libs->GetLogicalLibs();
56 
57  adapter->AddLibrariesWithProgress( libNicknames, this );
58 
59  dialogTitle.Printf( _( "Choose Component (%d items loaded)" ),
60  adapter->GetComponentsCount() );
61  DIALOG_CHOOSE_COMPONENT dlg( this, dialogTitle, adapter, m_convert, false );
62 
63  if( dlg.ShowQuasiModal() == wxID_CANCEL )
64  return;
65 
68  LIB_ID id = dlg.GetSelectedLibId( &m_unit );
69 
70  if( !id.IsValid() || id.GetLibNickname().empty() )
71  return;
72 
73  if( m_libraryName == id.GetLibNickname() )
74  {
75  if( m_entryName != id.GetLibItemName() )
76  SetSelectedComponent( id.GetLibItemName() );
77  }
78  else
79  {
80  m_entryName = id.GetLibItemName();
81  SetSelectedLibrary( id.GetLibNickname() );
82  SetSelectedComponent( id.GetLibItemName() );
83  }
84 
85  Zoom_Automatique( false );
86 }
87 
88 
89 void LIB_VIEW_FRAME::onSelectNextSymbol( wxCommandEvent& aEvent )
90 {
91  wxCommandEvent evt( wxEVT_COMMAND_LISTBOX_SELECTED, ID_LIBVIEW_CMP_LIST );
92  int ii = m_cmpList->GetSelection();
93 
94  // Select the next symbol or stop at the end of the list.
95  if( ii != wxNOT_FOUND || ii != (int)m_cmpList->GetCount() - 1 )
96  ii += 1;
97 
98  m_cmpList->SetSelection( ii );
99  ProcessEvent( evt );
100 }
101 
102 
103 void LIB_VIEW_FRAME::onSelectPreviousSymbol( wxCommandEvent& aEvent )
104 {
105  wxCommandEvent evt( wxEVT_COMMAND_LISTBOX_SELECTED, ID_LIBVIEW_CMP_LIST );
106  int ii = m_cmpList->GetSelection();
107 
108  // Select the previous symbol or stop at the beginning of list.
109  if( ii != wxNOT_FOUND && ii != 0 )
110  ii -= 1;
111 
112  m_cmpList->SetSelection( ii );
113  ProcessEvent( evt );
114 }
115 
116 
117 void LIB_VIEW_FRAME::onViewSymbolDocument( wxCommandEvent& aEvent )
118 {
120  LIB_ALIAS* entry = Prj().SchSymbolLibTable()->LoadSymbol( id );
121 
122  if( entry && !entry->GetDocFileName().IsEmpty() )
123  {
124  SEARCH_STACK* lib_search = Prj().SchSearchS();
125 
126  GetAssociatedDocument( this, entry->GetDocFileName(), lib_search );
127  }
128 }
129 
130 
131 void LIB_VIEW_FRAME::onSelectSymbolBodyStyle( wxCommandEvent& aEvent )
132 {
133  int id = aEvent.GetId();
134 
135  switch( id )
136  {
137  default:
139  m_convert = 1;
140  break;
141 
143  m_convert = 2;
144  break;
145  }
146 
147  m_canvas->Refresh();
148 }
149 
150 
151 void LIB_VIEW_FRAME::onSelectSymbolUnit( wxCommandEvent& aEvent )
152 {
153  int ii = m_selpartBox->GetCurrentSelection();
154 
155  if( ii < 0 )
156  return;
157 
158  m_unit = ii + 1;
159  m_canvas->Refresh();
160 }
161 
162 
163 void LIB_VIEW_FRAME::OnLeftClick( wxDC* DC, const wxPoint& MousePos )
164 {
165 }
166 
167 
168 bool LIB_VIEW_FRAME::OnRightClick( const wxPoint& MousePos, wxMenu* PopMenu )
169 {
170  return true;
171 }
172 
173 
175 {
176  if( m_libList && !m_libList->IsEmpty() && !m_libraryName.IsEmpty() )
177  {
178  const SYMBOL_LIB_TABLE_ROW* row = Prj().SchSymbolLibTable()->FindRow( m_libraryName );
179 
180  wxString title = wxString::Format( L"Symbol Library Browser \u2014 %s",
181  row ? row->GetFullURI() : "no library selected" );
182  SetTitle( title );
183  }
184 }
185 
186 
187 void LIB_VIEW_FRAME::RedrawActiveWindow( wxDC* DC, bool EraseBg )
188 {
190  LIB_ALIAS* entry = nullptr;
191 
192  try
193  {
194  entry = Prj().SchSymbolLibTable()->LoadSymbol( id );
195  }
196  catch( const IO_ERROR& e ) {} // ignore, it is handled below
197 
198  if( !entry )
199  return;
200 
201  LIB_PART* part = entry->GetPart();
202 
203  if( !part )
204  return;
205 
206  wxString msg;
207  wxString tmp;
208 
209  m_canvas->DrawBackGround( DC );
210 
211  if( !entry->IsRoot() )
212  {
213  // Temporarily change the name field text to reflect the alias name.
214  msg = entry->GetName();
215  tmp = part->GetName();
216 
217  part->SetName( msg );
218 
219  if( m_unit < 1 )
220  m_unit = 1;
221 
222  if( m_convert < 1 )
223  m_convert = 1;
224  }
225  else
226  msg = _( "None" );
227 
228  auto opts = PART_DRAW_OPTIONS::Default();
229  opts.show_elec_type = GetShowElectricalType();
230  part->Draw( m_canvas, DC, wxPoint( 0, 0 ), m_unit, m_convert, opts );
231 
232  // Redraw the cursor
233  m_canvas->DrawCrossHair( DC );
234 
235  if( !tmp.IsEmpty() )
236  part->SetName( tmp );
237 
238  ClearMsgPanel();
239  AppendMsgPanel( _( "Part" ), part->GetName(), BLUE, 6 );
240  AppendMsgPanel( _( "Alias" ), msg, RED, 6 );
241  AppendMsgPanel( _( "Description" ), entry->GetDescription(), CYAN, 6 );
242  AppendMsgPanel( _( "Key words" ), entry->GetKeyWords(), DARKDARKGRAY );
243 }
Definition: colors.h:57
static int m_unit
static int m_convert
static wxString m_entryName
void Draw(EDA_DRAW_PANEL *aPanel, wxDC *aDc, const wxPoint &aOffset, int aMulti, int aConvert, const PART_DRAW_OPTIONS &aOpts)
Draw part.
Part library alias object definition.
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Update the board display after modifying it bu a python script (note: it is automatically called by a...
Definition: draw_panel.cpp:325
This file is part of the common library.
This file is part of the common library.
bool OnRightClick(const wxPoint &MousePos, wxMenu *PopMenu) override
Definition: viewlibs.cpp:168
void onSelectPreviousSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:103
void DrawCrossHair(wxDC *aDC, COLOR4D aColor=COLOR4D::WHITE)
Function DrawCrossHair draws the user cross hair.
Definition: draw_panel.cpp:215
void OnLeftClick(wxDC *DC, const wxPoint &MousePos) override
Definition: viewlibs.cpp:163
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, const wxPathList *aPaths)
Function GetAssociatedDocument open a document (file) with the suitable browser.
Definition: eda_doc.cpp:86
wxListBox * m_cmpList
Class SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
static wxString m_libraryName
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
void SetSelectedComponent(const wxString &aComponentName)
Set the selected component.
const wxString GetFullURI(bool aSubstituted=false) const
Return the full location specifying URI for the LIB, either in original UI form or in environment var...
static PTR Create(SYMBOL_LIB_TABLE *aLibs)
Factory function: create a model adapter in a reference-counting container.
Dialog class to select a component from the libraries.
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
void Zoom_Automatique(bool aWarpPointer)
Function Zoom_Automatique redraws the screen with best zoom level and the best centering that shows a...
Definition: zoom.cpp:77
bool IsRoot() const
void OnSelectSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:47
void onViewSymbolDocument(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:117
Definitions for the Eeschema program SCH_SCREEN class.
const wxString & GetName() const
int ShowQuasiModal()
wxString GetDescription() const
void RedrawActiveWindow(wxDC *DC, bool EraseBg) override
Definition: viewlibs.cpp:187
void onSelectSymbolBodyStyle(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:131
Definition: colors.h:59
Class LIB_PART defines a library part object.
Helper dialog and control classes.
Definition: colors.h:60
static PART_DRAW_OPTIONS Default()
void DisplayLibInfos()
Definition: viewlibs.cpp:174
LIB_PART * GetPart() const
Function GetPart gets the shared LIB_PART.
void onSelectNextSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:89
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:93
void AppendMsgPanel(const wxString &textUpper, const wxString &textLower, COLOR4D color, int pad=6)
Append a message to the message panel.
Definition: draw_frame.cpp:764
wxString GetDocFileName() const
see class PGM_BASE
const wxString & GetName() const
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
wxListBox * m_libList
static bool empty(const wxTextEntryBase *aCtrl)
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
To be called after this dialog returns from ShowModal().
virtual void SetName(const wxString &aName)
void DrawBackGround(wxDC *DC)
Function DrawBackGround.
Definition: draw_panel.cpp:665
bool GetShowElectricalType()
void onSelectSymbolUnit(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:151
Definition for part library class.
wxString GetKeyWords() const
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
void ClearMsgPanel(void)
Clear all messages from the message panel.
Definition: draw_frame.cpp:775
bool ProcessEvent(wxEvent &aEvent) override
Function ProcessEvent overrides the default process event handler to implement the auto save feature...
Definition: basicframe.cpp:185
wxComboBox * m_selpartBox
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE...
void SetSelectedLibrary(const wxString &aLibName)
Set the selected library in the library window.