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  wxString dialogTitle;
47  SYMBOL_LIB_TABLE* libs = Prj().SchSymbolLibTable();
48 
49  // Container doing search-as-you-type.
50  auto adapter( CMP_TREE_MODEL_ADAPTER::Create( libs ) );
51 
52  const auto libNicknames = libs->GetLogicalLibs();
53 
54  adapter->AddLibrariesWithProgress( libNicknames, this );
55 
56  dialogTitle.Printf( _( "Choose Symbol (%d items loaded)" ),
57  adapter->GetComponentsCount() );
58  DIALOG_CHOOSE_COMPONENT dlg( this, dialogTitle, adapter, m_convert, false, false );
59 
60  if( dlg.ShowQuasiModal() == wxID_CANCEL )
61  return;
62 
65  LIB_ID id = dlg.GetSelectedLibId( &m_unit );
66 
67  if( !id.IsValid() || id.GetLibNickname().empty() )
68  return;
69 
70  if( m_libraryName == id.GetLibNickname() )
71  {
72  if( m_entryName != id.GetLibItemName() )
73  SetSelectedComponent( id.GetLibItemName() );
74  }
75  else
76  {
77  m_entryName = id.GetLibItemName();
78  SetSelectedLibrary( id.GetLibNickname() );
79  SetSelectedComponent( id.GetLibItemName() );
80  }
81 
82  Zoom_Automatique( false );
83 }
84 
85 
86 void LIB_VIEW_FRAME::onSelectNextSymbol( wxCommandEvent& aEvent )
87 {
88  wxCommandEvent evt( wxEVT_COMMAND_LISTBOX_SELECTED, ID_LIBVIEW_CMP_LIST );
89  int ii = m_cmpList->GetSelection();
90 
91  // Select the next symbol or stop at the end of the list.
92  if( ii != wxNOT_FOUND || ii != (int)m_cmpList->GetCount() - 1 )
93  ii += 1;
94 
95  m_cmpList->SetSelection( ii );
96  ProcessEvent( evt );
97 }
98 
99 
100 void LIB_VIEW_FRAME::onSelectPreviousSymbol( wxCommandEvent& aEvent )
101 {
102  wxCommandEvent evt( wxEVT_COMMAND_LISTBOX_SELECTED, ID_LIBVIEW_CMP_LIST );
103  int ii = m_cmpList->GetSelection();
104 
105  // Select the previous symbol or stop at the beginning of list.
106  if( ii != wxNOT_FOUND && ii != 0 )
107  ii -= 1;
108 
109  m_cmpList->SetSelection( ii );
110  ProcessEvent( evt );
111 }
112 
113 
114 void LIB_VIEW_FRAME::onViewSymbolDocument( wxCommandEvent& aEvent )
115 {
117  LIB_ALIAS* entry = Prj().SchSymbolLibTable()->LoadSymbol( id );
118 
119  if( entry && !entry->GetDocFileName().IsEmpty() )
120  {
121  SEARCH_STACK* lib_search = Prj().SchSearchS();
122 
123  GetAssociatedDocument( this, entry->GetDocFileName(), lib_search );
124  }
125 }
126 
127 
128 void LIB_VIEW_FRAME::onSelectSymbolBodyStyle( wxCommandEvent& aEvent )
129 {
130  int id = aEvent.GetId();
131 
132  switch( id )
133  {
134  default:
136  m_convert = 1;
137  break;
138 
140  m_convert = 2;
141  break;
142  }
143 
144  m_canvas->Refresh();
145 }
146 
147 
148 void LIB_VIEW_FRAME::onSelectSymbolUnit( wxCommandEvent& aEvent )
149 {
150  int ii = m_selpartBox->GetCurrentSelection();
151 
152  if( ii < 0 )
153  return;
154 
155  m_unit = ii + 1;
156  m_canvas->Refresh();
157 }
158 
159 
160 void LIB_VIEW_FRAME::OnLeftClick( wxDC* DC, const wxPoint& MousePos )
161 {
162 }
163 
164 
165 bool LIB_VIEW_FRAME::OnRightClick( const wxPoint& MousePos, wxMenu* PopMenu )
166 {
167  return true;
168 }
169 
170 
172 {
173  if( m_libList && !m_libList->IsEmpty() && !m_libraryName.IsEmpty() )
174  {
175  const SYMBOL_LIB_TABLE_ROW* row = Prj().SchSymbolLibTable()->FindRow( m_libraryName );
176 
177  wxString title = wxString::Format( _( "Symbol Library Browser -- %s" ),
178  row ? row->GetFullURI() : _( "no library selected" ) );
179  SetTitle( title );
180  }
181 }
182 
183 
184 void LIB_VIEW_FRAME::RedrawActiveWindow( wxDC* DC, bool EraseBg )
185 {
187  LIB_ALIAS* entry = nullptr;
188 
189  try
190  {
191  entry = Prj().SchSymbolLibTable()->LoadSymbol( id );
192  }
193  catch( const IO_ERROR& e ) {} // ignore, it is handled below
194 
195  if( !entry )
196  return;
197 
198  LIB_PART* part = entry->GetPart();
199 
200  if( !part )
201  return;
202 
203  wxString msg;
204  wxString tmp;
205 
206  m_canvas->DrawBackGround( DC );
207 
208  if( !entry->IsRoot() )
209  {
210  // Temporarily change the name field text to reflect the alias name.
211  msg = entry->GetName();
212  tmp = part->GetName();
213 
214  part->SetName( msg );
215 
216  if( m_unit < 1 )
217  m_unit = 1;
218 
219  if( m_convert < 1 )
220  m_convert = 1;
221  }
222  else
223  msg = _( "None" );
224 
225  auto opts = PART_DRAW_OPTIONS::Default();
226  opts.show_elec_type = GetShowElectricalType();
227  part->Draw( m_canvas, DC, wxPoint( 0, 0 ), m_unit, m_convert, opts );
228 
229  // Redraw the cursor
230  m_canvas->DrawCrossHair( DC );
231 
232  if( !tmp.IsEmpty() )
233  part->SetName( tmp );
234 
235  ClearMsgPanel();
236  AppendMsgPanel( _( "Name" ), part->GetName(), BLUE, 6 );
237  AppendMsgPanel( _( "Alias" ), msg, RED, 6 );
238  AppendMsgPanel( _( "Description" ), entry->GetDescription(), CYAN, 6 );
239  AppendMsgPanel( _( "Key words" ), entry->GetKeyWords(), DARKDARKGRAY );
240 }
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:339
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:165
void onSelectPreviousSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:100
void DrawCrossHair(wxDC *aDC, COLOR4D aColor=COLOR4D::WHITE)
Function DrawCrossHair draws the user cross hair.
Definition: draw_panel.cpp:222
void OnLeftClick(wxDC *DC, const wxPoint &MousePos) override
Definition: viewlibs.cpp:160
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:102
bool IsRoot() const
void OnSelectSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:44
void onViewSymbolDocument(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:114
int ShowQuasiModal()
wxString GetDescription() const
void RedrawActiveWindow(wxDC *DC, bool EraseBg) override
Definition: viewlibs.cpp:184
void onSelectSymbolBodyStyle(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:128
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:171
LIB_PART * GetPart() const
Get the shared LIB_PART.
void onSelectNextSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:86
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:770
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:695
bool GetShowElectricalType()
void onSelectSymbolUnit(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:148
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:781
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.