KiCad PCB EDA Suite
hierarch.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) 2004 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
6  * Copyright (C) 2004-2020 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <fctsys.h>
27 #include <sch_draw_panel.h>
28 #include <confirm.h>
29 #include <id.h>
30 #include <bitmaps.h>
31 #include <dialog_shim.h>
32 #include <sch_edit_frame.h>
33 #include <general.h>
34 #include <sch_sheet.h>
35 #include <sch_sheet_path.h>
36 #include <schematic.h>
37 #include <wx/imaglist.h>
38 #include <wx/treectrl.h>
39 #include <tool/tool_manager.h>
40 #include <tools/ee_actions.h>
42 //#include <netlist_object.h>
43 #include <sch_sheet_path.h>
44 
45 #include <hierarch.h>
46 #include <view/view.h>
47 #include <kiface_i.h>
48 #include "eeschema_settings.h"
49 
51 
52 
56 class TreeItemData : public wxTreeItemData
57 {
58 public:
60 
61  TreeItemData( SCH_SHEET_PATH& sheet ) : wxTreeItemData()
62  {
63  m_SheetPath = sheet;
64  }
65 };
66 
68  wxTreeCtrl( (wxWindow*) parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
69  wxTR_HAS_BUTTONS, wxDefaultValidator, wxT( "HierachyTreeCtrl" ) )
70 {
71  m_parent = parent;
72 
73  // Make an image list containing small icons
74  // All icons are expected having the same size.
75  wxBitmap tree_nosel_bm( KiBitmap( tree_nosel_xpm ) );
76  imageList = new wxImageList( tree_nosel_bm.GetWidth(), tree_nosel_bm.GetHeight(), true, 2 );
77 
78  imageList->Add( tree_nosel_bm );
79  imageList->Add( KiBitmap( tree_sel_xpm ) );
80 
81  AssignImageList( imageList );
82 }
83 
85  DIALOG_SHIM( aParent, wxID_ANY, _( "Navigator" ), wxDefaultPosition, wxDefaultSize,
86  wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER, HIERARCHY_NAVIG_DLG_WNAME )
87 {
88  wxASSERT( dynamic_cast< SCH_EDIT_FRAME* >( aParent ) );
89 
90  m_SchFrameEditor = aParent;
91  m_currSheet = aParent->GetCurrentSheet();
92  m_Tree = new HIERARCHY_TREE( this );
93  m_nbsheets = 1;
94 
95  // root is the link to the main sheet.
96  wxTreeItemId root = m_Tree->AddRoot( _( "Root" ), 0, 1 );
97  m_Tree->SetItemBold( root, true );
98 
100  m_Tree->SetItemData( root, new TreeItemData( m_list ) );
101 
103  m_Tree->SelectItem( root );
104 
105  buildHierarchyTree( &m_list, &root );
106 
107  m_Tree->ExpandAll();
108 
109  // This bloc gives a good size to the dialog, better than the default "best" size,
110  // the first time the dialog is opened, during a session
111  wxRect itemrect;
112  wxSize tree_size;
113 
114  m_Tree->GetBoundingRect( root, itemrect );
115 
116  // Set dialog window size to be large enough
117  tree_size.x = itemrect.GetWidth() + 20;
118  tree_size.x = std::max( tree_size.x, 250 );
119 
120  // Readjust the size of the frame to an optimal value.
121  tree_size.y = m_nbsheets * itemrect.GetHeight();
122 
123  if( m_nbsheets < 2 )
124  tree_size.y += 10; // gives a better look for small trees
125 
126  SetClientSize( tree_size );
127 
128  // manage the ESC key to close the dialog, because thre is no Cancel button
129  // in dialog
130  m_Tree->Connect( wxEVT_CHAR, wxKeyEventHandler( HIERARCHY_TREE::onChar ) );
131 
132  // Manage double click on a selection, or the enter key:
133  Bind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
134  // Manage a simple click on a selection, if the selection changes
135  Bind( wxEVT_TREE_SEL_CHANGED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
136 
137  // Connect close event for the dialog:
138  this->Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( HIERARCHY_NAVIG_DLG::OnCloseNav ) );
139 }
140 
141 
143 {
144  Unbind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
145  Unbind( wxEVT_TREE_SEL_CHANGED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
146  m_Tree->Disconnect( wxEVT_CHAR, wxKeyEventHandler( HIERARCHY_TREE::onChar ) );
147  this->Disconnect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( HIERARCHY_NAVIG_DLG::OnCloseNav ) );
148 }
149 
150 
151 void HIERARCHY_TREE::onChar( wxKeyEvent& event )
152 {
153  if( event.GetKeyCode() == WXK_ESCAPE )
154  m_parent->Close( true );
155  else
156  event.Skip();
157 }
158 
159 
160 void HIERARCHY_NAVIG_DLG::buildHierarchyTree( SCH_SHEET_PATH* aList, wxTreeItemId* aPreviousmenu )
161 {
162  wxCHECK_RET( m_nbsheets < NB_MAX_SHEET, "Maximum number of sheets exceeded." );
163 
164  std::vector<SCH_ITEM*> sheetChildren;
165  aList->LastScreen()->GetSheets( &sheetChildren );
166 
167  for( SCH_ITEM* aItem : sheetChildren )
168  {
169  SCH_SHEET* sheet = static_cast<SCH_SHEET*>( aItem );
170  wxString sheetName = sheet->GetFields()[ SHEETNAME ].GetShownText();
171  m_nbsheets++;
172  wxTreeItemId menu;
173  menu = m_Tree->AppendItem( *aPreviousmenu, sheetName, 0, 1 );
174  aList->push_back( sheet );
175  m_Tree->SetItemData( menu, new TreeItemData( *aList ) );
176 
177  if( *aList == m_currSheet )
178  {
179  m_Tree->EnsureVisible( menu );
180  m_Tree->SelectItem( menu );
181  }
182 
183  buildHierarchyTree( aList, &menu );
184  aList->pop_back();
185 
186  if( m_nbsheets >= NB_MAX_SHEET )
187  break;
188  }
189 }
190 
192 {
193  Freeze();
194 
196  wxTreeItemId root = m_Tree->GetRootItem();
197  m_Tree->DeleteChildren( root );
198  m_list.clear();
200  buildHierarchyTree( &m_list, &root );
201 
202  Thaw();
203 }
204 
205 void HIERARCHY_NAVIG_DLG::onSelectSheetPath( wxTreeEvent& event )
206 {
209 
210  wxTreeItemId itemSel = m_Tree->GetSelection();
211  TreeItemData* itemData = static_cast<TreeItemData*>( m_Tree->GetItemData( itemSel ) );
212 
215 
216  EESCHEMA_SETTINGS* appSettings = static_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
217 
218  if( !appSettings->m_Appearance.navigator_stays_open )
219  Close( true );
220 }
221 
222 
223 void HIERARCHY_NAVIG_DLG::OnCloseNav( wxCloseEvent& event )
224 {
225  Destroy();
226 }
227 
228 
230 {
233 
234  SCH_SCREEN* screen = GetCurrentSheet().LastScreen();
235 
236  wxASSERT( screen );
237 
238  SetScreen( screen );
239 
240  // update the References
243 
244  if( !screen->m_Initialized )
245  {
247  screen->m_Initialized = true;
248  screen->ClearUndoORRedoList( screen->m_UndoList, 1 );
249  }
250  else
251  {
252  // RedrawScreen() will set zoom to last used
253  RedrawScreen( (wxPoint) GetScreen()->m_ScrollCenter, false );
254  }
255 
256  UpdateTitle();
257 
260  editTool->UpdateNetHighlighting( dummy );
261 
262  HardRedraw(); // Ensure any item has its view updated, especially the worksheet items
263 }
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
void onSelectSheetPath(wxTreeEvent &event)
Open the selected sheet and display the corresponding screen when a tree item is selected.
Definition: hierarch.cpp:205
#define NB_MAX_SHEET
Max number of sheets in a hierarchy project.
Definition: sch_screen.h:78
virtual void RedrawScreen(const wxPoint &aCenterPoint, bool aWarpPointer)
wxImageList * imageList
Definition: hierarch.h:49
SCH_SHEET * Last() const
Function Last returns a pointer to the last sheet of the list One can see the others sheet as the "pa...
This file is part of the common library.
void SetScreen(BASE_SCREEN *aScreen) override
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
static TOOL_ACTION cancelInteractive
Definition: actions.h:65
Store an SCH_SHEET_PATH of each sheet in hierarchy.
Definition: hierarch.cpp:56
static TOOL_ACTION zoomFitScreen
Definition: actions.h:93
void GetSheets(std::vector< SCH_ITEM * > *aItems)
Similar to GetItems().OfType( SCH_SHEET_T ), but return the sheets in a deterministic order (L-R,...
Definition: sch_screen.cpp:952
void UpdateTitle()
Set the main window title bar text.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:140
HIERARCHY_TREE * m_Tree
Definition: hierarch.h:64
Schematic editor (Eeschema) main window.
Handle hierarchy tree control.
Definition: hierarch.h:45
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:83
void DisplayCurrentSheet()
Draw the current sheet on the display.
Definition: hierarch.cpp:229
void UpdateAllScreenReferences()
Function UpdateAllScreenReferences updates the reference and the m_Multi parameter (part selection) f...
SCH_EDIT_FRAME * m_SchFrameEditor
Definition: hierarch.h:63
TreeItemData(SCH_SHEET_PATH &sheet)
Definition: hierarch.cpp:61
void pop_back()
Forwarded method from std::vector.
SCH_SHEET_PATH m_currSheet
Definition: hierarch.h:61
std::vector< SCH_FIELD > & GetFields()
Definition: sch_sheet.h:268
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
SCH_EDITOR_CONTROL.
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:80
void UpdateHierarchyTree()
Update the hierarchical tree of the schematic.
Definition: hierarch.cpp:191
void HardRedraw() override
Rebuild the GAL and redraw the screen.
UNDO_REDO_CONTAINER m_UndoList
Objects list for the undo command (old data)
Definition: base_screen.h:82
bool m_Initialized
Definition: base_screen.h:79
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
TOOL_EVENT.
Definition: tool_event.h:171
void onChar(wxKeyEvent &event)
Definition: hierarch.cpp:151
SCHEMATIC & Schematic() const
HIERARCHY_NAVIG_DLG(SCH_EDIT_FRAME *aParent)
Definition: hierarch.cpp:84
#define HIERARCHY_NAVIG_DLG_WNAME
Definition: hierarch.h:35
const BITMAP_OPAQUE tree_sel_xpm[1]
Definition: tree_sel.cpp:57
static TOOL_ACTION clearSelection
Clears the current selection.
Definition: ee_actions.h:56
SCH_SHEET_PATH.
int UpdateNetHighlighting(const TOOL_EVENT &aEvent)
Updates net highlighting after an edit
void clear()
Forwarded method from std::vector.
HIERARCHY_TREE(HIERARCHY_NAVIG_DLG *parent)
Definition: hierarch.cpp:67
void buildHierarchyTree(SCH_SHEET_PATH *aList, wxTreeItemId *aPreviousmenu)
Create the hierarchical tree of the schematic.
Definition: hierarch.cpp:160
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:216
SCH_SHEET_PATH m_list
Definition: hierarch.h:62
SCH_SHEET & Root() const
Definition: schematic.h:97
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:48
SCH_SCREEN * LastScreen()
Function LastScreen.
const BITMAP_OPAQUE tree_nosel_xpm[1]
Definition: tree_nosel.cpp:56
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
#define _(s)
Definition: 3d_actions.cpp:33
HIERARCHY_NAVIG_DLG * m_parent
Definition: hierarch.h:48
void OnCloseNav(wxCloseEvent &event)
Definition: hierarch.cpp:223
SCH_SHEET_PATH & GetCurrentSheet() const
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:74
virtual void ClearUndoORRedoList(UNDO_REDO_CONTAINER &aList, int aItemCount=-1) override
Free the undo or redo list from aList element.
Definition: sch_screen.cpp:804
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
SCH_SHEET_PATH m_SheetPath
Definition: hierarch.cpp:59
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:186