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 
30 #include <fctsys.h>
31 #include <sch_draw_panel.h>
32 #include <confirm.h>
33 #include <id.h>
34 #include <bitmaps.h>
35 #include <dialog_shim.h>
36 
37 #include <sch_edit_frame.h>
38 #include <general.h>
39 #include <sch_sheet.h>
40 #include <sch_sheet_path.h>
41 
42 #include <wx/imaglist.h>
43 #include <wx/treectrl.h>
44 
45 #include <netlist_object.h>
46 #include <sch_sheet_path.h>
47 
48 #include <view/view.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 
67 
71 class HIERARCHY_TREE : public wxTreeCtrl
72 {
73 private:
75  wxImageList* imageList;
76 
77 public:
79 
80  // Closes the dialog on escape key
81  void onChar( wxKeyEvent& event );
82 };
83 
84 
86  wxTreeCtrl( (wxWindow*) parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
87  wxTR_HAS_BUTTONS, wxDefaultValidator, wxT( "HierachyTreeCtrl" ) )
88 {
89  m_parent = parent;
90 
91  // Make an image list containing small icons
92  // All icons are expected having the same size.
93  wxBitmap tree_nosel_bm( KiBitmap( tree_nosel_xpm ) );
94  imageList = new wxImageList( tree_nosel_bm.GetWidth(),
95  tree_nosel_bm.GetHeight(), true, 2 );
96 
97  imageList->Add( tree_nosel_bm );
98  imageList->Add( KiBitmap( tree_sel_xpm ) );
99 
100  AssignImageList( imageList );
101 }
102 
103 
105 {
106 public:
110 
111 private:
112  SCH_SHEET_PATH m_currSheet; // The currently opened scheet in hierarchy
113 
114 public:
115  HIERARCHY_NAVIG_DLG( SCH_EDIT_FRAME* aParent, const wxPoint& aPos );
116 
118 
119  // Select the sheet currently selected in the tree, and close the dialog
120  void SelectNewSheetAndQuit();
121 
122 private:
130  void buildHierarchyTree( SCH_SHEET_PATH* aList, wxTreeItemId* aPreviousmenu );
131 
136  void onSelectSheetPath( wxTreeEvent& event );
137 };
138 
139 
141 {
142  HIERARCHY_NAVIG_DLG* treeframe = new HIERARCHY_NAVIG_DLG( this, pos );
143 
144  treeframe->ShowQuasiModal();
145  treeframe->Destroy();
146 }
147 
148 
150  DIALOG_SHIM( aParent, wxID_ANY, _( "Navigator" ), wxDefaultPosition, wxDefaultSize,
151  wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
152 {
153  wxASSERT( dynamic_cast< SCH_EDIT_FRAME* >( aParent ) );
154 
155  m_SchFrameEditor = aParent;
156  m_currSheet = aParent->GetCurrentSheet();
157  m_Tree = new HIERARCHY_TREE( this );
158  m_nbsheets = 1;
159 
160  // root is the link to the main sheet.
161  wxTreeItemId root;
162  root = m_Tree->AddRoot( _( "Root" ), 0, 1 );
163  m_Tree->SetItemBold( root, true );
164 
165  SCH_SHEET_PATH list;
166  list.push_back( g_RootSheet );
167  m_Tree->SetItemData( root, new TreeItemData( list ) );
168 
170  m_Tree->SelectItem( root );
171 
172  buildHierarchyTree( &list, &root );
173 
174  m_Tree->ExpandAll();
175 
176  // This bloc gives a good size to the dialog, better than the default "best" size,
177  // the first time the dialog is opened, during a session
178  wxRect itemrect;
179  wxSize tree_size;
180 
181  m_Tree->GetBoundingRect( root, itemrect );
182 
183  // Set dialog window size to be large enough
184  tree_size.x = itemrect.GetWidth() + 20;
185  tree_size.x = std::max( tree_size.x, 250 );
186 
187  // Readjust the size of the frame to an optimal value.
188  tree_size.y = m_nbsheets * itemrect.GetHeight();
189 
190  if( m_nbsheets < 2 )
191  tree_size.y += 10; // gives a better look for small trees
192 
193  SetClientSize( tree_size );
194 
195  // manage the ESC key to close the dialog, because thre is no Cancel button
196  // in dialog
197  m_Tree->Connect( wxEVT_CHAR, wxKeyEventHandler( HIERARCHY_TREE::onChar ) );
198 
199  // Manage double click on a selection, or the enter key:
200  Bind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
201  // Manage a simple click on a selection, if the selection changes
202  Bind( wxEVT_TREE_SEL_CHANGED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
203 }
204 
205 
207 {
208  Unbind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
209  Unbind( wxEVT_TREE_SEL_CHANGED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
210  m_Tree->Disconnect( wxEVT_CHAR, wxKeyEventHandler( HIERARCHY_TREE::onChar ) );
211 }
212 
213 
214 void HIERARCHY_TREE::onChar( wxKeyEvent& event )
215 {
216  if( event.GetKeyCode() == WXK_ESCAPE )
217  m_parent->Close( true );
218  else
219  event.Skip();
220 }
221 
222 
223 void HIERARCHY_NAVIG_DLG::buildHierarchyTree( SCH_SHEET_PATH* aList, wxTreeItemId* aPreviousmenu )
224 {
225  wxCHECK_RET( m_nbsheets < NB_MAX_SHEET, "Maximum number of sheets exceeded." );
226 
227  SCH_ITEM* schitem = aList->LastDrawList();
228 
229  while( schitem && m_nbsheets < NB_MAX_SHEET )
230  {
231  if( schitem->Type() == SCH_SHEET_T )
232  {
233  SCH_SHEET* sheet = (SCH_SHEET*) schitem;
234  m_nbsheets++;
235  wxTreeItemId menu;
236  menu = m_Tree->AppendItem( *aPreviousmenu, sheet->GetName(), 0, 1 );
237  aList->push_back( sheet );
238  m_Tree->SetItemData( menu, new TreeItemData( *aList ) );
239 
240  if( *aList == m_currSheet )
241  {
242  m_Tree->EnsureVisible( menu );
243  m_Tree->SelectItem( menu );
244  }
245 
246  buildHierarchyTree( aList, &menu );
247 
248  aList->pop_back();
249  }
250 
251  schitem = schitem->Next();
252  }
253 }
254 
255 
256 void HIERARCHY_NAVIG_DLG::onSelectSheetPath( wxTreeEvent& event )
257 {
258  wxTreeItemId ItemSel = m_Tree->GetSelection();
259  m_SchFrameEditor->SetCurrentSheet(( (TreeItemData*) m_Tree->GetItemData( ItemSel ) )->m_SheetPath );
261  Close( true );
262 }
263 
264 
266 {
267  SetRepeatItem( NULL );
268  ClearMsgPanel();
269 
270  SCH_SCREEN* screen = m_CurrentSheet->LastScreen();
271 
272  // Switch to current sheet,
273  // and update the grid size, because it can be modified in latest screen
274  SetScreen( screen );
276 
277  // update the References
280  m_canvas->SetCanStartBlock( -1 );
281 
282  if( !screen->m_Initialized )
283  {
284  Zoom_Automatique( false );
285  screen->m_Initialized = true;
286 
287  // Ensure the schematic is fully segmented on first display
289  SchematicCleanUp( true );
290  screen->ClearUndoORRedoList( screen->m_UndoList, 1 );
291 
292  screen->TestDanglingEnds();
293  }
294  else
295  {
297  // RedrawScreen() will set zoom to last used
299  }
300 
301  // Some items (wires, labels) can be highlighted. So prepare the highlight flag:
303 
304  UpdateTitle();
305 
306  GetCanvas()->Refresh();
307 }
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
Definition of the SCH_SHEET class for Eeschema.
void onSelectSheetPath(wxTreeEvent &event)
Open the selected sheet and display the corresponding screen when a tree item is selected.
Definition: hierarch.cpp:256
bool SchematicCleanUp(bool aAppend=false)
Performs routine schematic cleaning including breaking wire and buses and deleting identical objects ...
virtual void Zoom_Automatique(bool aWarpPointer) override
Redraw the screen with best zoom level and the best centering that shows all the page or the board.
#define NB_MAX_SHEET
Max number of sheets in a hierarchy project.
Definition: sch_screen.h:65
wxImageList * imageList
Definition: hierarch.cpp:75
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
virtual void SetScreen(BASE_SCREEN *aScreen) override
bool BreakSegmentsOnJunctions(bool aApped=false)
Tests all junctions and bus entries in the schematic for intersections with wires and buses and break...
Store an SCH_SHEET_PATH of each sheet in hierarchy.
Definition: hierarch.cpp:56
void UpdateTitle()
Set the main window title bar text.
void SetCanStartBlock(int aStartBlock)
HIERARCHY_TREE * m_Tree
Definition: hierarch.cpp:108
Schematic editor (Eeschema) main window.
Handle hierarchy tree control.
Definition: hierarch.cpp:71
Class DIALOG_SHIM may sit in the inheritance tree between wxDialog and any class written by wxFormBui...
Definition: dialog_shim.h:83
void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
void DisplayCurrentSheet()
Draw the current sheet on the display.
Definition: hierarch.cpp:265
void SelectNewSheetAndQuit()
void UpdateAllScreenReferences()
Function UpdateAllScreenReferences updates the reference and the m_Multi parameter (part selection) f...
SCH_EDIT_FRAME * m_SchFrameEditor
Definition: hierarch.cpp:107
void SetRepeatItem(SCH_ITEM *aItem)
Clone aItem and owns that clone in this container.
void InstallHierarchyFrame(wxPoint &pos)
Definition: hierarch.cpp:140
TreeItemData(SCH_SHEET_PATH &sheet)
Definition: hierarch.cpp:61
SCH_SHEET_PATH m_currSheet
Definition: hierarch.cpp:112
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
SCH_SHEET_PATH * m_CurrentSheet
which sheet we are presently working on.
const wxPoint & GetScrollCenterPosition() const
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:56
wxString GetName() const
Definition: sch_sheet.h:267
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()
SCH_DRAW_PANEL * GetCanvas() const override
SCH_SHEET_PATH & GetCurrentSheet()
void onChar(wxKeyEvent &event)
Definition: hierarch.cpp:214
Class SCH_SHEET_PATH.
int m_LastGridSizeId
Definition: draw_frame.h:110
HIERARCHY_TREE(HIERARCHY_NAVIG_DLG *parent)
Definition: hierarch.cpp:85
void buildHierarchyTree(SCH_SHEET_PATH *aList, wxTreeItemId *aPreviousmenu)
Create the hierarchical tree of the schematic.
Definition: hierarch.cpp:223
HIERARCHY_NAVIG_DLG(SCH_EDIT_FRAME *aParent, const wxPoint &aPos)
Definition: hierarch.cpp:149
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:209
SCH_ITEM * LastDrawList() const
Function LastDrawList.
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:126
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
bool SetCurrentSheetHighlightFlags(std::vector< EDA_ITEM * > *aItemsToRedrawList)
Set or reset the BRIGHTENED of connected objects inside the current sheet, according to the highlight...
bool TestDanglingEnds()
Test all of the connectable objects in the schematic for unused connection points.
Definition: sch_screen.cpp:912
HIERARCHY_NAVIG_DLG * m_parent
Definition: hierarch.cpp:74
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:548
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.
Definition of the NETLIST_OBJECT class.
SCH_SHEET_PATH m_SheetPath
Definition: hierarch.cpp:59
Class SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container clas...
void ClearMsgPanel(void)
Clear all messages from the message panel.
SCH_SCREEN * LastScreen() const
Function LastScreen.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201