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 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 
20 #include <class_draw_panel_gal.h>
21 #include <eda_draw_frame.h>
22 #include <id.h>
24 #include <tool/actions.h>
25 #include <tool/tool_manager.h>
26 #include <tool/zoom_tool.h>
27 #include <view/view.h>
28 #include <view/view_controls.h>
29 
30 
32  TOOL_INTERACTIVE( "common.Control.zoomTool" )
33 {
34  m_frame = NULL;
35 }
36 
37 
39 
40 
42 {
43  m_frame = getEditFrame<EDA_DRAW_FRAME>();
44 }
45 
46 
47 int ZOOM_TOOL::Main( const TOOL_EVENT& aEvent )
48 {
49  std::string tool = aEvent.GetCommandStr().get();
50  m_frame->PushTool( tool );
51 
52  while( TOOL_EVENT* evt = Wait() )
53  {
54  m_frame->GetCanvas()->SetCurrentCursor( wxCURSOR_ARROW );
55 
56  if( evt->IsCancelInteractive() || evt->IsActivate() )
57  break;
58 
59  else if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
60  {
61  if( selectRegion() )
62  break;
63  }
64 
65  else
66  evt->SetPassEvent();
67  }
68 
69  // Exit zoom tool
70  m_frame->PopTool( tool );
71  return 0;
72 }
73 
74 
76 {
77  bool cancelled = false;
78  KIGFX::VIEW* view = getView();
80 
81  getViewControls()->SetAutoPan( true );
82 
84  view->Add( &area );
85 
86  while( TOOL_EVENT* evt = Wait() )
87  {
88  if( evt->IsCancelInteractive() || evt->IsActivate() )
89  {
90  cancelled = true;
91  break;
92  }
93 
94  if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
95  {
96  area.SetOrigin( evt->DragOrigin() );
97  area.SetEnd( evt->Position() );
98  view->SetVisible( &area, true );
99  view->Update( &area, KIGFX::GEOMETRY );
100  }
101 
102  if( evt->IsMouseUp( BUT_LEFT ) || evt->IsMouseUp( BUT_RIGHT ) )
103  {
104  view->SetVisible( &area, false );
105  auto selectionBox = area.ViewBBox();
106 
107  if( selectionBox.GetWidth() == 0 || selectionBox.GetHeight() == 0 )
108  {
109  break;
110  }
111  else
112  {
113  VECTOR2D sSize = view->ToWorld( canvas->GetClientSize(), false );
114  VECTOR2D vSize = selectionBox.GetSize();
115  double scale;
116  double ratio = std::max( fabs( vSize.x / sSize.x ), fabs( vSize.y / sSize.y ) );
117 
118  if( evt->IsMouseUp( BUT_LEFT ) )
119  scale = view->GetScale() / ratio;
120  else
121  scale = view->GetScale() * ratio;
122 
123  view->SetScale( scale );
124  view->SetCenter( selectionBox.Centre() );
125 
126  break;
127  }
128  }
129  }
130 
131  view->SetVisible( &area, false );
132  view->Remove( &area );
133  getViewControls()->SetAutoPan( false );
134 
135  return cancelled;
136 }
137 
138 
140 {
141  Go( &ZOOM_TOOL::Main, ACTIONS::zoomTool.MakeEvent() );
142 }
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
void SetEnd(VECTOR2I aEnd)
Set the current end of the rectangle (the corner that moves with the cursor.
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Function ToWorld() Converts a screen space point/vector to a point/vector in world space coordinates.
Definition: view.cpp:475
void SetOrigin(VECTOR2I aOrigin)
Set the origin of the rectange (the fixed corner)
void SetCurrentCursor(wxStockCursor aStockCursorID)
Function SetCurrentCursor Set the current cursor shape for this panel.
VIEW_CONTROLS class definition.
EDA_DRAW_FRAME * m_frame
Definition: zoom_tool.h:45
void setTransitions() override
Definition: zoom_tool.cpp:139
static TOOL_ACTION zoomTool
Definition: actions.h:94
virtual EDA_DRAW_PANEL_GAL * GetCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
virtual void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:376
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
void SetCenter(const VECTOR2D &aCenter)
Function SetCenter() Sets the center point of the VIEW (i.e.
Definition: view.cpp:604
Class TOOL_EVENT.
Definition: tool_event.h:168
const BOX2I ViewBBox() const override
Function ViewBBox() returns the bounding box of the item covering all its layers.
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags)
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1540
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:36
virtual void SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg.
int Main(const TOOL_EVENT &aEvent)
Main loop.
Definition: zoom_tool.cpp:47
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Function SetScale() Sets the scaling factor, zooming around a given anchor point.
Definition: view.cpp:578
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:455
const int scale
#define max(a, b)
Definition: auxiliary.h:86
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:79
virtual void PopTool(const std::string &actionName)
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Sets the item visibility.
Definition: view.cpp:1486
Class SELECTION_AREA.
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
Definition: zoom_tool.cpp:41
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:346
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:42
Class VIEW.
Definition: view.h:61
double GetScale() const
Function GetScale()
Definition: view.h:257
bool selectRegion()
Definition: zoom_tool.cpp:75
Color has changed.
Definition: view_item.h:57