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-2016 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 2004-2016 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 <schframe.h>
35 #include <bitmaps.h>
36 
37 #include <general.h>
38 #include <sch_sheet.h>
39 #include <sch_sheet_path.h>
40 
41 #include <wx/imaglist.h>
42 #include <wx/treectrl.h>
43 
44 #include <class_netlist_object.h>
45 #include <sch_sheet_path.h>
46 
47 enum
48 {
50 };
51 
52 
54 
55 
56 /* This class derived from wxTreeItemData stores the SCH_SHEET_PATH of each
57  * sheet in hierarchy in each TreeItem, in its associated data buffer
58 */
59 class TreeItemData : public wxTreeItemData
60 {
61 public:
63 
64  TreeItemData( SCH_SHEET_PATH& sheet ) : wxTreeItemData()
65  {
66  m_SheetPath = sheet;
67  }
68 };
69 
70 /* Class to handle hierarchy tree. */
71 class HIERARCHY_TREE : public wxTreeCtrl
72 {
73 private:
75  wxImageList* imageList;
76 
77 public:
79  {
80  m_Parent = NULL;
81  imageList = NULL;
82  }
83 
85 
86  DECLARE_DYNAMIC_CLASS( HIERARCHY_TREE )
87 };
88 
89 IMPLEMENT_DYNAMIC_CLASS( HIERARCHY_TREE, wxTreeCtrl )
90 
91 
93  wxTreeCtrl( (wxWindow*)parent, ID_TREECTRL_HIERARCHY, wxDefaultPosition, wxDefaultSize,
94  wxTR_HAS_BUTTONS, wxDefaultValidator, wxT( "HierachyTreeCtrl" ) )
95 {
96  m_Parent = parent;
97 
98  // Make an image list containing small icons
99  // All icons are expected having the same size.
100  wxBitmap tree_nosel_bm( KiBitmap( tree_nosel_xpm ) );
101  imageList = new wxImageList( tree_nosel_bm.GetWidth(),
102  tree_nosel_bm.GetHeight(), true, 2 );
103 
104  imageList->Add( tree_nosel_bm );
105  imageList->Add( KiBitmap( tree_sel_xpm ) );
106 
107  AssignImageList( imageList );
108 }
109 
110 
111 class HIERARCHY_NAVIG_DLG : public wxDialog
112 {
113 public:
117 
118 private:
119  wxSize m_TreeSize;
120  int maxposx;
121 
122 public:
123  HIERARCHY_NAVIG_DLG( SCH_EDIT_FRAME* aParent, const wxPoint& aPos );
124  void BuildSheetsTree( SCH_SHEET_PATH* list, wxTreeItemId* previousmenu );
125 
127 
128  void OnSelect( wxTreeEvent& event );
129 
130 private:
131  void OnQuit( wxCommandEvent& event );
132 
133  DECLARE_EVENT_TABLE()
134 };
135 
136 BEGIN_EVENT_TABLE( HIERARCHY_NAVIG_DLG, wxDialog )
137  EVT_TREE_ITEM_ACTIVATED( ID_TREECTRL_HIERARCHY, HIERARCHY_NAVIG_DLG::OnSelect )
138 END_EVENT_TABLE()
139 
140 
141 void SCH_EDIT_FRAME::InstallHierarchyFrame( wxPoint& pos )
142 {
143  HIERARCHY_NAVIG_DLG* treeframe = new HIERARCHY_NAVIG_DLG( this, pos );
144 
145  treeframe->ShowModal();
146  treeframe->Destroy();
147 }
148 
149 
151  wxDialog( aParent, wxID_ANY, _( "Navigator" ), aPos, wxSize( 110, 50 ),
152  wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
153 {
154  wxTreeItemId cellule;
155 
156  m_Parent = aParent;
157  m_Tree = new HIERARCHY_TREE( this );
158 
159  m_nbsheets = 1;
160 
161  cellule = m_Tree->AddRoot( _( "Root" ), 0, 1 );
162  m_Tree->SetItemBold( cellule, true );
163 
164  SCH_SHEET_PATH list;
165  list.push_back( g_RootSheet );
166  m_Tree->SetItemData( cellule, new TreeItemData( list ) );
167 
169  m_Tree->SelectItem( cellule ); //root.
170 
171  maxposx = 15;
172  BuildSheetsTree( &list, &cellule );
173 
174  m_Tree->Expand( cellule );
175 
176  wxRect itemrect;
177  m_Tree->GetBoundingRect( cellule, itemrect );
178 
179  // Set dialog window size to be large enough
180  m_TreeSize.x = itemrect.GetWidth() + 20;
181  m_TreeSize.x = std::max( m_TreeSize.x, 250 );
182 
183  // Readjust the size of the frame to an optimal value.
184  m_TreeSize.y = m_nbsheets * itemrect.GetHeight();
185  m_TreeSize.y += 10;
186 
187  SetClientSize( m_TreeSize );
188 }
189 
190 
192 {
193 }
194 
195 
196 void HIERARCHY_NAVIG_DLG::OnQuit( wxCommandEvent& event )
197 {
198  // true is to force the frame to close
199  Close( true );
200 }
201 
202 
203 /* Routine to create the hierarchical tree of the schematic
204  * This routine is re-entrant!
205  */
206 void HIERARCHY_NAVIG_DLG::BuildSheetsTree( SCH_SHEET_PATH* list, wxTreeItemId* previousmenu )
207 
208 {
209  wxTreeItemId menu;
210 
211  if( m_nbsheets > NB_MAX_SHEET )
212  {
213  if( m_nbsheets == (NB_MAX_SHEET + 1) )
214  {
215  wxString msg;
216  msg << wxT( "BuildSheetsTree: Error: nbsheets > " ) << NB_MAX_SHEET;
217  DisplayError( this, msg );
218  m_nbsheets++;
219  }
220 
221  return;
222  }
223 
224  maxposx += m_Tree->GetIndent();
225  SCH_ITEM* schitem = list->LastDrawList();
226 
227  while( schitem && m_nbsheets < NB_MAX_SHEET )
228  {
229  if( schitem->Type() == SCH_SHEET_T )
230  {
231  SCH_SHEET* sheet = (SCH_SHEET*) schitem;
232  m_nbsheets++;
233  menu = m_Tree->AppendItem( *previousmenu, sheet->GetName(), 0, 1 );
234  list->push_back( sheet );
235  m_Tree->SetItemData( menu, new TreeItemData( *list ) );
236  int ll = m_Tree->GetItemText( menu ).Len();
237 
238 #ifdef __WINDOWS__
239  ll *= 9; // * char width
240 #else
241  ll *= 12; // * char width
242 #endif
243  ll += maxposx + 20;
244  m_TreeSize.x = std::max( m_TreeSize.x, ll );
245  m_TreeSize.y += 1;
246 
247  if( *list == m_Parent->GetCurrentSheet() )
248  {
249  m_Tree->EnsureVisible( menu );
250  m_Tree->SelectItem( menu );
251  }
252 
253  BuildSheetsTree( list, &menu );
254  m_Tree->Expand( menu );
255  list->pop_back();
256  }
257 
258  schitem = schitem->Next();
259  }
260 
261  maxposx -= m_Tree->GetIndent();
262 }
263 
264 
265 /* Called on a double-click on a tree item:
266  * Open the selected sheet, and display the corresponding screen
267  */
268 void HIERARCHY_NAVIG_DLG::OnSelect( wxTreeEvent& event )
269 
270 {
271  wxTreeItemId ItemSel = m_Tree->GetSelection();
272 
273  m_Parent->SetCurrentSheet(( (TreeItemData*) m_Tree->GetItemData( ItemSel ) )->m_SheetPath );
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:573
Definition of the SCH_SHEET class for Eeschema.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
HIERARCHY_NAVIG_DLG * m_Parent
Definition: hierarch.cpp:74
wxImageList * imageList
Definition: hierarch.cpp:75
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Definition: draw_panel.cpp:326
void BuildSheetsTree(SCH_SHEET_PATH *list, wxTreeItemId *previousmenu)
Definition: hierarch.cpp:206
This file is part of the common library.
void OnQuit(wxCommandEvent &event)
Definition: hierarch.cpp:196
void SetScreen(BASE_SCREEN *aScreen)
Definition: draw_frame.h:140
void SetCanStartBlock(int aStartBlock)
const wxPoint & GetScrollCenterPosition() const
HIERARCHY_TREE * m_Tree
Definition: hierarch.cpp:115
Schematic editor (Eeschema) main window.
Definition: schframe.h:117
SCH_SCREEN * LastScreen() const
Function LastScreen.
void DisplayCurrentSheet()
Function DisplayCurrentSheet draws 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:348
SCH_ITEM * Next() const
void SetRepeatItem(SCH_ITEM *aItem)
Function SetRepeatItem clones aItem and owns that clone in this container.
Definition: schframe.cpp:459
TreeItemData(SCH_SHEET_PATH &sheet)
Definition: hierarch.cpp:64
SCH_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
Definition: schframe.cpp:520
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:120
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:54
bool SchematicCleanUp()
Function SchematicCleanUp performs routine schematic cleaning including breaking wire and buses and d...
Definition: sch_screen.cpp:426
SCH_EDIT_FRAME * m_Parent
Definition: hierarch.cpp:114
SCH_SHEET_PATH & GetCurrentSheet()
Definition: schframe.cpp:565
Class SCH_SHEET_PATH.
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...
int m_LastGridSizeId
Definition: draw_frame.h:77
HIERARCHY_NAVIG_DLG(SCH_EDIT_FRAME *aParent, const wxPoint &aPos)
Definition: hierarch.cpp:150
Class SCH_SHEET is the sheet symbol placed in a schematic, and is the entry point for a sub schematic...
Definition: sch_sheet.h:216
Definition of the NETLIST_OBJECT class.
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:92
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()
Function SetSheetNumberAndCount Set the m_ScreenNumber and m_NumberOfScreens members for screens must...
Definition: schframe.cpp:485
#define max(a, b)
Definition: auxiliary.h:86
void OnSelect(wxTreeEvent &event)
Definition: hierarch.cpp:268
#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:274
SCH_SHEET_PATH m_SheetPath
Definition: hierarch.cpp:62
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:69
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:742
bool SetCurrentSheetHighlightFlags()
Function SetCurrentSheetHighlightFlags Set/reset the BRIGHTENED of connected objects inside the curre...