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 <sch_draw_panel.h>
33 #include <sch_view.h>
34 #include <confirm.h>
35 #include <eda_doc.h>
36 
37 #include <viewlib_frame.h>
38 #include <eeschema_id.h>
39 #include <class_library.h>
40 #include <dialog_helpers.h>
43 #include <symbol_lib_table.h>
44 
45 void LIB_VIEW_FRAME::OnSelectSymbol( wxCommandEvent& aEvent )
46 {
47  std::unique_lock<std::mutex> dialogLock( DIALOG_CHOOSE_COMPONENT::g_Mutex, std::defer_lock );
48 
49  // One CHOOSE_COMPONENT dialog at a time. User probaby can't handle more anyway.
50  if( !dialogLock.try_lock() )
51  return;
52 
53  // Container doing search-as-you-type.
54  SYMBOL_LIB_TABLE* libs = Prj().SchSymbolLibTable();
55  auto adapterPtr( SYMBOL_TREE_MODEL_ADAPTER::Create( libs ) );
56  auto adapter = static_cast<SYMBOL_TREE_MODEL_ADAPTER*>( adapterPtr.get() );
57 
58  const auto libNicknames = libs->GetLogicalLibs();
59  adapter->AddLibraries( libNicknames, this );
60 
61  LIB_ID id;
62  int unit;
63 
64  wxString dialogTitle;
65  dialogTitle.Printf( _( "Choose Symbol (%d items loaded)" ), adapter->GetItemCount() );
66 
67  DIALOG_CHOOSE_COMPONENT dlg( this, dialogTitle, adapterPtr, m_convert, false, false, false );
68 
69  if( dlg.ShowQuasiModal() == wxID_CANCEL )
70  return;
71 
72  id = dlg.GetSelectedLibId( &unit );
73 
74  if( !id.IsValid() )
75  return;
76 
77  SetSelectedLibrary( id.GetLibNickname() );
78  SetSelectedComponent( id.GetLibItemName() );
79  SetUnitAndConvert( unit, 1 );
80 }
81 
82 
83 void LIB_VIEW_FRAME::onSelectNextSymbol( wxCommandEvent& aEvent )
84 {
85  wxCommandEvent evt( wxEVT_COMMAND_LISTBOX_SELECTED, ID_LIBVIEW_CMP_LIST );
86  int ii = m_cmpList->GetSelection();
87 
88  // Select the next symbol or stop at the end of the list.
89  if( ii != wxNOT_FOUND || ii != (int)m_cmpList->GetCount() - 1 )
90  ii += 1;
91 
92  m_cmpList->SetSelection( ii );
93  ProcessEvent( evt );
94 }
95 
96 
97 void LIB_VIEW_FRAME::onSelectPreviousSymbol( wxCommandEvent& aEvent )
98 {
99  wxCommandEvent evt( wxEVT_COMMAND_LISTBOX_SELECTED, ID_LIBVIEW_CMP_LIST );
100  int ii = m_cmpList->GetSelection();
101 
102  // Select the previous symbol or stop at the beginning of list.
103  if( ii != wxNOT_FOUND && ii != 0 )
104  ii -= 1;
105 
106  m_cmpList->SetSelection( ii );
107  ProcessEvent( evt );
108 }
109 
110 
111 void LIB_VIEW_FRAME::onUpdateDocButton( wxUpdateUIEvent& aEvent )
112 {
113  LIB_ALIAS* entry = getSelectedAlias();
114 
115  aEvent.Enable( entry && !entry->GetDocFileName().IsEmpty() );
116 }
117 
118 
119 void LIB_VIEW_FRAME::onViewSymbolDocument( wxCommandEvent& aEvent )
120 {
121  LIB_ALIAS* entry = getSelectedAlias();
122 
123  if( entry && !entry->GetDocFileName().IsEmpty() )
124  {
125  SEARCH_STACK* lib_search = Prj().SchSearchS();
126 
127  GetAssociatedDocument( this, entry->GetDocFileName(), lib_search );
128  }
129 }
130 
131 
132 void LIB_VIEW_FRAME::onSelectSymbolBodyStyle( wxCommandEvent& aEvent )
133 {
134  int id = aEvent.GetId();
135 
136  switch( id )
137  {
138  default:
140  m_convert = 1;
141  break;
142 
144  m_convert = 2;
145  break;
146  }
147 
149 }
150 
151 
152 void LIB_VIEW_FRAME::onSelectSymbolUnit( wxCommandEvent& aEvent )
153 {
154  int ii = m_unitChoice->GetSelection();
155 
156  if( ii < 0 )
157  return;
158 
159  m_unit = ii + 1;
160 
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 {
191  DisplayLibInfos();
192  UpdateStatusBar();
193 }
static int m_unit
static int m_convert
Part library alias object definition.
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
void SetUnitAndConvert(int aUnit, int aConvert)
Set unit and convert, and set flag preventing them from automatically resetting to 1...
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:97
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...
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 OnSelectSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:45
void onViewSymbolDocument(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:119
int ShowQuasiModal()
void RedrawActiveWindow(wxDC *DC, bool EraseBg) override
Definition: viewlibs.cpp:189
void onSelectSymbolBodyStyle(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:132
Helper dialog and control classes.
void DisplayLibInfos()
Definition: viewlibs.cpp:176
void UpdateStatusBar() override
Update the status bar information.
const wxString & GetDocFileName() const
void onSelectNextSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:83
void updatePreviewSymbol()
static PTR Create(LIB_TABLE *aLibs)
Factory function: create a model adapter in a reference-counting container.
see class PGM_BASE
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
LIB_ALIAS * getSelectedAlias() const
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
To be called after this dialog returns from ShowModal().
wxChoice * m_unitChoice
void onSelectSymbolUnit(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:152
Definition for part library class.
void onUpdateDocButton(wxUpdateUIEvent &aEvent)
Definition: viewlibs.cpp:111
bool ProcessEvent(wxEvent &aEvent) override
Function ProcessEvent overrides the default process event handler to implement the auto save feature...
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.