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  * Copyright (C) 2016-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #ifndef __TOOL_BASE_H
28 #define __TOOL_BASE_H
29 
30 #include <cassert>
31 #include <eda_item.h> // for KICAD_T
32 
33 #include <tool/tool_event.h>
34 
35 #include <functional>
36 
37 class EDA_ITEM;
38 class TOOL_MANAGER;
39 class TOOLS_HOLDER;
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 
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 
90  virtual bool Init()
91  {
92  return true;
93  }
94 
102  virtual void Reset( RESET_REASON aReason ) = 0;
103 
110  {
111  return m_type;
112  }
113 
121  TOOL_ID GetId() const
122  {
123  return m_toolId;
124  }
125 
134  const std::string& GetName() const
135  {
136  return m_toolName;
137  }
138 
145  {
146  return m_toolMgr;
147  }
148 
149  //TOOL_SETTINGS& GetAdapter();
150 
151  bool IsToolActive() const;
152 
153 protected:
154  friend class TOOL_MANAGER;
155  friend class TOOL_SETTINGS;
156 
162  void attachManager( TOOL_MANAGER* aManager );
163 
169  KIGFX::VIEW* getView() const;
170 
180 
184  template <typename T>
185  T* getEditFrame() const
186  {
187 #if !defined( QA_TEST ) // Dynamic casts give the linker a siezure in the test framework
188  wxASSERT( dynamic_cast<T*>( getToolHolderInt() ) );
189 #endif
190  return static_cast<T*>( getToolHolderInt() );
191  }
192 
196  template <typename T>
197  T* getModel() const
198  {
199  EDA_ITEM* m = getModelInt();
200 #if !defined( QA_TEST ) // Dynamic casts give the linker a siezure in the test framework
201  wxASSERT( dynamic_cast<T*>( m ) );
202 #endif
203  return static_cast<T*>( m );
204  }
205 
208 
211 
214  std::string m_toolName;
216  //TOOL_SETTINGS m_toolSettings;
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;
223 };
224 
225 #endif
TOOL_TYPE GetType() const
Return the type of the tool.
Definition: tool_base.h:109
virtual bool Init()
Init() is called once upon a registration of the tool.
Definition: tool_base.h:90
std::string m_toolName
Definition: tool_base.h:214
T * getModel() const
Return the model object if it matches the requested type.
Definition: tool_base.h:197
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
Model changes (required full reload)
Definition: tool_base.h:81
TOOL_ID GetId() const
Return the unique identifier of the tool.
Definition: tool_base.h:121
Tool is invoked after being inactive.
Definition: tool_base.h:80
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:215
TOOL_ID m_toolId
Name of the tool.
Definition: tool_base.h:210
void attachManager(TOOL_MANAGER *aManager)
Set the TOOL_MANAGER the tool will belong to.
Definition: tool_base.cpp:60
TOOLS_HOLDER * getToolHolderInt() const
Definition: tool_base.cpp:48
TOOL_MANAGER * GetManager() const
Return the instance of TOOL_MANAGER that takes care of the tool.
Definition: tool_base.h:144
Master controller class:
Definition: tool_manager.h:52
TOOL_TYPE
Definition: tool_base.h:47
virtual void Reset(RESET_REASON aReason)=0
Bring 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
#define NULL
virtual ~TOOL_BASE()
Definition: tool_base.h:75
TOOL_TYPE m_type
Unique identifier for the tool, assigned by a TOOL_MANAGER instance.
Definition: tool_base.h:207
friend class TOOL_SETTINGS
Definition: tool_base.h:155
Generic, UI-independent tool event.
Definition: tool_event.h:173
An interface for classes handling user events controlling the view behavior such as zooming,...
EDA_ITEM * getModelInt() const
Definition: tool_base.cpp:54
std::function< int(const TOOL_EVENT &)> TOOL_STATE_FUNC
Definition: tool_base.h:59
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:36
Base abstract interface for all kinds of tools.
Definition: tool_base.h:66
bool IsToolActive() const
Definition: tool_base.cpp:31
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:150
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
T * getEditFrame() const
Return the application window object, casted to requested user type.
Definition: tool_base.h:185
KIGFX::VIEW_CONTROLS * getViewControls() const
Return the instance of VIEW_CONTROLS object used in the application.
Definition: tool_base.cpp:42
const std::string & GetName() const
Return the name of the tool.
Definition: tool_base.h:134
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
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