2013-09-09 07:34:52 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* 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_board.h>
|
|
|
|
#include <class_module.h>
|
|
|
|
#include <tool/tool_manager.h>
|
2013-09-09 09:45:20 +00:00
|
|
|
#include <view/view_controls.h>
|
2013-11-11 09:48:49 +00:00
|
|
|
#include <confirm.h>
|
2013-09-09 07:34:52 +00:00
|
|
|
|
2013-09-27 18:52:34 +00:00
|
|
|
#include "common_actions.h"
|
2013-09-09 07:34:52 +00:00
|
|
|
#include "selection_tool.h"
|
|
|
|
#include "move_tool.h"
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
using namespace KIGFX;
|
2013-09-09 07:34:52 +00:00
|
|
|
using boost::optional;
|
|
|
|
|
|
|
|
MOVE_TOOL::MOVE_TOOL() :
|
2013-10-14 14:13:35 +00:00
|
|
|
TOOL_INTERACTIVE( "pcbnew.InteractiveMove" ), m_selectionTool( NULL )
|
2013-09-09 07:34:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MOVE_TOOL::~MOVE_TOOL()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MOVE_TOOL::Reset()
|
2013-09-26 16:38:58 +00:00
|
|
|
{
|
|
|
|
// The tool launches upon reception of action event ("pcbnew.InteractiveMove")
|
2013-09-27 18:52:34 +00:00
|
|
|
Go( &MOVE_TOOL::Main, COMMON_ACTIONS::moveActivate.MakeEvent() );
|
2013-09-26 16:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool MOVE_TOOL::Init()
|
2013-09-09 07:34:52 +00:00
|
|
|
{
|
|
|
|
// Find the selection tool, so they can cooperate
|
2013-09-26 16:38:58 +00:00
|
|
|
TOOL_BASE* selectionTool = m_toolMgr->FindTool( "pcbnew.InteractiveSelection" );
|
2013-09-09 07:34:52 +00:00
|
|
|
|
|
|
|
if( selectionTool )
|
|
|
|
{
|
|
|
|
m_selectionTool = static_cast<SELECTION_TOOL*>( selectionTool );
|
2013-09-26 16:38:58 +00:00
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
// Add context menu entries that are displayed when selection tool is active
|
2013-09-27 18:52:34 +00:00
|
|
|
m_selectionTool->AddMenuItem( COMMON_ACTIONS::moveActivate );
|
|
|
|
m_selectionTool->AddMenuItem( COMMON_ACTIONS::rotate );
|
|
|
|
m_selectionTool->AddMenuItem( COMMON_ACTIONS::flip );
|
2013-09-09 07:34:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-11 09:48:49 +00:00
|
|
|
DisplayError( NULL, wxT( "pcbnew.InteractiveSelection tool is not available" ) );
|
2013-09-26 16:38:58 +00:00
|
|
|
return false;
|
2013-09-09 07:34:52 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 16:38:58 +00:00
|
|
|
return true;
|
2013-09-09 07:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int MOVE_TOOL::Main( TOOL_EVENT& aEvent )
|
|
|
|
{
|
2013-10-02 11:57:21 +00:00
|
|
|
const SELECTION_TOOL::SELECTION& selection = m_selectionTool->GetSelection();
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-10-02 08:21:05 +00:00
|
|
|
if( selection.Empty() )
|
2013-10-14 14:13:35 +00:00
|
|
|
return 0; // there are no items to operate on
|
2013-10-02 08:21:05 +00:00
|
|
|
|
2013-09-09 07:34:52 +00:00
|
|
|
VECTOR2D dragPosition;
|
|
|
|
bool dragging = false;
|
2013-09-19 15:02:57 +00:00
|
|
|
bool restore = false; // Should items' state be restored when finishing the tool?
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2013-10-02 08:21:05 +00:00
|
|
|
VIEW_CONTROLS* controls = getViewControls();
|
2013-09-20 13:01:08 +00:00
|
|
|
controls->ShowCursor( true );
|
|
|
|
controls->SetSnapping( true );
|
|
|
|
controls->SetAutoPan( true );
|
2013-09-09 07:34:52 +00:00
|
|
|
|
|
|
|
// Main loop: keep receiving events
|
|
|
|
while( OPT_TOOL_EVENT evt = Wait() )
|
|
|
|
{
|
|
|
|
if( evt->IsCancel() )
|
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
restore = true; // Cancelling the tool means that items have to be restored
|
|
|
|
break; // Finish
|
2013-09-09 07:34:52 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 13:49:43 +00:00
|
|
|
// Dispatch TOOL_ACTIONs
|
2013-10-14 18:40:36 +00:00
|
|
|
else if( evt->Category() == TC_COMMAND )
|
2013-09-24 13:49:43 +00:00
|
|
|
{
|
|
|
|
VECTOR2D cursorPos = getView()->ToWorld( getViewControls()->GetCursorPosition() );
|
|
|
|
|
2013-09-27 18:52:34 +00:00
|
|
|
if( evt->IsAction( &COMMON_ACTIONS::rotate ) ) // got rotation event?
|
2013-09-24 13:49:43 +00:00
|
|
|
{
|
|
|
|
m_state.Rotate( cursorPos, 900.0 );
|
2013-10-02 08:21:05 +00:00
|
|
|
selection.group->ViewUpdate( VIEW_ITEM::GEOMETRY );
|
2013-09-24 13:49:43 +00:00
|
|
|
}
|
2013-09-27 18:52:34 +00:00
|
|
|
else if( evt->IsAction( &COMMON_ACTIONS::flip ) ) // got flip event?
|
2013-09-24 13:49:43 +00:00
|
|
|
{
|
|
|
|
m_state.Flip( cursorPos );
|
2013-10-02 08:21:05 +00:00
|
|
|
selection.group->ViewUpdate( VIEW_ITEM::GEOMETRY );
|
2013-09-24 13:49:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-15 08:41:00 +00:00
|
|
|
else if( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) )
|
2013-09-09 07:34:52 +00:00
|
|
|
{
|
|
|
|
if( dragging )
|
|
|
|
{
|
2013-09-24 13:49:43 +00:00
|
|
|
// Drag items to the current cursor position
|
2013-09-09 07:34:52 +00:00
|
|
|
VECTOR2D movement = ( evt->Position() - dragPosition );
|
2013-09-24 13:49:43 +00:00
|
|
|
m_state.Move( movement );
|
2013-09-09 07:34:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-19 15:02:57 +00:00
|
|
|
// Prepare to drag
|
2013-09-09 07:34:52 +00:00
|
|
|
std::set<BOARD_ITEM*>::iterator it;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-10-02 08:21:05 +00:00
|
|
|
for( it = selection.items.begin(); it != selection.items.end(); ++it )
|
2013-09-09 07:34:52 +00:00
|
|
|
{
|
2013-09-24 13:49:43 +00:00
|
|
|
// Save the state of the selected items, in case it has to be restored
|
|
|
|
m_state.Save( *it );
|
2013-09-09 07:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dragging = true;
|
|
|
|
}
|
|
|
|
|
2013-10-02 08:21:05 +00:00
|
|
|
selection.group->ViewUpdate( VIEW_ITEM::GEOMETRY );
|
2013-09-09 07:34:52 +00:00
|
|
|
dragPosition = evt->Position();
|
|
|
|
}
|
2013-10-15 08:41:00 +00:00
|
|
|
else if( evt->IsMouseUp( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) )
|
2013-10-14 14:13:35 +00:00
|
|
|
break; // Finish
|
2013-09-09 07:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( restore )
|
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
// Modifications has to be rollbacked, so restore the previous state of items
|
2013-10-02 09:25:04 +00:00
|
|
|
selection.group->ItemsViewUpdate( VIEW_ITEM::APPEARANCE );
|
2013-09-24 13:49:43 +00:00
|
|
|
m_state.RestoreAll();
|
2013-09-09 07:34:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
// Changes are applied, so update the items
|
2013-10-02 08:21:05 +00:00
|
|
|
selection.group->ItemsViewUpdate( m_state.GetUpdateFlag() );
|
2013-09-24 13:49:43 +00:00
|
|
|
m_state.Apply();
|
2013-09-09 07:34:52 +00:00
|
|
|
}
|
|
|
|
|
2013-09-20 13:01:08 +00:00
|
|
|
controls->ShowCursor( false );
|
|
|
|
controls->SetSnapping( false );
|
|
|
|
controls->SetAutoPan( false );
|
2013-09-09 07:34:52 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|