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 <sch_draw_panel.h>
27 #include <confirm.h>
28 #include <id.h>
29 #include <bitmaps.h>
30 #include <dialog_shim.h>
31 #include <sch_edit_frame.h>
32 #include <sch_sheet.h>
33 #include <sch_sheet_path.h>
34 #include <schematic.h>
35 #include <tool/tool_manager.h>
36 #include <tools/ee_actions.h>
38 #include <sch_sheet_path.h>
39 
40 #include <hierarch.h>
41 #include <view/view.h>
42 #include <kiface_i.h>
43 #include "eeschema_settings.h"
44 
45 #include <wx/wx.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 int HIERARCHY_TREE::OnCompareItems( const wxTreeItemId& item1, const wxTreeItemId& item2 )
158 {
159  return GetItemText( item1 ).CmpNoCase( GetItemText( item2 ) );
160 }
161 
162 
163 void HIERARCHY_NAVIG_DLG::buildHierarchyTree( SCH_SHEET_PATH* aList, wxTreeItemId* aPreviousmenu )
164 {
165  wxCHECK_RET( m_nbsheets < NB_MAX_SHEET, "Maximum number of sheets exceeded." );
166 
167  std::vector<SCH_ITEM*> sheetChildren;
168  aList->LastScreen()->GetSheets( &sheetChildren );
169 
170  for( SCH_ITEM* aItem : sheetChildren )
171  {
172  SCH_SHEET* sheet = static_cast<SCH_SHEET*>( aItem );
173  wxString sheetName = sheet->GetFields()[ SHEETNAME ].GetShownText();
174  m_nbsheets++;
175  wxTreeItemId menu;
176  menu = m_Tree->AppendItem( *aPreviousmenu, sheetName, 0, 1 );
177  aList->push_back( sheet );
178  m_Tree->SetItemData( menu, new TreeItemData( *aList ) );
179 
180  if( *aList == m_currSheet )
181  {
182  m_Tree->EnsureVisible( menu );
183  m_Tree->SelectItem( menu );
184  }
185 
186  buildHierarchyTree( aList, &menu );
187  aList->pop_back();
188 
189  if( m_nbsheets >= NB_MAX_SHEET )
190  break;
191  }
192 
193  m_Tree->SortChildren( *aPreviousmenu );
194 }
195 
197 {
198  Freeze();
199 
201  wxTreeItemId root = m_Tree->GetRootItem();
202  m_Tree->DeleteChildren( root );
203  m_list.clear();
205  buildHierarchyTree( &m_list, &root );
206 
207  Thaw();
208 }
209 
210 void HIERARCHY_NAVIG_DLG::onSelectSheetPath( wxTreeEvent& event )
211 {
214 
215  wxTreeItemId itemSel = m_Tree->GetSelection();
216  TreeItemData* itemData = static_cast<TreeItemData*>( m_Tree->GetItemData( itemSel ) );
217 
220 
221  EESCHEMA_SETTINGS* appSettings = static_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
222 
223  if( !appSettings->m_Appearance.navigator_stays_open )
224  Close( true );
225 }
226 
227 
228 void HIERARCHY_NAVIG_DLG::OnCloseNav( wxCloseEvent& event )
229 {
230  Destroy();
231 }
232 
233 
235 {
238 
239  SCH_SCREEN* screen = GetCurrentSheet().LastScreen();
240 
241  wxASSERT( screen );
242 
243  SetScreen( screen );
244 
245  // update the References
248 
249  if( !screen->m_Initialized )
250  {
252  screen->m_Initialized = true;
253  }
254  else
255  {
256  // RedrawScreen() will set zoom to last used
257  RedrawScreen( (wxPoint) GetScreen()->m_ScrollCenter, false );
258  }
259 
260  UpdateTitle();
261 
264  editTool->UpdateNetHighlighting( dummy );
265 
266  HardRedraw(); // Ensure any item has its view updated, especially the worksheet items
267 }
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:210
#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
Return a pointer to the last SCH_SHEET of the list.
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:53
static TOOL_ACTION zoomFitScreen
Definition: actions.h:94
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:960
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:141
int OnCompareItems(const wxTreeItemId &item1, const wxTreeItemId &item2) override
Definition: hierarch.cpp:157
HIERARCHY_TREE * m_Tree
Definition: hierarch.h:66
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:234
void UpdateAllScreenReferences()
Update all the symbol references for this sheet path.
SCH_EDIT_FRAME * m_SchFrameEditor
Definition: hierarch.h:65
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:63
std::vector< SCH_FIELD > & GetFields()
Definition: sch_sheet.h:270
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:82
void UpdateHierarchyTree()
Update the hierarchical tree of the schematic.
Definition: hierarch.cpp:196
void HardRedraw() override
Rebuild the GAL and redraw the screen.
bool m_Initialized
Definition: base_screen.h:104
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
TOOL_EVENT.
Definition: tool_event.h:171
void onChar(wxKeyEvent &event)
Definition: hierarch.cpp:148
SCHEMATIC & Schematic() const
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:56
static TOOL_ACTION clearSelection
Clears the current selection.
Definition: ee_actions.h:56
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
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:163
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:219
SCH_SHEET_PATH m_list
Definition: hierarch.h:64
SCH_SHEET & Root() const
Definition: schematic.h:96
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:48
SCH_SCREEN * LastScreen()
const BITMAP_OPAQUE tree_nosel_xpm[1]
Definition: tree_nosel.cpp:55
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:228
SCH_SHEET_PATH & GetCurrentSheet() const
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:76
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
SCH_SHEET_PATH m_SheetPath
Definition: hierarch.cpp:56
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:194