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 <math/vector2wx.h>
28
29
31 TOOL_INTERACTIVE( "common.Control.zoomTool" )
32{
33 m_frame = nullptr;
34}
35
36
38
39
40
42{
43 auto& ctxMenu = m_menu.GetMenu();
44
45 // cancel current tool goes in main context menu at the top if present
47 ctxMenu.AddSeparator( 1 );
48
49 // Finally, add the standard zoom/grid items
50 getEditFrame<EDA_DRAW_FRAME>()->AddStandardSubMenus( m_menu );
51
52 return true;
53}
54
55
57{
58 m_frame = getEditFrame<EDA_DRAW_FRAME>();
59}
60
61
62int ZOOM_TOOL::Main( const TOOL_EVENT& aEvent )
63{
64 m_frame->PushTool( aEvent );
65
66 auto setCursor =
67 [&]()
68 {
69 m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::ZOOM_IN );
70 };
71
72 // Set initial cursor
73 setCursor();
74
75 while( TOOL_EVENT* evt = Wait() )
76 {
77 setCursor();
78
79 if( evt->IsCancelInteractive() || evt->IsActivate() )
80 {
81 break;
82 }
83 else if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
84 {
85 if( selectRegion() )
86 break;
87 }
88 else if( evt->IsClick( BUT_RIGHT ) )
89 {
92 }
93 else
94 {
95 evt->SetPassEvent();
96 }
97 }
98
99 // Exit zoom tool
100 m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
101 m_frame->PopTool( aEvent );
102 return 0;
103}
104
105
107{
108 bool cancelled = false;
109 KIGFX::VIEW* view = getView();
111
112 getViewControls()->SetAutoPan( true );
113
115 view->Add( &area );
116
117 while( TOOL_EVENT* evt = Wait() )
118 {
119 if( evt->IsCancelInteractive() || evt->IsActivate() )
120 {
121 cancelled = true;
122 break;
123 }
124
125 if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
126 {
127 area.SetOrigin( evt->DragOrigin() );
128 area.SetEnd( evt->Position() );
129 view->SetVisible( &area, true );
130 view->Update( &area, KIGFX::GEOMETRY );
131 }
132
133 if( evt->IsMouseUp( BUT_LEFT ) || evt->IsMouseUp( BUT_RIGHT ) )
134 {
135 view->SetVisible( &area, false );
136 auto selectionBox = area.ViewBBox();
137
138 if( selectionBox.GetWidth() == 0 || selectionBox.GetHeight() == 0 )
139 {
140 break;
141 }
142 else
143 {
144 VECTOR2D sSize = view->ToWorld( ToVECTOR2I( canvas->GetClientSize() ), false );
145 VECTOR2D vSize = selectionBox.GetSize();
146 double scale;
147 double ratio = std::max( fabs( vSize.x / sSize.x ), fabs( vSize.y / sSize.y ) );
148
149 if( evt->IsMouseUp( BUT_LEFT ) )
150 scale = view->GetScale() / ratio;
151 else
152 scale = view->GetScale() * ratio;
153
154 view->SetScale( scale );
155 view->SetCenter( selectionBox.Centre() );
156
157 break;
158 }
159 }
160 }
161
162 view->SetVisible( &area, false );
163 view->Remove( &area );
164 getViewControls()->SetAutoPan( false );
165
166 return cancelled;
167}
168
169
171{
172 Go( &ZOOM_TOOL::Main, ACTIONS::zoomTool.MakeEvent() );
173}
static TOOL_ACTION cancelInteractive
Definition: actions.h:63
static TOOL_ACTION zoomTool
Definition: actions.h:127
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Add a menu entry to run a TOOL_ACTION on selected items.
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:271
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition: view.cpp:552
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:315
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition: view.cpp:354
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:1639
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:449
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition: view.cpp:578
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1566
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).
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.
CONDITIONAL_MENU & GetMenu()
Definition: tool_menu.cpp:44
void ShowContextMenu(SELECTION &aSelection)
Helper function to set and immediately show a CONDITIONAL_MENU in concert with the given SELECTION.
Definition: tool_menu.cpp:57
bool Init() override
Init() is called once upon a registration of the tool.
Definition: zoom_tool.cpp:41
void setTransitions() override
Definition: zoom_tool.cpp:170
bool selectRegion()
Definition: zoom_tool.cpp:106
EDA_DRAW_FRAME * m_frame
Definition: zoom_tool.h:48
int Main(const TOOL_EVENT &aEvent)
Main loop.
Definition: zoom_tool.cpp:62
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
Definition: zoom_tool.cpp:56
@ 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