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 
34 #include <functional>
35 
36 class EDA_ITEM;
37 class TOOL_MANAGER;
38 
39 namespace KIGFX
40 {
41 class VIEW;
42 class VIEW_CONTROLS;
43 }
44 
46 {
48  INTERACTIVE = 0x01,
49 
51  BATCH = 0x02
52 };
53 
55 typedef int TOOL_ID;
56 
57 using TOOL_STATE_FUNC = std::function<int(const TOOL_EVENT&)>;
58 
59 
66 class TOOL_BASE
67 {
68 public:
69  TOOL_BASE( TOOL_TYPE aType, TOOL_ID aId, const std::string& aName = std::string( "" ) ) :
70  m_type( aType ),
71  m_toolId( aId ),
72  m_toolName( aName ),
73  m_toolMgr( NULL ) {};
74 
75  virtual ~TOOL_BASE() {};
76 
79  {
80  RUN,
83  };
84 
91  virtual bool Init()
92  {
93  return true;
94  }
95 
102  virtual void Reset( RESET_REASON aReason ) = 0;
103 
110  {
111  return m_type;
112  }
113 
120  TOOL_ID GetId() const
121  {
122  return m_toolId;
123  }
124 
131  const std::string& GetName() const
132  {
133  return m_toolName;
134  }
135 
143  {
144  return m_toolMgr;
145  }
146 
147  //TOOL_SETTINGS& GetSettings();
148 
149  bool IsToolActive() const;
150 
151 protected:
152  friend class TOOL_MANAGER;
153  friend class TOOL_SETTINGS;
154 
161  void attachManager( TOOL_MANAGER* aManager );
162 
169  KIGFX::VIEW* getView() const;
170 
179 
185  template <typename T>
186  T* getEditFrame() const
187  {
188 #if !defined( QA_TEST ) // Dynamic casts give the linker a siezure in the test framework
189  wxASSERT( dynamic_cast<T*>( getEditFrameInt() ) );
190 #endif
191  return static_cast<T*>( getEditFrameInt() );
192  }
193 
199  template <typename T>
200  T* getModel() const
201  {
202  EDA_ITEM* m = getModelInt();
203 #if !defined( QA_TEST ) // Dynamic casts give the linker a siezure in the test framework
204  wxASSERT( dynamic_cast<T*>( m ) );
205 #endif
206  return static_cast<T*>( m );
207  }
208 
211 
214 
217  std::string m_toolName;
219  //TOOL_SETTINGS m_toolSettings;
220 
221 private:
222  // hide the implementation to avoid spreading half of
223  // kicad and wxWidgets headers to the tools that may not need them at all!
224  EDA_ITEM* getModelInt() const;
226 };
227 
228 #endif
TOOL_TYPE GetType() const
Function GetType() Returns the type of the tool.
Definition: tool_base.h:109
virtual bool Init()
Function Init() Init() is called once upon a registration of the tool.
Definition: tool_base.h:91
std::string m_toolName
Name of the tool.
Definition: tool_base.h:217
T * getModel() const
Function getModel()
Definition: tool_base.h:200
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:131
Model changes (required full reload)
Definition: tool_base.h:81
TOOL_ID GetId() const
Function GetId() Returns the unique identifier of the tool.
Definition: tool_base.h:120
Tool is invoked after being inactive.
Definition: tool_base.h:80
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:218
TOOL_ID m_toolId
Unique identifier for the tool, assigned by a TOOL_MANAGER instance.
Definition: tool_base.h:213
void attachManager(TOOL_MANAGER *aManager)
Function attachManager()
Definition: tool_base.cpp:60
EDA_BASE_FRAME * getEditFrameInt() const
Definition: tool_base.cpp:48
TOOL_MANAGER * GetManager() const
Function GetManager() Returns the instance of TOOL_MANAGER that takes care of the tool.
Definition: tool_base.h:142
TOOL_MANAGER.
Definition: tool_manager.h:50
TOOL_TYPE
Definition: tool_base.h:45
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:55
Tool that interacts with the user
Definition: tool_base.h:48
#define NULL
virtual ~TOOL_BASE()
Definition: tool_base.h:75
TOOL_TYPE m_type
Stores the type of the tool.
Definition: tool_base.h:210
friend class TOOL_SETTINGS
Definition: tool_base.h:153
TOOL_EVENT.
Definition: tool_event.h:171
VIEW_CONTROLS is an interface for classes handling user events controlling the view behaviour (such a...
EDA_ITEM * getModelInt() const
Definition: tool_base.cpp:54
Tool that runs in the background without any user intervention
Definition: tool_base.h:51
std::function< int(const TOOL_EVENT &)> TOOL_STATE_FUNC
Definition: tool_base.h:57
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:36
TOOL_BASE.
Definition: tool_base.h:66
The base frame for deriving all KiCad main window classes.
bool IsToolActive() const
Definition: tool_base.cpp:31
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:166
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:78
T * getEditFrame() const
Function getEditFrame()
Definition: tool_base.h:186
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:42
const std::string & GetName() const
Function GetName() Returns the name of the tool.
Definition: tool_base.h:131
VIEW.
Definition: view.h:61
Rendering engine changes.
Definition: tool_base.h:82
TOOL_BASE(TOOL_TYPE aType, TOOL_ID aId, const std::string &aName=std::string(""))
Definition: tool_base.h:69