KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_navigate_tool.cpp
Go to the documentation of this file.
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, see <https://www.gnu.org/licenses/>.
19 */
20
22#include <sch_edit_frame.h>
23#include <tool/tool_manager.h>
24#include <schematic.h>
25#include <eeschema_id.h>
26#include <tools/sch_actions.h>
28#include <common.h>
29#include "eda_doc.h"
30
31
32wxString SCH_NAVIGATE_TOOL::g_BackLink = wxT( "HYPERTEXT_BACK" );
33
34
36{
37 m_navHistory.clear();
38 m_navHistory.push_back( m_frame->GetCurrentSheet() );
39 m_navIndex = m_navHistory.begin();
40}
41
42
44{
45 wxCHECK( m_frame, /* void */ );
46
47 SCH_SHEET_LIST sheets = m_frame->Schematic().Hierarchy();
48
49 wxCHECK( !sheets.empty(), /* void */ );
50
51 // Search through our history, and removing any entries
52 // that the no longer point to a sheet on the schematic
53 auto entry = m_navHistory.begin();
54
55 while( entry != m_navHistory.end() )
56 {
57 if( std::find( sheets.begin(), sheets.end(), *entry ) != sheets.end() )
58 {
59 // Don't allow multiple consecutive instances of the same history.
60 if( ( entry != m_navHistory.begin() ) && ( *entry == *std::prev( entry ) ) )
61 entry = m_navHistory.erase( entry );
62 else
63 ++entry;
64 }
65 else
66 {
67 entry = m_navHistory.erase( entry );
68 }
69 }
70 if( m_navHistory.size() <= 1 )
71 m_navIndex = m_navHistory.begin();
72 else
73 m_navIndex = --m_navHistory.end();
74}
75
76
77void SCH_NAVIGATE_TOOL::HypertextCommand( const wxString& aHref )
78{
79 wxString destPage;
80 wxString href = ResolveUriByEnvVars( aHref, &m_frame->Prj() );
81
83 {
85 Back( dummy );
86 }
87 else if( EDA_TEXT::IsGotoPageHref( href, &destPage ) && !destPage.IsEmpty() )
88 {
89 for( const SCH_SHEET_PATH& sheet : m_frame->Schematic().Hierarchy() )
90 {
91 if( sheet.GetPageNumber() == destPage )
92 {
93 changeSheet( sheet );
94 return;
95 }
96 }
97
98 m_frame->ShowInfoBarError( wxString::Format( _( "Page '%s' not found." ), destPage ) );
99 }
100 else
101 {
102 wxMenu menu;
103
104 menu.Append( 1, wxString::Format( _( "Open %s" ), href ) );
105
106 if( m_frame->GetPopupMenuSelectionFromUser( menu ) == 1 )
107 GetAssociatedDocument( m_frame, href, &m_frame->Prj(), nullptr, { &m_frame->Schematic() } );
108 }
109}
110
111
113{
114 // Checks for CanGoUp()
115 LeaveSheet( aEvent );
116 return 0;
117}
118
119
121{
122 if( CanGoForward() )
123 {
124 m_navIndex++;
125
126 m_frame->GetToolManager()->RunAction( ACTIONS::cancelInteractive );
127 m_frame->GetToolManager()->RunAction( ACTIONS::selectionClear );
128
129 m_frame->SetCurrentSheet( *m_navIndex );
130 m_frame->DisplayCurrentSheet();
131 }
132 else
133 {
134 wxBell();
135 }
136
137 return 0;
138}
139
140
142{
143 if( CanGoBack() )
144 {
145 m_navIndex--;
146
147 m_frame->GetToolManager()->RunAction( ACTIONS::cancelInteractive );
148 m_frame->GetToolManager()->RunAction( ACTIONS::selectionClear );
149
150 m_frame->SetCurrentSheet( *m_navIndex );
151 m_frame->DisplayCurrentSheet();
152 }
153 else
154 {
155 wxBell();
156 }
157
158 return 0;
159}
160
161
163{
164 if( CanGoPrevious() )
165 {
166 int targetSheet = m_frame->GetCurrentSheet().GetVirtualPageNumber() - 1;
167 changeSheet( m_frame->Schematic().Hierarchy().at( targetSheet - 1 ) );
168 }
169 else
170 {
171 wxBell();
172 }
173
174 return 0;
175}
176
177
179{
180 if( CanGoNext() )
181 {
182 int targetSheet = m_frame->GetCurrentSheet().GetVirtualPageNumber() + 1;
183 changeSheet( m_frame->Schematic().Hierarchy().at( targetSheet - 1 ) );
184 }
185 else
186 {
187 wxBell();
188 }
189
190 return 0;
191}
192
193
195{
196 return m_navHistory.size() > 0 && m_navIndex != m_navHistory.begin();
197}
198
199
201{
202 return m_navHistory.size() > 0 && m_navIndex != --m_navHistory.end();
203}
204
205
207{
208 std::vector<SCH_SHEET*> topLevelSheets = m_frame->Schematic().GetTopLevelSheets();
209
210 for( SCH_SHEET* top_sheet : topLevelSheets )
211 {
212 if( m_frame->GetCurrentSheet().Last() == top_sheet )
213 return false;
214 }
215
216 return true;
217}
218
219
221{
222 return m_frame->GetCurrentSheet().GetVirtualPageNumber() > 1;
223}
224
225
227{
228 if( !m_frame->Schematic().IsValid() )
229 return false;
230
231 return m_frame->GetCurrentSheet().GetVirtualPageNumber()
232 < (int) m_frame->Schematic().Hierarchy().size();
233}
234
235
237{
239 wxCHECK( path, 0 );
240
241 changeSheet( *path );
242
243 return 0;
244}
245
246
248{
249 SCH_SELECTION_TOOL* selTool = m_toolMgr->GetTool<SCH_SELECTION_TOOL>();
250 const SCH_SELECTION& selection = selTool->RequestSelection( { SCH_SHEET_T } );
251
252 if( selection.GetSize() == 1 )
253 {
254 SCH_SHEET_PATH pushed = m_frame->GetCurrentSheet();
255 pushed.push_back( (SCH_SHEET*) selection.Front() );
256
257 changeSheet( pushed );
258 }
259
260 return 0;
261}
262
263
265{
266 if( CanGoUp() )
267 {
268 SCH_SHEET_PATH popped = m_frame->GetCurrentSheet();
269 popped.pop_back();
270
271 changeSheet( popped );
272 }
273 else
274 {
275 wxBell();
276 }
277
278 return 0;
279}
280
281
295
296
298{
299 if( CanGoForward() )
300 m_navHistory.erase( std::next( m_navIndex ), m_navHistory.end() );
301
302 if( m_navHistory.empty() || ( *(--m_navHistory.end()) != aPath ) )
303 m_navHistory.push_back( aPath );
304
305 m_navIndex = --m_navHistory.end();
306}
307
308
310{
311 m_frame->GetToolManager()->RunAction( ACTIONS::cancelInteractive );
312 m_frame->GetToolManager()->RunAction( ACTIONS::selectionClear );
313
314 // Store the current zoom level into the current screen before switching
315 m_frame->GetScreen()->m_LastZoomLevel = m_frame->GetCanvas()->GetView()->GetScale();
316
317 pushToHistory( aPath );
318
319 m_frame->ClearFocus();
320 m_frame->Schematic().SetCurrentSheet( aPath );
321 m_frame->DisplayCurrentSheet();
322}
static TOOL_ACTION cancelInteractive
Definition actions.h:68
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:220
static bool IsGotoPageHref(const wxString &aHref, wxString *aDestination=nullptr)
Check if aHref is a valid internal hyperlink.
static TOOL_ACTION navigateBack
static TOOL_ACTION leaveSheet
static TOOL_ACTION navigateNext
static TOOL_ACTION navigateForward
static TOOL_ACTION navigatePrevious
static TOOL_ACTION changeSheet
static TOOL_ACTION enterSheet
static TOOL_ACTION navigateUp
void ResetHistory()
Reset navigation history. Must be done when schematic changes.
void CleanHistory()
Remove deleted pages from history.
int Back(const TOOL_EVENT &aEvent)
Navigate back in sheet history.
int Previous(const TOOL_EVENT &aEvent)
Navigate to previous sheet by numeric sheet number.
int Next(const TOOL_EVENT &aEvent)
Navigate to next sheet by numeric sheet number.
std::list< SCH_SHEET_PATH >::iterator m_navIndex
void changeSheet(const SCH_SHEET_PATH &aPath)
Change current sheet to aPath and handle history, zooming, etc.
int ChangeSheet(const TOOL_EVENT &aEvent)
Enter sheet provided in aEvent.
int Forward(const TOOL_EVENT &aEvent)
Navigate forward in sheet history.
int EnterSheet(const TOOL_EVENT &aEvent)
Enter selected sheet.
static wxString g_BackLink
void pushToHistory(const SCH_SHEET_PATH &aPath)
Clear history after this nav index and pushes aPath to history.
void HypertextCommand(const wxString &aHref)
int Up(const TOOL_EVENT &aEvent)
Navigate up in sheet hierarchy.
void setTransitions() override
Set up handlers for various events.
std::list< SCH_SHEET_PATH > m_navHistory
int LeaveSheet(const TOOL_EVENT &aEvent)
Return to parent sheet. Synonymous with Up.
SCH_SELECTION & RequestSelection(const std::vector< KICAD_T > &aScanTypes={ SCH_LOCATE_ANY_T }, bool aPromoteCellSelections=false, bool aPromoteGroups=false)
Return either an existing selection (filtered), or the selection at the current cursor position if th...
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
void pop_back()
Forwarded method from std::vector.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
Generic, UI-independent tool event.
Definition tool_event.h:167
T Parameter() const
Return a parameter assigned to the event.
Definition tool_event.h:484
void Go(int(SCH_EDIT_FRAME::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
const wxString ResolveUriByEnvVars(const wxString &aUri, const PROJECT *aProject)
Replace any environment and/or text variables in URIs.
Definition common.cpp:789
#define _(s)
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, PROJECT *aProject, SEARCH_STACK *aPaths, std::vector< EMBEDDED_FILES * > aFilesStack)
Open a document (file) with the suitable browser.
Definition eda_doc.cpp:59
This file is part of the common library.
std::vector< FAB_LAYER_COLOR > dummy
std::string path
@ SCH_SHEET_T
Definition typeinfo.h:171