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_ALIAS *current = getSelectedAlias();
62  LIB_ID id;
63  int unit = 0;
64 
65  if( current )
66  {
67  id = current->GetLibId();
68  adapter->SetPreselectNode( id, unit );
69  }
70 
71  wxString dialogTitle;
72  dialogTitle.Printf( _( "Choose Symbol (%d items loaded)" ), adapter->GetItemCount() );
73 
74  DIALOG_CHOOSE_COMPONENT dlg( this, dialogTitle, adapterPtr, m_convert, false, false, false );
75 
76  if( dlg.ShowQuasiModal() == wxID_CANCEL )
77  return;
78 
79  id = dlg.GetSelectedLibId( &unit );
80 
81  if( !id.IsValid() )
82  return;
83 
84  SetSelectedLibrary( id.GetLibNickname() );
85  SetSelectedComponent( id.GetLibItemName() );
86  SetUnitAndConvert( unit, 1 );
87 }
88 
89 
90 void LIB_VIEW_FRAME::onSelectNextSymbol( wxCommandEvent& aEvent )
91 {
92  wxCommandEvent evt( wxEVT_COMMAND_LISTBOX_SELECTED, ID_LIBVIEW_CMP_LIST );
93  int ii = m_cmpList->GetSelection();
94 
95  // Select the next symbol or stop at the end of the list.
96  if( ii != wxNOT_FOUND || ii != (int)m_cmpList->GetCount() - 1 )
97  ii += 1;
98 
99  m_cmpList->SetSelection( ii );
100  ProcessEvent( evt );
101 }
102 
103 
104 void LIB_VIEW_FRAME::onSelectPreviousSymbol( wxCommandEvent& aEvent )
105 {
106  wxCommandEvent evt( wxEVT_COMMAND_LISTBOX_SELECTED, ID_LIBVIEW_CMP_LIST );
107  int ii = m_cmpList->GetSelection();
108 
109  // Select the previous symbol or stop at the beginning of list.
110  if( ii != wxNOT_FOUND && ii != 0 )
111  ii -= 1;
112 
113  m_cmpList->SetSelection( ii );
114  ProcessEvent( evt );
115 }
116 
117 
118 void LIB_VIEW_FRAME::onUpdateDocButton( wxUpdateUIEvent& aEvent )
119 {
120  LIB_ALIAS* entry = getSelectedAlias();
121 
122  aEvent.Enable( entry && !entry->GetDocFileName().IsEmpty() );
123 }
124 
125 
126 void LIB_VIEW_FRAME::onViewSymbolDocument( wxCommandEvent& aEvent )
127 {
128  LIB_ALIAS* entry = getSelectedAlias();
129 
130  if( entry && !entry->GetDocFileName().IsEmpty() )
131  {
132  SEARCH_STACK* lib_search = Prj().SchSearchS();
133 
134  GetAssociatedDocument( this, entry->GetDocFileName(), lib_search );
135  }
136 }
137 
138 
139 void LIB_VIEW_FRAME::onSelectSymbolBodyStyle( wxCommandEvent& aEvent )
140 {
141  int id = aEvent.GetId();
142 
143  switch( id )
144  {
145  default:
147  m_convert = 1;
148  break;
149 
151  m_convert = 2;
152  break;
153  }
154 
156 }
157 
158 
159 void LIB_VIEW_FRAME::onSelectSymbolUnit( wxCommandEvent& aEvent )
160 {
161  int ii = m_unitChoice->GetSelection();
162 
163  if( ii < 0 )
164  return;
165 
166  m_unit = ii + 1;
167 
169 }
170 
171 
172 void LIB_VIEW_FRAME::OnLeftClick( wxDC* DC, const wxPoint& MousePos )
173 {
174 }
175 
176 
177 bool LIB_VIEW_FRAME::OnRightClick( const wxPoint& MousePos, wxMenu* PopMenu )
178 {
179  return true;
180 }
181 
182 
184 {
185  if( m_libList && !m_libList->IsEmpty() && !m_libraryName.IsEmpty() )
186  {
187  const SYMBOL_LIB_TABLE_ROW* row = Prj().SchSymbolLibTable()->FindRow( m_libraryName );
188 
189  wxString title = wxString::Format( _( "Symbol Library Browser -- %s" ),
190  row ? row->GetFullURI() : _( "no library selected" ) );
191  SetTitle( title );
192  }
193 }
194 
195 
196 void LIB_VIEW_FRAME::RedrawActiveWindow( wxDC* DC, bool EraseBg )
197 {
198  DisplayLibInfos();
199  UpdateStatusBar();
200 }
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.
LIB_ID GetLibId() const override
This file is part of the common library.
bool OnRightClick(const wxPoint &MousePos, wxMenu *PopMenu) override
Definition: viewlibs.cpp:177
void onSelectPreviousSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:104
void OnLeftClick(wxDC *DC, const wxPoint &MousePos) override
Definition: viewlibs.cpp:172
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 & GetDocFileName() const
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.
void OnSelectSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:45
void onViewSymbolDocument(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:126
int ShowQuasiModal()
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
void RedrawActiveWindow(wxDC *DC, bool EraseBg) override
Definition: viewlibs.cpp:196
void onSelectSymbolBodyStyle(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:139
Helper dialog and control classes.
void DisplayLibInfos()
Definition: viewlibs.cpp:183
void UpdateStatusBar() override
Update the status bar information.
void onSelectNextSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:90
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
wxChoice * m_unitChoice
void onSelectSymbolUnit(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:159
Definition for part library class.
void onUpdateDocButton(wxUpdateUIEvent &aEvent)
Definition: viewlibs.cpp:118
bool ProcessEvent(wxEvent &aEvent) override
Function ProcessEvent overrides the default process event handler to implement the auto save feature.
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
To be called after this dialog returns from ShowModal().
LIB_ALIAS * getSelectedAlias() const
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.