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