You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
8.4 KiB

  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. #include <fctsys.h>
  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 <general.h>
  33. #include <sch_sheet.h>
  34. #include <sch_sheet_path.h>
  35. #include <wx/imaglist.h>
  36. #include <wx/treectrl.h>
  37. #include <tool/tool_manager.h>
  38. #include <tools/sch_actions.h>
  39. #include <netlist_object.h>
  40. #include <sch_sheet_path.h>
  41. #include <view/view.h>
  42. class HIERARCHY_NAVIG_DLG;
  43. /**
  44. * Store an SCH_SHEET_PATH of each sheet in hierarchy.
  45. */
  46. class TreeItemData : public wxTreeItemData
  47. {
  48. public:
  49. SCH_SHEET_PATH m_SheetPath;
  50. TreeItemData( SCH_SHEET_PATH& sheet ) : wxTreeItemData()
  51. {
  52. m_SheetPath = sheet;
  53. }
  54. };
  55. /**
  56. * Handle hierarchy tree control.
  57. */
  58. class HIERARCHY_TREE : public wxTreeCtrl
  59. {
  60. private:
  61. HIERARCHY_NAVIG_DLG* m_parent;
  62. wxImageList* imageList;
  63. public:
  64. HIERARCHY_TREE( HIERARCHY_NAVIG_DLG* parent );
  65. // Closes the dialog on escape key
  66. void onChar( wxKeyEvent& event );
  67. };
  68. HIERARCHY_TREE::HIERARCHY_TREE( HIERARCHY_NAVIG_DLG* parent ) :
  69. wxTreeCtrl( (wxWindow*) parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
  70. wxTR_HAS_BUTTONS, wxDefaultValidator, wxT( "HierachyTreeCtrl" ) )
  71. {
  72. m_parent = parent;
  73. // Make an image list containing small icons
  74. // All icons are expected having the same size.
  75. wxBitmap tree_nosel_bm( KiBitmap( tree_nosel_xpm ) );
  76. imageList = new wxImageList( tree_nosel_bm.GetWidth(), tree_nosel_bm.GetHeight(), true, 2 );
  77. imageList->Add( tree_nosel_bm );
  78. imageList->Add( KiBitmap( tree_sel_xpm ) );
  79. AssignImageList( imageList );
  80. }
  81. class HIERARCHY_NAVIG_DLG : public DIALOG_SHIM
  82. {
  83. public:
  84. SCH_EDIT_FRAME* m_SchFrameEditor;
  85. HIERARCHY_TREE* m_Tree;
  86. int m_nbsheets;
  87. private:
  88. SCH_SHEET_PATH m_currSheet; // The currently opened scheet in hierarchy
  89. public:
  90. HIERARCHY_NAVIG_DLG( SCH_EDIT_FRAME* aParent, const wxPoint& aPos );
  91. ~HIERARCHY_NAVIG_DLG();
  92. private:
  93. /**
  94. * Create the hierarchical tree of the schematic.
  95. *
  96. * This routine is reentrant!
  97. * @param aList = the SCH_SHEET_PATH* list to explore
  98. * @param aPreviousmenu = the wxTreeItemId used as parent to add sub items
  99. */
  100. void buildHierarchyTree( SCH_SHEET_PATH* aList, wxTreeItemId* aPreviousmenu );
  101. /**
  102. * Open the selected sheet and display the corresponding screen when a tree item is
  103. * selected.
  104. */
  105. void onSelectSheetPath( wxTreeEvent& event );
  106. };
  107. void SCH_EDIT_FRAME::InstallHierarchyFrame( wxPoint& pos )
  108. {
  109. HIERARCHY_NAVIG_DLG* treeframe = new HIERARCHY_NAVIG_DLG( this, pos );
  110. treeframe->ShowQuasiModal();
  111. treeframe->Destroy();
  112. }
  113. HIERARCHY_NAVIG_DLG::HIERARCHY_NAVIG_DLG( SCH_EDIT_FRAME* aParent, const wxPoint& aPos ) :
  114. DIALOG_SHIM( aParent, wxID_ANY, _( "Navigator" ), wxDefaultPosition, wxDefaultSize,
  115. wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
  116. {
  117. wxASSERT( dynamic_cast< SCH_EDIT_FRAME* >( aParent ) );
  118. m_SchFrameEditor = aParent;
  119. m_currSheet = aParent->GetCurrentSheet();
  120. m_Tree = new HIERARCHY_TREE( this );
  121. m_nbsheets = 1;
  122. // root is the link to the main sheet.
  123. wxTreeItemId root = m_Tree->AddRoot( _( "Root" ), 0, 1 );
  124. m_Tree->SetItemBold( root, true );
  125. SCH_SHEET_PATH list;
  126. list.push_back( g_RootSheet );
  127. m_Tree->SetItemData( root, new TreeItemData( list ) );
  128. if( m_SchFrameEditor->GetCurrentSheet().Last() == g_RootSheet )
  129. m_Tree->SelectItem( root );
  130. buildHierarchyTree( &list, &root );
  131. m_Tree->ExpandAll();
  132. // This bloc gives a good size to the dialog, better than the default "best" size,
  133. // the first time the dialog is opened, during a session
  134. wxRect itemrect;
  135. wxSize tree_size;
  136. m_Tree->GetBoundingRect( root, itemrect );
  137. // Set dialog window size to be large enough
  138. tree_size.x = itemrect.GetWidth() + 20;
  139. tree_size.x = std::max( tree_size.x, 250 );
  140. // Readjust the size of the frame to an optimal value.
  141. tree_size.y = m_nbsheets * itemrect.GetHeight();
  142. if( m_nbsheets < 2 )
  143. tree_size.y += 10; // gives a better look for small trees
  144. SetClientSize( tree_size );
  145. // manage the ESC key to close the dialog, because thre is no Cancel button
  146. // in dialog
  147. m_Tree->Connect( wxEVT_CHAR, wxKeyEventHandler( HIERARCHY_TREE::onChar ) );
  148. // Manage double click on a selection, or the enter key:
  149. Bind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
  150. // Manage a simple click on a selection, if the selection changes
  151. Bind( wxEVT_TREE_SEL_CHANGED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
  152. }
  153. HIERARCHY_NAVIG_DLG::~HIERARCHY_NAVIG_DLG()
  154. {
  155. Unbind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
  156. Unbind( wxEVT_TREE_SEL_CHANGED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
  157. m_Tree->Disconnect( wxEVT_CHAR, wxKeyEventHandler( HIERARCHY_TREE::onChar ) );
  158. }
  159. void HIERARCHY_TREE::onChar( wxKeyEvent& event )
  160. {
  161. if( event.GetKeyCode() == WXK_ESCAPE )
  162. m_parent->Close( true );
  163. else
  164. event.Skip();
  165. }
  166. void HIERARCHY_NAVIG_DLG::buildHierarchyTree( SCH_SHEET_PATH* aList, wxTreeItemId* aPreviousmenu )
  167. {
  168. wxCHECK_RET( m_nbsheets < NB_MAX_SHEET, "Maximum number of sheets exceeded." );
  169. SCH_ITEM* schitem = aList->LastDrawList();
  170. while( schitem && m_nbsheets < NB_MAX_SHEET )
  171. {
  172. if( schitem->Type() == SCH_SHEET_T )
  173. {
  174. SCH_SHEET* sheet = (SCH_SHEET*) schitem;
  175. m_nbsheets++;
  176. wxTreeItemId menu;
  177. menu = m_Tree->AppendItem( *aPreviousmenu, sheet->GetName(), 0, 1 );
  178. aList->push_back( sheet );
  179. m_Tree->SetItemData( menu, new TreeItemData( *aList ) );
  180. if( *aList == m_currSheet )
  181. {
  182. m_Tree->EnsureVisible( menu );
  183. m_Tree->SelectItem( menu );
  184. }
  185. buildHierarchyTree( aList, &menu );
  186. aList->pop_back();
  187. }
  188. schitem = schitem->Next();
  189. }
  190. }
  191. void HIERARCHY_NAVIG_DLG::onSelectSheetPath( wxTreeEvent& event )
  192. {
  193. wxTreeItemId ItemSel = m_Tree->GetSelection();
  194. m_SchFrameEditor->SetCurrentSheet(( (TreeItemData*) m_Tree->GetItemData( ItemSel ) )->m_SheetPath );
  195. m_SchFrameEditor->DisplayCurrentSheet();
  196. Close( true );
  197. }
  198. void SCH_EDIT_FRAME::DisplayCurrentSheet()
  199. {
  200. SetRepeatItem( NULL );
  201. ClearMsgPanel();
  202. SCH_SCREEN* screen = g_CurrentSheet->LastScreen();
  203. // Switch to current sheet,
  204. // and update the grid size, because it can be modified in latest screen
  205. SetScreen( screen );
  206. GetScreen()->SetGrid( m_LastGridSizeId + ID_POPUP_GRID_LEVEL_1000 );
  207. // update the References
  208. g_CurrentSheet->UpdateAllScreenReferences();
  209. SetSheetNumberAndCount();
  210. m_canvas->SetCanStartBlock( -1 );
  211. if( !screen->m_Initialized )
  212. {
  213. Zoom_Automatique( false );
  214. screen->m_Initialized = true;
  215. screen->ClearUndoORRedoList( screen->m_UndoList, 1 );
  216. }
  217. else
  218. {
  219. CenterScreen( GetScrollCenterPosition(), false );
  220. // RedrawScreen() will set zoom to last used
  221. RedrawScreen( GetScrollCenterPosition(), false );
  222. }
  223. UpdateTitle();
  224. GetToolManager()->RunAction( SCH_ACTIONS::highlightNetSelection, true );
  225. GetCanvas()->Refresh();
  226. }