2017-02-23 06:23:17 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 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
|
|
|
|
*/
|
|
|
|
|
2019-05-12 11:49:58 +00:00
|
|
|
#include "pcb_tool_base.h"
|
2017-02-23 06:23:17 +00:00
|
|
|
|
|
|
|
#include <view/view_controls.h>
|
|
|
|
#include <view/view.h>
|
|
|
|
#include <tool/tool_manager.h>
|
|
|
|
#include <board_commit.h>
|
|
|
|
#include <class_module.h>
|
2017-10-30 19:17:23 +00:00
|
|
|
#include <pcb_draw_panel_gal.h>
|
2017-02-23 06:23:17 +00:00
|
|
|
#include "selection_tool.h"
|
|
|
|
#include "pcb_actions.h"
|
|
|
|
#include "tool_event_utils.h"
|
|
|
|
|
2019-05-12 11:49:58 +00:00
|
|
|
void PCB_TOOL_BASE::doInteractiveItemPlacement( INTERACTIVE_PLACER_BASE* aPlacer,
|
2019-06-05 19:15:57 +00:00
|
|
|
const wxString& aCommitMessage, int aOptions )
|
2017-02-23 06:23:17 +00:00
|
|
|
{
|
|
|
|
using namespace std::placeholders;
|
|
|
|
std::unique_ptr<BOARD_ITEM> newItem;
|
|
|
|
|
|
|
|
Activate();
|
|
|
|
|
2017-04-22 15:46:31 +00:00
|
|
|
BOARD_COMMIT commit( frame() );
|
2017-02-23 06:23:17 +00:00
|
|
|
|
|
|
|
GetManager()->RunAction( PCB_ACTIONS::selectionClear, true );
|
|
|
|
|
|
|
|
// do not capture or auto-pan until we start placing an item
|
2017-04-22 15:46:31 +00:00
|
|
|
controls()->ShowCursor( true );
|
|
|
|
controls()->SetSnapping( true );
|
2017-02-23 06:23:17 +00:00
|
|
|
|
|
|
|
// Add a VIEW_GROUP that serves as a preview for the new item
|
2019-06-08 21:48:22 +00:00
|
|
|
PCBNEW_SELECTION preview;
|
2017-04-22 15:46:31 +00:00
|
|
|
view()->Add( &preview );
|
|
|
|
|
|
|
|
aPlacer->m_board = board();
|
|
|
|
aPlacer->m_frame = frame();
|
2018-05-07 20:49:36 +00:00
|
|
|
aPlacer->m_modifiers = 0;
|
2017-04-22 15:46:31 +00:00
|
|
|
|
2018-08-01 16:58:27 +00:00
|
|
|
if( aOptions & IPO_SINGLE_CLICK && !( aOptions & IPO_PROPERTIES ) )
|
2017-04-22 15:46:31 +00:00
|
|
|
{
|
|
|
|
VECTOR2I cursorPos = controls()->GetCursorPosition();
|
|
|
|
|
|
|
|
newItem = aPlacer->CreateItem();
|
|
|
|
|
2018-10-09 22:18:15 +00:00
|
|
|
if( newItem )
|
|
|
|
{
|
|
|
|
newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
|
|
|
|
preview.Add( newItem.get() );
|
|
|
|
}
|
2017-04-22 15:46:31 +00:00
|
|
|
}
|
2017-02-23 06:23:17 +00:00
|
|
|
|
|
|
|
// Main loop: keep receiving events
|
2019-06-17 13:43:22 +00:00
|
|
|
while( TOOL_EVENT* evt = Wait() )
|
2017-02-23 06:23:17 +00:00
|
|
|
{
|
2017-04-22 15:46:31 +00:00
|
|
|
VECTOR2I cursorPos = controls()->GetCursorPosition();
|
2018-05-07 20:49:36 +00:00
|
|
|
aPlacer->m_modifiers = evt->Modifier();
|
2017-02-23 06:23:17 +00:00
|
|
|
|
2019-06-18 17:56:40 +00:00
|
|
|
if( TOOL_EVT_UTILS::IsCancelInteractive( *evt ) || evt->IsActivate() )
|
2017-02-23 06:23:17 +00:00
|
|
|
{
|
|
|
|
if( newItem )
|
|
|
|
{
|
|
|
|
// Delete the old item and have another try
|
|
|
|
newItem = nullptr;
|
|
|
|
|
|
|
|
preview.Clear();
|
|
|
|
|
2017-04-22 15:46:31 +00:00
|
|
|
controls()->SetAutoPan( false );
|
|
|
|
controls()->CaptureCursor( false );
|
|
|
|
controls()->ShowCursor( true );
|
2019-06-27 11:47:24 +00:00
|
|
|
|
|
|
|
if( aOptions & IPO_SINGLE_CLICK )
|
|
|
|
break;
|
2017-02-23 06:23:17 +00:00
|
|
|
}
|
2019-06-27 11:47:24 +00:00
|
|
|
else if( TOOL_EVT_UTILS::IsCancelInteractive( *evt ) )
|
2017-02-23 06:23:17 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( evt->IsActivate() ) // now finish unconditionally
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if( evt->IsClick( BUT_LEFT ) )
|
|
|
|
{
|
|
|
|
if( !newItem )
|
|
|
|
{
|
|
|
|
// create the item if possible
|
2017-04-22 15:46:31 +00:00
|
|
|
newItem = aPlacer->CreateItem();
|
2017-02-23 06:23:17 +00:00
|
|
|
|
|
|
|
// no item created, so wait for another click
|
|
|
|
if( !newItem )
|
|
|
|
continue;
|
|
|
|
|
2017-04-22 15:46:31 +00:00
|
|
|
controls()->CaptureCursor( true );
|
|
|
|
controls()->SetAutoPan( true );
|
2017-02-23 06:23:17 +00:00
|
|
|
|
|
|
|
newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
|
|
|
|
|
|
|
|
preview.Add( newItem.get() );
|
|
|
|
|
|
|
|
if( newItem->Type() == PCB_MODULE_T )
|
|
|
|
{
|
|
|
|
auto module = dyn_cast<MODULE*>( newItem.get() );
|
|
|
|
|
|
|
|
// modules have more drawable parts
|
|
|
|
module->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Add, &preview, _1 ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-23 15:18:44 +00:00
|
|
|
auto oldFlags = newItem->GetFlags();
|
2017-02-23 06:23:17 +00:00
|
|
|
newItem->ClearFlags();
|
|
|
|
|
2019-01-23 15:18:44 +00:00
|
|
|
if( !aPlacer->PlaceItem( newItem.get(), commit ) )
|
|
|
|
{
|
|
|
|
newItem->SetFlags( oldFlags );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
preview.Remove( newItem.get() );
|
2017-04-22 15:46:31 +00:00
|
|
|
|
2017-02-23 06:23:17 +00:00
|
|
|
if( newItem->Type() == PCB_MODULE_T )
|
|
|
|
{
|
|
|
|
auto module = dyn_cast<MODULE*>( newItem.get() );
|
|
|
|
module->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Remove, &preview, _1 ) );
|
|
|
|
}
|
|
|
|
|
2018-04-08 16:47:39 +00:00
|
|
|
newItem.release();
|
2017-02-23 06:23:17 +00:00
|
|
|
commit.Push( aCommitMessage );
|
|
|
|
|
2017-04-22 15:46:31 +00:00
|
|
|
controls()->CaptureCursor( false );
|
|
|
|
controls()->SetAutoPan( false );
|
|
|
|
controls()->ShowCursor( true );
|
2017-06-23 11:56:28 +00:00
|
|
|
|
|
|
|
if( !( aOptions & IPO_REPEAT ) )
|
2017-04-22 15:46:31 +00:00
|
|
|
break;
|
|
|
|
|
2018-08-01 16:58:27 +00:00
|
|
|
if( aOptions & IPO_SINGLE_CLICK && !( aOptions & IPO_PROPERTIES ) )
|
2017-04-22 15:46:31 +00:00
|
|
|
{
|
2017-06-23 09:36:05 +00:00
|
|
|
VECTOR2I pos = controls()->GetCursorPosition();
|
2017-04-22 15:46:31 +00:00
|
|
|
|
|
|
|
newItem = aPlacer->CreateItem();
|
|
|
|
|
2018-10-09 22:18:15 +00:00
|
|
|
if( newItem )
|
|
|
|
{
|
|
|
|
newItem->SetPosition( wxPoint( pos.x, pos.y ) );
|
|
|
|
preview.Add( newItem.get() );
|
|
|
|
}
|
2017-04-22 15:46:31 +00:00
|
|
|
}
|
2017-02-23 06:23:17 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-01 16:58:27 +00:00
|
|
|
else if( evt->IsClick( BUT_RIGHT ) )
|
|
|
|
{
|
2019-05-19 21:04:04 +00:00
|
|
|
m_menu.ShowContextMenu( selection() );
|
2018-08-01 16:58:27 +00:00
|
|
|
}
|
2017-02-23 06:23:17 +00:00
|
|
|
else if( newItem && evt->Category() == TC_COMMAND )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Handle any events that can affect the item as we move
|
|
|
|
* it around, eg rotate and flip
|
|
|
|
*/
|
|
|
|
|
2017-04-22 15:46:31 +00:00
|
|
|
if( TOOL_EVT_UTILS::IsRotateToolEvt( *evt ) && ( aOptions & IPO_ROTATE ) )
|
2017-02-23 06:23:17 +00:00
|
|
|
{
|
2018-10-21 12:50:31 +00:00
|
|
|
const int rotationAngle = TOOL_EVT_UTILS::GetEventRotationAngle( *frame(), *evt );
|
2017-02-23 06:23:17 +00:00
|
|
|
newItem->Rotate( newItem->GetPosition(), rotationAngle );
|
2017-04-22 15:46:31 +00:00
|
|
|
view()->Update( &preview );
|
2017-02-23 06:23:17 +00:00
|
|
|
}
|
2017-04-22 15:46:31 +00:00
|
|
|
else if( evt->IsAction( &PCB_ACTIONS::flip ) && ( aOptions & IPO_FLIP ) )
|
2017-02-23 06:23:17 +00:00
|
|
|
{
|
|
|
|
newItem->Flip( newItem->GetPosition() );
|
2017-04-22 15:46:31 +00:00
|
|
|
view()->Update( &preview );
|
2017-02-23 06:23:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if( newItem && evt->IsMotion() )
|
|
|
|
{
|
|
|
|
// track the cursor
|
|
|
|
newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
|
2018-04-08 16:47:39 +00:00
|
|
|
aPlacer->SnapItem( newItem.get() );
|
2017-02-23 06:23:17 +00:00
|
|
|
|
|
|
|
// Show a preview of the item
|
2017-04-22 15:46:31 +00:00
|
|
|
view()->Update( &preview );
|
2017-02-23 06:23:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-22 15:46:31 +00:00
|
|
|
view()->Remove( &preview );
|
2017-02-23 06:23:17 +00:00
|
|
|
}
|
2017-10-30 19:17:23 +00:00
|
|
|
|
2019-06-05 19:15:57 +00:00
|
|
|
|
2019-05-12 11:49:58 +00:00
|
|
|
bool PCB_TOOL_BASE::Init()
|
2018-08-01 16:58:27 +00:00
|
|
|
{
|
|
|
|
// 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 );
|
2019-06-15 16:40:14 +00:00
|
|
|
ctxMenu.AddSeparator( 1 );
|
2018-08-01 16:58:27 +00:00
|
|
|
|
|
|
|
// Finally, add the standard zoom/grid items
|
2019-06-11 14:38:21 +00:00
|
|
|
getEditFrame<PCB_BASE_FRAME>()->AddStandardSubMenus( m_menu );
|
2018-08-01 16:58:27 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-12 11:49:58 +00:00
|
|
|
void PCB_TOOL_BASE::Reset( RESET_REASON aReason )
|
2017-10-31 11:01:59 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-05 19:15:57 +00:00
|
|
|
|
2019-05-12 11:49:58 +00:00
|
|
|
void PCB_TOOL_BASE::setTransitions()
|
2017-10-31 11:01:59 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-05 19:15:57 +00:00
|
|
|
|
2019-05-12 11:49:58 +00:00
|
|
|
PCB_DISPLAY_OPTIONS* PCB_TOOL_BASE::displayOptions() const
|
2017-10-30 19:17:23 +00:00
|
|
|
{
|
|
|
|
return static_cast<PCB_DISPLAY_OPTIONS*>( frame()->GetDisplayOptions() );
|
|
|
|
}
|
|
|
|
|
2019-05-12 11:49:58 +00:00
|
|
|
PCB_DRAW_PANEL_GAL* PCB_TOOL_BASE::canvas() const
|
2017-10-30 19:17:23 +00:00
|
|
|
{
|
2019-06-13 17:28:55 +00:00
|
|
|
return static_cast<PCB_DRAW_PANEL_GAL*>( frame()->GetCanvas() );
|
2017-10-30 19:17:23 +00:00
|
|
|
}
|
2017-11-30 16:21:28 +00:00
|
|
|
|
2019-06-05 19:15:57 +00:00
|
|
|
|
2019-06-08 21:48:22 +00:00
|
|
|
const PCBNEW_SELECTION& PCB_TOOL_BASE::selection() const
|
2017-11-30 16:21:28 +00:00
|
|
|
{
|
|
|
|
auto selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
|
|
|
|
const auto& selection = selTool->GetSelection();
|
|
|
|
return selection;
|
|
|
|
}
|
|
|
|
|
2019-06-05 19:15:57 +00:00
|
|
|
|
2019-06-08 21:48:22 +00:00
|
|
|
PCBNEW_SELECTION& PCB_TOOL_BASE::selection()
|
2017-11-30 16:21:28 +00:00
|
|
|
{
|
|
|
|
auto selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
|
|
|
|
auto& selection = selTool->GetSelection();
|
|
|
|
return selection;
|
|
|
|
}
|
2018-04-08 16:47:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
void INTERACTIVE_PLACER_BASE::SnapItem( BOARD_ITEM *aItem )
|
|
|
|
{
|
|
|
|
// Base implementation performs no snapping
|
|
|
|
}
|
|
|
|
|
2019-06-05 19:15:57 +00:00
|
|
|
|
2019-01-23 15:18:44 +00:00
|
|
|
bool INTERACTIVE_PLACER_BASE::PlaceItem( BOARD_ITEM *aItem, BOARD_COMMIT& aCommit )
|
2018-04-08 16:47:39 +00:00
|
|
|
{
|
|
|
|
aCommit.Add( aItem );
|
2019-01-23 15:18:44 +00:00
|
|
|
return true;
|
2018-04-08 16:47:39 +00:00
|
|
|
}
|