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 {
64  double zoomScale = 1.0;
65 
66  if( aEvent.IsAction( &ACTIONS::zoomIn ) )
67  zoomScale = 1.3;
68  else if( aEvent.IsAction( &ACTIONS::zoomOut ) )
69  zoomScale = 1/1.3;
70 
71  view->SetScale( view->GetScale() * zoomScale, getViewControls()->GetCursorPosition() );
72 
73  if( ctls->IsCursorWarpingEnabled() )
74  ctls->CenterOnCursor();
75 
76  return 0;
77 }
78 
79 
81 {
82  KIGFX::VIEW* view = getView();
83  double zoomScale = 1.0;
84 
85  if( aEvent.IsAction( &ACTIONS::zoomInCenter ) )
86  zoomScale = 1.3;
87  else if( aEvent.IsAction( &ACTIONS::zoomOutCenter ) )
88  zoomScale = 1/1.3;
89 
90  view->SetScale( view->GetScale() * zoomScale );
91 
92  return 0;
93 }
94 
95 
97 {
99 
100  ctls->CenterOnCursor();
101 
102  return 0;
103 }
104 
105 
107 {
108  KIGFX::VIEW* view = getView();
109  EDA_DRAW_PANEL_GAL* galCanvas = m_frame->GetGalCanvas();
110  EDA_ITEM* model = getModel<EDA_ITEM>();
111  EDA_BASE_FRAME* frame = getEditFrame<EDA_BASE_FRAME>();
112 
113  BOX2I bBox = model->ViewBBox();
114  VECTOR2D scrollbarSize = VECTOR2D( galCanvas->GetSize() - galCanvas->GetClientSize() );
115  VECTOR2D screenSize = view->ToWorld( galCanvas->GetClientSize(), false );
116 
117  if( bBox.GetWidth() == 0 || bBox.GetHeight() == 0 )
118  {
119  bBox = galCanvas->GetDefaultViewBBox();
120  }
121 
122  VECTOR2D vsize = bBox.GetSize();
123  double scale = view->GetScale() / std::max( fabs( vsize.x / screenSize.x ),
124  fabs( vsize.y / screenSize.y ) );
125 
126  // Reserve a 10% margin around component bounding box.
127  double margin_scale_factor = 1.1;
128 
129  // Leave 20% for library editors & viewers
130  if( frame->IsType( FRAME_PCB_MODULE_VIEWER ) || frame->IsType( FRAME_PCB_MODULE_VIEWER_MODAL )
131  || frame->IsType( FRAME_SCH_VIEWER ) || frame->IsType( FRAME_SCH_VIEWER_MODAL )
132  || frame->IsType( FRAME_SCH_LIB_EDITOR ) || frame->IsType( FRAME_PCB_MODULE_EDITOR ) )
133  {
134  margin_scale_factor = 1.2;
135  }
136 
137  view->SetScale( scale / margin_scale_factor );
138  view->SetCenter( bBox.Centre() );
139 
140  // Take scrollbars into account
141  VECTOR2D worldScrollbarSize = view->ToWorld( scrollbarSize, false );
142  view->SetCenter( view->GetCenter() + worldScrollbarSize / 2.0 );
143 
144  return 0;
145 }
146 
147 
149 {
150  unsigned int idx = aEvent.Parameter<intptr_t>();
151  std::vector<double>& zoomList = m_frame->GetScreen()->m_ZoomList;
152  KIGFX::VIEW* view = m_frame->GetGalCanvas()->GetView();
153  KIGFX::GAL* gal = m_frame->GetGalCanvas()->GetGAL();
154 
155  m_frame->SetPresetZoom( idx );
156 
157  if( idx == 0 ) // Zoom Auto
158  {
159  return ZoomFitScreen( aEvent );
160  }
161  else if( idx >= zoomList.size() )
162  {
163  assert( false );
164  return 0;
165  }
166 
167  double selectedZoom = zoomList[idx];
168  double zoomFactor = gal->GetWorldScale() / gal->GetZoomFactor();
169  view->SetScale( 1.0 / ( zoomFactor * selectedZoom ) );
170 
171  return 0;
172 }
173 
174 
175 // Grid control
176 int COMMON_TOOLS::GridNext( const TOOL_EVENT& aEvent )
177 {
178  m_frame->SetNextGrid();
179  updateGrid();
180 
181  return 0;
182 }
183 
184 
185 int COMMON_TOOLS::GridPrev( const TOOL_EVENT& aEvent )
186 {
187  m_frame->SetPrevGrid();
188  updateGrid();
189 
190  return 0;
191 }
192 
193 
195 {
196  long idx = aEvent.Parameter<intptr_t>();
197 
198  m_frame->SetPresetGrid( idx );
199  updateGrid();
200 
201  return 0;
202 }
203 
204 
206 {
207  auto& galOpts = m_frame->GetGalDisplayOptions();
208 
209  galOpts.m_forceDisplayCursor = !galOpts.m_forceDisplayCursor;
210  galOpts.NotifyChanged();
211 
212  return 0;
213 }
214 
215 
217 {
218  Go( &COMMON_TOOLS::ZoomInOut, ACTIONS::zoomIn.MakeEvent() );
225 
229 
231 }
232 
233 
235 {
236  BASE_SCREEN* screen = m_frame->GetScreen();
237  getView()->GetGAL()->SetGridSize( VECTOR2D( screen->GetGridSize() ) );
239 }
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:916
double GetWorldScale() const
Get the world scale.
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:656
void SetScale(double aScale)
Function SetScale() Sets the scaling factor.
Definition: view.h:247
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:361
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)
Class BASE_SCREEN handles how to draw a screen (a board, a schematic ...)
Definition: base_screen.h:76
VECTOR2D GetCursorPosition() const
Returns the current cursor position in world coordinates.
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:245
void SetPresetGrid(int aIndex)
Function SetPresetGrid() changes the grid size to one of the preset values.
Definition: draw_frame.cpp:670
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:642
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
#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
bool IsCursorWarpingEnabled() const
Function IsCursorWarpingEnabled()
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:895
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.