KiCad PCB EDA Suite
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
28
30 TOOL_INTERACTIVE( "common.Control.zoomTool" )
31{
32 m_frame = nullptr;
33}
34
35
37
38
39
41{
42 auto& ctxMenu = m_menu.GetMenu();
43
44 // cancel current tool goes in main context menu at the top if present
46 ctxMenu.AddSeparator( 1 );
47
48 // Finally, add the standard zoom/grid items
49 getEditFrame<EDA_DRAW_FRAME>()->AddStandardSubMenus( m_menu );
50
51 return true;
52}
53
54
56{
57 m_frame = getEditFrame<EDA_DRAW_FRAME>();
58}
59
60
61int ZOOM_TOOL::Main( const TOOL_EVENT& aEvent )
62{
63 m_frame->PushTool( aEvent );
64
65 auto setCursor =
66 [&]()
67 {
69 };
70
71 // Set initial cursor
72 setCursor();
73
74 while( TOOL_EVENT* evt = Wait() )
75 {
76 setCursor();
77
78 if( evt->IsCancelInteractive() || evt->IsActivate() )
79 {
80 break;
81 }
82 else if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
83 {
84 if( selectRegion() )
85 break;
86 }
87 else if( evt->IsClick( BUT_RIGHT ) )
88 {
91 }
92 else
93 {
94 evt->SetPassEvent();
95 }
96 }
97
98 // Exit zoom tool
100 m_frame->PopTool( aEvent );
101 return 0;
102}
103
104
106{
107 bool cancelled = false;
108 KIGFX::VIEW* view = getView();
110
111 getViewControls()->SetAutoPan( true );
112
114 view->Add( &area );
115
116 while( TOOL_EVENT* evt = Wait() )
117 {
118 if( evt->IsCancelInteractive() || evt->IsActivate() )
119 {
120 cancelled = true;
121 break;
122 }
123
124 if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
125 {
126 area.SetOrigin( evt->DragOrigin() );
127 area.SetEnd( evt->Position() );
128 view->SetVisible( &area, true );
129 view->Update( &area, KIGFX::GEOMETRY );
130 }
131
132 if( evt->IsMouseUp( BUT_LEFT ) || evt->IsMouseUp( BUT_RIGHT ) )
133 {
134 view->SetVisible( &area, false );
135 auto selectionBox = area.ViewBBox();
136
137 if( selectionBox.GetWidth() == 0 || selectionBox.GetHeight() == 0 )
138 {
139 break;
140 }
141 else
142 {
143 VECTOR2D sSize = view->ToWorld( canvas->GetClientSize(), false );
144 VECTOR2D vSize = selectionBox.GetSize();
145 double scale;
146 double ratio = std::max( fabs( vSize.x / sSize.x ), fabs( vSize.y / sSize.y ) );
147
148 if( evt->IsMouseUp( BUT_LEFT ) )
149 scale = view->GetScale() / ratio;
150 else
151 scale = view->GetScale() * ratio;
152
153 view->SetScale( scale );
154 view->SetCenter( selectionBox.Centre() );
155
156 break;
157 }
158 }
159 }
160
161 view->SetVisible( &area, false );
162 view->Remove( &area );
163 getViewControls()->SetAutoPan( false );
164
165 return cancelled;
166}
167
168
170{
171 Go( &ZOOM_TOOL::Main, ACTIONS::zoomTool.MakeEvent() );
172}
static TOOL_ACTION cancelInteractive
Definition: actions.h:63
static TOOL_ACTION zoomTool
Definition: actions.h:101
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:69
double GetScale() const
Definition: view.h:269
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition: view.cpp:548
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:316
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition: view.cpp:346
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:1586
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:445
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition: view.cpp:574
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1524
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:156
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:40
void setTransitions() override
Definition: zoom_tool.cpp:169
bool selectRegion()
Definition: zoom_tool.cpp:105
EDA_DRAW_FRAME * m_frame
Definition: zoom_tool.h:48
int Main(const TOOL_EVENT &aEvent)
Main loop.
Definition: zoom_tool.cpp:61
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
Definition: zoom_tool.cpp:55
@ GEOMETRY
Position or shape has changed.
Definition: view_item.h:49
const int scale
std::vector< FAB_LAYER_COLOR > dummy
@ BUT_LEFT
Definition: tool_event.h:127
@ BUT_RIGHT
Definition: tool_event.h:128