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.

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