KiCad PCB EDA Suite
tool_base.h
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) 2013 CERN
5  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #ifndef __TOOL_BASE_H
27 #define __TOOL_BASE_H
28 
29 #include <cassert>
30 #include <base_struct.h> // for KICAD_T
31 
32 #include <tool/tool_event.h>
33 #include <tool/tool_settings.h>
34 
35 #include <functional>
36 
37 class EDA_ITEM;
38 class TOOL_MANAGER;
39 class wxWindow;
40 
41 namespace KIGFX
42 {
43 class VIEW;
44 class VIEW_CONTROLS;
45 };
46 
48 {
50  INTERACTIVE = 0x01,
51 
53  BATCH = 0x02
54 };
55 
57 typedef int TOOL_ID;
58 
59 using TOOL_STATE_FUNC = std::function<int(const TOOL_EVENT&)>;
60 
61 
68 class TOOL_BASE
69 {
70 public:
71  TOOL_BASE( TOOL_TYPE aType, TOOL_ID aId, const std::string& aName = std::string( "" ) ) :
72  m_type( aType ),
73  m_toolId( aId ),
74  m_toolName( aName ),
75  m_toolMgr( NULL ) {};
76 
77  virtual ~TOOL_BASE() {};
78 
81  {
82  RUN,
85  };
86 
93  virtual bool Init()
94  {
95  return true;
96  }
97 
104  virtual void Reset( RESET_REASON aReason ) = 0;
105 
112  {
113  return m_type;
114  }
115 
122  TOOL_ID GetId() const
123  {
124  return m_toolId;
125  }
126 
133  const std::string& GetName() const
134  {
135  return m_toolName;
136  }
137 
145  {
146  return m_toolMgr;
147  }
148 
150 
151  bool IsToolActive() const;
152 
153 protected:
154  friend class TOOL_MANAGER;
155  friend class TOOL_SETTINGS;
156 
163  void attachManager( TOOL_MANAGER* aManager );
164 
171  KIGFX::VIEW* getView() const;
172 
181 
187  template <typename T>
188  T* getEditFrame() const
189  {
190  return static_cast<T*>( getEditFrameInt() );
191  }
192 
198  template <typename T>
199  T* getModel() const
200  {
201  EDA_ITEM* m = getModelInt();
202 
203  return static_cast<T*>( m );
204  }
205 
208 
211 
214  std::string m_toolName;
217 
218 private:
219  // hide the implementation to avoid spreading half of
220  // kicad and wxWidgets headers to the tools that may not need them at all!
221  EDA_ITEM* getModelInt() const;
222  wxWindow* getEditFrameInt() const;
223 };
224 
225 #endif
virtual bool Init()
Function Init() Init() is called once upon a registration of the tool.
Definition: tool_base.h:93
std::string m_toolName
Name of the tool.
Definition: tool_base.h:214
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:58
Model changes (required full reload)
Definition: tool_base.h:83
T
enum T contains all this lexer's tokens.
Tool is invoked after being inactive.
Definition: tool_base.h:82
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:215
TOOL_ID m_toolId
Unique identifier for the tool, assigned by a TOOL_MANAGER instance.
Definition: tool_base.h:210
void attachManager(TOOL_MANAGER *aManager)
Function attachManager()
Definition: tool_base.cpp:59
wxWindow * getEditFrameInt() const
Definition: tool_base.cpp:47
TOOL_SETTINGS m_toolSettings
Definition: tool_base.h:216
Class TOOL_MANAGER.
Definition: tool_manager.h:49
TOOL_TYPE
Definition: tool_base.h:47
virtual void Reset(RESET_REASON aReason)=0
Function Reset() Brings the tool to a known, initial state.
int TOOL_ID
Unique identifier for tools.
Definition: tool_base.h:57
Tool that interacts with the user
Definition: tool_base.h:50
T * getModel() const
Function getModel()
Definition: tool_base.h:199
bool IsToolActive() const
Definition: tool_base.cpp:30
virtual ~TOOL_BASE()
Definition: tool_base.h:77
TOOL_TYPE m_type
Stores the type of the tool.
Definition: tool_base.h:207
Class VIEW_CONTROLS is an interface for classes handling user events controlling the view behaviour (...
TOOL_ID GetId() const
Function GetId() Returns the unique identifier of the tool.
Definition: tool_base.h:122
Tool that runs in the background without any user intervention
Definition: tool_base.h:53
std::function< int(const TOOL_EVENT &)> TOOL_STATE_FUNC
Definition: tool_base.h:59
TOOL_SETTINGS & GetSettings()
Definition: tool_base.cpp:77
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:41
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:35
TOOL_TYPE GetType() const
Function GetType() Returns the type of the tool.
Definition: tool_base.h:111
Class TOOL_BASE.
Definition: tool_base.h:68
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:165
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:80
TOOL_MANAGER * GetManager() const
Function GetManager() Returns the instance of TOOL_MANAGER that takes care of the tool...
Definition: tool_base.h:144
Basic classes for most KiCad items.
Class VIEW.
Definition: view.h:58
const std::string & GetName() const
Function GetName() Returns the name of the tool.
Definition: tool_base.h:133
Rendering engine changes.
Definition: tool_base.h:84
TOOL_BASE(TOOL_TYPE aType, TOOL_ID aId, const std::string &aName=std::string(""))
Definition: tool_base.h:71
T * getEditFrame() const
Function getEditFrame()
Definition: tool_base.h:188
EDA_ITEM * getModelInt() const
Definition: tool_base.cpp:53