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.

311 lines
9.1 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-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. /**
  26. * @file hierarch.cpp
  27. */
  28. #include <fctsys.h>
  29. #include <class_drawpanel.h>
  30. #include <confirm.h>
  31. #include <id.h>
  32. #include <bitmaps.h>
  33. #include <dialog_shim.h>
  34. #include <schframe.h>
  35. #include <general.h>
  36. #include <sch_sheet.h>
  37. #include <sch_sheet_path.h>
  38. #include <wx/imaglist.h>
  39. #include <wx/treectrl.h>
  40. #include <class_netlist_object.h>
  41. #include <sch_sheet_path.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(),
  77. tree_nosel_bm.GetHeight(), true, 2 );
  78. imageList->Add( tree_nosel_bm );
  79. imageList->Add( KiBitmap( tree_sel_xpm ) );
  80. AssignImageList( imageList );
  81. }
  82. class HIERARCHY_NAVIG_DLG : public DIALOG_SHIM
  83. {
  84. public:
  85. SCH_EDIT_FRAME* m_SchFrameEditor;
  86. HIERARCHY_TREE* m_Tree;
  87. int m_nbsheets;
  88. private:
  89. SCH_SHEET_PATH m_currSheet; // The currently opened scheet in hierarchy
  90. public:
  91. HIERARCHY_NAVIG_DLG( SCH_EDIT_FRAME* aParent, const wxPoint& aPos );
  92. ~HIERARCHY_NAVIG_DLG();
  93. // Select the sheet currently selected in the tree, and close the dialog
  94. void SelectNewSheetAndQuit();
  95. private:
  96. /**
  97. * Create the hierarchical tree of the schematic.
  98. *
  99. * This routine is reentrant!
  100. * @param aList = the SCH_SHEET_PATH* list to explore
  101. * @param aPreviousmenu = the wxTreeItemId used as parent to add sub items
  102. */
  103. void buildHierarchyTree( SCH_SHEET_PATH* aList, wxTreeItemId* aPreviousmenu );
  104. /**
  105. * Open the selected sheet and display the corresponding screen when a tree item is
  106. * selected.
  107. */
  108. void onSelectSheetPath( wxTreeEvent& event );
  109. };
  110. void SCH_EDIT_FRAME::InstallHierarchyFrame( wxPoint& pos )
  111. {
  112. HIERARCHY_NAVIG_DLG* treeframe = new HIERARCHY_NAVIG_DLG( this, pos );
  113. treeframe->ShowQuasiModal();
  114. treeframe->Destroy();
  115. }
  116. HIERARCHY_NAVIG_DLG::HIERARCHY_NAVIG_DLG( SCH_EDIT_FRAME* aParent, const wxPoint& aPos ) :
  117. DIALOG_SHIM( aParent, wxID_ANY, _( "Navigator" ), wxDefaultPosition, wxDefaultSize,
  118. wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
  119. {
  120. wxASSERT( dynamic_cast< SCH_EDIT_FRAME* >( aParent ) );
  121. m_SchFrameEditor = aParent;
  122. m_currSheet = aParent->GetCurrentSheet();
  123. m_Tree = new HIERARCHY_TREE( this );
  124. m_nbsheets = 1;
  125. // root is the link to the main sheet.
  126. wxTreeItemId root;
  127. root = m_Tree->AddRoot( _( "Root" ), 0, 1 );
  128. m_Tree->SetItemBold( root, true );
  129. SCH_SHEET_PATH list;
  130. list.push_back( g_RootSheet );
  131. m_Tree->SetItemData( root, new TreeItemData( list ) );
  132. if( m_SchFrameEditor->GetCurrentSheet().Last() == g_RootSheet )
  133. m_Tree->SelectItem( root );
  134. buildHierarchyTree( &list, &root );
  135. m_Tree->ExpandAll();
  136. // This bloc gives a good size to the dialog, better than the default "best" size,
  137. // the first time the dialog is opened, during a session
  138. wxRect itemrect;
  139. wxSize tree_size;
  140. m_Tree->GetBoundingRect( root, itemrect );
  141. // Set dialog window size to be large enough
  142. tree_size.x = itemrect.GetWidth() + 20;
  143. tree_size.x = std::max( tree_size.x, 250 );
  144. // Readjust the size of the frame to an optimal value.
  145. tree_size.y = m_nbsheets * itemrect.GetHeight();
  146. if( m_nbsheets < 2 )
  147. tree_size.y += 10; // gives a better look for small trees
  148. SetClientSize( tree_size );
  149. // manage the ESC key to close the dialog, because thre is no Cancel button
  150. // in dialog
  151. m_Tree->Connect( wxEVT_CHAR, wxKeyEventHandler( HIERARCHY_TREE::onChar ) );
  152. // Manage double click on a selection, or the enter key:
  153. Bind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
  154. // Manage a simple click on a selection, if the selection changes
  155. Bind( wxEVT_TREE_SEL_CHANGED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
  156. }
  157. HIERARCHY_NAVIG_DLG::~HIERARCHY_NAVIG_DLG()
  158. {
  159. Unbind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
  160. Unbind( wxEVT_TREE_SEL_CHANGED, &HIERARCHY_NAVIG_DLG::onSelectSheetPath, this );
  161. m_Tree->Disconnect( wxEVT_CHAR, wxKeyEventHandler( HIERARCHY_TREE::onChar ) );
  162. }
  163. void HIERARCHY_TREE::onChar( wxKeyEvent& event )
  164. {
  165. if( event.GetKeyCode() == WXK_ESCAPE )
  166. m_parent->Close( true );
  167. else
  168. event.Skip();
  169. }
  170. void HIERARCHY_NAVIG_DLG::buildHierarchyTree( SCH_SHEET_PATH* aList, wxTreeItemId* aPreviousmenu )
  171. {
  172. wxCHECK_RET( m_nbsheets < NB_MAX_SHEET, "Maximum number of sheets exceeded." );
  173. SCH_ITEM* schitem = aList->LastDrawList();
  174. while( schitem && m_nbsheets < NB_MAX_SHEET )
  175. {
  176. if( schitem->Type() == SCH_SHEET_T )
  177. {
  178. SCH_SHEET* sheet = (SCH_SHEET*) schitem;
  179. m_nbsheets++;
  180. wxTreeItemId menu;
  181. menu = m_Tree->AppendItem( *aPreviousmenu, sheet->GetName(), 0, 1 );
  182. aList->push_back( sheet );
  183. m_Tree->SetItemData( menu, new TreeItemData( *aList ) );
  184. if( *aList == m_currSheet )
  185. {
  186. m_Tree->EnsureVisible( menu );
  187. m_Tree->SelectItem( menu );
  188. }
  189. buildHierarchyTree( aList, &menu );
  190. aList->pop_back();
  191. }
  192. schitem = schitem->Next();
  193. }
  194. }
  195. void HIERARCHY_NAVIG_DLG::onSelectSheetPath( wxTreeEvent& event )
  196. {
  197. wxTreeItemId ItemSel = m_Tree->GetSelection();
  198. m_SchFrameEditor->SetCurrentSheet(( (TreeItemData*) m_Tree->GetItemData( ItemSel ) )->m_SheetPath );
  199. m_SchFrameEditor->DisplayCurrentSheet();
  200. Close( true );
  201. }
  202. void SCH_EDIT_FRAME::DisplayCurrentSheet()
  203. {
  204. SetRepeatItem( NULL );
  205. ClearMsgPanel();
  206. SCH_SCREEN* screen = m_CurrentSheet->LastScreen();
  207. // Switch to current sheet,
  208. // and update the grid size, because it can be modified in latest screen
  209. SetScreen( screen );
  210. GetScreen()->SetGrid( m_LastGridSizeId + ID_POPUP_GRID_LEVEL_1000 );
  211. // update the References
  212. m_CurrentSheet->UpdateAllScreenReferences();
  213. SetSheetNumberAndCount();
  214. m_canvas->SetCanStartBlock( -1 );
  215. if( screen->m_FirstRedraw )
  216. {
  217. Zoom_Automatique( false );
  218. screen->m_FirstRedraw = false;
  219. SetCrossHairPosition( GetScrollCenterPosition() );
  220. m_canvas->MoveCursorToCrossHair();
  221. // Ensure the schematic is fully segmented on first display
  222. BreakSegmentsOnJunctions();
  223. SchematicCleanUp( true );
  224. screen->ClearUndoORRedoList( screen->m_UndoList, 1 );
  225. screen->TestDanglingEnds();
  226. }
  227. else
  228. {
  229. RedrawScreen( GetScrollCenterPosition(), true );
  230. }
  231. // Some items (wires, labels) can be highlighted. So prepare the highlight flag:
  232. SetCurrentSheetHighlightFlags();
  233. // Now refresh m_canvas. Should be not necessary, but because screen has changed
  234. // the previous refresh has set all new draw parameters (scroll position ..)
  235. // but most of time there were some inconsitencies about cursor parameters
  236. // ( previous position of cursor ...) and artefacts can happen
  237. // mainly when sheet size has changed
  238. // This second refresh clears artefacts because at this point,
  239. // all parameters are now updated
  240. m_canvas->Refresh();
  241. }