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.

266 lines
7.1 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-2022 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. #include "eda_doc.h"
  29. wxString SCH_NAVIGATE_TOOL::g_BackLink = wxT( "HYPERTEXT_BACK" );
  30. void SCH_NAVIGATE_TOOL::ResetHistory()
  31. {
  32. m_navHistory.clear();
  33. m_navHistory.push_back( m_frame->GetCurrentSheet() );
  34. m_navIndex = m_navHistory.begin();
  35. }
  36. void SCH_NAVIGATE_TOOL::CleanHistory()
  37. {
  38. SCH_SHEET_LIST sheets = m_frame->Schematic().GetSheets();
  39. // Search through our history, and removing any entries
  40. // that the no longer point to a sheet on the schematic
  41. auto entry = m_navHistory.begin();
  42. while( entry != m_navHistory.end() )
  43. {
  44. if( std::find( sheets.begin(), sheets.end(), *entry ) != sheets.end() )
  45. ++entry;
  46. else
  47. entry = m_navHistory.erase( entry );
  48. }
  49. }
  50. void SCH_NAVIGATE_TOOL::HypertextCommand( const wxString& href )
  51. {
  52. wxString destPage;
  53. if( href == SCH_NAVIGATE_TOOL::g_BackLink )
  54. {
  55. TOOL_EVENT dummy;
  56. Back( dummy );
  57. }
  58. else if( EDA_TEXT::IsGotoPageHref( href, &destPage ) && !destPage.IsEmpty() )
  59. {
  60. for( const SCH_SHEET_PATH& sheet : m_frame->Schematic().GetSheets() )
  61. {
  62. if( sheet.GetPageNumber() == destPage )
  63. {
  64. changeSheet( sheet );
  65. return;
  66. }
  67. }
  68. m_frame->ShowInfoBarError( wxString::Format( _( "Page '%s' not found." ), destPage ) );
  69. }
  70. else
  71. {
  72. wxMenu menu;
  73. menu.Append( 1, wxString::Format( _( "Open %s" ), href ) );
  74. if( m_frame->GetPopupMenuSelectionFromUser( menu ) == 1 )
  75. GetAssociatedDocument( m_frame, href, &m_frame->Prj() );
  76. }
  77. }
  78. int SCH_NAVIGATE_TOOL::Up( const TOOL_EVENT& aEvent )
  79. {
  80. // Checks for CanGoUp()
  81. LeaveSheet( aEvent );
  82. return 0;
  83. }
  84. int SCH_NAVIGATE_TOOL::Forward( const TOOL_EVENT& aEvent )
  85. {
  86. if( CanGoForward() )
  87. {
  88. m_navIndex++;
  89. m_frame->GetToolManager()->RunAction( ACTIONS::cancelInteractive, true );
  90. m_frame->GetToolManager()->RunAction( EE_ACTIONS::clearSelection, true );
  91. m_frame->SetCurrentSheet( *m_navIndex );
  92. m_frame->DisplayCurrentSheet();
  93. }
  94. return 0;
  95. }
  96. int SCH_NAVIGATE_TOOL::Back( const TOOL_EVENT& aEvent )
  97. {
  98. if( CanGoBack() )
  99. {
  100. m_navIndex--;
  101. m_frame->GetToolManager()->RunAction( ACTIONS::cancelInteractive, true );
  102. m_frame->GetToolManager()->RunAction( EE_ACTIONS::clearSelection, true );
  103. m_frame->SetCurrentSheet( *m_navIndex );
  104. m_frame->DisplayCurrentSheet();
  105. }
  106. return 0;
  107. }
  108. int SCH_NAVIGATE_TOOL::Previous( const TOOL_EVENT& aEvent )
  109. {
  110. if( CanGoPrevious() )
  111. changeSheet( m_frame->Schematic().GetSheets().at(
  112. m_frame->GetCurrentSheet().GetVirtualPageNumber() - 2 ) );
  113. return 0;
  114. }
  115. int SCH_NAVIGATE_TOOL::Next( const TOOL_EVENT& aEvent )
  116. {
  117. if( CanGoNext() )
  118. changeSheet( m_frame->Schematic().GetSheets().at(
  119. m_frame->GetCurrentSheet().GetVirtualPageNumber() ) );
  120. return 0;
  121. }
  122. bool SCH_NAVIGATE_TOOL::CanGoBack()
  123. {
  124. return m_navHistory.size() > 0 && m_navIndex != m_navHistory.begin();
  125. }
  126. bool SCH_NAVIGATE_TOOL::CanGoForward()
  127. {
  128. return m_navHistory.size() > 0 && m_navIndex != --m_navHistory.end();
  129. }
  130. bool SCH_NAVIGATE_TOOL::CanGoUp()
  131. {
  132. return m_frame->GetCurrentSheet().Last() != &m_frame->Schematic().Root();
  133. }
  134. bool SCH_NAVIGATE_TOOL::CanGoPrevious()
  135. {
  136. return m_frame->GetCurrentSheet().GetVirtualPageNumber() > 1;
  137. }
  138. bool SCH_NAVIGATE_TOOL::CanGoNext()
  139. {
  140. return m_frame->GetCurrentSheet().GetVirtualPageNumber()
  141. < (int) m_frame->Schematic().GetSheets().size();
  142. }
  143. int SCH_NAVIGATE_TOOL::ChangeSheet( const TOOL_EVENT& aEvent )
  144. {
  145. SCH_SHEET_PATH* path = aEvent.Parameter<SCH_SHEET_PATH*>();
  146. wxCHECK( path, 0 );
  147. changeSheet( *path );
  148. return 0;
  149. }
  150. int SCH_NAVIGATE_TOOL::EnterSheet( const TOOL_EVENT& aEvent )
  151. {
  152. EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
  153. const EE_SELECTION& selection = selTool->RequestSelection( { SCH_SHEET_T } );
  154. if( selection.GetSize() == 1 )
  155. {
  156. SCH_SHEET_PATH pushed = m_frame->GetCurrentSheet();
  157. pushed.push_back( (SCH_SHEET*) selection.Front() );
  158. changeSheet( pushed );
  159. }
  160. return 0;
  161. }
  162. int SCH_NAVIGATE_TOOL::LeaveSheet( const TOOL_EVENT& aEvent )
  163. {
  164. if( CanGoUp() )
  165. {
  166. SCH_SHEET_PATH popped = m_frame->GetCurrentSheet();
  167. popped.pop_back();
  168. changeSheet( popped );
  169. }
  170. return 0;
  171. }
  172. void SCH_NAVIGATE_TOOL::setTransitions()
  173. {
  174. Go( &SCH_NAVIGATE_TOOL::ChangeSheet, EE_ACTIONS::changeSheet.MakeEvent() );
  175. Go( &SCH_NAVIGATE_TOOL::EnterSheet, EE_ACTIONS::enterSheet.MakeEvent() );
  176. Go( &SCH_NAVIGATE_TOOL::LeaveSheet, EE_ACTIONS::leaveSheet.MakeEvent() );
  177. Go( &SCH_NAVIGATE_TOOL::Up, EE_ACTIONS::navigateUp.MakeEvent() );
  178. Go( &SCH_NAVIGATE_TOOL::Forward, EE_ACTIONS::navigateForward.MakeEvent() );
  179. Go( &SCH_NAVIGATE_TOOL::Back, EE_ACTIONS::navigateBack.MakeEvent() );
  180. Go( &SCH_NAVIGATE_TOOL::Previous, EE_ACTIONS::navigatePrevious.MakeEvent() );
  181. Go( &SCH_NAVIGATE_TOOL::Next, EE_ACTIONS::navigateNext.MakeEvent() );
  182. }
  183. void SCH_NAVIGATE_TOOL::pushToHistory( SCH_SHEET_PATH aPath )
  184. {
  185. if( CanGoForward() )
  186. m_navHistory.erase( std::next( m_navIndex ), m_navHistory.end() );
  187. m_navHistory.push_back( aPath );
  188. m_navIndex = --m_navHistory.end();
  189. }
  190. void SCH_NAVIGATE_TOOL::changeSheet( SCH_SHEET_PATH aPath )
  191. {
  192. m_frame->GetToolManager()->RunAction( ACTIONS::cancelInteractive, true );
  193. m_frame->GetToolManager()->RunAction( EE_ACTIONS::clearSelection, true );
  194. // Store the current zoom level into the current screen before switching
  195. m_frame->GetScreen()->m_LastZoomLevel = m_frame->GetCanvas()->GetView()->GetScale();
  196. pushToHistory( aPath );
  197. m_frame->SetCurrentSheet( aPath );
  198. m_frame->DisplayCurrentSheet();
  199. }