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 #include <class_sch_screen.h>
37 
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 
46 
47 void LIB_VIEW_FRAME::OnSelectSymbol( wxCommandEvent& aEvent )
48 {
49  wxString dialogTitle;
50  PART_LIBS* libs = Prj().SchLibs();
51 
52  // Container doing search-as-you-type.
53  auto adapter( CMP_TREE_MODEL_ADAPTER::Create( libs ) );
54 
55  for( PART_LIB& lib : *libs )
56  {
57  adapter->AddLibrary( lib );
58  }
59 
60  dialogTitle.Printf( _( "Choose Component (%d items loaded)" ),
61  adapter->GetComponentsCount() );
62  DIALOG_CHOOSE_COMPONENT dlg( this, dialogTitle, adapter, m_convert, false );
63 
64  if( dlg.ShowQuasiModal() == wxID_CANCEL )
65  return;
66 
69  LIB_ALIAS* const alias = dlg.GetSelectedAlias( &m_unit );
70 
71  if( !alias || !alias->GetLib() )
72  return;
73 
74  if( m_libraryName == alias->GetLib()->GetName() )
75  {
76  if( m_entryName != alias->GetName() )
77  SetSelectedComponent( alias->GetName() );
78  }
79  else
80  {
81  m_entryName = alias->GetName();
82  SetSelectedLibrary( alias->GetLib()->GetName() );
83  }
84 }
85 
86 
87 void LIB_VIEW_FRAME::onSelectNextSymbol( wxCommandEvent& aEvent )
88 {
89  wxCommandEvent evt( wxEVT_COMMAND_LISTBOX_SELECTED, ID_LIBVIEW_CMP_LIST );
90  int ii = m_cmpList->GetSelection();
91 
92  // Select the next symbol or stop at the end of the list.
93  if( ii != wxNOT_FOUND || ii != (int)m_cmpList->GetCount() - 1 )
94  ii += 1;
95 
96  m_cmpList->SetSelection( ii );
97  ProcessEvent( evt );
98 }
99 
100 
101 void LIB_VIEW_FRAME::onSelectPreviousSymbol( wxCommandEvent& aEvent )
102 {
103  wxCommandEvent evt( wxEVT_COMMAND_LISTBOX_SELECTED, ID_LIBVIEW_CMP_LIST );
104  int ii = m_cmpList->GetSelection();
105 
106  // Select the previous symbol or stop at the beginning of list.
107  if( ii != wxNOT_FOUND && ii != 0 )
108  ii -= 1;
109 
110  m_cmpList->SetSelection( ii );
111  ProcessEvent( evt );
112 }
113 
114 
115 void LIB_VIEW_FRAME::onViewSymbolDocument( wxCommandEvent& aEvent )
116 {
117  LIB_ID id( wxEmptyString, m_entryName );
118  LIB_ALIAS* entry = Prj().SchLibs()->FindLibraryAlias( id, m_libraryName );
119 
120  if( entry && !entry->GetDocFileName().IsEmpty() )
121  {
122  SEARCH_STACK* lib_search = Prj().SchSearchS();
123 
124  GetAssociatedDocument( this, entry->GetDocFileName(), lib_search );
125  }
126 }
127 
128 
129 void LIB_VIEW_FRAME::onSelectSymbolBodyStyle( wxCommandEvent& aEvent )
130 {
131  int id = aEvent.GetId();
132 
133  switch( id )
134  {
135  default:
137  m_convert = 1;
138  break;
139 
141  m_convert = 2;
142  break;
143  }
144 
145  m_canvas->Refresh();
146 }
147 
148 
149 void LIB_VIEW_FRAME::onSelectSymbolUnit( wxCommandEvent& aEvent )
150 {
151  int ii = m_selpartBox->GetCurrentSelection();
152 
153  if( ii < 0 )
154  return;
155 
156  m_unit = ii + 1;
157  m_canvas->Refresh();
158 }
159 
160 
161 void LIB_VIEW_FRAME::OnLeftClick( wxDC* DC, const wxPoint& MousePos )
162 {
163 }
164 
165 
166 bool LIB_VIEW_FRAME::OnRightClick( const wxPoint& MousePos, wxMenu* PopMenu )
167 {
168  return true;
169 }
170 
171 
173 {
174  PART_LIBS* libs = Prj().SchLibs();
175 
176  if( libs )
177  {
178  PART_LIB* lib = libs->FindLibrary( m_libraryName );
179 
180  wxString title = wxString::Format( L"Library Browser \u2014 %s",
181  lib ? lib->GetFullFileName() : "no library selected" );
182  SetTitle( title );
183  }
184 }
185 
186 
187 void LIB_VIEW_FRAME::RedrawActiveWindow( wxDC* DC, bool EraseBg )
188 {
189  LIB_ID id( wxEmptyString, m_entryName );
190  LIB_ALIAS* entry = Prj().SchLibs()->FindLibraryAlias( id, m_libraryName );
191 
192  if( !entry )
193  return;
194 
195  LIB_PART* part = entry->GetPart();
196 
197  if( !part )
198  return;
199 
200  wxString msg;
201  wxString tmp;
202 
203  m_canvas->DrawBackGround( DC );
204 
205  if( !entry->IsRoot() )
206  {
207  // Temporarily change the name field text to reflect the alias name.
208  msg = entry->GetName();
209  tmp = part->GetName();
210 
211  part->SetName( msg );
212 
213  if( m_unit < 1 )
214  m_unit = 1;
215 
216  if( m_convert < 1 )
217  m_convert = 1;
218  }
219  else
220  msg = _( "None" );
221 
222  auto opts = PART_DRAW_OPTIONS::Default();
223  opts.show_elec_type = GetShowElectricalType();
224  part->Draw( m_canvas, DC, wxPoint( 0, 0 ), m_unit, m_convert, opts );
225 
226  // Redraw the cursor
227  m_canvas->DrawCrossHair( DC );
228 
229  if( !tmp.IsEmpty() )
230  part->SetName( tmp );
231 
232  ClearMsgPanel();
233  AppendMsgPanel( _( "Part" ), part->GetName(), BLUE, 6 );
234  AppendMsgPanel( _( "Alias" ), msg, RED, 6 );
235  AppendMsgPanel( _( "Description" ), entry->GetDescription(), CYAN, 6 );
236  AppendMsgPanel( _( "Key words" ), entry->GetKeyWords(), DARKDARKGRAY );
237 }
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.
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Definition: draw_panel.cpp:326
This file is part of the common library.
wxString GetFullFileName()
Function GetFullFileName returns the full file library name with path and extension.
This file is part of the common library.
bool OnRightClick(const wxPoint &MousePos, wxMenu *PopMenu) override
Definition: viewlibs.cpp:166
void onSelectPreviousSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:101
void DrawCrossHair(wxDC *aDC, COLOR4D aColor=COLOR4D::WHITE)
Function DrawCrossHair draws the user cross hair.
Definition: draw_panel.cpp:216
void OnLeftClick(wxDC *DC, const wxPoint &MousePos) override
Definition: viewlibs.cpp:161
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
Class LIB_ID.
Definition: lib_id.h:56
PART_LIB * FindLibrary(const wxString &aName)
Function FindLibrary finds a part library by aName.
PART_LIB * GetLib()
void SetSelectedComponent(const wxString &aComponentName)
Set the selected component.
static PTR Create(PART_LIBS *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.
bool IsRoot() const
void OnSelectSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:47
void onViewSymbolDocument(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:115
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:129
Definition: colors.h:59
const wxString GetName() const
Return the file name without path or extension.
Class LIB_PART defines a library part object.
Helper dialog and control classes.
Definition: colors.h:60
LIB_ALIAS * GetSelectedAlias(int *aUnit) const
Function GetSelectedAlias To be called after this dialog returns from ShowModal().
static PART_DRAW_OPTIONS Default()
void DisplayLibInfos()
Definition: viewlibs.cpp:172
LIB_PART * GetPart() const
Function GetPart gets the shared LIB_PART.
Class PART_LIBS is a collection of PART_LIBs.
void onSelectNextSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:87
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:92
void AppendMsgPanel(const wxString &textUpper, const wxString &textLower, COLOR4D color, int pad=6)
Append a message to the message panel.
Definition: draw_frame.cpp:731
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
virtual void SetName(const wxString &aName)
void DrawBackGround(wxDC *DC)
Function DrawBackGround.
Definition: draw_panel.cpp:666
bool GetShowElectricalType()
void onSelectSymbolUnit(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:149
Definition for part library class.
Class PART_LIB is used to load, save, search, and otherwise manipulate part library files...
wxString GetKeyWords() const
void ClearMsgPanel(void)
Clear all messages from the message panel.
Definition: draw_frame.cpp:742
bool ProcessEvent(wxEvent &aEvent) override
Function ProcessEvent overrides the default process event handler to implement the auto save feature...
Definition: basicframe.cpp:166
wxComboBox * m_selpartBox
void SetSelectedLibrary(const wxString &aLibName)
Set the selected library in the library window.