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) 2016 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>
27 
28 #include "zoom_tool.h"
29 #include "pcb_actions.h"
30 
31 
33  TOOL_INTERACTIVE( "pcbnew.Control.zoomTool" )
34 {
35  m_frame = NULL;
36 }
37 
38 
40 
41 
43 {
44  m_frame = getEditFrame<PCB_EDIT_FRAME>();
45 }
46 
47 
48 int ZOOM_TOOL::Main( const TOOL_EVENT& aEvent )
49 {
50  m_frame->SetToolID( ID_ZOOM_SELECTION, wxCURSOR_MAGNIFIER, _( "Zoom to selection" ) );
51 
52  while( auto evt = Wait() )
53  {
54  if( evt->IsCancel() || evt->IsActivate() )
55  break;
56 
57  else if( evt->IsDrag( BUT_LEFT ) )
58  {
59  if( selectRegion() )
60  break;
61  }
62 
63  else
65  }
66 
67  m_frame->SetToolID( ID_NO_TOOL_SELECTED, wxCURSOR_DEFAULT, wxEmptyString );
68  return 0;
69 }
70 
71 
73 {
74  bool cancelled = false;
75  auto view = getView();
76  auto canvas = m_frame->GetGalCanvas();
77  getViewControls()->SetAutoPan( true );
78 
80  view->Add( &area );
81 
82  while( auto evt = Wait() )
83  {
84  if( evt->IsCancel() || evt->IsActivate() )
85  {
86  cancelled = true;
87  break;
88  }
89 
90  if( evt->IsDrag( BUT_LEFT ) )
91  {
92  area.SetOrigin( evt->DragOrigin() );
93  area.SetEnd( evt->Position() );
94  view->SetVisible( &area, true );
95  view->Update( &area, KIGFX::GEOMETRY );
96  }
97 
98  if( evt->IsMouseUp( BUT_LEFT ) )
99  {
100  view->SetVisible( &area, false );
101  auto selectionBox = area.ViewBBox();
102 
103  VECTOR2D screenSize = view->ToWorld( canvas->GetClientSize(), false );
104 
105  if( selectionBox.GetWidth() == 0 || selectionBox.GetHeight() == 0 )
106  {
107  break;
108  }
109  else
110  {
111  VECTOR2D vsize = selectionBox.GetSize();
112  double scale = view->GetScale() / std::max( fabs( vsize.x / screenSize.x ),
113  fabs( vsize.y / screenSize.y ) );
114  view->SetScale( scale );
115  view->SetCenter( selectionBox.Centre() );
116 
117  break;
118  }
119  }
120  }
121 
122  view->SetVisible( &area, false );
123  view->Remove( &area );
124  getViewControls()->SetAutoPan( false );
125 
126  return cancelled;
127 }
128 
129 
131 {
132  Go( &ZOOM_TOOL::Main, PCB_ACTIONS::zoomTool.MakeEvent() );
133 }
void SetEnd(VECTOR2I aEnd)
Set the current end of the rectangle (the corner that moves with the cursor.
virtual void SetToolID(int aId, int aCursor, const wxString &aToolMsg) override
Function SetToolID sets the tool command ID to aId and sets the cursor to aCursor.
void SetOrigin(VECTOR2I aOrigin)
Set the origin of the rectange (the fixed corner)
VIEW_CONTROLS class definition.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:220
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:336
static TOOL_ACTION zoomTool
Definition: pcb_actions.h:335
PCB_EDIT_FRAME * m_frame
Definition: zoom_tool.h:45
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 SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg...
void SetTransitions() override
Function SetTransitions() This method is meant to be overridden in order to specify handlers for even...
Definition: zoom_tool.cpp:130
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:36
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:30
int Main(const TOOL_EVENT &aEvent)
Main loop.
Definition: zoom_tool.cpp:48
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:803
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
Definition: zoom_tool.cpp:42
bool selectRegion()
Definition: zoom_tool.cpp:72
Color has changed.
Definition: view_item.h:57