2013-08-02 14:46:53 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
2016-07-04 22:14:32 +00:00
|
|
|
* Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-08-02 14:46:53 +00:00
|
|
|
*
|
|
|
|
* 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>
|
2013-08-07 09:20:12 +00:00
|
|
|
#include <base_struct.h> // for KICAD_T
|
2013-08-02 14:46:53 +00:00
|
|
|
|
|
|
|
#include <tool/tool_event.h>
|
2015-07-02 14:09:51 +00:00
|
|
|
#include <tool/tool_settings.h>
|
|
|
|
|
2016-07-04 22:14:32 +00:00
|
|
|
#include <functional>
|
2013-08-02 14:46:53 +00:00
|
|
|
|
|
|
|
class EDA_ITEM;
|
|
|
|
class TOOL_MANAGER;
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
namespace KIGFX
|
2013-08-07 09:20:12 +00:00
|
|
|
{
|
|
|
|
class VIEW;
|
|
|
|
class VIEW_CONTROLS;
|
2013-08-02 14:46:53 +00:00
|
|
|
};
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
enum TOOL_TYPE
|
2013-08-07 09:20:12 +00:00
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
///> Tool that interacts with the user
|
2013-10-14 18:40:36 +00:00
|
|
|
INTERACTIVE = 0x01,
|
2013-09-27 14:23:43 +00:00
|
|
|
|
|
|
|
///> Tool that runs in the background without any user intervention
|
2013-10-14 18:40:36 +00:00
|
|
|
BATCH = 0x02
|
2013-08-02 14:46:53 +00:00
|
|
|
};
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
/// Unique identifier for tools
|
2013-08-02 14:46:53 +00:00
|
|
|
typedef int TOOL_ID;
|
2016-07-04 22:14:32 +00:00
|
|
|
|
|
|
|
using TOOL_STATE_FUNC = std::function<int(const TOOL_EVENT&)>;
|
|
|
|
|
2013-08-02 14:46:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class TOOL_BASE
|
|
|
|
*
|
2013-09-27 14:23:43 +00:00
|
|
|
* Base abstract interface for all kinds of tools.
|
2013-08-02 14:46:53 +00:00
|
|
|
*/
|
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
class TOOL_BASE
|
2013-08-02 14:46:53 +00:00
|
|
|
{
|
|
|
|
public:
|
2013-10-14 18:40:36 +00:00
|
|
|
TOOL_BASE( TOOL_TYPE aType, TOOL_ID aId, const std::string& aName = std::string( "" ) ) :
|
2013-10-14 11:43:57 +00:00
|
|
|
m_type( aType ),
|
|
|
|
m_toolId( aId ),
|
|
|
|
m_toolName( aName ),
|
2013-10-14 14:13:35 +00:00
|
|
|
m_toolMgr( NULL ) {};
|
2013-10-14 11:43:57 +00:00
|
|
|
|
|
|
|
virtual ~TOOL_BASE() {};
|
|
|
|
|
2013-12-09 09:42:38 +00:00
|
|
|
///> 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;
|
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
/**
|
|
|
|
* Function GetType()
|
|
|
|
* Returns the type of the tool.
|
|
|
|
* @return The type of the tool.
|
|
|
|
*/
|
2013-10-14 18:40:36 +00:00
|
|
|
TOOL_TYPE GetType() const
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-04-30 08:46:03 +00:00
|
|
|
/**
|
|
|
|
* 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() {};
|
|
|
|
|
2015-07-02 14:09:51 +00:00
|
|
|
TOOL_SETTINGS& GetSettings();
|
|
|
|
|
2013-08-02 14:46:53 +00:00
|
|
|
protected:
|
2013-10-14 11:43:57 +00:00
|
|
|
friend class TOOL_MANAGER;
|
2015-07-02 14:09:51 +00:00
|
|
|
friend class TOOL_SETTINGS;
|
2013-10-14 11:43:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-10-14 14:13:35 +00:00
|
|
|
KIGFX::VIEW* getView() const;
|
2013-10-14 11:43:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-10-14 14:13:35 +00:00
|
|
|
KIGFX::VIEW_CONTROLS* getViewControls() const;
|
2013-10-14 11:43:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function getEditFrame()
|
|
|
|
*
|
|
|
|
* Returns the application window object, casted to requested user type.
|
|
|
|
*/
|
2013-10-14 14:13:35 +00:00
|
|
|
template <typename T>
|
2013-10-14 11:43:57 +00:00
|
|
|
T* getEditFrame() const
|
|
|
|
{
|
|
|
|
return static_cast<T*>( getEditFrameInt() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function getModel()
|
|
|
|
*
|
|
|
|
* Returns the model object if it matches the requested type.
|
|
|
|
*/
|
2013-10-14 14:13:35 +00:00
|
|
|
template <typename T>
|
2014-06-06 12:59:25 +00:00
|
|
|
T* getModel() const
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
|
|
|
EDA_ITEM* m = getModelInt();
|
|
|
|
|
|
|
|
return static_cast<T*>( m );
|
|
|
|
}
|
|
|
|
|
|
|
|
///> Stores the type of the tool.
|
2013-10-14 18:40:36 +00:00
|
|
|
TOOL_TYPE m_type;
|
2013-10-14 11:43:57 +00:00
|
|
|
|
|
|
|
///> 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;
|
2015-07-02 14:09:51 +00:00
|
|
|
TOOL_SETTINGS m_toolSettings;
|
2013-08-02 14:46:53 +00:00
|
|
|
|
|
|
|
private:
|
2013-10-14 11:43:57 +00:00
|
|
|
// 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;
|
2013-08-02 14:46:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|