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.

255 lines
8.0 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-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. #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/ee_actions.h>
  39. #include <tools/sch_editor_control.h>
  40. //#include <netlist_object.h>
  41. #include <sch_sheet_path.h>
  42. #include <hierarch.h>
  43. #include <view/view.h>
  44. class HIERARCHY_NAVIG_DLG;
  45. /**
  46. * Store an SCH_SHEET_PATH of each sheet in hierarchy.
  47. */
  48. class TreeItemData : public wxTreeItemData
  49. {
  50. public:
  51. SCH_SHEET_PATH m_SheetPath;
  52. TreeItemData( SCH_SHEET_PATH& sheet ) : wxTreeItemData()
  53. {
  54. m_SheetPath = sheet;
  55. }
  56. };
  57. HIERARCHY_TREE::HIERARCHY_TREE( HIERARCHY_NAVIG_DLG* parent ) :
  58. wxTreeCtrl( (wxWindow*) parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
  59. wxTR_HAS_BUTTONS, wxDefaultValidator, wxT( "HierachyTreeCtrl" ) )
  60. {
  61. m_parent = parent;
  62. // Make an image list containing small icons
  63. // All icons are expected having the same size.
  64. wxBitmap tree_nosel_bm( KiBitmap( tree_nosel_xpm ) );
  65. imageList = new wxImageList( tree_nosel_bm.GetWidth(), tree_nosel_bm.GetHeight(), true, 2 );
  66. imageList->Add( tree_nosel_bm );
  67. imageList->Add( KiBitmap( tree_sel_xpm ) );
  68. AssignImageList( imageList );
  69. }
  70. HIERARCHY_NAVIG_DLG::HIERARCHY_NAVIG_DLG( SCH_EDIT_FRAME* aParent ) :
  71. DIALOG_SHIM( aParent, wxID_ANY, _( "Navigator" ), wxDefaultPosition, wxDefaultSize,
  72. wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER, HIERARCHY_NAVIG_DLG_WNAME )
  73. {
  74. wxASSERT( dynamic_cast< SCH_EDIT_FRAME* >( aParent ) );
  75. m_SchFrameEditor = aParent;
  76. m_currSheet = aParent->GetCurrentSheet();
  77. m_Tree = new HIERARCHY_TREE( this );
  78. m_nbsheets = 1;
  79. // root is the link to the main sheet.
  80. wxTreeItemId root = m_Tree->AddRoot( _( "Root" ), 0, 1 );
  81. m_Tree->SetItemBold( root, true );
  82. m_list.push_back( g_RootSheet );
  83. m_Tree->SetItemData( root, new TreeItemData( m_list ) );
  84. if( m_SchFrameEditor->GetCurrentSheet().Last() == g_RootSheet )
  85. m_Tree->SelectItem( root );
  86. buildHierarchyTree( &m_list, &root );
  87. m_Tree->ExpandAll();
  88. // This bloc gives a good size to the dialog, better than the default "best" size,
  89. // the first time the dialog is opened, during a session
  90. wxRect itemrect;
  91. wxSize tree_size;
  92. m_Tree->GetBoundingRect( root, itemrect );
  93. // Set dialog window size to be large enough
  94. tree_size.x = itemrect.GetWidth() + 20;
  95. tree_size.x = std::max( tree_size.x, 250 );
  96. // Readjust the size of the frame to an optimal value.
  97. tree_size.y = m_nbsheets * itemrect.GetHeight();
  98. if( m_nbsheets < 2 )
  99. tree_size.y += 10; // gives a better look for small trees
  100. SetClientSize( tree_size );
  101. // manage the ESC key to close the dialog, because thre is no Cancel button
  102. // in dialog
  103. m_Tree->Connect( wxEVT_CHAR, wxKeyEventHandler( HIERARCHY_TREE::onChar ) );
  104. // Manage double click on a selection, or the enter key:
  105. Bind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
  106. // Manage a simple click on a selection, if the selection changes
  107. Bind( wxEVT_TREE_SEL_CHANGED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
  108. // Connect close event for the dialog:
  109. this->Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( HIERARCHY_NAVIG_DLG::OnCloseNav ) );
  110. }
  111. HIERARCHY_NAVIG_DLG::~HIERARCHY_NAVIG_DLG()
  112. {
  113. Unbind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
  114. Unbind( wxEVT_TREE_SEL_CHANGED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
  115. m_Tree->Disconnect( wxEVT_CHAR, wxKeyEventHandler( HIERARCHY_TREE::onChar ) );
  116. this->Disconnect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( HIERARCHY_NAVIG_DLG::OnCloseNav ) );
  117. }
  118. void HIERARCHY_TREE::onChar( wxKeyEvent& event )
  119. {
  120. if( event.GetKeyCode() == WXK_ESCAPE )
  121. m_parent->Close( true );
  122. else
  123. event.Skip();
  124. }
  125. void HIERARCHY_NAVIG_DLG::buildHierarchyTree( SCH_SHEET_PATH* aList, wxTreeItemId* aPreviousmenu )
  126. {
  127. wxCHECK_RET( m_nbsheets < NB_MAX_SHEET, "Maximum number of sheets exceeded." );
  128. for( auto aItem : aList->LastScreen()->Items().OfType( SCH_SHEET_T ) )
  129. {
  130. SCH_SHEET* sheet = static_cast<SCH_SHEET*>( aItem );
  131. m_nbsheets++;
  132. wxTreeItemId menu;
  133. menu = m_Tree->AppendItem( *aPreviousmenu, sheet->GetName(), 0, 1 );
  134. aList->push_back( sheet );
  135. m_Tree->SetItemData( menu, new TreeItemData( *aList ) );
  136. if( *aList == m_currSheet )
  137. {
  138. m_Tree->EnsureVisible( menu );
  139. m_Tree->SelectItem( menu );
  140. }
  141. buildHierarchyTree( aList, &menu );
  142. aList->pop_back();
  143. if( m_nbsheets >= NB_MAX_SHEET )
  144. break;
  145. }
  146. }
  147. void HIERARCHY_NAVIG_DLG::UpdateHierarchyTree()
  148. {
  149. Freeze();
  150. m_currSheet = m_SchFrameEditor->GetCurrentSheet();
  151. wxTreeItemId root = m_Tree->GetRootItem();
  152. m_Tree->DeleteChildren( root );
  153. m_list.clear();
  154. m_list.push_back( g_RootSheet );
  155. buildHierarchyTree( &m_list, &root );
  156. Thaw();
  157. }
  158. void HIERARCHY_NAVIG_DLG::onSelectSheetPath( wxTreeEvent& event )
  159. {
  160. m_SchFrameEditor->GetToolManager()->RunAction( ACTIONS::cancelInteractive, true );
  161. m_SchFrameEditor->GetToolManager()->RunAction( EE_ACTIONS::clearSelection, true );
  162. wxTreeItemId ItemSel = m_Tree->GetSelection();
  163. m_SchFrameEditor->SetCurrentSheet(( (TreeItemData*) m_Tree->GetItemData( ItemSel ) )->m_SheetPath );
  164. m_SchFrameEditor->DisplayCurrentSheet();
  165. if( m_SchFrameEditor->GetNavigatorStaysOpen() == false )
  166. Close( true );
  167. }
  168. void HIERARCHY_NAVIG_DLG::OnCloseNav( wxCloseEvent& event )
  169. {
  170. Destroy();
  171. }
  172. void SCH_EDIT_FRAME::DisplayCurrentSheet()
  173. {
  174. m_toolManager->RunAction( ACTIONS::cancelInteractive, true );
  175. m_toolManager->RunAction( EE_ACTIONS::clearSelection, true );
  176. SCH_SCREEN* screen = g_CurrentSheet->LastScreen();
  177. wxASSERT( screen );
  178. // Switch to current sheet,
  179. // and update the grid size, because it can be modified in latest screen
  180. SetScreen( screen );
  181. GetScreen()->SetGrid( m_LastGridSizeId + ID_POPUP_GRID_LEVEL_1000 );
  182. // update the References
  183. g_CurrentSheet->UpdateAllScreenReferences();
  184. SetSheetNumberAndCount();
  185. if( !screen->m_Initialized )
  186. {
  187. m_toolManager->RunAction( ACTIONS::zoomFitScreen, true );
  188. screen->m_Initialized = true;
  189. screen->ClearUndoORRedoList( screen->m_UndoList, 1 );
  190. }
  191. else
  192. {
  193. // RedrawScreen() will set zoom to last used
  194. RedrawScreen( (wxPoint) GetScreen()->m_ScrollCenter, false );
  195. }
  196. UpdateTitle();
  197. SCH_EDITOR_CONTROL* editTool = m_toolManager->GetTool<SCH_EDITOR_CONTROL>();
  198. TOOL_EVENT dummy;
  199. editTool->UpdateNetHighlighting( dummy );
  200. HardRedraw(); // Ensure any item has its view updated, especially the worksheet items
  201. }