2015-06-18 17:51:51 +02: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.
|
2015-06-18 17:51:51 +02: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
|
|
|
|
*/
|
|
|
|
|
2019-07-16 00:44:01 +01:00
|
|
|
#include <tool/actions.h>
|
|
|
|
#include <tool/picker_tool.h>
|
2019-05-20 11:23:32 +01:00
|
|
|
#include <view/view_controls.h>
|
2019-07-16 00:44:01 +01:00
|
|
|
#include <eda_draw_frame.h>
|
2023-05-01 11:01:48 -04:00
|
|
|
#include <wx/debug.h>
|
2018-02-11 01:15:16 +00:00
|
|
|
|
2017-02-20 13:10:20 -05:00
|
|
|
|
2021-01-31 09:50:19 -05:00
|
|
|
void PICKER_TOOL_BASE::reset()
|
|
|
|
{
|
|
|
|
m_cursor = KICURSOR::ARROW;
|
|
|
|
m_snap = true;
|
|
|
|
|
2022-08-25 15:50:47 -07:00
|
|
|
m_picked = std::nullopt;
|
|
|
|
m_clickHandler = std::nullopt;
|
|
|
|
m_motionHandler = std::nullopt;
|
|
|
|
m_cancelHandler = std::nullopt;
|
|
|
|
m_finalizeHandler = std::nullopt;
|
2021-01-31 09:50:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PICKER_TOOL::PICKER_TOOL( const std::string& aName ) :
|
|
|
|
TOOL_INTERACTIVE( aName ),
|
|
|
|
PICKER_TOOL_BASE()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-13 19:39:49 +00:00
|
|
|
PICKER_TOOL::PICKER_TOOL() :
|
|
|
|
TOOL_INTERACTIVE( "common.InteractivePicker" ),
|
2021-01-31 09:50:19 -05:00
|
|
|
PICKER_TOOL_BASE()
|
2015-06-18 17:51:51 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-16 00:44:01 +01:00
|
|
|
bool PICKER_TOOL::Init()
|
2019-05-20 11:23:32 +01:00
|
|
|
{
|
2019-07-16 00:44:01 +01:00
|
|
|
m_frame = getEditFrame<EDA_DRAW_FRAME>();
|
2019-05-20 11:23:32 +01:00
|
|
|
|
2024-07-30 17:57:33 +02:00
|
|
|
auto& ctxMenu = m_menu->GetMenu();
|
2019-05-20 11:23:32 +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-20 11:23:32 +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-20 11:23:32 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-16 00:44:01 +01:00
|
|
|
int PICKER_TOOL::Main( const TOOL_EVENT& aEvent )
|
2015-06-18 17:51:51 +02:00
|
|
|
{
|
|
|
|
KIGFX::VIEW_CONTROLS* controls = getViewControls();
|
2018-11-20 20:10:46 -08:00
|
|
|
int finalize_state = WAIT_CANCEL;
|
|
|
|
|
2023-05-01 11:01:48 -04:00
|
|
|
wxCHECK_MSG( aEvent.Parameter<const TOOL_EVENT*>(), -1,
|
|
|
|
wxT( "PICKER_TOOL::Main() called without a source event" ) );
|
|
|
|
|
|
|
|
const TOOL_EVENT sourceEvent = *aEvent.Parameter<const TOOL_EVENT*>();
|
|
|
|
|
|
|
|
m_frame->PushTool( sourceEvent );
|
2019-07-16 00:44:01 +01:00
|
|
|
Activate();
|
|
|
|
|
2015-07-24 10:58:47 +02:00
|
|
|
setControls();
|
2015-06-18 17:51:51 +02:00
|
|
|
|
2020-10-09 18:51:10 -04:00
|
|
|
auto setCursor =
|
|
|
|
[&]()
|
|
|
|
{
|
|
|
|
m_frame->GetCanvas()->SetCurrentCursor( m_cursor );
|
|
|
|
};
|
|
|
|
|
|
|
|
// Set initial cursor
|
|
|
|
setCursor();
|
|
|
|
|
2019-06-17 14:43:22 +01:00
|
|
|
while( TOOL_EVENT* evt = Wait() )
|
2015-06-18 17:51:51 +02:00
|
|
|
{
|
2020-10-09 18:51:10 -04:00
|
|
|
setCursor();
|
2021-01-31 09:50:19 -05:00
|
|
|
VECTOR2D cursorPos = controls->GetCursorPosition( m_snap && m_frame->IsGridVisible() );
|
2024-08-11 18:59:26 -04:00
|
|
|
m_modifiers = aEvent.Modifier();
|
2017-09-25 00:18:51 +02:00
|
|
|
|
2019-08-30 10:32:43 +01:00
|
|
|
if( evt->IsCancelInteractive() || evt->IsActivate() )
|
|
|
|
{
|
|
|
|
if( m_cancelHandler )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
(*m_cancelHandler)();
|
|
|
|
}
|
2020-08-18 10:17:16 -04:00
|
|
|
catch( std::exception& )
|
2019-08-30 10:32:43 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Activating a new tool may have alternate finalization from canceling the current
|
|
|
|
// tool
|
|
|
|
if( evt->IsActivate() )
|
2020-08-25 12:53:39 +01:00
|
|
|
{
|
2019-08-30 10:32:43 +01:00
|
|
|
finalize_state = END_ACTIVATE;
|
2020-08-25 12:53:39 +01:00
|
|
|
}
|
2019-08-30 10:32:43 +01:00
|
|
|
else
|
2020-08-25 12:53:39 +01:00
|
|
|
{
|
|
|
|
evt->SetPassEvent( false );
|
2019-08-30 10:32:43 +01:00
|
|
|
finalize_state = EVT_CANCEL;
|
2020-08-25 12:53:39 +01:00
|
|
|
}
|
2019-08-30 10:32:43 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if( evt->IsClick( BUT_LEFT ) )
|
2015-06-18 17:51:51 +02:00
|
|
|
{
|
|
|
|
bool getNext = false;
|
|
|
|
|
2018-10-03 22:10:50 -07:00
|
|
|
m_picked = cursorPos;
|
2017-11-01 12:14:16 +01:00
|
|
|
|
2015-06-18 17:51:51 +02:00
|
|
|
if( m_clickHandler )
|
2015-07-30 13:49:35 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
getNext = (*m_clickHandler)( *m_picked );
|
|
|
|
}
|
2020-08-18 10:17:16 -04:00
|
|
|
catch( std::exception& )
|
2015-07-30 13:49:35 +02:00
|
|
|
{
|
2018-11-20 20:10:46 -08:00
|
|
|
finalize_state = EXCEPTION_CANCEL;
|
2015-07-30 13:49:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-06-18 17:51:51 +02:00
|
|
|
|
|
|
|
if( !getNext )
|
2018-11-20 20:10:46 -08:00
|
|
|
{
|
|
|
|
finalize_state = CLICK_CANCEL;
|
2015-06-18 17:51:51 +02:00
|
|
|
break;
|
2018-11-20 20:10:46 -08:00
|
|
|
}
|
2015-07-24 10:58:47 +02:00
|
|
|
else
|
2016-01-29 10:56:29 +01:00
|
|
|
setControls();
|
2015-06-18 17:51:51 +02:00
|
|
|
}
|
|
|
|
|
2019-06-25 15:12:40 +01:00
|
|
|
else if( evt->IsMotion() )
|
|
|
|
{
|
|
|
|
if( m_motionHandler )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
(*m_motionHandler)( cursorPos );
|
|
|
|
}
|
2020-08-18 10:17:16 -04:00
|
|
|
catch( std::exception& )
|
2019-06-25 15:12:40 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 10:32:43 +01:00
|
|
|
else if( evt->IsDblClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) )
|
2018-08-22 19:05:40 +01:00
|
|
|
{
|
2019-08-30 10:32:43 +01:00
|
|
|
// Not currently used, but we don't want to pass them either
|
2018-08-22 19:05:40 +01:00
|
|
|
}
|
2019-08-30 10:32:43 +01:00
|
|
|
|
2018-08-22 19:05:40 +01:00
|
|
|
else if( evt->IsClick( BUT_RIGHT ) )
|
2019-05-19 22:04:04 +01:00
|
|
|
{
|
2024-07-30 17:57:33 +02:00
|
|
|
m_menu->ShowContextMenu();
|
2019-05-19 22:04:04 +01:00
|
|
|
}
|
2019-08-30 10:32:43 +01:00
|
|
|
|
2016-01-25 16:16:05 +01:00
|
|
|
else
|
2019-06-16 12:06:49 +01:00
|
|
|
evt->SetPassEvent();
|
2015-06-18 17:51:51 +02:00
|
|
|
}
|
|
|
|
|
2018-11-20 20:10:46 -08:00
|
|
|
if( m_finalizeHandler )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
(*m_finalizeHandler)( finalize_state );
|
|
|
|
}
|
2020-08-18 10:17:16 -04:00
|
|
|
catch( std::exception& )
|
2018-11-20 20:10:46 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 09:50:19 -05:00
|
|
|
reset();
|
2017-09-25 00:18:51 +02:00
|
|
|
controls->ForceCursorPosition( false );
|
2023-05-01 11:01:48 -04:00
|
|
|
m_frame->PopTool( sourceEvent );
|
2015-06-18 17:51:51 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-16 00:44:01 +01:00
|
|
|
void PICKER_TOOL::setTransitions()
|
2015-06-18 17:51:51 +02:00
|
|
|
{
|
2019-07-16 00:44:01 +01:00
|
|
|
Go( &PICKER_TOOL::Main, ACTIONS::pickerTool.MakeEvent() );
|
2015-06-18 17:51:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-16 00:44:01 +01:00
|
|
|
void PICKER_TOOL::setControls()
|
2015-07-24 10:58:47 +02:00
|
|
|
{
|
|
|
|
KIGFX::VIEW_CONTROLS* controls = getViewControls();
|
|
|
|
|
2019-07-02 21:09:23 +01:00
|
|
|
controls->CaptureCursor( false );
|
|
|
|
controls->SetAutoPan( false );
|
2015-06-18 17:51:51 +02:00
|
|
|
}
|