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.

257 lines
6.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 CERN
  5. * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <schematic.h>
  25. #include <eeschema_id.h>
  26. #include <tools/ee_actions.h>
  27. #include <tools/sch_navigate_tool.h>
  28. void SCH_NAVIGATE_TOOL::ResetHistory()
  29. {
  30. m_navHistory.clear();
  31. m_navHistory.push_back( m_frame->GetCurrentSheet() );
  32. m_navIndex = m_navHistory.begin();
  33. }
  34. void SCH_NAVIGATE_TOOL::CleanHistory()
  35. {
  36. SCH_SHEET_LIST sheets = m_frame->Schematic().GetSheets();
  37. // Search through our history, and removing any entries
  38. // that the no longer point to a sheet on the schematic
  39. auto entry = m_navHistory.begin();
  40. while( entry != m_navHistory.end() )
  41. {
  42. if( std::find( sheets.begin(), sheets.end(), *entry ) != sheets.end() )
  43. ++entry;
  44. else
  45. entry = m_navHistory.erase( entry );
  46. }
  47. }
  48. int SCH_NAVIGATE_TOOL::HypertextCommand( const TOOL_EVENT& aEvent )
  49. {
  50. wxString* page = aEvent.Parameter<wxString*>();
  51. wxCHECK( page, 0 );
  52. auto goToPage =
  53. [&]( wxString* aPage )
  54. {
  55. for( const SCH_SHEET_PATH& sheet : m_frame->Schematic().GetSheets() )
  56. {
  57. if( sheet.GetPageNumber() == *aPage )
  58. {
  59. changeSheet( sheet );
  60. return;
  61. }
  62. }
  63. };
  64. if( *page == "HYPERTEXT_BACK" )
  65. Back( aEvent );
  66. else
  67. goToPage( page );
  68. return 0;
  69. }
  70. int SCH_NAVIGATE_TOOL::Up( const TOOL_EVENT& aEvent )
  71. {
  72. // Checks for CanGoUp()
  73. LeaveSheet( aEvent );
  74. return 0;
  75. }
  76. int SCH_NAVIGATE_TOOL::Forward( const TOOL_EVENT& aEvent )
  77. {
  78. if( CanGoForward() )
  79. {
  80. m_navIndex++;
  81. m_frame->GetToolManager()->RunAction( ACTIONS::cancelInteractive, true );
  82. m_frame->GetToolManager()->RunAction( EE_ACTIONS::clearSelection, true );
  83. m_frame->SetCurrentSheet( *m_navIndex );
  84. m_frame->DisplayCurrentSheet();
  85. }
  86. return 0;
  87. }
  88. int SCH_NAVIGATE_TOOL::Back( const TOOL_EVENT& aEvent )
  89. {
  90. if( CanGoBack() )
  91. {
  92. m_navIndex--;
  93. m_frame->GetToolManager()->RunAction( ACTIONS::cancelInteractive, true );
  94. m_frame->GetToolManager()->RunAction( EE_ACTIONS::clearSelection, true );
  95. m_frame->SetCurrentSheet( *m_navIndex );
  96. m_frame->DisplayCurrentSheet();
  97. }
  98. return 0;
  99. }
  100. int SCH_NAVIGATE_TOOL::Previous( const TOOL_EVENT& aEvent )
  101. {
  102. if( CanGoPrevious() )
  103. changeSheet( m_frame->Schematic().GetSheets().at(
  104. m_frame->GetCurrentSheet().GetVirtualPageNumber() - 2 ) );
  105. return 0;
  106. }
  107. int SCH_NAVIGATE_TOOL::Next( const TOOL_EVENT& aEvent )
  108. {
  109. if( CanGoNext() )
  110. changeSheet( m_frame->Schematic().GetSheets().at(
  111. m_frame->GetCurrentSheet().GetVirtualPageNumber() ) );
  112. return 0;
  113. }
  114. bool SCH_NAVIGATE_TOOL::CanGoBack()
  115. {
  116. return m_navHistory.size() > 0 && m_navIndex != m_navHistory.begin();
  117. }
  118. bool SCH_NAVIGATE_TOOL::CanGoForward()
  119. {
  120. return m_navHistory.size() > 0 && m_navIndex != --m_navHistory.end();
  121. }
  122. bool SCH_NAVIGATE_TOOL::CanGoUp()
  123. {
  124. return m_frame->GetCurrentSheet().Last() != &m_frame->Schematic().Root();
  125. }
  126. bool SCH_NAVIGATE_TOOL::CanGoPrevious()
  127. {
  128. return m_frame->GetCurrentSheet().GetVirtualPageNumber() > 1;
  129. }
  130. bool SCH_NAVIGATE_TOOL::CanGoNext()
  131. {
  132. return m_frame->GetCurrentSheet().GetVirtualPageNumber()
  133. < (int) m_frame->Schematic().GetSheets().size();
  134. }
  135. int SCH_NAVIGATE_TOOL::ChangeSheet( const TOOL_EVENT& aEvent )
  136. {
  137. SCH_SHEET_PATH* path = aEvent.Parameter<SCH_SHEET_PATH*>();
  138. wxCHECK( path, 0 );
  139. changeSheet( *path );
  140. return 0;
  141. }
  142. int SCH_NAVIGATE_TOOL::EnterSheet( const TOOL_EVENT& aEvent )
  143. {
  144. EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
  145. const EE_SELECTION& selection = selTool->RequestSelection( EE_COLLECTOR::SheetsOnly );
  146. if( selection.GetSize() == 1 )
  147. {
  148. SCH_SHEET_PATH pushed = m_frame->GetCurrentSheet();
  149. pushed.push_back( (SCH_SHEET*) selection.Front() );
  150. changeSheet( pushed );
  151. }
  152. return 0;
  153. }
  154. int SCH_NAVIGATE_TOOL::LeaveSheet( const TOOL_EVENT& aEvent )
  155. {
  156. if( CanGoUp() )
  157. {
  158. SCH_SHEET_PATH popped = m_frame->GetCurrentSheet();
  159. popped.pop_back();
  160. changeSheet( popped );
  161. }
  162. return 0;
  163. }
  164. void SCH_NAVIGATE_TOOL::setTransitions()
  165. {
  166. Go( &SCH_NAVIGATE_TOOL::ChangeSheet, EE_ACTIONS::changeSheet.MakeEvent() );
  167. Go( &SCH_NAVIGATE_TOOL::EnterSheet, EE_ACTIONS::enterSheet.MakeEvent() );
  168. Go( &SCH_NAVIGATE_TOOL::LeaveSheet, EE_ACTIONS::leaveSheet.MakeEvent() );
  169. Go( &SCH_NAVIGATE_TOOL::HypertextCommand, EE_ACTIONS::hypertextCommand.MakeEvent() );
  170. Go( &SCH_NAVIGATE_TOOL::Up, EE_ACTIONS::navigateUp.MakeEvent() );
  171. Go( &SCH_NAVIGATE_TOOL::Forward, EE_ACTIONS::navigateForward.MakeEvent() );
  172. Go( &SCH_NAVIGATE_TOOL::Back, EE_ACTIONS::navigateBack.MakeEvent() );
  173. Go( &SCH_NAVIGATE_TOOL::Previous, EE_ACTIONS::navigatePrevious.MakeEvent() );
  174. Go( &SCH_NAVIGATE_TOOL::Next, EE_ACTIONS::navigateNext.MakeEvent() );
  175. }
  176. void SCH_NAVIGATE_TOOL::pushToHistory( SCH_SHEET_PATH aPath )
  177. {
  178. if( CanGoForward() )
  179. m_navHistory.erase( std::next( m_navIndex ), m_navHistory.end() );
  180. m_navHistory.push_back( aPath );
  181. m_navIndex = --m_navHistory.end();
  182. }
  183. void SCH_NAVIGATE_TOOL::changeSheet( SCH_SHEET_PATH aPath )
  184. {
  185. m_frame->GetToolManager()->RunAction( ACTIONS::cancelInteractive, true );
  186. m_frame->GetToolManager()->RunAction( EE_ACTIONS::clearSelection, true );
  187. // Store the current zoom level into the current screen before switching
  188. m_frame->GetScreen()->m_LastZoomLevel = m_frame->GetCanvas()->GetView()->GetScale();
  189. pushToHistory( aPath );
  190. m_frame->SetCurrentSheet( aPath );
  191. m_frame->DisplayCurrentSheet();
  192. }