kicad/eeschema/tools/ee_tool_base.h

165 lines
5.2 KiB
C
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
2019-08-14 08:28:07 +00:00
* Copyright (C) 2019 CERN
* Copyright (C) 2019-2020 KiCad Developers, see AUTHORS.txt for contributors.
*
* 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 EE_TOOL_BASE_H
#define EE_TOOL_BASE_H
#include <tool/tool_event.h>
#include <tool/tool_interactive.h>
#include <tool/tool_manager.h>
#include <tool/tool_menu.h>
#include <tool/actions.h>
#include <tools/ee_selection_tool.h>
#include <sch_view.h>
#include <sch_edit_frame.h>
#include <lib_edit_frame.h>
#include <undo_redo_container.h>
class EE_SELECTION;
/**
* EE_TOOL_BASE
*
* A foundation class for a tool operating on a schematic or symbol
**/
template <class T>
class EE_TOOL_BASE : public TOOL_INTERACTIVE
{
public:
/**
* Constructor
*
2019-12-12 19:55:21 +00:00
* Creates a tool with given name. The name must be unique.
*/
EE_TOOL_BASE( const std::string& aName ) :
TOOL_INTERACTIVE ( aName ),
m_frame( nullptr ),
m_view( nullptr ),
m_selectionTool( nullptr ),
m_isLibEdit( false )
{};
~EE_TOOL_BASE() override {};
/// @copydoc TOOL_INTERACTIVE::Init()
bool Init() override
{
m_frame = getEditFrame<T>();
m_selectionTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
2019-06-01 19:48:01 +00:00
m_isLibEdit = m_frame->IsType( FRAME_SCH_LIB_EDITOR );
// A basic context manu. Many (but not all) tools will choose to override this.
auto& ctxMenu = m_menu.GetMenu();
// cancel current tool goes in main context menu at the top if present
ctxMenu.AddItem( ACTIONS::cancelInteractive, SELECTION_CONDITIONS::ShowAlways, 1 );
ctxMenu.AddSeparator( 1 );
// Finally, add the standard zoom/grid items
m_frame->AddStandardSubMenus( m_menu );
return true;
}
/// @copydoc TOOL_INTERACTIVE::Reset()
void Reset( RESET_REASON aReason ) override
{
if( aReason == MODEL_RELOAD )
{
// Init variables used by every drawing tool
m_frame = getEditFrame<T>();
m_isLibEdit = dynamic_cast<LIB_EDIT_FRAME*>( m_frame ) != nullptr;
}
m_view = static_cast<KIGFX::SCH_VIEW*>( getView() );
}
protected:
///> Similar to getView()->Update(), but handles items that are redrawn by their parents.
void updateView( EDA_ITEM* aItem ) const
{
KICAD_T itemType = aItem->Type();
if( itemType == SCH_PIN_T || itemType == SCH_FIELD_T || itemType == SCH_SHEET_PIN_T )
getView()->Update( aItem->GetParent() );
getView()->Update( aItem );
}
///> Similar to m_frame->SaveCopyInUndoList(), but handles items that are owned by their
///> parents.
2020-08-26 18:04:32 +00:00
void saveCopyInUndoList( EDA_ITEM* aItem, UNDO_REDO aType, bool aAppend = false )
{
KICAD_T itemType = aItem->Type();
bool selected = aItem->IsSelected();
// IS_SELECTED flag should not be set on undo items which were added for
// a drag operation.
if( selected && aItem->HasFlag( TEMP_SELECTED ) )
aItem->ClearSelected();
if( m_isLibEdit )
{
LIB_EDIT_FRAME* editFrame = dynamic_cast<LIB_EDIT_FRAME*>( m_frame );
wxASSERT( editFrame );
editFrame->SaveCopyInUndoList( static_cast<LIB_ITEM*>( aItem ), aType, aAppend );
}
else
{
SCH_EDIT_FRAME* editFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame );
wxASSERT( editFrame );
if( itemType == SCH_PIN_T || itemType == SCH_FIELD_T || itemType == SCH_SHEET_PIN_T )
{
editFrame->SaveCopyInUndoList( editFrame->GetScreen(),
static_cast<SCH_ITEM*>( aItem->GetParent() ),
2020-08-26 18:04:32 +00:00
UNDO_REDO::CHANGED, aAppend );
}
else
{
editFrame->SaveCopyInUndoList( editFrame->GetScreen(),
static_cast<SCH_ITEM*>( aItem ),
aType, aAppend );
}
}
if( selected && aItem->HasFlag( TEMP_SELECTED ) )
aItem->SetSelected();
}
protected:
T* m_frame;
KIGFX::SCH_VIEW* m_view;
EE_SELECTION_TOOL* m_selectionTool;
bool m_isLibEdit;
};
#endif