2019-05-12 11:49:58 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2021-01-26 15:23:37 +00:00
|
|
|
* Copyright (C) 2019-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2019-05-12 11:49:58 +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 EE_TOOL_BASE_H
|
|
|
|
#define EE_TOOL_BASE_H
|
|
|
|
|
2021-06-06 17:26:26 +00:00
|
|
|
#include <math/vector2d.h>
|
2019-05-12 11:49:58 +00:00
|
|
|
#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>
|
2020-10-31 01:27:16 +00:00
|
|
|
#include <symbol_edit_frame.h>
|
2019-05-12 11:49:58 +00:00
|
|
|
#include <undo_redo_container.h>
|
|
|
|
|
|
|
|
|
2019-06-08 21:48:22 +00:00
|
|
|
class EE_SELECTION;
|
2019-05-12 11:49:58 +00:00
|
|
|
|
|
|
|
/**
|
2021-01-26 15:23:37 +00:00
|
|
|
* A foundation class for a tool operating on a schematic or symbol.
|
|
|
|
*/
|
2019-05-12 11:49:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class EE_TOOL_BASE : public TOOL_INTERACTIVE
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2021-01-26 15:23:37 +00:00
|
|
|
* Create a tool with given name. The name must be unique.
|
2019-05-12 11:49:58 +00:00
|
|
|
*/
|
|
|
|
EE_TOOL_BASE( const std::string& aName ) :
|
2020-10-31 01:27:16 +00:00
|
|
|
TOOL_INTERACTIVE ( aName ),
|
|
|
|
m_frame( nullptr ),
|
|
|
|
m_view( nullptr ),
|
|
|
|
m_selectionTool( nullptr ),
|
|
|
|
m_isSymbolEditor( false )
|
2019-05-12 11:49:58 +00:00
|
|
|
{};
|
|
|
|
|
2019-06-05 19:15:57 +00:00
|
|
|
~EE_TOOL_BASE() override {};
|
2019-05-12 11:49:58 +00:00
|
|
|
|
|
|
|
/// @copydoc TOOL_INTERACTIVE::Init()
|
|
|
|
bool Init() override
|
|
|
|
{
|
|
|
|
m_frame = getEditFrame<T>();
|
|
|
|
m_selectionTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
|
2020-10-31 01:27:16 +00:00
|
|
|
m_isSymbolEditor = m_frame->IsType( FRAME_SCH_SYMBOL_EDITOR );
|
2019-05-12 11:49:58 +00:00
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
// A basic context menu. Many (but not all) tools will choose to override this.
|
2019-05-12 11:49:58 +00:00
|
|
|
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 );
|
2019-06-15 16:40:14 +00:00
|
|
|
ctxMenu.AddSeparator( 1 );
|
2019-05-12 11:49:58 +00:00
|
|
|
|
|
|
|
// Finally, add the standard zoom/grid items
|
2019-06-11 14:38:21 +00:00
|
|
|
m_frame->AddStandardSubMenus( m_menu );
|
2019-05-12 11:49:58 +00:00
|
|
|
|
|
|
|
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>();
|
2020-10-31 01:27:16 +00:00
|
|
|
m_isSymbolEditor = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame ) != nullptr;
|
2019-05-12 11:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_view = static_cast<KIGFX::SCH_VIEW*>( getView() );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-10-30 15:15:20 +00:00
|
|
|
/**
|
|
|
|
* Similar to getView()->Update(), but handles items that are redrawn by their parents
|
|
|
|
* and updating the SCH_SCREEN's RTree.
|
|
|
|
*/
|
|
|
|
void updateItem( EDA_ITEM* aItem, bool aUpdateRTree ) const
|
2019-05-12 11:49:58 +00:00
|
|
|
{
|
2020-10-30 15:15:20 +00:00
|
|
|
switch( aItem->Type() )
|
|
|
|
{
|
|
|
|
case SCH_SHEET_PIN_T:
|
|
|
|
getView()->Update( aItem );
|
|
|
|
getView()->Update( aItem->GetParent() );
|
|
|
|
|
|
|
|
// Moving sheet pins does not change the BBox.
|
|
|
|
break;
|
2019-05-12 11:49:58 +00:00
|
|
|
|
2020-10-30 15:15:20 +00:00
|
|
|
case SCH_PIN_T:
|
|
|
|
case SCH_FIELD_T:
|
|
|
|
getView()->Update( aItem );
|
2019-05-12 11:49:58 +00:00
|
|
|
getView()->Update( aItem->GetParent() );
|
|
|
|
|
2020-10-30 15:15:20 +00:00
|
|
|
if( aUpdateRTree )
|
|
|
|
m_frame->GetScreen()->Update( static_cast<SCH_ITEM*>( aItem->GetParent() ) );
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
getView()->Update( aItem );
|
2019-05-12 11:49:58 +00:00
|
|
|
|
2020-10-30 15:15:20 +00:00
|
|
|
if( aUpdateRTree )
|
|
|
|
m_frame->GetScreen()->Update( static_cast<SCH_ITEM*>( aItem ) );
|
|
|
|
}
|
|
|
|
}
|
2019-05-12 11:49:58 +00:00
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< Similar to m_frame->SaveCopyInUndoList(), but handles items that are owned by their
|
|
|
|
///< parents.
|
2022-03-16 13:43:19 +00:00
|
|
|
void saveCopyInUndoList( EDA_ITEM* aItem, UNDO_REDO aType, bool aAppend = false,
|
|
|
|
bool aDirtyConnectivity = true )
|
2019-05-12 11:49:58 +00:00
|
|
|
{
|
|
|
|
KICAD_T itemType = aItem->Type();
|
2019-12-24 22:12:16 +00:00
|
|
|
bool selected = aItem->IsSelected();
|
|
|
|
|
|
|
|
// IS_SELECTED flag should not be set on undo items which were added for
|
|
|
|
// a drag operation.
|
2022-03-03 14:35:57 +00:00
|
|
|
if( selected && aItem->HasFlag( SELECTED_BY_DRAG ) )
|
2019-12-24 22:12:16 +00:00
|
|
|
aItem->ClearSelected();
|
2019-05-12 11:49:58 +00:00
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
if( m_isSymbolEditor )
|
2019-05-12 11:49:58 +00:00
|
|
|
{
|
2020-10-31 01:27:16 +00:00
|
|
|
SYMBOL_EDIT_FRAME* editFrame = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame );
|
2022-03-27 08:31:21 +00:00
|
|
|
wxCHECK_RET( editFrame, wxT( "editFrame is null" ) );
|
2020-01-12 19:47:13 +00:00
|
|
|
|
2020-01-16 14:09:40 +00:00
|
|
|
editFrame->SaveCopyInUndoList( static_cast<LIB_ITEM*>( aItem ), aType, aAppend );
|
2019-05-12 11:49:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SCH_EDIT_FRAME* editFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame );
|
2020-01-12 19:47:13 +00:00
|
|
|
wxASSERT( editFrame );
|
2019-05-12 11:49:58 +00:00
|
|
|
|
2022-04-01 15:42:27 +00:00
|
|
|
if( editFrame )
|
2020-07-13 11:21:40 +00:00
|
|
|
{
|
2022-04-01 15:42:27 +00:00
|
|
|
if( itemType == SCH_PIN_T || itemType == SCH_FIELD_T || itemType == SCH_SHEET_PIN_T )
|
|
|
|
{
|
|
|
|
editFrame->SaveCopyInUndoList( editFrame->GetScreen(),
|
|
|
|
static_cast<SCH_ITEM*>( aItem->GetParent() ),
|
|
|
|
UNDO_REDO::CHANGED, aAppend,
|
|
|
|
aDirtyConnectivity );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
editFrame->SaveCopyInUndoList( editFrame->GetScreen(),
|
|
|
|
static_cast<SCH_ITEM*>( aItem ), aType,
|
|
|
|
aAppend, aDirtyConnectivity );
|
|
|
|
}
|
2020-07-13 11:21:40 +00:00
|
|
|
}
|
2019-05-12 11:49:58 +00:00
|
|
|
}
|
2019-12-24 22:12:16 +00:00
|
|
|
|
2022-03-03 14:35:57 +00:00
|
|
|
if( selected && aItem->HasFlag( SELECTED_BY_DRAG ) )
|
2019-12-24 22:12:16 +00:00
|
|
|
aItem->SetSelected();
|
2019-05-12 11:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
T* m_frame;
|
|
|
|
KIGFX::SCH_VIEW* m_view;
|
|
|
|
EE_SELECTION_TOOL* m_selectionTool;
|
2020-10-31 01:27:16 +00:00
|
|
|
bool m_isSymbolEditor;
|
2019-05-12 11:49:58 +00:00
|
|
|
};
|
|
|
|
|
2021-06-06 17:26:26 +00:00
|
|
|
|
|
|
|
// For LibEdit
|
|
|
|
inline VECTOR2I mapCoords( const wxPoint& aCoord )
|
|
|
|
{
|
|
|
|
return VECTOR2I( aCoord.x, -aCoord.y );
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:37:37 +00:00
|
|
|
inline VECTOR2I mapCoords( const VECTOR2I& aCoord )
|
2021-06-06 17:26:26 +00:00
|
|
|
{
|
2022-01-07 17:08:40 +00:00
|
|
|
return VECTOR2I( aCoord.x, -aCoord.y );
|
2021-06-06 17:26:26 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:37:37 +00:00
|
|
|
inline VECTOR2I mapCoords( const int x, const int y )
|
2021-06-06 17:26:26 +00:00
|
|
|
{
|
2022-01-07 17:08:40 +00:00
|
|
|
return VECTOR2I( x, -y );
|
2021-06-06 17:26:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-12 11:49:58 +00:00
|
|
|
#endif
|