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-2019 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 
25 #include <fctsys.h>
26 #include <kiway.h>
27 #include <pgm_base.h>
28 #include <sch_draw_panel.h>
29 #include <sch_view.h>
30 #include <confirm.h>
31 #include <eda_doc.h>
32 
33 #include <viewlib_frame.h>
34 #include <eeschema_id.h>
35 #include <class_library.h>
36 #include <dialog_helpers.h>
39 #include <symbol_lib_table.h>
40 
41 void LIB_VIEW_FRAME::OnSelectSymbol( wxCommandEvent& aEvent )
42 {
43  std::unique_lock<std::mutex> dialogLock( DIALOG_CHOOSE_COMPONENT::g_Mutex, std::defer_lock );
44 
45  // One CHOOSE_COMPONENT dialog at a time. User probaby can't handle more anyway.
46  if( !dialogLock.try_lock() )
47  return;
48 
49  // Container doing search-as-you-type.
50  SYMBOL_LIB_TABLE* libs = Prj().SchSymbolLibTable();
51  auto adapterPtr( SYMBOL_TREE_MODEL_ADAPTER::Create( libs ) );
52  auto adapter = static_cast<SYMBOL_TREE_MODEL_ADAPTER*>( adapterPtr.get() );
53 
54  const auto libNicknames = libs->GetLogicalLibs();
55  adapter->AddLibraries( libNicknames, this );
56 
57  LIB_ALIAS *current = GetSelectedAlias();
58  LIB_ID id;
59  int unit = 0;
60 
61  if( current )
62  {
63  id = current->GetLibId();
64  adapter->SetPreselectNode( id, unit );
65  }
66 
67  wxString dialogTitle;
68  dialogTitle.Printf( _( "Choose Symbol (%d items loaded)" ), adapter->GetItemCount() );
69 
70  DIALOG_CHOOSE_COMPONENT dlg( this, dialogTitle, adapterPtr, m_convert, false, false, false );
71 
72  if( dlg.ShowQuasiModal() == wxID_CANCEL )
73  return;
74 
75  id = dlg.GetSelectedLibId( &unit );
76 
77  if( !id.IsValid() )
78  return;
79 
80  SetSelectedLibrary( id.GetLibNickname() );
81  SetSelectedComponent( id.GetLibItemName() );
82  SetUnitAndConvert( unit, 1 );
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::onSelectSymbolUnit( wxCommandEvent& aEvent )
115 {
116  int ii = m_unitChoice->GetSelection();
117 
118  if( ii < 0 )
119  return;
120 
121  m_unit = ii + 1;
122 
124 }
125 
126 
128 {
129  if( m_libList && !m_libList->IsEmpty() && !m_libraryName.IsEmpty() )
130  {
131  const SYMBOL_LIB_TABLE_ROW* row = Prj().SchSymbolLibTable()->FindRow( m_libraryName );
132 
133  wxString title = wxString::Format( _( "Symbol Library Browser -- %s" ),
134  row ? row->GetFullURI() : _( "no library selected" ) );
135  SetTitle( title );
136  }
137 }
138 
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.
void onSelectPreviousSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:100
wxListBox * m_cmpList
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.
void OnSelectSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:41
int ShowQuasiModal()
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
Helper dialog and control classes.
#define _(s)
void DisplayLibInfos()
Definition: viewlibs.cpp:127
void onSelectNextSymbol(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:86
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
LIB_ALIAS * GetSelectedAlias() const
wxListBox * m_libList
wxChoice * m_unitChoice
void onSelectSymbolUnit(wxCommandEvent &aEvent)
Definition: viewlibs.cpp:114
Definition for part library class.
bool ProcessEvent(wxEvent &aEvent) override
Override 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().
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.