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 <eda_item.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 class TOOLS_HOLDER;
39 
40 namespace KIGFX
41 {
42 class VIEW;
43 class VIEW_CONTROLS;
44 }
45 
47 {
49  INTERACTIVE = 0x01,
50 
52  BATCH = 0x02
53 };
54 
56 typedef int TOOL_ID;
57 
58 using TOOL_STATE_FUNC = std::function<int(const TOOL_EVENT&)>;
59 
60 
67 class TOOL_BASE
68 {
69 public:
70  TOOL_BASE( TOOL_TYPE aType, TOOL_ID aId, const std::string& aName = std::string( "" ) ) :
71  m_type( aType ),
72  m_toolId( aId ),
73  m_toolName( aName ),
74  m_toolMgr( NULL ) {};
75 
76  virtual ~TOOL_BASE() {};
77 
80  {
81  RUN,
84  };
85 
92  virtual bool Init()
93  {
94  return true;
95  }
96 
103  virtual void Reset( RESET_REASON aReason ) = 0;
104 
111  {
112  return m_type;
113  }
114 
121  TOOL_ID GetId() const
122  {
123  return m_toolId;
124  }
125 
132  const std::string& GetName() const
133  {
134  return m_toolName;
135  }
136 
144  {
145  return m_toolMgr;
146  }
147 
148  //TOOL_SETTINGS& GetAdapter();
149 
150  bool IsToolActive() const;
151 
152 protected:
153  friend class TOOL_MANAGER;
154  friend class TOOL_SETTINGS;
155 
162  void attachManager( TOOL_MANAGER* aManager );
163 
170  KIGFX::VIEW* getView() const;
171 
180 
186  template <typename T>
187  T* getEditFrame() const
188  {
189 #if !defined( QA_TEST ) // Dynamic casts give the linker a siezure in the test framework
190  wxASSERT( dynamic_cast<T*>( getToolHolderInt() ) );
191 #endif
192  return static_cast<T*>( getToolHolderInt() );
193  }
194 
200  template <typename T>
201  T* getModel() const
202  {
203  EDA_ITEM* m = getModelInt();
204 #if !defined( QA_TEST ) // Dynamic casts give the linker a siezure in the test framework
205  wxASSERT( dynamic_cast<T*>( m ) );
206 #endif
207  return static_cast<T*>( m );
208  }
209 
212 
215 
218  std::string m_toolName;
220  //TOOL_SETTINGS m_toolSettings;
221 
222 private:
223  // hide the implementation to avoid spreading half of
224  // kicad and wxWidgets headers to the tools that may not need them at all!
225  EDA_ITEM* getModelInt() const;
227 };
228 
229 #endif
TOOL_TYPE GetType() const
Function GetType() Returns the type of the tool.
Definition: tool_base.h:110
virtual bool Init()
Function Init() Init() is called once upon a registration of the tool.
Definition: tool_base.h:92
std::string m_toolName
Name of the tool.
Definition: tool_base.h:218
T * getModel() const
Function getModel()
Definition: tool_base.h:201
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
Model changes (required full reload)
Definition: tool_base.h:82
TOOL_ID GetId() const
Function GetId() Returns the unique identifier of the tool.
Definition: tool_base.h:121
Tool is invoked after being inactive.
Definition: tool_base.h:81
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
TOOL_ID m_toolId
Unique identifier for the tool, assigned by a TOOL_MANAGER instance.
Definition: tool_base.h:214
void attachManager(TOOL_MANAGER *aManager)
Function attachManager()
Definition: tool_base.cpp:60
TOOLS_HOLDER * getToolHolderInt() 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:143
TOOL_MANAGER.
Definition: tool_manager.h:51
TOOL_TYPE
Definition: tool_base.h:46
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:56
Tool that interacts with the user
Definition: tool_base.h:49
#define NULL
virtual ~TOOL_BASE()
Definition: tool_base.h:76
TOOL_TYPE m_type
Stores the type of the tool.
Definition: tool_base.h:211
friend class TOOL_SETTINGS
Definition: tool_base.h:154
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:52
std::function< int(const TOOL_EVENT &)> TOOL_STATE_FUNC
Definition: tool_base.h:58
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:36
TOOL_BASE.
Definition: tool_base.h:67
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: eda_item.h:148
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:79
T * getEditFrame() const
Function getEditFrame()
Definition: tool_base.h:187
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:132
VIEW.
Definition: view.h:63
Rendering engine changes.
Definition: tool_base.h:83
TOOL_BASE(TOOL_TYPE aType, TOOL_ID aId, const std::string &aName=std::string(""))
Definition: tool_base.h:70