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  auto setCursor =
53  [&]()
54  {
56  };
57 
58  // Set initial cursor
59  setCursor();
60 
61  while( TOOL_EVENT* evt = Wait() )
62  {
63  setCursor();
64 
65  if( evt->IsCancelInteractive() || evt->IsActivate() )
66  {
67  break;
68  }
69  else if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
70  {
71  if( selectRegion() )
72  break;
73  }
74  else
75  {
76  evt->SetPassEvent();
77  }
78  }
79 
80  // Exit zoom tool
82  m_frame->PopTool( tool );
83  return 0;
84 }
85 
86 
88 {
89  bool cancelled = false;
90  KIGFX::VIEW* view = getView();
92 
93  getViewControls()->SetAutoPan( true );
94 
96  view->Add( &area );
97 
98  while( TOOL_EVENT* evt = Wait() )
99  {
100  if( evt->IsCancelInteractive() || evt->IsActivate() )
101  {
102  cancelled = true;
103  break;
104  }
105 
106  if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
107  {
108  area.SetOrigin( evt->DragOrigin() );
109  area.SetEnd( evt->Position() );
110  view->SetVisible( &area, true );
111  view->Update( &area, KIGFX::GEOMETRY );
112  }
113 
114  if( evt->IsMouseUp( BUT_LEFT ) || evt->IsMouseUp( BUT_RIGHT ) )
115  {
116  view->SetVisible( &area, false );
117  auto selectionBox = area.ViewBBox();
118 
119  if( selectionBox.GetWidth() == 0 || selectionBox.GetHeight() == 0 )
120  {
121  break;
122  }
123  else
124  {
125  VECTOR2D sSize = view->ToWorld( canvas->GetClientSize(), false );
126  VECTOR2D vSize = selectionBox.GetSize();
127  double scale;
128  double ratio = std::max( fabs( vSize.x / sSize.x ), fabs( vSize.y / sSize.y ) );
129 
130  if( evt->IsMouseUp( BUT_LEFT ) )
131  scale = view->GetScale() / ratio;
132  else
133  scale = view->GetScale() * ratio;
134 
135  view->SetScale( scale );
136  view->SetCenter( selectionBox.Centre() );
137 
138  break;
139  }
140  }
141  }
142 
143  view->SetVisible( &area, false );
144  view->Remove( &area );
145  getViewControls()->SetAutoPan( false );
146 
147  return cancelled;
148 }
149 
150 
152 {
153  Go( &ZOOM_TOOL::Main, ACTIONS::zoomTool.MakeEvent() );
154 }
void SetEnd(VECTOR2I aEnd)
Set the current end of the rectangle (the corner that moves with the cursor.
void SetCurrentCursor(KICURSOR aCursor)
Set the current cursor shape for this panel.
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Suspend execution of the tool until an event specified in aEventList arrives.
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Converts a screen space point/vector to a point/vector in world space coordinates.
Definition: view.cpp:451
void SetOrigin(VECTOR2I aOrigin)
EDA_DRAW_FRAME * m_frame
Definition: zoom_tool.h:45
void setTransitions() override
Definition: zoom_tool.cpp:151
static TOOL_ACTION zoomTool
Definition: actions.h:99
virtual EDA_DRAW_PANEL_GAL * GetCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition: view.cpp:352
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition: view.cpp:580
#define NULL
virtual void PopTool(const std::string &actionName)
Generic, UI-independent tool event.
Definition: tool_event.h:152
const BOX2I ViewBBox() const override
Set the origin of the rectangle (the fixed corner)
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:36
virtual void SetAutoPan(bool aEnabled)
Turn 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 })
Set the scaling factor, zooming around a given anchor point.
Definition: view.cpp:554
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:455
const int scale
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:77
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1450
Represent a selection area (currently a rectangle) in a VIEW, drawn corner-to-corner between two poin...
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
Definition: zoom_tool.cpp:41
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:322
KIGFX::VIEW_CONTROLS * getViewControls() const
Return the instance of VIEW_CONTROLS object used in the application.
Definition: tool_base.cpp:42
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
double GetScale() const
Definition: view.h:263
bool selectRegion()
Definition: zoom_tool.cpp:87
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: view.cpp:1504
Position or shape has changed.
Definition: view_item.h:54