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>
|
2018-02-11 01:15:16 +00:00
|
|
|
|
2017-02-20 18:10:20 +00:00
|
|
|
|
2020-01-13 19:39:49 +00:00
|
|
|
PICKER_TOOL::PICKER_TOOL() :
|
|
|
|
TOOL_INTERACTIVE( "common.InteractivePicker" ),
|
|
|
|
m_frame( nullptr )
|
2015-06-18 15:51:51 +00:00
|
|
|
{
|
2019-05-20 10:23:32 +00:00
|
|
|
resetPicker();
|
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;
|
|
|
|
|
2019-07-15 23:44:01 +00:00
|
|
|
std::string tool = *aEvent.Parameter<std::string*>();
|
|
|
|
m_frame->PushTool( tool );
|
|
|
|
Activate();
|
|
|
|
|
|
|
|
// To many things are off-grid in LibEdit; turn snapping off.
|
|
|
|
bool snap = !m_frame->IsType( FRAME_SCH_LIB_EDITOR );
|
|
|
|
|
2015-07-24 08:58:47 +00:00
|
|
|
setControls();
|
2015-06-18 15:51:51 +00:00
|
|
|
|
2019-06-17 13:43:22 +00:00
|
|
|
while( TOOL_EVENT* evt = Wait() )
|
2015-06-18 15:51:51 +00:00
|
|
|
{
|
2019-08-04 20:46:09 +00:00
|
|
|
m_frame->GetCanvas()->SetCurrentCursor( m_cursor );
|
2019-07-15 23:44:01 +00:00
|
|
|
VECTOR2D cursorPos = controls->GetCursorPosition( snap && !evt->Modifier( MD_ALT ) );
|
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() )
|
|
|
|
finalize_state = END_ACTIVATE;
|
|
|
|
else
|
|
|
|
finalize_state = EVT_CANCEL;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 10:23:32 +00:00
|
|
|
resetPicker();
|
2017-09-24 22:18:51 +00:00
|
|
|
controls->ForceCursorPosition( false );
|
2019-07-15 23:44:01 +00:00
|
|
|
m_frame->PopTool( tool );
|
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::resetPicker()
|
2015-06-18 15:51:51 +00:00
|
|
|
{
|
2019-07-15 23:44:01 +00:00
|
|
|
m_cursor = wxStockCursor( wxCURSOR_ARROW );
|
|
|
|
|
2018-08-22 18:05:40 +00:00
|
|
|
m_picked = NULLOPT;
|
2017-11-01 11:14:16 +00:00
|
|
|
m_clickHandler = NULLOPT;
|
2019-07-15 23:44:01 +00:00
|
|
|
m_motionHandler = NULLOPT;
|
2018-08-22 18:05:40 +00:00
|
|
|
m_cancelHandler = NULLOPT;
|
2018-11-21 04:10:46 +00:00
|
|
|
m_finalizeHandler = NULLOPT;
|
2015-07-24 08:58:47 +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
|
|
|
}
|