2019-05-12 12:49:58 +01:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2019-08-14 09:28:07 +01:00
|
|
|
* Copyright (C) 2019 CERN
|
2025-01-01 13:30:11 -08:00
|
|
|
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
|
2019-05-12 12:49:58 +01: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
|
|
|
|
*/
|
|
|
|
|
2025-08-17 12:18:28 +01:00
|
|
|
#pragma once
|
2019-05-12 12:49:58 +01:00
|
|
|
|
2025-08-18 12:43:57 +01:00
|
|
|
#include "increment.h"
|
2021-06-06 18:26:26 +01:00
|
|
|
#include <math/vector2d.h>
|
2019-05-12 12:49:58 +01: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>
|
2025-03-12 14:41:25 +00:00
|
|
|
#include <tools/sch_selection_tool.h>
|
2019-05-12 12:49:58 +01:00
|
|
|
#include <sch_edit_frame.h>
|
2023-04-28 17:02:42 -07:00
|
|
|
#include <sch_view.h>
|
2020-10-31 01:27:16 +00:00
|
|
|
#include <symbol_edit_frame.h>
|
2025-04-07 11:16:13 +01:00
|
|
|
#include <sch_shape.h>
|
2025-08-18 12:43:57 +01:00
|
|
|
#include <pin_layout_cache.h>
|
|
|
|
#include <sch_commit.h>
|
|
|
|
#include <tool/picker_tool.h>
|
2025-08-19 09:16:08 +02:00
|
|
|
#include <view/view_controls.h>
|
2019-05-12 12:49:58 +01:00
|
|
|
|
2025-03-12 14:41:25 +00:00
|
|
|
class SCH_SELECTION;
|
2019-05-12 12:49:58 +01:00
|
|
|
|
|
|
|
/**
|
2021-01-26 10:23:37 -05:00
|
|
|
* A foundation class for a tool operating on a schematic or symbol.
|
|
|
|
*/
|
2019-05-12 12:49:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
2025-03-12 14:41:25 +00:00
|
|
|
class SCH_TOOL_BASE : public TOOL_INTERACTIVE
|
2019-05-12 12:49:58 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2021-01-26 10:23:37 -05:00
|
|
|
* Create a tool with given name. The name must be unique.
|
2019-05-12 12:49:58 +01:00
|
|
|
*/
|
2025-03-12 14:41:25 +00:00
|
|
|
SCH_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 ),
|
2025-08-18 12:43:57 +01:00
|
|
|
m_isSymbolEditor( false ),
|
|
|
|
m_pickerItem( nullptr )
|
2019-05-12 12:49:58 +01:00
|
|
|
{};
|
|
|
|
|
2025-03-12 14:41:25 +00:00
|
|
|
~SCH_TOOL_BASE() override {};
|
2019-05-12 12:49:58 +01:00
|
|
|
|
|
|
|
/// @copydoc TOOL_INTERACTIVE::Init()
|
|
|
|
bool Init() override
|
|
|
|
{
|
|
|
|
m_frame = getEditFrame<T>();
|
2025-03-12 14:41:25 +00:00
|
|
|
m_selectionTool = m_toolMgr->GetTool<SCH_SELECTION_TOOL>();
|
2020-10-31 01:27:16 +00:00
|
|
|
m_isSymbolEditor = m_frame->IsType( FRAME_SCH_SYMBOL_EDITOR );
|
2019-05-12 12:49:58 +01:00
|
|
|
|
2021-01-26 10:23:37 -05:00
|
|
|
// A basic context menu. Many (but not all) tools will choose to override this.
|
2024-07-30 17:57:33 +02:00
|
|
|
auto& ctxMenu = m_menu->GetMenu();
|
2019-05-12 12:49:58 +01:00
|
|
|
|
|
|
|
// cancel current tool goes in main context menu at the top if present
|
|
|
|
ctxMenu.AddItem( ACTIONS::cancelInteractive, SELECTION_CONDITIONS::ShowAlways, 1 );
|
2019-06-15 17:40:14 +01:00
|
|
|
ctxMenu.AddSeparator( 1 );
|
2019-05-12 12:49:58 +01:00
|
|
|
|
|
|
|
// Finally, add the standard zoom/grid items
|
2024-07-30 17:57:33 +02:00
|
|
|
m_frame->AddStandardSubMenus( *m_menu.get() );
|
2019-05-12 12:49:58 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @copydoc TOOL_INTERACTIVE::Reset()
|
|
|
|
void Reset( RESET_REASON aReason ) override
|
|
|
|
{
|
2023-04-12 11:50:56 +01:00
|
|
|
if( aReason == MODEL_RELOAD || aReason == SUPERMODEL_RELOAD )
|
2019-05-12 12:49:58 +01:00
|
|
|
{
|
|
|
|
// 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 12:49:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m_view = static_cast<KIGFX::SCH_VIEW*>( getView() );
|
|
|
|
}
|
|
|
|
|
2023-04-28 17:02:42 -07:00
|
|
|
/**
|
|
|
|
* Returns true if the tool is running in the symbol editor
|
|
|
|
*/
|
|
|
|
bool IsSymbolEditor() const
|
|
|
|
{
|
|
|
|
return m_isSymbolEditor;
|
|
|
|
}
|
|
|
|
|
2025-08-18 12:43:57 +01:00
|
|
|
int Increment( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
|
|
|
static const std::vector<KICAD_T> incrementable = { SCH_LABEL_T,
|
|
|
|
SCH_GLOBAL_LABEL_T,
|
|
|
|
SCH_HIER_LABEL_T,
|
|
|
|
SCH_PIN_T,
|
|
|
|
SCH_TEXT_T };
|
|
|
|
|
|
|
|
const ACTIONS::INCREMENT param = { 1, 0 };
|
|
|
|
|
|
|
|
if( aEvent.HasParameter() )
|
|
|
|
aEvent.Parameter<ACTIONS::INCREMENT>();
|
|
|
|
|
|
|
|
SCH_SELECTION& selection = m_selectionTool->RequestSelection( incrementable );
|
|
|
|
|
|
|
|
if( selection.Empty() )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
KICAD_T type = selection.Front()->Type();
|
|
|
|
bool allSameType = true;
|
|
|
|
|
|
|
|
for( EDA_ITEM* item : selection )
|
|
|
|
{
|
|
|
|
if( item->Type() != type )
|
|
|
|
{
|
|
|
|
allSameType = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Incrementing multiple types at once seems confusing though it would work.
|
|
|
|
if( !allSameType )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const VECTOR2I mousePosition = getViewControls()->GetMousePosition();
|
|
|
|
|
|
|
|
STRING_INCREMENTER incrementer;
|
|
|
|
// In schematics, it's probably less common to be operating
|
|
|
|
// on pin numbers which are usually IOSQXZ-skippy.
|
|
|
|
incrementer.SetSkipIOSQXZ( m_isSymbolEditor );
|
|
|
|
|
|
|
|
// If we're coming via another action like 'Move', use that commit
|
|
|
|
SCH_COMMIT localCommit( m_toolMgr );
|
|
|
|
SCH_COMMIT* commit = dynamic_cast<SCH_COMMIT*>( aEvent.Commit() );
|
|
|
|
|
|
|
|
if( !commit )
|
|
|
|
commit = &localCommit;
|
|
|
|
|
|
|
|
const auto modifyItem =
|
|
|
|
[&]( EDA_ITEM& aItem )
|
|
|
|
{
|
|
|
|
if( aItem.IsNew() )
|
|
|
|
m_toolMgr->PostAction( ACTIONS::refreshPreview );
|
|
|
|
|
|
|
|
commit->Modify( &aItem, m_frame->GetScreen() );
|
|
|
|
};
|
|
|
|
|
|
|
|
for( EDA_ITEM* item : selection )
|
|
|
|
{
|
|
|
|
switch( item->Type() )
|
|
|
|
{
|
|
|
|
case SCH_PIN_T:
|
|
|
|
{
|
|
|
|
SCH_PIN& pin = static_cast<SCH_PIN&>( *item );
|
|
|
|
PIN_LAYOUT_CACHE& layout = pin.GetLayoutCache();
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
OPT_BOX2I bbox = layout.GetPinNumberBBox();
|
|
|
|
|
|
|
|
if( bbox && bbox->Contains( mousePosition ) )
|
|
|
|
{
|
|
|
|
std::optional<wxString> nextNumber = incrementer.Increment( pin.GetNumber(), param.Delta,
|
|
|
|
param.Index );
|
|
|
|
|
|
|
|
if( nextNumber )
|
|
|
|
{
|
|
|
|
modifyItem( pin );
|
|
|
|
pin.SetNumber( *nextNumber );
|
|
|
|
}
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !found )
|
|
|
|
{
|
|
|
|
bbox = layout.GetPinNameBBox();
|
|
|
|
|
|
|
|
if( bbox && bbox->Contains( mousePosition ) )
|
|
|
|
{
|
|
|
|
std::optional<wxString> nextName = incrementer.Increment( pin.GetName(), param.Delta,
|
|
|
|
param.Index );
|
|
|
|
|
|
|
|
if( nextName )
|
|
|
|
{
|
|
|
|
modifyItem( pin );
|
|
|
|
pin.SetName( *nextName );
|
|
|
|
}
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SCH_LABEL_T:
|
|
|
|
case SCH_GLOBAL_LABEL_T:
|
|
|
|
case SCH_HIER_LABEL_T:
|
|
|
|
case SCH_TEXT_T:
|
|
|
|
{
|
|
|
|
SCH_TEXT& label = static_cast<SCH_TEXT&>( *item );
|
|
|
|
|
|
|
|
std::optional<wxString> newLabel = incrementer.Increment( label.GetText(), param.Delta,
|
|
|
|
param.Index );
|
|
|
|
|
|
|
|
if( newLabel )
|
|
|
|
{
|
|
|
|
modifyItem( label );
|
|
|
|
label.SetText( *newLabel );
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// No increment for other items
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
commit->Push( _( "Increment" ) );
|
|
|
|
|
|
|
|
if( selection.IsHover() )
|
|
|
|
m_toolMgr->RunAction( ACTIONS::selectionClear );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int InteractiveDelete( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
|
|
|
static std::vector<KICAD_T> deletableItems =
|
|
|
|
{
|
|
|
|
LIB_SYMBOL_T,
|
|
|
|
SCH_MARKER_T,
|
|
|
|
SCH_JUNCTION_T,
|
|
|
|
SCH_LINE_T,
|
|
|
|
SCH_BUS_BUS_ENTRY_T,
|
|
|
|
SCH_BUS_WIRE_ENTRY_T,
|
|
|
|
SCH_SHAPE_T,
|
|
|
|
SCH_RULE_AREA_T,
|
|
|
|
SCH_TEXT_T,
|
|
|
|
SCH_TEXTBOX_T,
|
|
|
|
SCH_TABLECELL_T, // Clear contents
|
|
|
|
SCH_TABLE_T,
|
|
|
|
SCH_LABEL_T,
|
|
|
|
SCH_GLOBAL_LABEL_T,
|
|
|
|
SCH_HIER_LABEL_T,
|
|
|
|
SCH_DIRECTIVE_LABEL_T,
|
|
|
|
SCH_NO_CONNECT_T,
|
|
|
|
SCH_SHEET_T,
|
|
|
|
SCH_SHEET_PIN_T,
|
|
|
|
SCH_SYMBOL_T,
|
|
|
|
SCH_FIELD_T, // Will be hidden
|
|
|
|
SCH_BITMAP_T,
|
|
|
|
SCH_GROUP_T
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
PICKER_TOOL* picker = m_toolMgr->GetTool<PICKER_TOOL>();
|
|
|
|
|
|
|
|
m_toolMgr->RunAction( ACTIONS::selectionClear );
|
|
|
|
m_pickerItem = nullptr;
|
|
|
|
|
|
|
|
// Deactivate other tools; particularly important if another PICKER is currently running
|
|
|
|
Activate();
|
|
|
|
|
|
|
|
picker->SetCursor( KICURSOR::REMOVE );
|
|
|
|
picker->SetSnapping( false );
|
|
|
|
picker->ClearHandlers();
|
|
|
|
|
|
|
|
picker->SetClickHandler(
|
|
|
|
[this]( const VECTOR2D& aPosition ) -> bool
|
|
|
|
{
|
|
|
|
if( m_pickerItem )
|
|
|
|
{
|
|
|
|
SCH_SELECTION_TOOL* selectionTool = m_toolMgr->GetTool<SCH_SELECTION_TOOL>();
|
|
|
|
selectionTool->UnbrightenItem( m_pickerItem );
|
|
|
|
selectionTool->AddItemToSel( m_pickerItem, true /*quiet mode*/ );
|
|
|
|
m_toolMgr->RunAction( ACTIONS::doDelete );
|
|
|
|
m_pickerItem = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} );
|
|
|
|
|
|
|
|
picker->SetMotionHandler(
|
|
|
|
[this]( const VECTOR2D& aPos )
|
|
|
|
{
|
|
|
|
SCH_SELECTION_TOOL* selectionTool = m_toolMgr->GetTool<SCH_SELECTION_TOOL>();
|
|
|
|
SCH_COLLECTOR collector;
|
|
|
|
|
|
|
|
selectionTool->CollectHits( collector, aPos, deletableItems );
|
|
|
|
|
|
|
|
// Remove unselectable items
|
|
|
|
for( int i = collector.GetCount() - 1; i >= 0; --i )
|
|
|
|
{
|
|
|
|
if( !selectionTool->Selectable( collector[ i ] ) )
|
|
|
|
collector.Remove( i );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( collector.GetCount() > 1 )
|
|
|
|
selectionTool->GuessSelectionCandidates( collector, aPos );
|
|
|
|
|
|
|
|
EDA_ITEM* item = collector.GetCount() == 1 ? collector[ 0 ] : nullptr;
|
|
|
|
|
|
|
|
if( m_pickerItem != item )
|
|
|
|
{
|
|
|
|
if( m_pickerItem )
|
|
|
|
selectionTool->UnbrightenItem( m_pickerItem );
|
|
|
|
|
|
|
|
m_pickerItem = item;
|
|
|
|
|
|
|
|
if( m_pickerItem )
|
|
|
|
selectionTool->BrightenItem( m_pickerItem );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
picker->SetFinalizeHandler(
|
|
|
|
[this]( const int& aFinalState )
|
|
|
|
{
|
|
|
|
if( m_pickerItem )
|
|
|
|
m_toolMgr->GetTool<SCH_SELECTION_TOOL>()->UnbrightenItem( m_pickerItem );
|
|
|
|
|
|
|
|
// Wake the selection tool after exiting to ensure the cursor gets updated
|
|
|
|
m_toolMgr->PostAction( ACTIONS::selectionActivate );
|
|
|
|
} );
|
|
|
|
|
|
|
|
m_toolMgr->RunAction( ACTIONS::pickerTool, &aEvent );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-12 12:49:58 +01:00
|
|
|
protected:
|
2020-10-30 15:15:20 +00:00
|
|
|
/**
|
2025-08-17 12:18:28 +01:00
|
|
|
* Similar to getView()->Update(), but also updates the SCH_SCREEN's RTree.
|
2020-10-30 15:15:20 +00:00
|
|
|
*/
|
|
|
|
void updateItem( EDA_ITEM* aItem, bool aUpdateRTree ) const
|
2019-05-12 12:49:58 +01:00
|
|
|
{
|
2025-04-07 11:16:13 +01:00
|
|
|
m_frame->UpdateItem( aItem, false, aUpdateRTree );
|
2020-10-30 15:15:20 +00:00
|
|
|
}
|
2019-05-12 12:49:58 +01:00
|
|
|
|
2025-08-17 12:18:28 +01:00
|
|
|
///< Similar to m_frame->SaveCopyInUndoList(), but also handles connectivity.
|
|
|
|
void saveCopyInUndoList( EDA_ITEM* aItem, UNDO_REDO aType, bool aAppend = false, bool aDirtyConnectivity = true )
|
2019-05-12 12:49:58 +01:00
|
|
|
{
|
2025-08-17 12:18:28 +01:00
|
|
|
if( !aItem->IsSCH_ITEM() )
|
|
|
|
return;
|
2022-05-06 12:51:26 +02:00
|
|
|
|
2025-08-17 12:18:28 +01:00
|
|
|
SCH_ITEM* item = static_cast<SCH_ITEM*>( aItem );
|
|
|
|
bool selected = item->IsSelected();
|
2019-12-24 22:12:16 +00:00
|
|
|
|
|
|
|
// IS_SELECTED flag should not be set on undo items which were added for
|
|
|
|
// a drag operation.
|
2025-08-17 12:18:28 +01:00
|
|
|
if( selected && item->HasFlag( SELECTED_BY_DRAG ) )
|
|
|
|
item->ClearSelected();
|
2019-05-12 12:49:58 +01:00
|
|
|
|
2025-08-17 12:18:28 +01:00
|
|
|
if( SYMBOL_EDIT_FRAME* symbolEditFrame = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame ) )
|
2019-05-12 12:49:58 +01:00
|
|
|
{
|
2025-08-17 12:18:28 +01:00
|
|
|
symbolEditFrame->SaveCopyInUndoList( wxEmptyString, dynamic_cast<LIB_SYMBOL*>( item ) );
|
2019-05-12 12:49:58 +01:00
|
|
|
}
|
2025-08-17 12:18:28 +01:00
|
|
|
else if( SCH_EDIT_FRAME* schematicFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame ) )
|
2019-05-12 12:49:58 +01:00
|
|
|
{
|
2025-08-17 12:18:28 +01:00
|
|
|
schematicFrame->SaveCopyInUndoList( schematicFrame->GetScreen(), item, UNDO_REDO::CHANGED, aAppend );
|
2019-05-12 12:49:58 +01:00
|
|
|
|
2025-08-17 12:18:28 +01:00
|
|
|
if( aDirtyConnectivity )
|
2020-07-13 12:21:40 +01:00
|
|
|
{
|
2025-08-17 12:18:28 +01:00
|
|
|
if( !item->IsConnectivityDirty()
|
|
|
|
&& item->Connection()
|
|
|
|
&& ( item->Connection()->Name() == schematicFrame->GetHighlightedConnection()
|
|
|
|
|| item->Connection()->HasDriverChanged() ) )
|
2023-02-07 12:05:41 +00:00
|
|
|
{
|
2025-08-17 12:18:28 +01:00
|
|
|
schematicFrame->DirtyHighlightedConnection();
|
2022-04-01 17:42:27 +02:00
|
|
|
}
|
2025-08-17 12:18:28 +01:00
|
|
|
|
|
|
|
item->SetConnectivityDirty();
|
2020-07-13 12:21:40 +01:00
|
|
|
}
|
2019-05-12 12:49:58 +01:00
|
|
|
}
|
2019-12-24 22:12:16 +00:00
|
|
|
|
2022-03-03 09:35:57 -05:00
|
|
|
if( selected && aItem->HasFlag( SELECTED_BY_DRAG ) )
|
2019-12-24 22:12:16 +00:00
|
|
|
aItem->SetSelected();
|
2019-05-12 12:49:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2025-03-12 14:41:25 +00:00
|
|
|
T* m_frame;
|
|
|
|
KIGFX::SCH_VIEW* m_view;
|
|
|
|
SCH_SELECTION_TOOL* m_selectionTool;
|
|
|
|
bool m_isSymbolEditor;
|
2025-08-18 12:43:57 +01:00
|
|
|
EDA_ITEM* m_pickerItem;
|
2019-05-12 12:49:58 +01:00
|
|
|
};
|