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-2019 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 <view/view.h>
45 
47 
48 
52 class TreeItemData : public wxTreeItemData
53 {
54 public:
56 
57  TreeItemData( SCH_SHEET_PATH& sheet ) : wxTreeItemData()
58  {
59  m_SheetPath = sheet;
60  }
61 };
62 
63 
67 class HIERARCHY_TREE : public wxTreeCtrl
68 {
69 private:
71  wxImageList* imageList;
72 
73 public:
75 
76  // Closes the dialog on escape key
77  void onChar( wxKeyEvent& event );
78 };
79 
80 
82  wxTreeCtrl( (wxWindow*) parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
83  wxTR_HAS_BUTTONS, wxDefaultValidator, wxT( "HierachyTreeCtrl" ) )
84 {
85  m_parent = parent;
86 
87  // Make an image list containing small icons
88  // All icons are expected having the same size.
89  wxBitmap tree_nosel_bm( KiBitmap( tree_nosel_xpm ) );
90  imageList = new wxImageList( tree_nosel_bm.GetWidth(), tree_nosel_bm.GetHeight(), true, 2 );
91 
92  imageList->Add( tree_nosel_bm );
93  imageList->Add( KiBitmap( tree_sel_xpm ) );
94 
95  AssignImageList( imageList );
96 }
97 
98 
100 {
101 public:
105 
106 private:
107  SCH_SHEET_PATH m_currSheet; // The currently opened scheet in hierarchy
108 
109 public:
111 
113 
114 private:
122  void buildHierarchyTree( SCH_SHEET_PATH* aList, wxTreeItemId* aPreviousmenu );
123 
128  void onSelectSheetPath( wxTreeEvent& event );
129 };
130 
131 
133 {
134  HIERARCHY_NAVIG_DLG* treeframe = new HIERARCHY_NAVIG_DLG( m_frame );
135 
136  treeframe->ShowQuasiModal();
137  treeframe->Destroy();
138 
139  return 0;
140 }
141 
142 
144  DIALOG_SHIM( aParent, wxID_ANY, _( "Navigator" ), wxDefaultPosition, wxDefaultSize,
145  wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
146 {
147  wxASSERT( dynamic_cast< SCH_EDIT_FRAME* >( aParent ) );
148 
149  m_SchFrameEditor = aParent;
150  m_currSheet = aParent->GetCurrentSheet();
151  m_Tree = new HIERARCHY_TREE( this );
152  m_nbsheets = 1;
153 
154  // root is the link to the main sheet.
155  wxTreeItemId root = m_Tree->AddRoot( _( "Root" ), 0, 1 );
156  m_Tree->SetItemBold( root, true );
157 
158  SCH_SHEET_PATH list;
159  list.push_back( g_RootSheet );
160  m_Tree->SetItemData( root, new TreeItemData( list ) );
161 
163  m_Tree->SelectItem( root );
164 
165  buildHierarchyTree( &list, &root );
166 
167  m_Tree->ExpandAll();
168 
169  // This bloc gives a good size to the dialog, better than the default "best" size,
170  // the first time the dialog is opened, during a session
171  wxRect itemrect;
172  wxSize tree_size;
173 
174  m_Tree->GetBoundingRect( root, itemrect );
175 
176  // Set dialog window size to be large enough
177  tree_size.x = itemrect.GetWidth() + 20;
178  tree_size.x = std::max( tree_size.x, 250 );
179 
180  // Readjust the size of the frame to an optimal value.
181  tree_size.y = m_nbsheets * itemrect.GetHeight();
182 
183  if( m_nbsheets < 2 )
184  tree_size.y += 10; // gives a better look for small trees
185 
186  SetClientSize( tree_size );
187 
188  // manage the ESC key to close the dialog, because thre is no Cancel button
189  // in dialog
190  m_Tree->Connect( wxEVT_CHAR, wxKeyEventHandler( HIERARCHY_TREE::onChar ) );
191 
192  // Manage double click on a selection, or the enter key:
193  Bind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
194  // Manage a simple click on a selection, if the selection changes
195  Bind( wxEVT_TREE_SEL_CHANGED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
196 }
197 
198 
200 {
201  Unbind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
202  Unbind( wxEVT_TREE_SEL_CHANGED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
203  m_Tree->Disconnect( wxEVT_CHAR, wxKeyEventHandler( HIERARCHY_TREE::onChar ) );
204 }
205 
206 
207 void HIERARCHY_TREE::onChar( wxKeyEvent& event )
208 {
209  if( event.GetKeyCode() == WXK_ESCAPE )
210  m_parent->Close( true );
211  else
212  event.Skip();
213 }
214 
215 
216 void HIERARCHY_NAVIG_DLG::buildHierarchyTree( SCH_SHEET_PATH* aList, wxTreeItemId* aPreviousmenu )
217 {
218  wxCHECK_RET( m_nbsheets < NB_MAX_SHEET, "Maximum number of sheets exceeded." );
219 
220  SCH_ITEM* schitem = aList->LastDrawList();
221 
222  while( schitem && m_nbsheets < NB_MAX_SHEET )
223  {
224  if( schitem->Type() == SCH_SHEET_T )
225  {
226  SCH_SHEET* sheet = (SCH_SHEET*) schitem;
227  m_nbsheets++;
228  wxTreeItemId menu;
229  menu = m_Tree->AppendItem( *aPreviousmenu, sheet->GetName(), 0, 1 );
230  aList->push_back( sheet );
231  m_Tree->SetItemData( menu, new TreeItemData( *aList ) );
232 
233  if( *aList == m_currSheet )
234  {
235  m_Tree->EnsureVisible( menu );
236  m_Tree->SelectItem( menu );
237  }
238 
239  buildHierarchyTree( aList, &menu );
240 
241  aList->pop_back();
242  }
243 
244  schitem = schitem->Next();
245  }
246 }
247 
248 
249 void HIERARCHY_NAVIG_DLG::onSelectSheetPath( wxTreeEvent& event )
250 {
251  wxTreeItemId ItemSel = m_Tree->GetSelection();
252  m_SchFrameEditor->SetCurrentSheet(( (TreeItemData*) m_Tree->GetItemData( ItemSel ) )->m_SheetPath );
254  Close( true );
255 }
256 
257 
259 {
262 
263  SCH_SCREEN* screen = g_CurrentSheet->LastScreen();
264 
265  // Switch to current sheet,
266  // and update the grid size, because it can be modified in latest screen
267  SetScreen( screen );
269 
270  // update the References
273  m_canvas->SetCanStartBlock( -1 );
274 
275  if( !screen->m_Initialized )
276  {
277  m_toolManager->RunAction( "common.Control.zoomFitScreen", true );
278  screen->m_Initialized = true;
279  screen->ClearUndoORRedoList( screen->m_UndoList, 1 );
280  }
281  else
282  {
284  // RedrawScreen() will set zoom to last used
286  }
287 
288  UpdateTitle();
289 
291  HardRedraw(); // Ensure any item has its view updated, especially the worksheet items
292 }
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
TOOL_MANAGER * m_toolManager
Definition: draw_frame.h:130
void onSelectSheetPath(wxTreeEvent &event)
Open the selected sheet and display the corresponding screen when a tree item is selected.
Definition: hierarch.cpp:249
#define NB_MAX_SHEET
Max number of sheets in a hierarchy project.
Definition: sch_screen.h:67
wxImageList * imageList
Definition: hierarch.cpp:71
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.
SCH_ITEM * Next() const
Definition: sch_item.h:153
virtual void SetScreen(BASE_SCREEN *aScreen) override
int NavigateHierarchy(const TOOL_EVENT &aEvent)
Definition: hierarch.cpp:132
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
static TOOL_ACTION cancelInteractive
Definition: actions.h:45
Store an SCH_SHEET_PATH of each sheet in hierarchy.
Definition: hierarch.cpp:52
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:125
void SetCanStartBlock(int aStartBlock)
HIERARCHY_TREE * m_Tree
Definition: hierarch.cpp:103
Schematic editor (Eeschema) main window.
Handle hierarchy tree control.
Definition: hierarch.cpp:67
Class DIALOG_SHIM may sit in the inheritance tree between wxDialog and any class written by wxFormBui...
Definition: dialog_shim.h:83
void DisplayCurrentSheet()
Draw the current sheet on the display.
Definition: hierarch.cpp:258
void UpdateAllScreenReferences()
Function UpdateAllScreenReferences updates the reference and the m_Multi parameter (part selection) f...
SCH_EDIT_FRAME * m_SchFrameEditor
Definition: hierarch.cpp:102
static TOOL_ACTION updateNetHighlighting
Definition: ee_actions.h:184
TreeItemData(SCH_SHEET_PATH &sheet)
Definition: hierarch.cpp:57
void pop_back()
Forwarded method from std::vector.
SCH_SHEET_PATH m_currSheet
Definition: hierarch.cpp:107
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
const wxPoint & GetScrollCenterPosition() const
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:56
wxString GetName() const
Definition: sch_sheet.h:259
SCH_SHEET_PATH * g_CurrentSheet
With the new connectivity algorithm, many more places than before want to know what the current sheet...
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:210
bool m_Initialized
Definition: base_screen.h:207
int ShowQuasiModal()
TOOL_MANAGER * GetToolManager() const override
Return the tool manager instance, if any.
Definition: draw_frame.h:937
Class TOOL_EVENT.
Definition: tool_event.h:167
SCH_SHEET_PATH & GetCurrentSheet()
void onChar(wxKeyEvent &event)
Definition: hierarch.cpp:207
HIERARCHY_NAVIG_DLG(SCH_EDIT_FRAME *aParent)
Definition: hierarch.cpp:143
static TOOL_ACTION clearSelection
Clears the current selection.
Definition: ee_actions.h:57
Class SCH_SHEET_PATH.
int m_LastGridSizeId
Definition: draw_frame.h:112
HIERARCHY_TREE(HIERARCHY_NAVIG_DLG *parent)
Definition: hierarch.cpp:81
void buildHierarchyTree(SCH_SHEET_PATH *aList, wxTreeItemId *aPreviousmenu)
Create the hierarchical tree of the schematic.
Definition: hierarch.cpp:216
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:201
SCH_ITEM * LastDrawList() const
Function LastDrawList.
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:128
virtual void RedrawScreen(const wxPoint &aCenterPoint, bool aWarpPointer) override
Redraw the entire screen area by updating the scroll bars and mouse pointer in order to have aCenterP...
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
#define max(a, b)
Definition: auxiliary.h:86
HIERARCHY_NAVIG_DLG * m_parent
Definition: hierarch.cpp:70
virtual void CenterScreen(const wxPoint &aCenterPoint, bool aWarpPointer)
virtual void ClearUndoORRedoList(UNDO_REDO_CONTAINER &aList, int aItemCount=-1) override
Free the undo or redo list from aList element.
Definition: sch_screen.cpp:589
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:55
Class SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container clas...
Definition: sch_item.h:114
SCH_SCREEN * LastScreen() const
Function LastScreen.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:204