KiCad PCB EDA Suite
Loading...
Searching...
No Matches
zoom_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) 2017-2022 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21#include <eda_draw_frame.h>
23#include <tool/actions.h>
24#include <tool/tool_manager.h>
25#include <tool/zoom_tool.h>
26#include <view/view.h>
27#include <view/view_controls.h>
28#include <math/vector2wx.h>
29
30
32 TOOL_INTERACTIVE( "common.Control.zoomTool" )
33{
34 m_frame = nullptr;
35}
36
37
39
40
41
43{
44 auto& ctxMenu = m_menu->GetMenu();
45
46 // cancel current tool goes in main context menu at the top if present
48 ctxMenu.AddSeparator( 1 );
49
50 // Finally, add the standard zoom/grid items
51 getEditFrame<EDA_DRAW_FRAME>()->AddStandardSubMenus( *m_menu.get() );
52
53 return true;
54}
55
56
58{
59 m_frame = getEditFrame<EDA_DRAW_FRAME>();
60}
61
62
63int ZOOM_TOOL::Main( const TOOL_EVENT& aEvent )
64{
65 m_frame->PushTool( aEvent );
66
67 auto setCursor =
68 [&]()
69 {
70 m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::ZOOM_IN );
71 };
72
73 // Set initial cursor
74 setCursor();
75
76 while( TOOL_EVENT* evt = Wait() )
77 {
78 setCursor();
79
80 if( evt->IsCancelInteractive() || evt->IsActivate() )
81 {
82 break;
83 }
84 else if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
85 {
86 if( selectRegion() )
87 break;
88 }
89 else if( evt->IsClick( BUT_RIGHT ) )
90 {
92 m_menu->ShowContextMenu( dummy );
93 }
94 else
95 {
96 evt->SetPassEvent();
97 }
98 }
99
100 // Exit zoom tool
101 m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
102 m_frame->PopTool( aEvent );
103 return 0;
104}
105
106
108{
109 bool cancelled = false;
110 KIGFX::VIEW* view = getView();
112
113 getViewControls()->SetAutoPan( true );
114
116 view->Add( &area );
117
118 while( TOOL_EVENT* evt = Wait() )
119 {
120 if( evt->IsCancelInteractive() || evt->IsActivate() )
121 {
122 cancelled = true;
123 break;
124 }
125
126 if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
127 {
128 area.SetOrigin( evt->DragOrigin() );
129 area.SetEnd( evt->Position() );
130 view->SetVisible( &area, true );
131 view->Update( &area, KIGFX::GEOMETRY );
132 }
133
134 if( evt->IsMouseUp( BUT_LEFT ) || evt->IsMouseUp( BUT_RIGHT ) )
135 {
136 view->SetVisible( &area, false );
137 auto selectionBox = area.ViewBBox();
138
139 if( selectionBox.GetWidth() == 0 || selectionBox.GetHeight() == 0 )
140 {
141 break;
142 }
143 else
144 {
145 VECTOR2D sSize = view->ToWorld( ToVECTOR2I( canvas->GetClientSize() ), false );
146 VECTOR2D vSize = selectionBox.GetSize();
147 double scale;
148 double ratio = std::max( fabs( vSize.x / sSize.x ), fabs( vSize.y / sSize.y ) );
149
150 if( evt->IsMouseUp( BUT_LEFT ) )
151 scale = view->GetScale() / ratio;
152 else
153 scale = view->GetScale() * ratio;
154
155 view->SetScale( scale );
156 view->SetCenter( selectionBox.Centre() );
157
158 break;
159 }
160 }
161 }
162
163 view->SetVisible( &area, false );
164 view->Remove( &area );
165 getViewControls()->SetAutoPan( false );
166
167 return cancelled;
168}
169
170
172{
173 Go( &ZOOM_TOOL::Main, ACTIONS::zoomTool.MakeEvent() );
174}
static TOOL_ACTION cancelInteractive
Definition: actions.h:65
static TOOL_ACTION zoomTool
Definition: actions.h:138
virtual EDA_DRAW_PANEL_GAL * GetCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
void SetCurrentCursor(KICURSOR aCursor)
Set the current cursor shape for this panel.
Represent a selection area (currently a rectangle) in a VIEW, drawn corner-to-corner between two poin...
void SetOrigin(const VECTOR2I &aOrigin)
const BOX2I ViewBBox() const override
Set the origin of the rectangle (the fixed corner)
void SetEnd(const VECTOR2I &aEnd)
Set the current end of the rectangle (the corner that moves with the cursor.
virtual void SetAutoPan(bool aEnabled)
Turn on/off auto panning (this feature is used when there is a tool active (eg.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:68
double GetScale() const
Definition: view.h:277
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition: view.cpp:587
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:317
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition: view.cpp:357
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: view.cpp:1687
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Converts a screen space point/vector to a point/vector in world space coordinates.
Definition: view.cpp:484
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition: view.cpp:613
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1614
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
virtual void PopTool(const TOOL_EVENT &aEvent)
Pops a tool from the stack.
virtual void PushTool(const TOOL_EVENT &aEvent)
NB: the definition of "tool" is different at the user level.
KIGFX::VIEW_CONTROLS * getViewControls() const
Return the instance of VIEW_CONTROLS object used in the application.
Definition: tool_base.cpp:42
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:36
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
Generic, UI-independent tool event.
Definition: tool_event.h:167
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
std::unique_ptr< TOOL_MENU > m_menu
The functions below are not yet implemented - their interface may change.
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Suspend execution of the tool until an event specified in aEventList arrives.
bool Init() override
Init() is called once upon a registration of the tool.
Definition: zoom_tool.cpp:42
void setTransitions() override
Definition: zoom_tool.cpp:171
bool selectRegion()
Definition: zoom_tool.cpp:107
EDA_DRAW_FRAME * m_frame
Definition: zoom_tool.h:48
int Main(const TOOL_EVENT &aEvent)
Main loop.
Definition: zoom_tool.cpp:63
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
Definition: zoom_tool.cpp:57
@ GEOMETRY
Position or shape has changed.
Definition: view_item.h:54
const int scale
std::vector< FAB_LAYER_COLOR > dummy
@ BUT_LEFT
Definition: tool_event.h:131
@ BUT_RIGHT
Definition: tool_event.h:132
VECTOR2I ToVECTOR2I(const wxSize &aSize)
Definition: vector2wx.h:30