KiCad PCB EDA Suite
common_tools.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) 2014-2016 CERN
5  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <tool/actions.h>
26 #include <draw_frame.h>
27 #include <class_draw_panel_gal.h>
28 #include <view/view.h>
29 #include <view/view_controls.h>
31 #include <base_screen.h>
32 #include <hotkeys.h>
33 
34 #include <tool/common_tools.h>
35 
36 
37 static TOOL_ACTION ACT_toggleCursor( "common.Control.toggleCursor",
39  _( "Toggle Always Show Cursor" ),
40  _( "Toogle display of the cursor, even when not in an interactive tool" ) );
41 
42 
44  TOOL_INTERACTIVE( "common.Control" ), m_frame( NULL )
45 {
46 }
47 
48 
50 {
51 }
52 
53 
55 {
56  m_frame = getEditFrame<EDA_DRAW_FRAME>();
57 }
58 
59 
60 int COMMON_TOOLS::ZoomInOut( const TOOL_EVENT& aEvent )
61 {
62  bool direction = aEvent.IsAction( &ACTIONS::zoomIn );
63  return doZoomInOut( direction, true );
64 }
65 
66 
68 {
69  bool direction = aEvent.IsAction( &ACTIONS::zoomInCenter );
70  return doZoomInOut( direction, false );
71 }
72 
73 
74 int COMMON_TOOLS::doZoomInOut( bool aDirection, bool aCenterOnCursor )
75 {
76  double zoom = m_frame->GetGalCanvas()->GetLegacyZoom();
77 
78  // Step must be AT LEAST 1.3
79  if( aDirection )
80  zoom /= 1.3;
81  else
82  zoom *= 1.3;
83 
84  // Now look for the next closest menu step
85  std::vector<double>& zoomList = m_frame->GetScreen()->m_ZoomList;
86  int idx;
87 
88  if( aDirection )
89  {
90  for( idx = zoomList.size() - 1; idx >= 0; --idx )
91  {
92  if( zoomList[idx] <= zoom )
93  break;
94  }
95 
96  if( idx < 0 )
97  idx = 0; // if we ran off the end then peg to the end
98  }
99  else
100  {
101  for( idx = 0; idx < (int)zoomList.size(); ++idx )
102  {
103  if( zoomList[idx] >= zoom )
104  break;
105  }
106 
107  if( idx >= (int)zoomList.size() )
108  idx = zoomList.size() - 1; // if we ran off the end then peg to the end
109  }
110 
111  return doZoomToPreset( idx + 1, aCenterOnCursor );
112 }
113 
114 
116 {
118 
119  ctls->CenterOnCursor();
120 
121  return 0;
122 }
123 
124 
126 {
127  KIGFX::VIEW* view = getView();
128  EDA_DRAW_PANEL_GAL* galCanvas = m_frame->GetGalCanvas();
129  EDA_ITEM* model = getModel<EDA_ITEM>();
130  EDA_BASE_FRAME* frame = getEditFrame<EDA_BASE_FRAME>();
131 
132  BOX2I bBox = model->ViewBBox();
133  VECTOR2D scrollbarSize = VECTOR2D( galCanvas->GetSize() - galCanvas->GetClientSize() );
134  VECTOR2D screenSize = view->ToWorld( galCanvas->GetClientSize(), false );
135 
136  if( bBox.GetWidth() == 0 || bBox.GetHeight() == 0 )
137  {
138  bBox = galCanvas->GetDefaultViewBBox();
139  }
140 
141  VECTOR2D vsize = bBox.GetSize();
142  double scale = view->GetScale() / std::max( fabs( vsize.x / screenSize.x ),
143  fabs( vsize.y / screenSize.y ) );
144 
145  // Reserve a 10% margin around component bounding box.
146  double margin_scale_factor = 1.1;
147 
148  // Leave 20% for library editors & viewers
149  if( frame->IsType( FRAME_PCB_MODULE_VIEWER ) || frame->IsType( FRAME_PCB_MODULE_VIEWER_MODAL )
150  || frame->IsType( FRAME_SCH_VIEWER ) || frame->IsType( FRAME_SCH_VIEWER_MODAL )
151  || frame->IsType( FRAME_SCH_LIB_EDITOR ) || frame->IsType( FRAME_PCB_MODULE_EDITOR ) )
152  {
153  margin_scale_factor = 1.2;
154  }
155 
156  view->SetScale( scale / margin_scale_factor );
157  view->SetCenter( bBox.Centre() );
158 
159  // Take scrollbars into account
160  VECTOR2D worldScrollbarSize = view->ToWorld( scrollbarSize, false );
161  view->SetCenter( view->GetCenter() + worldScrollbarSize / 2.0 );
162 
163  return 0;
164 }
165 
166 
168 {
169  unsigned int idx = aEvent.Parameter<intptr_t>();
170  return doZoomToPreset( idx, false );
171 }
172 
173 
174 // Note: idx == 0 is Auto; idx == 1 is first entry in zoomList
175 int COMMON_TOOLS::doZoomToPreset( int idx, bool aCenterOnCursor )
176 {
177  std::vector<double>& zoomList = m_frame->GetScreen()->m_ZoomList;
178  KIGFX::VIEW* view = m_frame->GetGalCanvas()->GetView();
179  KIGFX::GAL* gal = m_frame->GetGalCanvas()->GetGAL();
180 
181  m_frame->SetPresetZoom( idx );
182 
183  if( idx == 0 ) // Zoom Auto
184  {
186  return ZoomFitScreen( dummy );
187  }
188  else
189  idx--;
190 
191  double selectedZoom = zoomList[idx];
192  double zoomFactor = gal->GetWorldScale() / gal->GetZoomFactor();
193  double scale = 1.0 / ( zoomFactor * selectedZoom );
194 
195  if( aCenterOnCursor )
196  {
197  view->SetScale( scale, getViewControls()->GetCursorPosition() );
198 
199  if( getViewControls()->IsCursorWarpingEnabled() )
201  }
202  else
203  view->SetScale( scale );
204 
205  return 0;
206 }
207 
208 
209 // Grid control
210 int COMMON_TOOLS::GridNext( const TOOL_EVENT& aEvent )
211 {
212  m_frame->SetNextGrid();
213  updateGrid();
214 
215  return 0;
216 }
217 
218 
219 int COMMON_TOOLS::GridPrev( const TOOL_EVENT& aEvent )
220 {
221  m_frame->SetPrevGrid();
222  updateGrid();
223 
224  return 0;
225 }
226 
227 
229 {
230  long idx = aEvent.Parameter<intptr_t>();
231 
232  m_frame->SetPresetGrid( idx );
233  updateGrid();
234 
235  return 0;
236 }
237 
238 
240 {
241  auto& galOpts = m_frame->GetGalDisplayOptions();
242 
243  galOpts.m_forceDisplayCursor = !galOpts.m_forceDisplayCursor;
244  galOpts.NotifyChanged();
245 
246  return 0;
247 }
248 
249 
251 {
252  Go( &COMMON_TOOLS::ZoomInOut, ACTIONS::zoomIn.MakeEvent() );
259 
263 
265 }
266 
267 
269 {
270  BASE_SCREEN* screen = m_frame->GetScreen();
271  getView()->GetGAL()->SetGridSize( VECTOR2D( screen->GetGridSize() ) );
273 }
EDA_DRAW_FRAME * m_frame
Pointer to the currently used edit frame.
Definition: common_tools.h:67
const VECTOR2D & GetCenter() const
Function GetCenter() Returns the center point of this VIEW (in world space coordinates) ...
Definition: view.h:316
int ZoomFitScreen(const TOOL_EVENT &aEvent)
static TOOL_ACTION zoomPreset
Definition: actions.h:57
static TOOL_ACTION ACT_toggleCursor("common.Control.toggleCursor", AS_GLOBAL, TOOL_ACTION::LegacyHotKey(HK_TOGGLE_CURSOR), _("Toggle Always Show Cursor"), _("Toogle display of the cursor, even when not in an interactive tool"))
static TOOL_ACTION zoomInCenter
Definition: actions.h:53
KIGFX::GAL_DISPLAY_OPTIONS & GetGalDisplayOptions()
Function GetGalDisplayOptions Returns a reference to the gal rendering options used by GAL for render...
Definition: draw_frame.h:924
double GetWorldScale() const
Get the world scale.
double GetLegacyZoom() const
Function GetLegacyZoom() Returns current view scale converted to zoom value used by the legacy canvas...
VIEW_CONTROLS class definition.
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Function ToWorld() Converts a screen space point/vector to a point/vector in world space coordinates...
Definition: view.cpp:463
static TOOL_ACTION zoomFitScreen
Definition: actions.h:56
static int LegacyHotKey(int aHotKey)
Creates a hot key code that refers to a legacy hot key setting, instead of a particular key...
Definition: tool_action.h:174
bool IsAction(const TOOL_ACTION *aAction) const
Function IsAction() Tests if the event contains an action issued upon activation of the given TOOL_AC...
Definition: tool_event.cpp:54
int GridPreset(const TOOL_EVENT &aEvent)
void updateGrid()
Applies the legacy canvas grid settings for GAL.
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
bool m_forceDisplayCursor
Force cursor display
virtual void SetPrevGrid()
Function SetPrevGrid() changes the grid size settings to the previous one available.
Definition: draw_frame.cpp:655
void SetScale(double aScale)
Function SetScale() Sets the scaling factor.
Definition: view.h:247
int doZoomToPreset(int idx, bool aCenterOnCursor)
Note: idx == 0 is Auto; idx == 1 is first entry in zoomList
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
static TOOL_ACTION zoomOutCenter
Definition: actions.h:54
Pcbnew hotkeys.
Auxiliary rendering target (noncached)
Definition: definitions.h:42
int GridNext(const TOOL_EVENT &aEvent)
int ZoomInOutCenter(const TOOL_EVENT &aEvent)
virtual void CenterOnCursor() const =0
Function CenterOnCursor() Sets the viewport center to the current cursor position and warps the curso...
KIGFX::GAL * GetGAL() const
Function GetGAL() Returns a pointer to the GAL instance used in the panel.
virtual BASE_SCREEN * GetScreen() const
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
Definition: draw_frame.h:369
void SetCenter(const VECTOR2D &aCenter)
Function SetCenter() Sets the center point of the VIEW (i.e.
Definition: view.cpp:587
static TOOL_ACTION gridPreset
Definition: actions.h:67
static TOOL_ACTION gridPrev
Definition: actions.h:64
VECTOR2< double > VECTOR2D
Definition: vector2d.h:588
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:177
void MarkTargetDirty(int aTarget)
Function MarkTargetDirty() Sets or clears target &#39;dirty&#39; flag.
Definition: view.h:577
int GridPrev(const TOOL_EVENT &aEvent)
int doZoomInOut(bool aDirection, bool aCenterOnCursor)
Class BASE_SCREEN handles how to draw a screen (a board, a schematic ...)
Definition: base_screen.h:76
Class TOOL_EVENT.
Definition: tool_event.h:162
int ZoomCenter(const TOOL_EVENT &aEvent)
Class VIEW_CONTROLS is an interface for classes handling user events controlling the view behaviour (...
int ZoomInOut(const TOOL_EVENT &aEvent)
void SetPresetZoom(int aIndex)
Function SetPresetZoom() changes zoom to one of the preset values.
Definition: zoom.cpp:249
void SetPresetGrid(int aIndex)
Function SetPresetGrid() changes the grid size to one of the preset values.
Definition: draw_frame.cpp:669
All active tools
Definition: tool_event.h:138
int ToggleCursor(const TOOL_EVENT &aEvent)
virtual BOX2I GetDefaultViewBBox() const
Returns the bounding box of the view that should be used if model is not valid For example...
void SetGridSize(const VECTOR2D &aGridSize)
Set the grid size.
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:41
void setTransitions() override
Sets up handlers for various events.
virtual void SetNextGrid()
Function SetNextGrid() changes the grid size settings to the next one available.
Definition: draw_frame.cpp:641
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:35
T Parameter() const
Function Parameter() Returns a non-standard parameter assigned to the event.
Definition: tool_event.h:378
TOOL_EVENT MakeEvent() const
Function HasHotKey() Checks if the action has a hot key assigned.
Definition: tool_action.h:104
const int scale
static TOOL_ACTION zoomCenter
Definition: actions.h:55
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
#define max(a, b)
Definition: auxiliary.h:86
int ZoomPreset(const TOOL_EVENT &aEvent)
Class EDA_BASE_FRAME is the base frame for deriving all KiCad main window classes.
Class TOOL_ACTION.
Definition: tool_action.h:46
const wxRealPoint & GetGridSize() const
Return the grid size of the currently selected grid.
Definition: base_screen.h:410
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:162
double GetZoomFactor() const
Get the zoom factor.
static TOOL_ACTION gridNext
Definition: actions.h:63
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:80
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
double GetScale() const
Function GetScale()
Definition: view.h:265
static TOOL_ACTION zoomIn
Definition: actions.h:51
static TOOL_ACTION zoomOut
Definition: actions.h:52
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Function GetGalCanvas returns a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:903
Class VIEW.
Definition: view.h:58
BASE_SCREEN class implementation.
virtual const BOX2I ViewBBox() const override
Function ViewBBox() returns the bounding box of the item covering all its layers. ...
std::vector< double > m_ZoomList
standard zoom (i.e. scale) coefficients.
Definition: base_screen.h:219
Class GAL is the abstract interface for drawing on a 2D-surface.