KiCad PCB EDA Suite
cmp_tree_pane.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) 2017 CERN
5  * @author Maciej Suminski <maciej.suminski@cern.ch>
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 3
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  * https://www.gnu.org/licenses/gpl-3.0.html
20  * or you may search the http://www.gnu.org website for the version 3 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 "cmp_tree_pane.h"
26 
27 #include <component_tree.h>
28 #include <eeschema_id.h>
29 #include <lib_manager.h>
30 #include <lib_edit_frame.h>
31 #include <symbol_lib_table.h>
32 #include <menus_helpers.h>
33 
34 
36  : wxPanel( aParent ),
37  m_libEditFrame( aParent ), m_tree( nullptr ), m_libMgr( aLibMgr )
38 {
39  // Create widgets
40  wxBoxSizer* boxSizer = new wxBoxSizer( wxVERTICAL );
43  boxSizer->Add( m_tree, 1, wxEXPAND | wxALL, 5 );
44 
45  SetSizer( boxSizer ); // should remove the previous sizer according to wxWidgets docs
46  Layout();
47  boxSizer->Fit( this );
48 
49  // Setup right click-context menus
50  std::unique_ptr<wxMenu> menuLibrary = std::make_unique<wxMenu>();
51 
52  AddMenuItem( menuLibrary.get(), ID_LIBEDIT_NEW_LIBRARY, _( "&New Library..." ),
53  KiBitmap( new_library_xpm ) );
54  AddMenuItem( menuLibrary.get(), ID_LIBEDIT_ADD_LIBRARY, _( "&Add Library..." ),
55  KiBitmap( add_library_xpm ) );
56  AddMenuItem( menuLibrary.get(), ID_LIBEDIT_SAVE_LIBRARY, _( "&Save Library" ),
57  KiBitmap( save_library_xpm ) );
58  AddMenuItem( menuLibrary.get(), ID_LIBEDIT_SAVE_LIBRARY_AS, _( "Save Library As..." ),
59  KiBitmap( save_library_xpm ) );
60  AddMenuItem( menuLibrary.get(), ID_LIBEDIT_REVERT_LIBRARY, _( "Revert Library" ),
61  KiBitmap( undo_xpm ) );
62 
63  menuLibrary->AppendSeparator();
64  AddMenuItem( menuLibrary.get(), ID_LIBEDIT_NEW_PART, _( "New Sy&mbol..." ),
65  KiBitmap( new_component_xpm ) );
66  AddMenuItem( menuLibrary.get(), ID_LIBEDIT_IMPORT_PART, _( "&Import Symbol..." ),
67  KiBitmap( import_part_xpm ) );
68  AddMenuItem( menuLibrary.get(), ID_LIBEDIT_PASTE_PART, _( "Paste Symbol" ),
69  KiBitmap( paste_xpm ) );
70 
71  std::unique_ptr<wxMenu> menuPart = std::make_unique<wxMenu>();
72  AddMenuItem( menuPart.get(), ID_LIBEDIT_EDIT_PART, _( "&Edit Symbol" ),
73  KiBitmap( edit_xpm ) );
74  AddMenuItem( menuPart.get(), ID_LIBEDIT_REMOVE_PART, _( "Remove Symbol" ),
75  KiBitmap( delete_xpm ) );
76  AddMenuItem( menuPart.get(), ID_LIBEDIT_EXPORT_PART, _( "E&xport Symbol..." ),
77  KiBitmap( export_part_xpm ) );
78  AddMenuItem( menuPart.get(), ID_LIBEDIT_SAVE_PART, _( "&Save Symbol" ),
79  KiBitmap( save_part_xpm ) );
80  AddMenuItem( menuPart.get(), ID_LIBEDIT_REVERT_PART, _( "Revert Symbol" ),
81  KiBitmap( undo_xpm ) );
82 
83  menuPart->AppendSeparator();
84  AddMenuItem( menuPart.get(), ID_LIBEDIT_CUT_PART, _( "Cut Symbol" ),
85  KiBitmap( cut_xpm ) );
86  AddMenuItem( menuPart.get(), ID_LIBEDIT_COPY_PART, _( "Copy Symbol" ),
87  KiBitmap( copy_xpm ) );
88  AddMenuItem( menuPart.get(), ID_LIBEDIT_DUPLICATE_PART, _( "Duplicate Symbol" ),
89  KiBitmap( duplicate_xpm ) );
90 
91  // Menu displayed when nothing is selected
92  std::unique_ptr<wxMenu> menuNoSelection = std::make_unique<wxMenu>();
93  AddMenuItem( menuNoSelection.get(), ID_LIBEDIT_NEW_LIBRARY, _( "&New Library..." ),
94  KiBitmap( new_library_xpm ) );
95  AddMenuItem( menuNoSelection.get(), ID_LIBEDIT_ADD_LIBRARY, _( "&Add Library..." ),
96  KiBitmap( add_library_xpm ) );
97 
98  m_tree->SetMenu( CMP_TREE_NODE::LIBID, std::move( menuPart ) );
99  m_tree->SetMenu( CMP_TREE_NODE::LIB, std::move( menuLibrary ) );
100  m_tree->SetMenu( CMP_TREE_NODE::INVALID, std::move( menuNoSelection ) );
101 
102  // Event handlers
103  Bind( COMPONENT_SELECTED, &CMP_TREE_PANE::onComponentSelected, this );
104 }
105 
106 
108 {
109  m_tree->Destroy();
110 }
111 
112 
114 {
115  if( m_tree )
116  m_tree->Regenerate();
117 }
118 
119 
120 void CMP_TREE_PANE::onComponentSelected( wxCommandEvent& aEvent )
121 {
122  // Repost the event
123  wxCommandEvent evt( ID_LIBEDIT_EDIT_PART );
124  // I cannot figure out why the two methods below do not work..
125  //wxPostEvent( libEditFrame, evt );
126  //wxQueueEvent( m_libEditFrame, new wxCommandEvent( ID_LIBEDIT_EDIT_PART ) );
127  m_libEditFrame->OnEditPart( evt );
128 
129  // Make sure current-part highlighting doesn't get lost in seleciton highlighting
130  m_tree->Unselect();
131 }
static SYMBOL_LIB_TABLE & GetGlobalLibTable()
CMP_TREE_MODEL_ADAPTER_BASE::PTR & GetAdapter()
Returns the adapter object that provides the stored data.
Definition: lib_manager.h:235
wxMenuItem * AddMenuItem(wxMenu *aMenu, int aId, const wxString &aText, const wxBitmap &aImage, wxItemKind aType=wxITEM_NORMAL)
Function AddMenuItem is an inline helper function to create and insert a menu item with an icon into ...
Definition: bitmap.cpp:175
void Regenerate()
Regenerates the tree.
LIB_EDIT_FRAME * m_libEditFrame
Definition: cmp_tree_pane.h:57
LIB_MANAGER * m_libMgr
Definition: cmp_tree_pane.h:59
COMPONENT_TREE * m_tree
component search tree widget
Definition: cmp_tree_pane.h:58
void onComponentSelected(wxCommandEvent &aEvent)
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Function KiBitmap constructs a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:78
CMP_TREE_PANE(LIB_EDIT_FRAME *aParent, LIB_MANAGER *aLibMgr)
Widget displaying a tree of components with optional search text control and description panel...
void Unselect()
Unselect currently selected item in wxDataViewCtrl.
Class to handle modifications to the symbol libraries.
Definition: lib_manager.h:49
The symbol library editor main window.
void OnEditPart(wxCommandEvent &aEvent)
Opens the selected part for editing.
Definition: libedit.cpp:358
void Regenerate()
Updates the component tree
void SetMenu(CMP_TREE_NODE::TYPE aType, std::unique_ptr< wxMenu > aMenu)
Associates a right click context menu for a specific node type.
Definition of class LIB_EDIT_FRAME.