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  // This method is called both when the zoom tool is activated (on) or deactivated (off)
51  bool zoom_tool_is_on = m_frame->GetMainToolBar()->GetToolToggled( ID_ZOOM_SELECTION );
52 
53  if( !zoom_tool_is_on ) // This is a tool deselection: do nothing
54  return 0;
55 
56  m_frame->SetToolID( ID_ZOOM_SELECTION, wxCURSOR_MAGNIFIER, _( "Zoom to selection" ) );
57 
58  while( auto evt = Wait() )
59  {
60  if( evt->IsCancel() || evt->IsActivate() )
61  break;
62 
63  else if( evt->IsDrag( BUT_LEFT ) )
64  {
65  if( selectRegion() )
66  break;
67  }
68 
69  else
71  }
72 
73  // Exit zoom tool
75  return 0;
76 }
77 
78 
80 {
81  bool cancelled = false;
82  auto view = getView();
83  auto canvas = m_frame->GetGalCanvas();
84  getViewControls()->SetAutoPan( true );
85 
87  view->Add( &area );
88 
89  while( auto evt = Wait() )
90  {
91  if( evt->IsCancel() || evt->IsActivate() )
92  {
93  cancelled = true;
94  break;
95  }
96 
97  if( evt->IsDrag( BUT_LEFT ) )
98  {
99  area.SetOrigin( evt->DragOrigin() );
100  area.SetEnd( evt->Position() );
101  view->SetVisible( &area, true );
102  view->Update( &area, KIGFX::GEOMETRY );
103  }
104 
105  if( evt->IsMouseUp( BUT_LEFT ) )
106  {
107  view->SetVisible( &area, false );
108  auto selectionBox = area.ViewBBox();
109 
110  VECTOR2D screenSize = view->ToWorld( canvas->GetClientSize(), false );
111 
112  if( selectionBox.GetWidth() == 0 || selectionBox.GetHeight() == 0 )
113  {
114  break;
115  }
116  else
117  {
118  VECTOR2D vsize = selectionBox.GetSize();
119  double scale = view->GetScale() / std::max( fabs( vsize.x / screenSize.x ),
120  fabs( vsize.y / screenSize.y ) );
121  view->SetScale( scale );
122  view->SetCenter( selectionBox.Centre() );
123 
124  break;
125  }
126  }
127  }
128 
129  view->SetVisible( &area, false );
130  view->Remove( &area );
131  getViewControls()->SetAutoPan( false );
132 
133  return cancelled;
134 }
135 
136 
138 {
139  Go( &ZOOM_TOOL::Main, PCB_ACTIONS::zoomTool.MakeEvent() );
140 }
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.
wxAuiToolBar * GetMainToolBar() const
Definition: draw_frame.h:393
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:349
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:137
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
virtual void SetNoToolSelected()
Select the ID_NO_TOOL_SELECTED id tool (Idle tool)
Definition: draw_frame.cpp:560
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:817
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:79
Color has changed.
Definition: view_item.h:57