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 <wx/imaglist.h>
37 #include <wx/treectrl.h>
38 #include <tool/tool_manager.h>
39 #include <tools/ee_actions.h>
41 //#include <netlist_object.h>
42 #include <sch_sheet_path.h>
43 
44 #include <hierarch.h>
45 #include <view/view.h>
46 
48 
49 
53 class TreeItemData : public wxTreeItemData
54 {
55 public:
57 
58  TreeItemData( SCH_SHEET_PATH& sheet ) : wxTreeItemData()
59  {
60  m_SheetPath = sheet;
61  }
62 };
63 
65  wxTreeCtrl( (wxWindow*) parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
66  wxTR_HAS_BUTTONS, wxDefaultValidator, wxT( "HierachyTreeCtrl" ) )
67 {
68  m_parent = parent;
69 
70  // Make an image list containing small icons
71  // All icons are expected having the same size.
72  wxBitmap tree_nosel_bm( KiBitmap( tree_nosel_xpm ) );
73  imageList = new wxImageList( tree_nosel_bm.GetWidth(), tree_nosel_bm.GetHeight(), true, 2 );
74 
75  imageList->Add( tree_nosel_bm );
76  imageList->Add( KiBitmap( tree_sel_xpm ) );
77 
78  AssignImageList( imageList );
79 }
80 
82  DIALOG_SHIM( aParent, wxID_ANY, _( "Navigator" ), wxDefaultPosition, wxDefaultSize,
83  wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER, HIERARCHY_NAVIG_DLG_WNAME )
84 {
85  wxASSERT( dynamic_cast< SCH_EDIT_FRAME* >( aParent ) );
86 
87  m_SchFrameEditor = aParent;
88  m_currSheet = aParent->GetCurrentSheet();
89  m_Tree = new HIERARCHY_TREE( this );
90  m_nbsheets = 1;
91 
92  // root is the link to the main sheet.
93  wxTreeItemId root = m_Tree->AddRoot( _( "Root" ), 0, 1 );
94  m_Tree->SetItemBold( root, true );
95 
97  m_Tree->SetItemData( root, new TreeItemData( m_list ) );
98 
100  m_Tree->SelectItem( root );
101 
102  buildHierarchyTree( &m_list, &root );
103 
104  m_Tree->ExpandAll();
105 
106  // This bloc gives a good size to the dialog, better than the default "best" size,
107  // the first time the dialog is opened, during a session
108  wxRect itemrect;
109  wxSize tree_size;
110 
111  m_Tree->GetBoundingRect( root, itemrect );
112 
113  // Set dialog window size to be large enough
114  tree_size.x = itemrect.GetWidth() + 20;
115  tree_size.x = std::max( tree_size.x, 250 );
116 
117  // Readjust the size of the frame to an optimal value.
118  tree_size.y = m_nbsheets * itemrect.GetHeight();
119 
120  if( m_nbsheets < 2 )
121  tree_size.y += 10; // gives a better look for small trees
122 
123  SetClientSize( tree_size );
124 
125  // manage the ESC key to close the dialog, because thre is no Cancel button
126  // in dialog
127  m_Tree->Connect( wxEVT_CHAR, wxKeyEventHandler( HIERARCHY_TREE::onChar ) );
128 
129  // Manage double click on a selection, or the enter key:
130  Bind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
131  // Manage a simple click on a selection, if the selection changes
132  Bind( wxEVT_TREE_SEL_CHANGED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
133 
134  // Connect close event for the dialog:
135  this->Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( HIERARCHY_NAVIG_DLG::OnCloseNav ) );
136 }
137 
138 
140 {
141  Unbind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
142  Unbind( wxEVT_TREE_SEL_CHANGED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
143  m_Tree->Disconnect( wxEVT_CHAR, wxKeyEventHandler( HIERARCHY_TREE::onChar ) );
144  this->Disconnect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( HIERARCHY_NAVIG_DLG::OnCloseNav ) );
145 }
146 
147 
148 void HIERARCHY_TREE::onChar( wxKeyEvent& event )
149 {
150  if( event.GetKeyCode() == WXK_ESCAPE )
151  m_parent->Close( true );
152  else
153  event.Skip();
154 }
155 
156 
157 void HIERARCHY_NAVIG_DLG::buildHierarchyTree( SCH_SHEET_PATH* aList, wxTreeItemId* aPreviousmenu )
158 {
159  wxCHECK_RET( m_nbsheets < NB_MAX_SHEET, "Maximum number of sheets exceeded." );
160 
161  for( auto aItem : aList->LastScreen()->Items().OfType( SCH_SHEET_T ) )
162  {
163  SCH_SHEET* sheet = static_cast<SCH_SHEET*>( aItem );
164  m_nbsheets++;
165  wxTreeItemId menu;
166  menu = m_Tree->AppendItem( *aPreviousmenu, sheet->GetName(), 0, 1 );
167  aList->push_back( sheet );
168  m_Tree->SetItemData( menu, new TreeItemData( *aList ) );
169 
170  if( *aList == m_currSheet )
171  {
172  m_Tree->EnsureVisible( menu );
173  m_Tree->SelectItem( menu );
174  }
175 
176  buildHierarchyTree( aList, &menu );
177  aList->pop_back();
178 
179  if( m_nbsheets >= NB_MAX_SHEET )
180  break;
181  }
182 }
183 
185 {
186  Freeze();
187 
189  wxTreeItemId root = m_Tree->GetRootItem();
190  m_Tree->DeleteChildren( root );
191  m_list.clear();
193  buildHierarchyTree( &m_list, &root );
194 
195  Thaw();
196 }
197 
198 void HIERARCHY_NAVIG_DLG::onSelectSheetPath( wxTreeEvent& event )
199 {
202 
203  wxTreeItemId ItemSel = m_Tree->GetSelection();
204  m_SchFrameEditor->SetCurrentSheet(( (TreeItemData*) m_Tree->GetItemData( ItemSel ) )->m_SheetPath );
206 
207  if( m_SchFrameEditor->GetNavigatorStaysOpen() == false )
208  Close( true );
209 }
210 
211 
212 void HIERARCHY_NAVIG_DLG::OnCloseNav( wxCloseEvent& event )
213 {
214  Destroy();
215 }
216 
217 
219 {
222 
223  SCH_SCREEN* screen = g_CurrentSheet->LastScreen();
224 
225  wxASSERT( screen );
226 
227  // Switch to current sheet,
228  // and update the grid size, because it can be modified in latest screen
229  SetScreen( screen );
231 
232  // update the References
235 
236  if( !screen->m_Initialized )
237  {
239  screen->m_Initialized = true;
240  screen->ClearUndoORRedoList( screen->m_UndoList, 1 );
241  }
242  else
243  {
244  // RedrawScreen() will set zoom to last used
245  RedrawScreen( (wxPoint) GetScreen()->m_ScrollCenter, false );
246  }
247 
248  UpdateTitle();
249 
252  editTool->UpdateNetHighlighting( dummy );
253 
254  HardRedraw(); // Ensure any item has its view updated, especially the worksheet items
255 }
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:198
#define NB_MAX_SHEET
Max number of sheets in a hierarchy project.
Definition: sch_screen.h:70
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
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.
EE_TYPE OfType(KICAD_T aType)
Definition: sch_rtree.h:219
static TOOL_ACTION cancelInteractive
Definition: actions.h:65
Store an SCH_SHEET_PATH of each sheet in hierarchy.
Definition: hierarch.cpp:53
static TOOL_ACTION zoomFitScreen
Definition: actions.h:93
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:139
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:84
void DisplayCurrentSheet()
Draw the current sheet on the display.
Definition: hierarch.cpp:218
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:58
void pop_back()
Forwarded method from std::vector.
SCH_SHEET_PATH m_currSheet
Definition: hierarch.h:61
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
wxString GetName() const
Definition: sch_sheet.h:280
SCH_SHEET_PATH * g_CurrentSheet
With the new connectivity algorithm, many more places than before want to know what the current sheet...
void UpdateHierarchyTree()
Update the hierarchical tree of the schematic.
Definition: hierarch.cpp:184
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:132
bool m_Initialized
Definition: base_screen.h:129
bool GetNavigatorStaysOpen() const
TOOL_EVENT.
Definition: tool_event.h:171
SCH_SHEET_PATH & GetCurrentSheet()
void onChar(wxKeyEvent &event)
Definition: hierarch.cpp:148
HIERARCHY_NAVIG_DLG(SCH_EDIT_FRAME *aParent)
Definition: hierarch.cpp:81
#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:58
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:64
void buildHierarchyTree(SCH_SHEET_PATH *aList, wxTreeItemId *aPreviousmenu)
Create the hierarchical tree of the schematic.
Definition: hierarch.cpp:157
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:215
SCH_SHEET_PATH m_list
Definition: hierarch.h:62
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
EE_RTREE & Items()
Definition: sch_screen.h:127
HIERARCHY_NAVIG_DLG * m_parent
Definition: hierarch.h:48
void OnCloseNav(wxCloseEvent &event)
Definition: hierarch.cpp:212
TOOL_MANAGER * m_toolManager
SCH_SHEET * g_RootSheet
virtual void ClearUndoORRedoList(UNDO_REDO_CONTAINER &aList, int aItemCount=-1) override
Free the undo or redo list from aList element.
Definition: sch_screen.cpp:627
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
int SetGrid(const wxRealPoint &size)
set the current grid size m_Grid.
SCH_SHEET_PATH m_SheetPath
Definition: hierarch.cpp:56