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-2018 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 <pgm_base.h>
32 #include <class_drawpanel.h>
33 #include <confirm.h>
34 #include <eda_doc.h>
35 
36 #include <viewlib_frame.h>
37 #include <eeschema_id.h>
38 #include <class_library.h>
39 #include <dialog_helpers.h>
41 #include <cmp_tree_model_adapter.h>
42 #include <symbol_lib_table.h>
43 
44 void LIB_VIEW_FRAME::OnSelectSymbol( wxCommandEvent& aEvent )
45 {
46  std::unique_lock<std::mutex> dialogLock( DIALOG_CHOOSE_COMPONENT::g_Mutex, std::defer_lock );
47  wxString dialogTitle;
48  SYMBOL_LIB_TABLE* libs = Prj().SchSymbolLibTable();
49 
50  // One CHOOSE_COMPONENT dialog at a time. User probaby can't handle more anyway.
51  if( !dialogLock.try_lock() )
52  return;
53 
54  // Container doing search-as-you-type.
55  auto adapter( CMP_TREE_MODEL_ADAPTER::Create( libs ) );
56 
57  const auto libNicknames = libs->GetLogicalLibs();
58 
59  adapter->AddLibrariesWithProgress( libNicknames, this );
60 
61  dialogTitle.Printf( _( "Choose Symbol (%d items loaded)" ),
62  adapter->GetComponentsCount() );
63  DIALOG_CHOOSE_COMPONENT dlg( this, dialogTitle, adapter, m_convert, false, false );
64 
65  if( dlg.ShowQuasiModal() == wxID_CANCEL )
66  return;
67 
70  LIB_ID id = dlg.GetSelectedLibId( &m_unit );
71 
72  if( !id.IsValid() || id.GetLibNickname().empty() )
73  return;
74 
75  if( m_libraryName == id.GetLibNickname() )
76  {
77  if( m_entryName != id.GetLibItemName() )
78  SetSelectedComponent( id.GetLibItemName() );
79  }
80  else
81  {
82  m_entryName = id.GetLibItemName();
83  SetSelectedLibrary( id.GetLibNickname() );
84  SetSelectedComponent( id.GetLibItemName() );
85  }
86 
87  Zoom_Automatique( false );
88 }
89 
90 
91 void LIB_VIEW_FRAME::onSelectNextSymbol( wxCommandEvent& aEvent )
92 {
93  wxCommandEvent evt( wxEVT_COMMAND_LISTBOX_SELECTED, ID_LIBVIEW_CMP_LIST );
94  int ii = m_cmpList->GetSelection();
95 
96  // Select the next symbol or stop at the end of the list.
97  if( ii != wxNOT_FOUND || ii != (int)m_cmpList->GetCount() - 1 )
98  ii += 1;
99 
100  m_cmpList->SetSelection( ii );
101  ProcessEvent( evt );
102 }
103 
104 
105 void LIB_VIEW_FRAME::onSelectPreviousSymbol( wxCommandEvent& aEvent )
106 {
107  wxCommandEvent evt( wxEVT_COMMAND_LISTBOX_SELECTED, ID_LIBVIEW_CMP_LIST );
108  int ii = m_cmpList->GetSelection();
109 
110  // Select the previous symbol or stop at the beginning of list.
111  if( ii != wxNOT_FOUND && ii != 0 )
112  ii -= 1;
113 
114  m_cmpList->SetSelection( ii );
115  ProcessEvent( evt );
116 }
117 
118 
119 void LIB_VIEW_FRAME::onViewSymbolDocument( wxCommandEvent& aEvent )
120 {
122  LIB_ALIAS* entry = Prj().SchSymbolLibTable()->LoadSymbol( id );
123 
124  if( entry && !entry->GetDocFileName().IsEmpty() )
125  {
126  SEARCH_STACK* lib_search = Prj().SchSearchS();
127 
128  GetAssociatedDocument( this, entry->GetDocFileName(), lib_search );
129  }
130 }
131 
132 
133 void LIB_VIEW_FRAME::onSelectSymbolBodyStyle( wxCommandEvent& aEvent )
134 {
135  int id = aEvent.GetId();
136 
137  switch( id )
138  {
139  default:
141  m_convert = 1;
142  break;
143 
145  m_convert = 2;
146  break;
147  }
148 
149  m_canvas->Refresh();
150 }
151 
152 
153 void LIB_VIEW_FRAME::onSelectSymbolUnit( wxCommandEvent& aEvent )
154 {
155  int ii = m_selpartBox->GetCurrentSelection();
156 
157  if( ii < 0 )
158  return;
159 
160  m_unit = ii + 1;
161  m_canvas->Refresh();
162 }
163 
164 
165 void LIB_VIEW_FRAME::OnLeftClick( wxDC* DC, const wxPoint& MousePos )
166 {
167 }
168 
169 
170 bool LIB_VIEW_FRAME::OnRightClick( const wxPoint& MousePos, wxMenu* PopMenu )
171 {
172  return true;
173 }
174 
175 
177 {
178  if( m_libList && !m_libList->IsEmpty() && !m_libraryName.IsEmpty() )
179  {
180  const SYMBOL_LIB_TABLE_ROW* row = Prj().SchSymbolLibTable()->FindRow( m_libraryName );
181 
182  wxString title = wxString::Format( _( "Symbol Library Browser -- %s" ),
183  row ? row->GetFullURI() : _( "no library selected" ) );
184  SetTitle( title );
185  }
186 }
187 
188 
189 void LIB_VIEW_FRAME::RedrawActiveWindow( wxDC* DC, bool EraseBg )
190 {
192  LIB_ALIAS* entry = nullptr;
193 
194  try
195  {
196  entry = Prj().SchSymbolLibTable()->LoadSymbol( id );
197  }
198  catch( const IO_ERROR& ) {} // ignore, it is handled below
199 
200  if( !entry )
201  return;
202 
203  LIB_PART* part = entry->GetPart();
204 
205  if( !part )
206  return;
207 
208  wxString msg;
209  wxString tmp;
210 
211  m_canvas->DrawBackGround( DC );
212 
213  if( !entry->IsRoot() )
214  {
215  // Temporarily change the name field text to reflect the alias name.
216  msg = entry->GetName();
217  tmp = part->GetName();
218 
219  part->SetName( msg );
220 
221  if( m_unit < 1 )
222  m_unit = 1;
223 
224  if( m_convert < 1 )
225  m_convert = 1;
226  }
227  else
228  msg = _( "None" );
229 
230  auto opts = PART_DRAW_OPTIONS::Default();
231  opts.show_elec_type = GetShowElectricalType();
232  part->Draw( m_canvas, DC, wxPoint( 0, 0 ), m_unit, m_convert, opts );
233 
234  // Redraw the cursor
235  m_canvas->DrawCrossHair( DC );
236 
237  if( !tmp.IsEmpty() )
238  part->SetName( tmp );
239 
240  ClearMsgPanel();
241  AppendMsgPanel( _( "Name" ), part->GetName(), BLUE, 6 );
242  AppendMsgPanel( _( "Alias" ), msg, RED, 6 );
243  AppendMsgPanel( _( "Description" ), entry->GetDescription(), CYAN, 6 );
244  AppendMsgPanel( _( "Key words" ), entry->GetKeyWords(), DARKDARKGRAY );
245 }
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:338
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:170
void onSelectPreviousSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:105
void DrawCrossHair(wxDC *aDC, COLOR4D aColor=COLOR4D::WHITE)
Function DrawCrossHair draws the user cross hair.
Definition: draw_panel.cpp:221
void OnLeftClick(wxDC *DC, const wxPoint &MousePos) override
Definition: viewlibs.cpp:165
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, const wxPathList *aPaths)
Function GetAssociatedDocument open a document (file) with the suitable browser.
Definition: eda_doc.cpp:87
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:102
bool IsRoot() const
void OnSelectSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:44
void onViewSymbolDocument(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:119
int ShowQuasiModal()
wxString GetDescription() const
void RedrawActiveWindow(wxDC *DC, bool EraseBg) override
Definition: viewlibs.cpp:189
void onSelectSymbolBodyStyle(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:133
Definition: colors.h:59
Define a library symbol object.
Helper dialog and control classes.
Definition: colors.h:60
static PART_DRAW_OPTIONS Default()
void DisplayLibInfos()
Definition: viewlibs.cpp:176
LIB_PART * GetPart() const
Get the shared LIB_PART.
void onSelectNextSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:91
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:98
void AppendMsgPanel(const wxString &textUpper, const wxString &textLower, COLOR4D color, int pad=6)
Append a message to the message panel.
Definition: draw_frame.cpp:812
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)
const wxString & GetName() const
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:694
bool GetShowElectricalType()
void onSelectSymbolUnit(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:153
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:823
bool ProcessEvent(wxEvent &aEvent) override
Function ProcessEvent overrides the default process event handler to implement the auto save feature...
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.