Allow user to change from move to drag and vice versa.

Also fixes bugs when doing a duplicate in the middle of a move.
This commit is contained in:
Jeff Young 2019-05-01 00:18:47 +01:00
parent 0af7c87277
commit e9dd8542d1
2 changed files with 106 additions and 78 deletions

View File

@ -170,7 +170,8 @@ SCH_EDIT_TOOL::SCH_EDIT_TOOL() :
m_controls( nullptr ), m_controls( nullptr ),
m_frame( nullptr ), m_frame( nullptr ),
m_menu( *this ), m_menu( *this ),
m_moveInProgress( false ) m_moveInProgress( false ),
m_totalMovement( 0, 0 )
{ {
} }
@ -405,12 +406,16 @@ bool SCH_EDIT_TOOL::Init()
void SCH_EDIT_TOOL::Reset( RESET_REASON aReason ) void SCH_EDIT_TOOL::Reset( RESET_REASON aReason )
{ {
m_moveInProgress = false; if( aReason == MODEL_RELOAD )
{
m_moveInProgress = false;
m_totalMovement = { 0, 0 };
// Init variables used by every drawing tool // Init variables used by every drawing tool
m_view = static_cast<KIGFX::SCH_VIEW*>( getView() ); m_view = static_cast<KIGFX::SCH_VIEW*>( getView() );
m_controls = getViewControls(); m_controls = getViewControls();
m_frame = getEditFrame<SCH_EDIT_FRAME>(); m_frame = getEditFrame<SCH_EDIT_FRAME>();
}
} }
@ -441,8 +446,8 @@ int SCH_EDIT_TOOL::Main( const TOOL_EVENT& aEvent )
controls->SetSnapping( true ); controls->SetSnapping( true );
VECTOR2I originalCursorPos = controls->GetCursorPosition(); VECTOR2I originalCursorPos = controls->GetCursorPosition();
// Be sure that there is at least one item that we can modify. If nothing was selected before, // Be sure that there is at least one item that we can move. If there's no selection try
// try looking for the stuff under mouse cursor (i.e. Kicad old-style hover selection) // looking for the stuff under mouse cursor (i.e. Kicad old-style hover selection).
SELECTION& selection = m_selectionTool->RequestSelection( movableItems ); SELECTION& selection = m_selectionTool->RequestSelection( movableItems );
bool unselect = selection.IsHover(); bool unselect = selection.IsHover();
@ -459,44 +464,43 @@ int SCH_EDIT_TOOL::Main( const TOOL_EVENT& aEvent )
controls->SetAutoPan( true ); controls->SetAutoPan( true );
bool restore_state = false; bool restore_state = false;
bool isDragOperation = aEvent.IsAction( &SCH_ACTIONS::drag ); bool chain_commands = false;
VECTOR2I totalMovement;
OPT_TOOL_EVENT evt = aEvent; OPT_TOOL_EVENT evt = aEvent;
VECTOR2I prevPos; VECTOR2I prevPos;
if( m_moveInProgress )
{
// User must have switched from move to drag or vice-versa. Reset the moved items
// so we can start again with the current m_isDragOperation and m_totalMovement.
m_frame->RollbackSchematicFromUndo();
m_moveInProgress = false;
// And give it a kick so it doesn't have to wait for the first mouse movement.
m_toolMgr->RunAction( SCH_ACTIONS::refreshPreview );
return 0;
}
// Main loop: keep receiving events // Main loop: keep receiving events
do do
{ {
controls->SetSnapping( !evt->Modifier( MD_ALT ) ); controls->SetSnapping( !evt->Modifier( MD_ALT ) );
if( evt->IsAction( &SCH_ACTIONS::move ) || evt->IsAction( &SCH_ACTIONS::drag ) if( evt->IsAction( &SCH_ACTIONS::move ) || evt->IsAction( &SCH_ACTIONS::drag )
|| evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) || evt->IsMotion() || evt->IsDrag( BUT_LEFT )
|| evt->IsAction( &SCH_ACTIONS::refreshPreview ) )
{ {
if( !m_moveInProgress ) // Prepare to start moving/dragging if( !m_moveInProgress ) // Prepare to start moving/dragging
{ {
// Add connections to the selection for a drag.
// //
// Save items, so changes can be undone if( m_frame->GetToolId() == ID_SCH_DRAG )
//
for( int i = 0; i < selection.GetSize(); ++i )
{ {
SCH_ITEM* item = static_cast<SCH_ITEM*>( selection.GetItem( i ) ); for( unsigned i = 0; i < selection.GetSize(); ++i )
{
SCH_ITEM* item = static_cast<SCH_ITEM*>( selection.GetItem( i ) );
item->ClearFlags( STARTPOINT | ENDPOINT ); item->ClearFlags( STARTPOINT | ENDPOINT );
}
// No need to save children of selected items
if( item->GetParent() && item->GetParent()->IsSelected() )
continue;
if( !item->IsNew() )
saveCopyInUndoList( item, UR_CHANGED, i > 0 );
}
//
// Add connections to the selection for a drag; mark the edges of the block
// with dangling pins for a move
//
if( isDragOperation )
{
for( unsigned i = 0; i < selection.GetSize(); ++i ) for( unsigned i = 0; i < selection.GetSize(); ++i )
{ {
SCH_ITEM* item = static_cast<SCH_ITEM*>( selection.GetItem( i ) ); SCH_ITEM* item = static_cast<SCH_ITEM*>( selection.GetItem( i ) );
@ -511,24 +515,49 @@ int SCH_EDIT_TOOL::Main( const TOOL_EVENT& aEvent )
} }
} }
} }
else
// Mark the edges of the block with dangling flags for a move.
//
if( m_frame->GetToolId() == ID_SCH_MOVE )
{ {
std::vector<DANGLING_END_ITEM> internalPoints; std::vector<DANGLING_END_ITEM> internalPoints;
for( int i = 0; i < selection.GetSize(); ++i ) for( unsigned i = 0; i < selection.GetSize(); ++i )
{ {
SCH_ITEM* item = static_cast<SCH_ITEM*>( selection.GetItem( i ) ); SCH_ITEM* item = static_cast<SCH_ITEM*>( selection.GetItem( i ) );
item->GetEndPoints( internalPoints ); item->GetEndPoints( internalPoints );
} }
for( int i = 0; i < selection.GetSize(); ++i ) for( unsigned i = 0; i < selection.GetSize(); ++i )
{ {
SCH_ITEM* item = static_cast<SCH_ITEM*>( selection.GetItem( i ) ); SCH_ITEM* item = static_cast<SCH_ITEM*>( selection.GetItem( i ) );
item->UpdateDanglingState( internalPoints ); item->UpdateDanglingState( internalPoints );
} }
} }
// Save items for undo.
// //
for( unsigned i = 0; i < selection.GetSize(); ++i )
{
SCH_ITEM* item = static_cast<SCH_ITEM*>( selection.GetItem( i ) );
// No need to save children of selected items
if( item->GetParent() && item->GetParent()->IsSelected() )
continue;
if( !item->IsNew() )
saveCopyInUndoList( item, UR_CHANGED, i > 0 );
}
// Apply any initial offset in case we're coming from a previous command.
//
for( unsigned i = 0; i < selection.GetSize(); ++i )
{
SCH_ITEM* item = static_cast<SCH_ITEM*>( selection.GetItem( i ) );
moveItem( item, m_totalMovement, m_frame->GetToolId() == ID_SCH_DRAG );
}
// Set up the starting position and move/drag offset // Set up the starting position and move/drag offset
// //
m_cursor = controls->GetCursorPosition(); m_cursor = controls->GetCursorPosition();
@ -546,7 +575,7 @@ int SCH_EDIT_TOOL::Main( const TOOL_EVENT& aEvent )
if( item->GetParent() && item->GetParent()->IsSelected() ) if( item->GetParent() && item->GetParent()->IsSelected() )
continue; continue;
moveItem( item, delta, isDragOperation ); moveItem( item, delta, m_frame->GetToolId() == ID_SCH_DRAG );
updateView( item ); updateView( item );
} }
@ -571,32 +600,28 @@ int SCH_EDIT_TOOL::Main( const TOOL_EVENT& aEvent )
m_moveInProgress = true; m_moveInProgress = true;
} }
else /* m_moveInProgress */ // Follow the mouse
//
m_cursor = controls->GetCursorPosition();
VECTOR2I movement( m_cursor - prevPos );
selection.SetReferencePoint( m_cursor );
m_totalMovement += movement;
prevPos = m_cursor;
for( int i = 0; i < selection.GetSize(); ++i )
{ {
// SCH_ITEM* item = static_cast<SCH_ITEM*>( selection.GetItem( i ) );
// Follow the mouse
//
m_cursor = controls->GetCursorPosition();
VECTOR2I movement( m_cursor - prevPos );
selection.SetReferencePoint( m_cursor );
totalMovement += movement; // Don't double move pins, fields, etc.
prevPos = m_cursor; if( item->GetParent() && item->GetParent()->IsSelected() )
continue;
for( int i = 0; i < selection.GetSize(); ++i ) moveItem( item, movement, m_frame->GetToolId() == ID_SCH_DRAG );
{ updateView( item );
SCH_ITEM* item = static_cast<SCH_ITEM*>( selection.GetItem( i ) );
// Don't double move pins, fields, etc.
if( item->GetParent() && item->GetParent()->IsSelected() )
continue;
moveItem( item, movement, isDragOperation );
updateView( item );
}
m_frame->UpdateMsgPanel();
} }
m_frame->UpdateMsgPanel();
} }
else if( TOOL_EVT_UTILS::IsCancelInteractive( evt.get() ) ) else if( TOOL_EVT_UTILS::IsCancelInteractive( evt.get() ) )
@ -623,7 +648,10 @@ int SCH_EDIT_TOOL::Main( const TOOL_EVENT& aEvent )
} }
else if( evt->IsAction( &SCH_ACTIONS::duplicate ) ) else if( evt->IsAction( &SCH_ACTIONS::duplicate ) )
{ {
// Exit on a duplicate action; it will start its own move operation. // Move original back and exit. The duplicate will run in its own loop.
restore_state = true;
unselect = false;
chain_commands = true;
break; break;
} }
} }
@ -645,28 +673,32 @@ int SCH_EDIT_TOOL::Main( const TOOL_EVENT& aEvent )
controls->SetSnapping( false ); controls->SetSnapping( false );
controls->SetAutoPan( false ); controls->SetAutoPan( false );
if( !chain_commands )
m_totalMovement = { 0, 0 };
m_moveInProgress = false; m_moveInProgress = false;
m_frame->SetNoToolSelected();
selection.ClearReferencePoint(); selection.ClearReferencePoint();
for( auto item : selection ) for( auto item : selection )
item->ClearFlags( IS_MOVED ); item->ClearFlags( IS_MOVED );
if( restore_state )
{
m_toolMgr->RunAction( SCH_ACTIONS::clearSelection, true );
m_frame->RollbackSchematicFromUndo();
return 0;
}
m_frame->CheckConnections( selection, true );
m_frame->SchematicCleanUp( true );
m_frame->TestDanglingEnds();
m_frame->OnModify();
if( unselect ) if( unselect )
m_toolMgr->RunAction( SCH_ACTIONS::clearSelection, true ); m_toolMgr->RunAction( SCH_ACTIONS::clearSelection, true );
if( restore_state )
{
m_frame->RollbackSchematicFromUndo();
}
else
{
m_frame->CheckConnections( selection, true );
m_frame->SchematicCleanUp( true );
m_frame->TestDanglingEnds();
m_frame->OnModify();
}
return 0; return 0;
} }
@ -892,16 +924,8 @@ int SCH_EDIT_TOOL::Rotate( const TOOL_EVENT& aEvent )
// Rotate the sheet on itself. Sheets do not have a anchor point. // Rotate the sheet on itself. Sheets do not have a anchor point.
rotPoint = m_frame->GetNearestGridPosition( item->GetBoundingBox().Centre() ); rotPoint = m_frame->GetNearestGridPosition( item->GetBoundingBox().Centre() );
if( clockwise ) for( int i = 0; clockwise ? i < 1 : i < 3; ++i )
{
item->Rotate( rotPoint ); item->Rotate( rotPoint );
}
else
{
item->Rotate( rotPoint );
item->Rotate( rotPoint );
item->Rotate( rotPoint );
}
break; break;
@ -1121,6 +1145,7 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
SCH_ITEM* newItem = DuplicateStruct( oldItem ); SCH_ITEM* newItem = DuplicateStruct( oldItem );
newItems.push_back( newItem ); newItems.push_back( newItem );
newItem->SetFlags( IS_NEW );
saveCopyInUndoList( newItem, UR_NEW, ii > 0 ); saveCopyInUndoList( newItem, UR_NEW, ii > 0 );
switch( newItem->Type() ) switch( newItem->Type() )

View File

@ -114,6 +114,9 @@ private:
///> Flag determining if anything is being dragged right now ///> Flag determining if anything is being dragged right now
bool m_moveInProgress; bool m_moveInProgress;
///> Used for chaining commands
VECTOR2I m_totalMovement;
///> Last cursor position (needed for getModificationPoint() to avoid changes ///> Last cursor position (needed for getModificationPoint() to avoid changes
///> of edit reference point). ///> of edit reference point).
VECTOR2I m_cursor; VECTOR2I m_cursor;