KiCad PCB EDA Suite
footprint_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) 2018 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * https://www.gnu.org/licenses/gpl-3.0.html
19  * or you may search the http://www.gnu.org website for the version 3 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include "footprint_tree_pane.h"
26 
27 #include <widgets/lib_tree.h>
28 #include <pcbnew_id.h>
29 #include <footprint_edit_frame.h>
30 #include <fp_lib_table.h>
31 #include <menus_helpers.h>
32 
33 
35  : wxPanel( aParent ),
36  m_frame( aParent ),
37  m_tree( nullptr )
38 {
39  // Create widgets
40  wxBoxSizer* boxSizer = new wxBoxSizer( wxVERTICAL );
42  boxSizer->Add( m_tree, 1, wxEXPAND, 5 );
43 
44  SetSizer( boxSizer ); // should remove the previous sizer according to wxWidgets docs
45  Layout();
46  boxSizer->Fit( this );
47 
48  // Setup right click-context menus
49  std::unique_ptr<wxMenu> menuLibrary = std::make_unique<wxMenu>();
50 
51  AddMenuItem( menuLibrary.get(), ID_MODEDIT_CREATE_NEW_LIB, _( "&New Library..." ),
52  KiBitmap( new_library_xpm ) );
53 
54  AddMenuItem( menuLibrary.get(), ID_MODEDIT_ADD_LIBRARY, _( "&Add Library..." ),
55  KiBitmap( add_library_xpm ) );
56 
57  AddMenuItem( menuLibrary.get(), ID_MODEDIT_SAVE, _( "&Save" ),
58  KiBitmap( save_xpm ) );
59  AddMenuItem( menuLibrary.get(), ID_MODEDIT_SAVE_AS, _( "Save a Copy &As..." ),
60  KiBitmap( save_as_xpm ) );
61 
62  menuLibrary->AppendSeparator();
63  AddMenuItem( menuLibrary.get(), ID_MODEDIT_NEW_MODULE, _( "&New Footprint..." ),
64  KiBitmap( new_component_xpm ) );
65 #ifdef KICAD_SCRIPTING
66  AddMenuItem( menuLibrary.get(), ID_MODEDIT_NEW_MODULE_FROM_WIZARD, _( "&Create Footprint from Wizard..." ),
67  KiBitmap( new_component_xpm ) );
68 #endif
69  AddMenuItem( menuLibrary.get(), ID_MODEDIT_IMPORT_PART, _( "&Import Footprint..." ),
70  KiBitmap( import_part_xpm ) );
71  AddMenuItem( menuLibrary.get(), ID_MODEDIT_PASTE_PART, _( "Paste Footprint" ),
72  KiBitmap( paste_xpm ) );
73 
74  std::unique_ptr<wxMenu> menuPart = std::make_unique<wxMenu>();
75  AddMenuItem( menuPart.get(), ID_MODEDIT_EDIT_MODULE, _( "&Edit Footprint" ),
76  KiBitmap( edit_xpm ) );
77 
78  menuPart->AppendSeparator();
79  AddMenuItem( menuPart.get(), ID_MODEDIT_SAVE, _( "&Save" ),
80  KiBitmap( save_xpm ) );
81  AddMenuItem( menuPart.get(), ID_MODEDIT_SAVE_AS, _( "Save &As..." ),
82  KiBitmap( save_xpm ) );
83  AddMenuItem( menuPart.get(), ID_MODEDIT_DELETE_PART, _( "&Delete" ),
84  KiBitmap( delete_xpm ) );
85  AddMenuItem( menuPart.get(), ID_MODEDIT_REVERT_PART, _( "Revert" ),
86  KiBitmap( undo_xpm ) );
87 
88  menuPart->AppendSeparator();
89  AddMenuItem( menuPart.get(), ID_MODEDIT_CUT_PART, _( "Cut" ),
90  KiBitmap( cut_xpm ) );
91  AddMenuItem( menuPart.get(), ID_MODEDIT_COPY_PART, _( "Copy" ),
92  KiBitmap( copy_xpm ) );
93 
94  menuPart->AppendSeparator();
95  AddMenuItem( menuPart.get(), ID_MODEDIT_EXPORT_PART, _( "E&xport Footprint..." ),
96  KiBitmap( export_part_xpm ) );
97 
98  // Menu displayed when nothing is selected
99  std::unique_ptr<wxMenu> menuNoSelection = std::make_unique<wxMenu>();
100  AddMenuItem( menuNoSelection.get(), ID_MODEDIT_CREATE_NEW_LIB, _( "&New Library..." ),
101  KiBitmap( new_library_xpm ) );
102 
103  AddMenuItem( menuNoSelection.get(), ID_MODEDIT_ADD_LIBRARY, _( "&Add Library..." ),
104  KiBitmap( add_library_xpm ) );
105 
106  m_tree->SetMenu( LIB_TREE_NODE::LIBID, std::move( menuPart ) );
107  m_tree->SetMenu( LIB_TREE_NODE::LIB, std::move( menuLibrary ) );
108  m_tree->SetMenu( LIB_TREE_NODE::INVALID, std::move( menuNoSelection ) );
109 
110  // Event handlers
111  Bind( COMPONENT_SELECTED, &FOOTPRINT_TREE_PANE::onComponentSelected, this );
112  m_tree->Bind( wxEVT_UPDATE_UI, &FOOTPRINT_TREE_PANE::onUpdateUI, this );
113 }
114 
115 
117 {
118  m_tree->Destroy();
119 }
120 
121 
123 {
124  if( m_tree )
125  m_tree->Regenerate();
126 }
127 
128 
129 void FOOTPRINT_TREE_PANE::onComponentSelected( wxCommandEvent& aEvent )
130 {
131  wxCommandEvent evt( wxEVT_COMMAND_TOOL_CLICKED, ID_MODEDIT_EDIT_MODULE );
133 
134  // Make sure current-part highlighting doesn't get lost in seleciton highlighting
135  m_tree->Unselect();
136 }
137 
138 
139 void FOOTPRINT_TREE_PANE::onUpdateUI( wxUpdateUIEvent& aEvent )
140 {
141  if( m_frame->GetGalCanvas()->HasFocus() )
142  {
143  // Don't allow a selected item in the tree when the canvas has focus: it's too easy
144  // to confuse the selected-highlighting with the being-edited-on-canvas-highlighting.
145  m_tree->Unselect();
146  }
147 }
LIB_TREE_MODEL_ADAPTER::PTR & GetLibTreeAdapter()
Returns the adapter object that provides the stored data.
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:223
Definition of class FOOTPRINT_EDIT_FRAME.
FP_LIB_TABLE GFootprintTable
The global footprint library table.
Definition: pcbnew.cpp:333
FOOTPRINT_EDIT_FRAME * m_frame
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
void Regenerate()
Updates the footprint tree
void SetMenu(LIB_TREE_NODE::TYPE aType, std::unique_ptr< wxMenu > aMenu)
Associates a right click context menu for a specific node type.
Definition: lib_tree.h:97
void onComponentSelected(wxCommandEvent &aEvent)
void Unselect()
Unselect currently selected item in wxDataViewCtrl.
Definition: lib_tree.cpp:174
void Process_Special_Functions(wxCommandEvent &event)
void Regenerate()
Regenerates the tree.
Definition: lib_tree.cpp:185
LIB_TREE * m_tree
component search tree widget
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:921
void onUpdateUI(wxUpdateUIEvent &aEvent)
FOOTPRINT_TREE_PANE(FOOTPRINT_EDIT_FRAME *aParent)
Widget displaying a tree of components with optional search text control and description panel...
Definition: lib_tree.h:42