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 <class_base_screen.h>
32 
33 #include <tool/common_tools.h>
34 
35 
36 static TOOL_ACTION ACT_toggleCursor( "common.Control.toggleCursor",
37  AS_GLOBAL, MD_CTRL + MD_SHIFT + 'X',
38  _( "Toggle Always Show Cursor" ),
39  _( "Toogle display of the cursor, even when not in an interactive tool" ) );
40 
41 
43  TOOL_INTERACTIVE( "common.Control" ), m_frame( NULL )
44 {
45 }
46 
47 
49 {
50 }
51 
52 
54 {
55  m_frame = getEditFrame<EDA_DRAW_FRAME>();
56 }
57 
58 
59 int COMMON_TOOLS::ZoomInOut( const TOOL_EVENT& aEvent )
60 {
63  double zoomScale = 1.0;
64 
65  if( aEvent.IsAction( &ACTIONS::zoomIn ) )
66  zoomScale = 1.3;
67  else if( aEvent.IsAction( &ACTIONS::zoomOut ) )
68  zoomScale = 1/1.3;
69 
70  view->SetScale( view->GetScale() * zoomScale, getViewControls()->GetCursorPosition() );
71 
72  if( ctls->IsCursorWarpingEnabled() )
73  ctls->CenterOnCursor();
74 
75  return 0;
76 }
77 
78 
80 {
81  KIGFX::VIEW* view = getView();
82  double zoomScale = 1.0;
83 
84  if( aEvent.IsAction( &ACTIONS::zoomInCenter ) )
85  zoomScale = 1.3;
86  else if( aEvent.IsAction( &ACTIONS::zoomOutCenter ) )
87  zoomScale = 1/1.3;
88 
89  view->SetScale( view->GetScale() * zoomScale );
90 
91  return 0;
92 }
93 
94 
96 {
98 
99  ctls->CenterOnCursor();
100 
101  return 0;
102 }
103 
104 
106 {
107  KIGFX::VIEW* view = getView();
108  EDA_DRAW_PANEL_GAL* galCanvas = m_frame->GetGalCanvas();
109  EDA_ITEM* model = getModel<EDA_ITEM>();
110 
111  BOX2I bBox = model->ViewBBox();
112  VECTOR2D scrollbarSize = VECTOR2D( galCanvas->GetSize() - galCanvas->GetClientSize() );
113  VECTOR2D screenSize = view->ToWorld( galCanvas->GetClientSize(), false );
114 
115  if( bBox.GetWidth() == 0 || bBox.GetHeight() == 0 )
116  {
117  // Empty view
118  view->SetScale( 17.0 ); // works fine for the standard worksheet frame
119 
120  view->SetCenter( screenSize / 2.0 );
121  }
122  else
123  {
124  VECTOR2D vsize = bBox.GetSize();
125  double scale = view->GetScale() / std::max( fabs( vsize.x / screenSize.x ),
126  fabs( vsize.y / screenSize.y ) );
127 
128  view->SetScale( scale );
129  view->SetCenter( bBox.Centre() );
130  }
131 
132 
133  // Take scrollbars into account
134  VECTOR2D worldScrollbarSize = view->ToWorld( scrollbarSize, false );
135  view->SetCenter( view->GetCenter() + worldScrollbarSize / 2.0 );
136 
137  return 0;
138 }
139 
140 
142 {
143  unsigned int idx = aEvent.Parameter<intptr_t>();
144  std::vector<double>& zoomList = m_frame->GetScreen()->m_ZoomList;
145  KIGFX::VIEW* view = m_frame->GetGalCanvas()->GetView();
146  KIGFX::GAL* gal = m_frame->GetGalCanvas()->GetGAL();
147 
148  m_frame->SetPresetZoom( idx );
149 
150  if( idx == 0 ) // Zoom Auto
151  {
152  return ZoomFitScreen( aEvent );
153  }
154  else if( idx >= zoomList.size() )
155  {
156  assert( false );
157  return 0;
158  }
159 
160  double selectedZoom = zoomList[idx];
161  double zoomFactor = gal->GetWorldScale() / gal->GetZoomFactor();
162  view->SetScale( 1.0 / ( zoomFactor * selectedZoom ) );
163 
164  return 0;
165 }
166 
167 
168 // Grid control
169 int COMMON_TOOLS::GridNext( const TOOL_EVENT& aEvent )
170 {
171  m_frame->SetNextGrid();
172  updateGrid();
173 
174  return 0;
175 }
176 
177 
178 int COMMON_TOOLS::GridPrev( const TOOL_EVENT& aEvent )
179 {
180  m_frame->SetPrevGrid();
181  updateGrid();
182 
183  return 0;
184 }
185 
186 
188 {
189  long idx = aEvent.Parameter<intptr_t>();
190 
191  m_frame->SetPresetGrid( idx );
192  updateGrid();
193 
194  return 0;
195 }
196 
197 
199 {
200  auto& galOpts = m_frame->GetGalDisplayOptions();
201 
202  galOpts.m_forceDisplayCursor = !galOpts.m_forceDisplayCursor;
203  galOpts.NotifyChanged();
204 
205  return 0;
206 }
207 
208 
210 {
211  Go( &COMMON_TOOLS::ZoomInOut, ACTIONS::zoomIn.MakeEvent() );
218 
222 
224 }
225 
226 
228 {
229  BASE_SCREEN* screen = m_frame->GetScreen();
230  getView()->GetGAL()->SetGridSize( VECTOR2D( screen->GetGridSize() ) );
232 }
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, MD_CTRL+MD_SHIFT+ 'X', _("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:838
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:440
static TOOL_ACTION zoomFitScreen
Definition: actions.h:56
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:600
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
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:309
void SetCenter(const VECTOR2D &aCenter)
Function SetCenter() Sets the center point of the VIEW (i.e.
Definition: view.cpp:564
static TOOL_ACTION gridPreset
Definition: actions.h:66
static TOOL_ACTION gridPrev
Definition: actions.h:63
VECTOR2< double > VECTOR2D
Definition: vector2d.h:589
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 'dirty' flag.
Definition: view.h:576
int GridPrev(const TOOL_EVENT &aEvent)
Class BASE_SCREEN handles how to draw a screen (a board, a schematic ...)
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 (...
Definition: view_controls.h:94
int ZoomInOut(const TOOL_EVENT &aEvent)
void SetPresetZoom(int aIndex)
Function SetPresetZoom() changes zoom to one of the preset values.
Definition: zoom.cpp:215
void SetPresetGrid(int aIndex)
Function SetPresetGrid() changes the grid size to one of the preset values.
Definition: draw_frame.cpp:614
All active tools
Definition: tool_event.h:138
int ToggleCursor(const TOOL_EVENT &aEvent)
void SetGridSize(const VECTOR2D &aGridSize)
Set the grid size.
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:36
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:586
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:30
T Parameter() const
Function Parameter() Returns a non-standard parameter assigned to the event.
Definition: tool_event.h:373
TOOL_EVENT MakeEvent() const
Function HasHotKey() Checks if the action has a hot key assigned.
Definition: tool_action.h:104
const int scale
BASE_SCREEN class implementation.
static TOOL_ACTION zoomCenter
Definition: actions.h:55
#define max(a, b)
Definition: auxiliary.h:86
int ZoomPreset(const TOOL_EVENT &aEvent)
Class TOOL_ACTION.
Definition: tool_action.h:46
const wxRealPoint & GetGridSize() const
Return the grid size of the currently selected grid.
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:151
double GetZoomFactor() const
Get the zoom factor.
static TOOL_ACTION gridNext
Definition: actions.h:62
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:817
Class VIEW.
Definition: view.h:58
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.
Class GAL is the abstract interface for drawing on a 2D-surface.