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-2017 KiCad Developers, see change_log.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 <class_drawpanel.h>
32 #include <confirm.h>
33 #include <id.h>
34 #include <bitmaps.h>
35 #include <dialog_shim.h>
36 
37 #include <schframe.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 <class_netlist_object.h>
46 #include <sch_sheet_path.h>
47 
48 
49 enum
50 {
52 };
53 
54 
56 
57 
61 class TreeItemData : public wxTreeItemData
62 {
63 public:
65 
66  TreeItemData( SCH_SHEET_PATH& sheet ) : wxTreeItemData()
67  {
68  m_SheetPath = sheet;
69  }
70 };
71 
72 
76 class HIERARCHY_TREE : public wxTreeCtrl
77 {
78 private:
80  wxImageList* imageList;
81 
82 public:
84  {
85  m_Parent = NULL;
86  imageList = NULL;
87  }
88 
90 
91  DECLARE_DYNAMIC_CLASS( HIERARCHY_TREE )
92 };
93 
94 
95 IMPLEMENT_DYNAMIC_CLASS( HIERARCHY_TREE, wxTreeCtrl )
96 
97 
99  wxTreeCtrl( (wxWindow*)parent, ID_TREECTRL_HIERARCHY, wxDefaultPosition, wxDefaultSize,
100  wxTR_HAS_BUTTONS, wxDefaultValidator, wxT( "HierachyTreeCtrl" ) )
101 {
102  m_Parent = parent;
103 
104  // Make an image list containing small icons
105  // All icons are expected having the same size.
106  wxBitmap tree_nosel_bm( KiBitmap( tree_nosel_xpm ) );
107  imageList = new wxImageList( tree_nosel_bm.GetWidth(),
108  tree_nosel_bm.GetHeight(), true, 2 );
109 
110  imageList->Add( tree_nosel_bm );
111  imageList->Add( KiBitmap( tree_sel_xpm ) );
112 
113  AssignImageList( imageList );
114 }
115 
116 
118 {
119 public:
122 
123 private:
124  wxSize m_TreeSize;
125  int maxposx;
126 
127 public:
128  HIERARCHY_NAVIG_DLG( SCH_EDIT_FRAME* aParent, const wxPoint& aPos );
129 
131 
137  void BuildTree( SCH_SHEET_PATH* list, wxTreeItemId* previousmenu );
138 
143  void OnSelect( wxTreeEvent& event );
144 
145 private:
146  void OnQuit( wxCommandEvent& event );
147 };
148 
149 
151 {
152  HIERARCHY_NAVIG_DLG* treeframe = new HIERARCHY_NAVIG_DLG( this, pos );
153 
154  treeframe->ShowQuasiModal();
155  treeframe->Destroy();
156 }
157 
158 
160  DIALOG_SHIM( aParent, wxID_ANY, _( "Navigator" ), wxDefaultPosition, wxDefaultSize,
161  wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
162 {
163  wxASSERT( dynamic_cast< SCH_EDIT_FRAME* >( aParent ) );
164 
165  wxTreeItemId root;
166 
167  m_Tree = new HIERARCHY_TREE( this );
168 
169  m_nbsheets = 1;
170 
171  root = m_Tree->AddRoot( _( "Root" ), 0, 1 );
172  m_Tree->SetItemBold( root, true );
173 
174  SCH_SHEET_PATH list;
175  list.push_back( g_RootSheet );
176  m_Tree->SetItemData( root, new TreeItemData( list ) );
177 
178  if( dynamic_cast< SCH_EDIT_FRAME* >( aParent )->GetCurrentSheet().Last() == g_RootSheet )
179  m_Tree->SelectItem( root ); //root.
180 
181  maxposx = 15;
182  BuildTree( &list, &root );
183 
184  m_Tree->Expand( root );
185 
186  wxRect itemrect;
187  m_Tree->GetBoundingRect( root, itemrect );
188 
189  // Set dialog window size to be large enough
190  m_TreeSize.x = itemrect.GetWidth() + 20;
191  m_TreeSize.x = std::max( m_TreeSize.x, 250 );
192 
193  // Readjust the size of the frame to an optimal value.
194  m_TreeSize.y = m_nbsheets * itemrect.GetHeight();
195  m_TreeSize.y += 10;
196 
197  SetClientSize( m_TreeSize );
198 
199  Bind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::OnSelect, this );
200 }
201 
202 
204 {
205  Unbind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::OnSelect, this );
206 }
207 
208 
209 void HIERARCHY_NAVIG_DLG::OnQuit( wxCommandEvent& event )
210 {
211  // true is to force the frame to close
212  Close( true );
213 }
214 
215 
216 void HIERARCHY_NAVIG_DLG::BuildTree( SCH_SHEET_PATH* list, wxTreeItemId* previousmenu )
217 
218 {
219  wxTreeItemId menu;
220 
221  wxCHECK_RET( m_nbsheets < NB_MAX_SHEET, "Maximum number of sheets exceeded." );
222 
223  maxposx += m_Tree->GetIndent();
224  SCH_ITEM* schitem = list->LastDrawList();
225 
226  while( schitem && m_nbsheets < NB_MAX_SHEET )
227  {
228  if( schitem->Type() == SCH_SHEET_T )
229  {
230  SCH_SHEET* sheet = (SCH_SHEET*) schitem;
231  m_nbsheets++;
232  menu = m_Tree->AppendItem( *previousmenu, sheet->GetName(), 0, 1 );
233  list->push_back( sheet );
234  m_Tree->SetItemData( menu, new TreeItemData( *list ) );
235  int ll = m_Tree->GetItemText( menu ).Len();
236 
237 #ifdef __WINDOWS__
238  ll *= 9; // * char width
239 #else
240  ll *= 12; // * char width
241 #endif
242  ll += maxposx + 20;
243  m_TreeSize.x = std::max( m_TreeSize.x, ll );
244  m_TreeSize.y += 1;
245 
246  if( *list == dynamic_cast< SCH_EDIT_FRAME* >( GetParent() )->GetCurrentSheet() )
247  {
248  m_Tree->EnsureVisible( menu );
249  m_Tree->SelectItem( menu );
250  }
251 
252  BuildTree( list, &menu );
253  m_Tree->Expand( menu );
254  list->pop_back();
255  }
256 
257  schitem = schitem->Next();
258  }
259 
260  maxposx -= m_Tree->GetIndent();
261 }
262 
263 
264 void HIERARCHY_NAVIG_DLG::OnSelect( wxTreeEvent& event )
265 
266 {
267  wxTreeItemId ItemSel = m_Tree->GetSelection();
268  SCH_EDIT_FRAME* parent = dynamic_cast< SCH_EDIT_FRAME* >( GetParent() );
269 
270  wxCHECK2_MSG( parent, Close( true ),
271  "Parent window of hierarchy dialog is not SCH_EDIT_FRAME." );
272 
273  parent->SetCurrentSheet(( (TreeItemData*) m_Tree->GetItemData( ItemSel ) )->m_SheetPath );
274  parent->DisplayCurrentSheet();
275  Close( true );
276 }
277 
278 
280 {
281  SetRepeatItem( NULL );
282  ClearMsgPanel();
283 
284  SCH_SCREEN* screen = m_CurrentSheet->LastScreen();
285 
286  // Switch to current sheet,
287  // and update the grid size, because it can be modified in latest screen
288  SetScreen( screen );
290 
291  // update the References
294  m_canvas->SetCanStartBlock( -1 );
295 
296  if( screen->m_FirstRedraw )
297  {
298  Zoom_Automatique( false );
299  screen->m_FirstRedraw = false;
302  screen->SchematicCleanUp();
303  }
304  else
305  {
307  }
308 
309  // Some items (wires, labels) can be highlighted. So prepare the highlight flag:
311 
312  // Now refresh m_canvas. Should be not necessary, but because screen has changed
313  // the previous refresh has set all new draw parameters (scroll position ..)
314  // but most of time there were some inconsitencies about cursor parameters
315  // ( previous position of cursor ...) and artefacts can happen
316  // mainly when sheet size has changed
317  // This second refresh clears artefacts because at this point,
318  // all parameters are now updated
319  m_canvas->Refresh();
320 }
SCH_ITEM * LastDrawList() const
Function LastDrawList.
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
Definition: schframe.cpp:585
Definition of the SCH_SHEET class for Eeschema.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:212
HIERARCHY_NAVIG_DLG * m_Parent
Definition: hierarch.cpp:79
wxImageList * imageList
Definition: hierarch.cpp:80
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Update the board display after modifying it bu a python script (note: it is automatically called by a...
Definition: draw_panel.cpp:325
This file is part of the common library.
void OnQuit(wxCommandEvent &event)
Definition: hierarch.cpp:209
void SetScreen(BASE_SCREEN *aScreen)
Definition: draw_frame.h:141
Store an SCH_SHEET_PATH of each sheet in hierarchy.
Definition: hierarch.cpp:61
void SetCanStartBlock(int aStartBlock)
const wxPoint & GetScrollCenterPosition() const
HIERARCHY_TREE * m_Tree
Definition: hierarch.cpp:120
Schematic editor (Eeschema) main window.
Definition: schframe.h:118
Handle hierarchy tree control.
Definition: hierarch.cpp:76
Class DIALOG_SHIM may sit in the inheritance tree between wxDialog and any class written by wxFormBui...
Definition: dialog_shim.h:60
SCH_SCREEN * LastScreen() const
Function LastScreen.
void DisplayCurrentSheet()
Draw the current sheet on the display.
Definition: hierarch.cpp:279
void UpdateAllScreenReferences()
Function UpdateAllScreenReferences updates the reference and the m_Multi parameter (part selection) f...
void MoveCursorToCrossHair()
Function MoveCursorToCrossHair warps the cursor to the current cross hair position.
Definition: draw_panel.cpp:347
SCH_ITEM * Next() const
void SetRepeatItem(SCH_ITEM *aItem)
Clone aItem and owns that clone in this container.
Definition: schframe.cpp:471
void InstallHierarchyFrame(wxPoint &pos)
Definition: hierarch.cpp:150
TreeItemData(SCH_SHEET_PATH &sheet)
Definition: hierarch.cpp:66
SCH_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
Definition: schframe.cpp:532
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Function KiBitmap constructs a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:36
SCH_SHEET_PATH * m_CurrentSheet
which sheet we are presently working on.
Definition: schframe.h:121
void Zoom_Automatique(bool aWarpPointer)
Function Zoom_Automatique redraws the screen with best zoom level and the best centering that shows a...
Definition: zoom.cpp:77
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:55
bool SchematicCleanUp()
Perform routine schematic cleaning including breaking wire and buses and deleting identical objects s...
Definition: sch_screen.cpp:438
int ShowQuasiModal()
Class SCH_SHEET_PATH.
int m_LastGridSizeId
Definition: draw_frame.h:78
HIERARCHY_NAVIG_DLG(SCH_EDIT_FRAME *aParent, const wxPoint &aPos)
Definition: hierarch.cpp:159
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:209
Definition of the NETLIST_OBJECT class.
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:93
void RedrawScreen(const wxPoint &aCenterPoint, bool aWarpPointer)
Function RedrawScreen redraws the entire screen area by updating the scroll bars and mouse pointer in...
Definition: zoom.cpp:46
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
Definition: schframe.cpp:497
#define max(a, b)
Definition: auxiliary.h:86
void OnSelect(wxTreeEvent &event)
Open the selected sheet and display the corresponding screen when a tree item is selected.
Definition: hierarch.cpp:264
#define NB_MAX_SHEET
Max number of sheets in a hierarchy project.
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
void SetCrossHairPosition(const wxPoint &aPosition, bool aSnapToGrid=true)
Function SetCrossHairPosition sets the screen cross hair position to aPosition in logical (drawing) u...
int SetGrid(const wxRealPoint &size)
set the current grid size m_Grid.
wxString GetName() const
Definition: sch_sheet.h:267
SCH_SHEET_PATH m_SheetPath
Definition: hierarch.cpp:64
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.
Definition: draw_frame.cpp:775
bool SetCurrentSheetHighlightFlags()
Set or reset the BRIGHTENED of connected objects inside the current sheet, according to the highlight...
void BuildTree(SCH_SHEET_PATH *list, wxTreeItemId *previousmenu)
Create the hierarchical tree of the schematic.
Definition: hierarch.cpp:216