2019-05-20 10:23:32 +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
|
2022-05-16 22:41:30 +00:00
|
|
|
* Copyright (C) 2019-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2019-05-20 10:23:32 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <class_draw_panel_gal.h>
|
|
|
|
#include <confirm.h>
|
|
|
|
#include <view/view_controls.h>
|
|
|
|
#include <tool/tool_manager.h>
|
|
|
|
#include <bitmaps.h>
|
2021-02-22 23:47:17 +00:00
|
|
|
#include <drawing_sheet/ds_draw_item.h>
|
|
|
|
#include <drawing_sheet/ds_data_item.h>
|
2020-10-13 05:04:31 +00:00
|
|
|
|
|
|
|
#include "pl_editor_frame.h"
|
|
|
|
#include "tools/pl_actions.h"
|
|
|
|
#include "tools/pl_selection_tool.h"
|
|
|
|
#include "tools/pl_drawing_tools.h"
|
2022-05-16 22:41:30 +00:00
|
|
|
#include "pgm_base.h"
|
2019-05-20 10:23:32 +00:00
|
|
|
|
|
|
|
PL_DRAWING_TOOLS::PL_DRAWING_TOOLS() :
|
|
|
|
TOOL_INTERACTIVE( "plEditor.InteractiveDrawing" ),
|
|
|
|
m_frame( nullptr ),
|
2019-06-05 19:15:57 +00:00
|
|
|
m_selectionTool( nullptr )
|
2019-05-20 10:23:32 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PL_DRAWING_TOOLS::Init()
|
|
|
|
{
|
|
|
|
m_frame = getEditFrame<PL_EDITOR_FRAME>();
|
|
|
|
m_selectionTool = m_toolMgr->GetTool<PL_SELECTION_TOOL>();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PL_DRAWING_TOOLS::Reset( RESET_REASON aReason )
|
|
|
|
{
|
|
|
|
if( aReason == MODEL_RELOAD )
|
|
|
|
m_frame = getEditFrame<PL_EDITOR_FRAME>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int PL_DRAWING_TOOLS::PlaceItem( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
2021-02-22 23:47:17 +00:00
|
|
|
DS_DATA_ITEM::DS_ITEM_TYPE type = aEvent.Parameter<DS_DATA_ITEM::DS_ITEM_TYPE>();
|
2019-06-18 17:56:40 +00:00
|
|
|
VECTOR2I cursorPos;
|
2021-02-22 23:47:17 +00:00
|
|
|
DS_DRAW_ITEM_BASE* item = nullptr;
|
2020-10-08 02:47:01 +00:00
|
|
|
bool isText = aEvent.IsAction( &PL_ACTIONS::placeText );
|
2019-05-20 10:23:32 +00:00
|
|
|
|
|
|
|
m_toolMgr->RunAction( PL_ACTIONS::clearSelection, true );
|
|
|
|
|
2022-09-14 17:31:56 +00:00
|
|
|
m_frame->PushTool( aEvent );
|
2019-06-18 17:56:40 +00:00
|
|
|
|
2020-10-09 01:15:03 +00:00
|
|
|
auto setCursor =
|
|
|
|
[&]()
|
2020-10-08 02:47:01 +00:00
|
|
|
{
|
|
|
|
if( item )
|
2020-10-11 03:45:21 +00:00
|
|
|
m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::PLACE );
|
2020-10-08 02:47:01 +00:00
|
|
|
else
|
|
|
|
m_frame->GetCanvas()->SetCurrentCursor( isText ? KICURSOR::TEXT : KICURSOR::PENCIL );
|
|
|
|
};
|
|
|
|
|
2022-05-16 22:41:30 +00:00
|
|
|
auto cleanup =
|
|
|
|
[&] ()
|
|
|
|
{
|
|
|
|
m_toolMgr->RunAction( PL_ACTIONS::clearSelection, true );
|
|
|
|
item = nullptr;
|
|
|
|
|
|
|
|
// There's nothing to roll-back, but we still need to pop the undo stack
|
|
|
|
// This also deletes the item being placed.
|
|
|
|
m_frame->RollbackFromUndo();
|
|
|
|
};
|
2021-09-13 12:23:10 +00:00
|
|
|
|
|
|
|
Activate();
|
|
|
|
// Must be done after Activate() so that it gets set into the correct context
|
|
|
|
getViewControls()->ShowCursor( true );
|
|
|
|
// Set initial cursor
|
2020-10-08 02:47:01 +00:00
|
|
|
setCursor();
|
|
|
|
|
2022-05-16 22:41:30 +00:00
|
|
|
if( aEvent.HasPosition() )
|
2022-05-16 09:22:55 +00:00
|
|
|
m_toolMgr->PrimeTool( aEvent.Position() );
|
2021-09-13 12:23:10 +00:00
|
|
|
|
2019-05-20 10:23:32 +00:00
|
|
|
// Main loop: keep receiving events
|
2019-06-17 13:43:22 +00:00
|
|
|
while( TOOL_EVENT* evt = Wait() )
|
2019-05-20 10:23:32 +00:00
|
|
|
{
|
2020-10-08 02:47:01 +00:00
|
|
|
setCursor();
|
2021-05-09 19:17:01 +00:00
|
|
|
cursorPos = getViewControls()->GetCursorPosition( !evt->DisableGridSnapping() );
|
2019-05-20 10:23:32 +00:00
|
|
|
|
2019-07-01 21:01:33 +00:00
|
|
|
if( evt->IsCancelInteractive() )
|
2019-05-20 10:23:32 +00:00
|
|
|
{
|
|
|
|
if( item )
|
2019-07-01 21:01:33 +00:00
|
|
|
cleanup();
|
|
|
|
else
|
2019-05-20 10:23:32 +00:00
|
|
|
{
|
2022-09-14 17:31:56 +00:00
|
|
|
m_frame->PopTool( aEvent );
|
2019-07-01 21:01:33 +00:00
|
|
|
break;
|
2019-05-20 10:23:32 +00:00
|
|
|
}
|
2019-07-01 21:01:33 +00:00
|
|
|
}
|
|
|
|
else if( evt->IsActivate() )
|
|
|
|
{
|
|
|
|
if( item )
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
if( evt->IsMoveTool() )
|
2019-06-18 17:56:40 +00:00
|
|
|
{
|
2019-07-01 21:01:33 +00:00
|
|
|
// leave ourselves on the stack so we come back after the move
|
2019-06-27 11:47:24 +00:00
|
|
|
break;
|
2019-06-18 21:34:31 +00:00
|
|
|
}
|
2019-07-01 21:01:33 +00:00
|
|
|
else
|
|
|
|
{
|
2022-09-14 17:31:56 +00:00
|
|
|
m_frame->PopTool( aEvent );
|
2019-06-18 17:56:40 +00:00
|
|
|
break;
|
2019-07-01 21:01:33 +00:00
|
|
|
}
|
2019-05-20 10:23:32 +00:00
|
|
|
}
|
2019-06-18 17:56:40 +00:00
|
|
|
else if( evt->IsClick( BUT_LEFT ) )
|
2019-05-20 10:23:32 +00:00
|
|
|
{
|
|
|
|
// First click creates...
|
|
|
|
if( !item )
|
|
|
|
{
|
2021-05-30 22:56:24 +00:00
|
|
|
DS_DATA_ITEM* dataItem = m_frame->AddDrawingSheetItem( type );
|
2019-05-20 10:23:32 +00:00
|
|
|
|
2019-06-20 11:05:23 +00:00
|
|
|
if( dataItem ) // dataItem = nullptr can happens if the command was cancelled
|
|
|
|
{
|
|
|
|
m_frame->SaveCopyInUndoList();
|
2019-05-20 10:23:32 +00:00
|
|
|
|
2019-06-20 11:05:23 +00:00
|
|
|
m_toolMgr->RunAction( PL_ACTIONS::clearSelection, true );
|
2019-05-20 10:23:32 +00:00
|
|
|
|
2019-06-20 11:05:23 +00:00
|
|
|
item = dataItem->GetDrawItems()[0];
|
2021-05-30 21:04:07 +00:00
|
|
|
item->SetFlags( IS_NEW | IS_MOVING );
|
2021-01-13 19:56:09 +00:00
|
|
|
|
|
|
|
// Select the item but don't inform other tools (to prevent the Properties
|
|
|
|
// panel from updating the item before it has been placed)
|
|
|
|
m_selectionTool->AddItemToSel( item, true );
|
2020-10-08 02:47:01 +00:00
|
|
|
|
|
|
|
// update the cursor so it looks correct before another event
|
|
|
|
setCursor();
|
2019-06-20 11:05:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-20 10:23:32 +00:00
|
|
|
// ... and second click places:
|
|
|
|
else
|
|
|
|
{
|
2022-08-30 14:13:51 +00:00
|
|
|
item->GetPeer()->MoveStartPointToUi( cursorPos );
|
2023-05-27 15:30:12 +00:00
|
|
|
item->SetPosition( item->GetPeer()->GetStartPosIU( 0 ) );
|
2019-05-20 10:23:32 +00:00
|
|
|
item->ClearEditFlags();
|
|
|
|
getView()->Update( item );
|
|
|
|
|
2021-01-13 19:56:09 +00:00
|
|
|
// Now we re-select and inform other tools, so that the Properties panel
|
|
|
|
// is updated.
|
|
|
|
m_toolMgr->RunAction( PL_ACTIONS::clearSelection, true );
|
|
|
|
m_selectionTool->AddItemToSel( item, false );
|
|
|
|
|
2019-05-20 10:23:32 +00:00
|
|
|
item = nullptr;
|
|
|
|
|
|
|
|
m_frame->OnModify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( evt->IsClick( BUT_RIGHT ) )
|
|
|
|
{
|
|
|
|
// Warp after context menu only if dragging...
|
|
|
|
if( !item )
|
|
|
|
m_toolMgr->VetoContextMenuMouseWarp();
|
|
|
|
|
|
|
|
m_menu.ShowContextMenu( m_selectionTool->GetSelection() );
|
|
|
|
}
|
2019-07-04 20:56:21 +00:00
|
|
|
else if( item && ( evt->IsAction( &ACTIONS::refreshPreview ) || evt->IsMotion() ) )
|
2019-05-20 10:23:32 +00:00
|
|
|
{
|
2022-08-30 14:13:51 +00:00
|
|
|
item->GetPeer()->MoveStartPointToUi( cursorPos );
|
2023-05-27 15:30:12 +00:00
|
|
|
item->SetPosition( item->GetPeer()->GetStartPosIU( 0 ) );
|
2019-05-20 10:23:32 +00:00
|
|
|
getView()->Update( item );
|
|
|
|
}
|
2019-07-26 18:16:44 +00:00
|
|
|
else
|
2020-11-19 20:08:58 +00:00
|
|
|
{
|
2019-07-26 18:16:44 +00:00
|
|
|
evt->SetPassEvent();
|
2020-11-19 20:08:58 +00:00
|
|
|
}
|
2019-05-20 10:23:32 +00:00
|
|
|
|
|
|
|
// Enable autopanning and cursor capture only when there is an item to be placed
|
2019-06-18 17:56:40 +00:00
|
|
|
getViewControls()->SetAutoPan( item != nullptr );
|
|
|
|
getViewControls()->CaptureCursor( item != nullptr );
|
2019-05-20 10:23:32 +00:00
|
|
|
}
|
|
|
|
|
2021-06-20 10:02:44 +00:00
|
|
|
getViewControls()->SetAutoPan( false );
|
|
|
|
getViewControls()->CaptureCursor( false );
|
2020-11-19 20:08:58 +00:00
|
|
|
m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
|
2019-05-20 10:23:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int PL_DRAWING_TOOLS::DrawShape( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
2021-02-22 23:47:17 +00:00
|
|
|
DS_DATA_ITEM::DS_ITEM_TYPE type = aEvent.Parameter<DS_DATA_ITEM::DS_ITEM_TYPE>();
|
|
|
|
DS_DRAW_ITEM_BASE* item = nullptr;
|
2019-06-18 17:56:40 +00:00
|
|
|
|
2019-05-20 10:23:32 +00:00
|
|
|
// We might be running as the same shape in another co-routine. Make sure that one
|
|
|
|
// gets whacked.
|
|
|
|
m_toolMgr->DeactivateTool();
|
|
|
|
|
|
|
|
m_toolMgr->RunAction( PL_ACTIONS::clearSelection, true );
|
|
|
|
|
2022-09-14 17:31:56 +00:00
|
|
|
m_frame->PushTool( aEvent );
|
2020-10-09 01:15:03 +00:00
|
|
|
|
|
|
|
auto setCursor =
|
|
|
|
[&]()
|
|
|
|
{
|
2020-10-08 02:47:01 +00:00
|
|
|
m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::PENCIL );
|
|
|
|
};
|
|
|
|
|
2021-09-13 12:23:10 +00:00
|
|
|
Activate();
|
|
|
|
// Must be done after Activate() so that it gets set into the correct context
|
|
|
|
getViewControls()->ShowCursor( true );
|
2020-10-08 02:47:01 +00:00
|
|
|
// Set initial cursor
|
|
|
|
setCursor();
|
2019-05-20 10:23:32 +00:00
|
|
|
|
2021-09-13 12:23:10 +00:00
|
|
|
if( aEvent.HasPosition() )
|
2022-05-16 09:22:55 +00:00
|
|
|
m_toolMgr->PrimeTool( aEvent.Position() );
|
2021-09-13 12:23:10 +00:00
|
|
|
|
2019-05-20 10:23:32 +00:00
|
|
|
// Main loop: keep receiving events
|
2019-06-17 13:43:22 +00:00
|
|
|
while( TOOL_EVENT* evt = Wait() )
|
2019-05-20 10:23:32 +00:00
|
|
|
{
|
2020-10-08 02:47:01 +00:00
|
|
|
setCursor();
|
2019-06-27 21:33:48 +00:00
|
|
|
|
2021-05-09 19:17:01 +00:00
|
|
|
VECTOR2I cursorPos = getViewControls()->GetCursorPosition( !evt->DisableGridSnapping() );
|
2019-05-20 10:23:32 +00:00
|
|
|
|
2019-07-01 21:01:33 +00:00
|
|
|
if( evt->IsCancelInteractive() || evt->IsActivate() )
|
2019-05-20 10:23:32 +00:00
|
|
|
{
|
|
|
|
m_toolMgr->RunAction( PL_ACTIONS::clearSelection, true );
|
|
|
|
|
|
|
|
if( item )
|
|
|
|
{
|
|
|
|
item = nullptr;
|
2020-10-09 18:39:03 +00:00
|
|
|
|
|
|
|
// Pop the undo stack and delete the item being placed
|
2019-05-24 09:23:05 +00:00
|
|
|
m_frame->RollbackFromUndo();
|
2019-05-20 10:23:32 +00:00
|
|
|
}
|
2019-07-01 21:01:33 +00:00
|
|
|
else if( evt->IsCancelInteractive() )
|
2019-06-18 17:56:40 +00:00
|
|
|
{
|
2019-06-27 11:47:24 +00:00
|
|
|
break;
|
2019-06-18 21:34:31 +00:00
|
|
|
}
|
2019-05-20 10:23:32 +00:00
|
|
|
|
2019-07-01 21:01:33 +00:00
|
|
|
if( evt->IsActivate() && !evt->IsPointEditor() && !evt->IsMoveTool() )
|
2019-06-18 17:56:40 +00:00
|
|
|
break;
|
2019-05-20 10:23:32 +00:00
|
|
|
}
|
2019-06-18 17:56:40 +00:00
|
|
|
else if( evt->IsClick( BUT_LEFT ) )
|
2019-05-20 10:23:32 +00:00
|
|
|
{
|
|
|
|
if( !item ) // start drawing
|
|
|
|
{
|
2019-05-24 09:23:05 +00:00
|
|
|
m_frame->SaveCopyInUndoList();
|
2019-05-20 10:23:32 +00:00
|
|
|
m_toolMgr->RunAction( PL_ACTIONS::clearSelection, true );
|
|
|
|
|
2021-05-30 22:56:24 +00:00
|
|
|
DS_DATA_ITEM* dataItem = m_frame->AddDrawingSheetItem( type );
|
2022-08-30 14:13:51 +00:00
|
|
|
dataItem->MoveToUi( cursorPos );
|
2019-05-20 10:23:32 +00:00
|
|
|
|
|
|
|
item = dataItem->GetDrawItems()[0];
|
|
|
|
item->SetFlags( IS_NEW );
|
2021-01-14 18:47:11 +00:00
|
|
|
|
|
|
|
// Select the item but don't inform other tools (to prevent the Properties
|
|
|
|
// panel from updating the item before it has been placed)
|
|
|
|
m_selectionTool->AddItemToSel( item, true );
|
2019-05-20 10:23:32 +00:00
|
|
|
}
|
|
|
|
else // finish drawing
|
|
|
|
{
|
2021-01-14 18:47:11 +00:00
|
|
|
// Now we re-select and inform other tools, so that the Properties panel
|
|
|
|
// is updated.
|
|
|
|
m_toolMgr->RunAction( PL_ACTIONS::clearSelection, true );
|
|
|
|
m_selectionTool->AddItemToSel( item, false );
|
|
|
|
|
2019-05-20 10:23:32 +00:00
|
|
|
item->ClearEditFlags();
|
|
|
|
item = nullptr;
|
2021-01-14 19:04:19 +00:00
|
|
|
|
|
|
|
// Activate point editor immediately to allow resizing of the item just created
|
|
|
|
m_toolMgr->RunAction( ACTIONS::activatePointEditor, true );
|
2019-05-20 10:23:32 +00:00
|
|
|
|
|
|
|
m_frame->OnModify();
|
|
|
|
}
|
|
|
|
}
|
2019-07-04 20:56:21 +00:00
|
|
|
else if( evt->IsAction( &ACTIONS::refreshPreview ) || evt->IsMotion() )
|
2019-05-20 10:23:32 +00:00
|
|
|
{
|
|
|
|
if( item )
|
|
|
|
{
|
2022-08-30 14:13:51 +00:00
|
|
|
item->GetPeer()->MoveEndPointToUi( cursorPos );
|
2023-05-27 15:30:12 +00:00
|
|
|
item->SetEnd( item->GetPeer()->GetEndPosIU( 0 ) );
|
2019-05-20 10:23:32 +00:00
|
|
|
getView()->Update( item );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( evt->IsClick( BUT_RIGHT ) )
|
|
|
|
{
|
|
|
|
// Warp after context menu only if dragging...
|
|
|
|
if( !item )
|
|
|
|
m_toolMgr->VetoContextMenuMouseWarp();
|
|
|
|
|
|
|
|
m_menu.ShowContextMenu( m_selectionTool->GetSelection() );
|
|
|
|
}
|
2019-07-26 18:16:44 +00:00
|
|
|
else
|
2020-11-19 20:08:58 +00:00
|
|
|
{
|
2019-07-26 18:16:44 +00:00
|
|
|
evt->SetPassEvent();
|
2020-11-19 20:08:58 +00:00
|
|
|
}
|
2019-07-26 18:16:44 +00:00
|
|
|
|
2019-05-20 10:23:32 +00:00
|
|
|
// Enable autopanning and cursor capture only when there is a shape being drawn
|
2019-06-18 17:56:40 +00:00
|
|
|
getViewControls()->SetAutoPan( item != nullptr );
|
|
|
|
getViewControls()->CaptureCursor( item != nullptr );
|
2019-05-20 10:23:32 +00:00
|
|
|
}
|
|
|
|
|
2021-06-20 10:02:44 +00:00
|
|
|
getViewControls()->SetAutoPan( false );
|
|
|
|
getViewControls()->CaptureCursor( false );
|
2020-11-19 20:08:58 +00:00
|
|
|
m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
|
2022-09-14 17:31:56 +00:00
|
|
|
m_frame->PopTool( aEvent );
|
2019-05-20 10:23:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PL_DRAWING_TOOLS::setTransitions()
|
|
|
|
{
|
|
|
|
Go( &PL_DRAWING_TOOLS::DrawShape, PL_ACTIONS::drawLine.MakeEvent() );
|
|
|
|
Go( &PL_DRAWING_TOOLS::DrawShape, PL_ACTIONS::drawRectangle.MakeEvent() );
|
|
|
|
Go( &PL_DRAWING_TOOLS::PlaceItem, PL_ACTIONS::placeText.MakeEvent() );
|
|
|
|
Go( &PL_DRAWING_TOOLS::PlaceItem, PL_ACTIONS::placeImage.MakeEvent() );
|
|
|
|
}
|