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 <pcb_edit_frame.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  wxMenuBar* menu = m_frame->GetMenuBar();
51 
52  bool zoom_tool_is_on = m_frame->GetToolToggled( ID_ZOOM_SELECTION ) ||
53  ( menu && menu->IsChecked( ID_ZOOM_SELECTION ) );
54 
55  if( !zoom_tool_is_on ) // This is a tool deselection: do nothing
56  return 0;
57 
58  m_frame->SetToolID( ID_ZOOM_SELECTION, wxCURSOR_MAGNIFIER, _( "Zoom to selection" ) );
59 
60  while( auto evt = Wait() )
61  {
62  if( evt->IsCancel() || evt->IsActivate() )
63  break;
64 
65  else if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
66  {
67  if( selectRegion() )
68  break;
69  }
70 
71  else
73  }
74 
75  // Exit zoom tool
77  return 0;
78 }
79 
80 
82 {
83  bool cancelled = false;
84  auto view = getView();
85  auto canvas = m_frame->GetGalCanvas();
86  getViewControls()->SetAutoPan( true );
87 
89  view->Add( &area );
90 
91  while( auto evt = Wait() )
92  {
93  if( evt->IsCancel() || evt->IsActivate() )
94  {
95  cancelled = true;
96  break;
97  }
98 
99  if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
100  {
101  area.SetOrigin( evt->DragOrigin() );
102  area.SetEnd( evt->Position() );
103  view->SetVisible( &area, true );
104  view->Update( &area, KIGFX::GEOMETRY );
105  }
106 
107  if( evt->IsMouseUp( BUT_LEFT ) || evt->IsMouseUp( BUT_RIGHT ) )
108  {
109  view->SetVisible( &area, false );
110  auto selectionBox = area.ViewBBox();
111 
112  VECTOR2D screenSize = view->ToWorld( canvas->GetClientSize(), false );
113 
114  if( selectionBox.GetWidth() == 0 || selectionBox.GetHeight() == 0 )
115  {
116  break;
117  }
118  else
119  {
120  VECTOR2D vsize = selectionBox.GetSize();
121  double scale;
122  double ratio = std::max( fabs( vsize.x / screenSize.x ),
123  fabs( vsize.y / screenSize.y ) );
124 
125  if( evt->IsMouseUp( BUT_LEFT ) )
126  {
127  scale = view->GetScale() / ratio;
128  }
129  else
130  {
131  scale = view->GetScale() * ratio;
132  }
133 
134  view->SetScale( scale );
135  view->SetCenter( selectionBox.Centre() );
136 
137  break;
138  }
139  }
140  }
141 
142  view->SetVisible( &area, false );
143  view->Remove( &area );
144  getViewControls()->SetAutoPan( false );
145 
146  return cancelled;
147 }
148 
149 
151 {
152  Go( &ZOOM_TOOL::Main, ACTIONS::zoomTool.MakeEvent() );
153 }
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:150
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:591
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:616
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:895
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.cpp:319
bool selectRegion()
Definition: zoom_tool.cpp:81
Color has changed.
Definition: view_item.h:57