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 
21 #include <wxPcbStruct.h>
22 #include <class_draw_panel_gal.h>
23 #include <view/view_controls.h>
24 #include <view/view.h>
25 #include <tool/tool_manager.h>
26 #include <tool/actions.h>
27 #include <tool/zoom_tool.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  // This method is called both when the zoom tool is activated (on) or deactivated (off)
50  bool zoom_tool_is_on = m_frame->GetToolToggled( ID_ZOOM_SELECTION );
51 
52  if( !zoom_tool_is_on ) // This is a tool deselection: do nothing
53  return 0;
54 
55  m_frame->SetToolID( ID_ZOOM_SELECTION, wxCURSOR_MAGNIFIER, _( "Zoom to selection" ) );
56 
57  while( auto evt = Wait() )
58  {
59  if( evt->IsCancel() || evt->IsActivate() )
60  break;
61 
62  else if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
63  {
64  if( selectRegion() )
65  break;
66  }
67 
68  else
70  }
71 
72  // Exit zoom tool
74  return 0;
75 }
76 
77 
79 {
80  bool cancelled = false;
81  auto view = getView();
82  auto canvas = m_frame->GetGalCanvas();
83  getViewControls()->SetAutoPan( true );
84 
86  view->Add( &area );
87 
88  while( auto evt = Wait() )
89  {
90  if( evt->IsCancel() || evt->IsActivate() )
91  {
92  cancelled = true;
93  break;
94  }
95 
96  if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
97  {
98  area.SetOrigin( evt->DragOrigin() );
99  area.SetEnd( evt->Position() );
100  view->SetVisible( &area, true );
101  view->Update( &area, KIGFX::GEOMETRY );
102  }
103 
104  if( evt->IsMouseUp( BUT_LEFT ) || evt->IsMouseUp( BUT_RIGHT ) )
105  {
106  view->SetVisible( &area, false );
107  auto selectionBox = area.ViewBBox();
108 
109  VECTOR2D screenSize = view->ToWorld( canvas->GetClientSize(), false );
110 
111  if( selectionBox.GetWidth() == 0 || selectionBox.GetHeight() == 0 )
112  {
113  break;
114  }
115  else
116  {
117  VECTOR2D vsize = selectionBox.GetSize();
118  double scale;
119  double ratio = std::max( fabs( vsize.x / screenSize.x ),
120  fabs( vsize.y / screenSize.y ) );
121 
122  if( evt->IsMouseUp( BUT_LEFT ) )
123  {
124  scale = view->GetScale() / ratio;
125  }
126  else
127  {
128  scale = view->GetScale() * ratio;
129  }
130 
131  view->SetScale( scale );
132  view->SetCenter( selectionBox.Centre() );
133 
134  break;
135  }
136  }
137  }
138 
139  view->SetVisible( &area, false );
140  view->Remove( &area );
141  getViewControls()->SetAutoPan( false );
142 
143  return cancelled;
144 }
145 
146 
148 {
149  Go( &ZOOM_TOOL::Main, ACTIONS::zoomTool.MakeEvent() );
150 }
void SetEnd(VECTOR2I aEnd)
Set the current end of the rectangle (the corner that moves with the cursor.
void SetOrigin(VECTOR2I aOrigin)
Set the origin of the rectange (the fixed corner)
VIEW_CONTROLS class definition.
EDA_DRAW_FRAME * m_frame
Definition: zoom_tool.h:45
void setTransitions() override
Definition: zoom_tool.cpp:147
static TOOL_ACTION zoomTool
Definition: actions.h:58
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:215
OPT_TOOL_EVENT Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
void PassEvent()
Allows a tool to pass the already handled event to the next tool on the stack.
Definition: tool_manager.h:351
Class TOOL_EVENT.
Definition: tool_event.h:162
const BOX2I ViewBBox() const override
Function ViewBBox() returns the bounding box of the item covering all its layers. ...
virtual void SetToolID(int aId, int aCursor, const wxString &aToolMsg)
Function SetToolID sets the tool command ID to aId and sets the cursor to aCursor.
Definition: draw_frame.cpp:546
virtual void SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg...
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:41
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:35
int Main(const TOOL_EVENT &aEvent)
Main loop.
Definition: zoom_tool.cpp:47
virtual void SetNoToolSelected()
Select the ID_NO_TOOL_SELECTED id tool (Idle tool)
Definition: draw_frame.cpp:571
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:80
Class SELECTION_AREA.
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Function GetGalCanvas returns a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:870
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
Definition: zoom_tool.cpp:41
bool GetToolToggled(int aToolId)
Checks all the toolbars and returns true if the given tool id is toggled.
Definition: draw_frame.h:440
bool selectRegion()
Definition: zoom_tool.cpp:78
Color has changed.
Definition: view_item.h:57