summaryrefslogtreecommitdiff
path: root/include/tool/tool_base.h
blob: 584a979c6b70d3d8904801c2042a29b5e351fde9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 CERN
 * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#ifndef __TOOL_BASE_H
#define __TOOL_BASE_H

#include <cassert>
#include <base_struct.h>    // for KICAD_T

#include <tool/tool_event.h>
#include <tool/tool_settings.h>

#include <tool/delegate.h>

class EDA_ITEM;
class TOOL_MANAGER;

namespace KIGFX
{
class VIEW;
class VIEW_CONTROLS;
};

enum TOOL_TYPE
{
    ///> Tool that interacts with the user
    INTERACTIVE = 0x01,

    ///> Tool that runs in the background without any user intervention
    BATCH       = 0x02
};

/// Unique identifier for tools
typedef int TOOL_ID;
typedef DELEGATE<int, const TOOL_EVENT&> TOOL_STATE_FUNC;

/**
 * Class TOOL_BASE
 *
 * Base abstract interface for all kinds of tools.
 */

class TOOL_BASE
{
public:
    TOOL_BASE( TOOL_TYPE aType, TOOL_ID aId, const std::string& aName = std::string( "" ) ) :
        m_type( aType ),
        m_toolId( aId ),
        m_toolName( aName ),
        m_toolMgr( NULL ) {};

    virtual ~TOOL_BASE() {};

    ///> Determines the reason of reset for a tool
    enum RESET_REASON
    {
        RUN,                ///< Tool is invoked after being inactive
        MODEL_RELOAD,       ///< Model changes
        GAL_SWITCH          ///< Rendering engine changes
    };

    /**
     * Function Init()
     * Init() is called once upon a registration of the tool.
     *
     * @return True if the initialization went fine, false - otherwise.
     */
    virtual bool Init()
    {
        return true;
    }

    /**
     * Function Reset()
     * Brings the tool to a known, initial state. If the tool claimed anything from
     * the model or the view, it must release it when its reset.
     * @param aReason contains information about the reason of tool reset.
     */
    virtual void Reset( RESET_REASON aReason ) = 0;

    /**
     * Function GetType()
     * Returns the type of the tool.
     * @return The type of the tool.
     */
    TOOL_TYPE GetType() const
    {
        return m_type;
    }

    /**
     * Function GetId()
     * Returns the unique identifier of the tool. The identifier is set by an instance of
     * TOOL_MANAGER.
     * @return Identifier of the tool.
     */
    TOOL_ID GetId() const
    {
        return m_toolId;
    }

    /**
     * Function GetName()
     * Returns the name of the tool. Tool names are expected to obey the format:
     * application.ToolName (eg. pcbnew.InteractiveSelection).
     * @return The name of the tool.
     */
    const std::string& GetName() const
    {
        return m_toolName;
    }

    /**
     * Function GetManager()
     * Returns the instance of TOOL_MANAGER that takes care of the tool.
     * @return Instance of the TOOL_MANAGER. If there is no TOOL_MANAGER associated, it returns
     * NULL.
     */
    TOOL_MANAGER* GetManager() const
    {
        return m_toolMgr;
    }

    /**
     * Function SetTransitions()
     * This method is meant to be overridden in order to specify handlers for events. It is called
     * every time tool is reset or finished.
     */
    virtual void SetTransitions() {};

    TOOL_SETTINGS& GetSettings();

protected:
    friend class TOOL_MANAGER;
    friend class TOOL_SETTINGS;

    /**
     * Function attachManager()
     *
     * Sets the TOOL_MANAGER the tool will belong to.
     * Called by TOOL_MANAGER::RegisterTool()
     */
    void attachManager( TOOL_MANAGER* aManager );

    /**
     * Function getView()
     *
     * Returns the instance of VIEW object used in the application. It allows tools to draw.
     * @return The instance of VIEW.
     */
    KIGFX::VIEW* getView() const;

    /**
     * Function getViewControls()
     *
     * Returns the instance of VIEW_CONTROLS object used in the application. It allows tools to
     * read & modify user input and its settings (eg. show cursor, enable snapping to grid, etc.)
     * @return The instance of VIEW_CONTROLS.
     */
    KIGFX::VIEW_CONTROLS* getViewControls() const;

    /**
     * Function getEditFrame()
     *
     * Returns the application window object, casted to requested user type.
     */
    template <typename T>
    T* getEditFrame() const
    {
        return static_cast<T*>( getEditFrameInt() );
    }

    /**
     * Function getModel()
     *
     * Returns the model object if it matches the requested type.
     */
    template <typename T>
    T* getModel() const
    {
        EDA_ITEM* m = getModelInt();

        return static_cast<T*>( m );
    }

    ///> Stores the type of the tool.
    TOOL_TYPE m_type;

    ///> Unique identifier for the tool, assigned by a TOOL_MANAGER instance.
    TOOL_ID m_toolId;

    ///> Name of the tool. Names are expected to obey the format application.ToolName
    ///> (eg. pcbnew.InteractiveSelection).
    std::string m_toolName;
    TOOL_MANAGER* m_toolMgr;
    TOOL_SETTINGS m_toolSettings;

private:
    // hide the implementation to avoid spreading half of
    // kicad and wxWidgets headers to the tools that may not need them at all!
    EDA_ITEM* getModelInt() const;
    wxWindow* getEditFrameInt() const;
};

#endif