kicad/pcbnew/tools/move_tool.cpp

226 lines
7.0 KiB
C++
Raw Normal View History

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-09-09 07:34:52 +00:00
#include "common_actions.h"
2013-09-09 07:34:52 +00:00
#include "selection_tool.h"
#include "move_tool.h"
using namespace KiGfx;
using boost::optional;
MOVE_TOOL::MOVE_TOOL() :
TOOL_INTERACTIVE( "pcbnew.InteractiveMove" ), m_selectionTool( NULL )
2013-09-09 07:34:52 +00:00
{
}
MOVE_TOOL::~MOVE_TOOL()
{
}
void MOVE_TOOL::Reset()
{
// The tool launches upon reception of action event ("pcbnew.InteractiveMove")
Go( &MOVE_TOOL::Main, COMMON_ACTIONS::moveActivate.MakeEvent() );
}
bool MOVE_TOOL::Init()
2013-09-09 07:34:52 +00:00
{
// Find the selection tool, so they can cooperate
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 );
// Add context menu entries that are displayed when selection tool is active
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-09-29 10:57:20 +00:00
wxLogError( wxT( "pcbnew.InteractiveSelection tool is not available" ) );
return false;
2013-09-09 07:34:52 +00:00
}
return true;
2013-09-09 07:34:52 +00:00
}
int MOVE_TOOL::Main( TOOL_EVENT& aEvent )
{
VECTOR2D dragPosition;
bool dragging = false;
bool restore = false; // Should items' state be restored when finishing the tool?
2013-09-20 13:01:08 +00:00
VIEW* view = getView();
VIEW_CONTROLS* controls = getViewControls();
2013-09-09 07:34:52 +00:00
// Add a VIEW_GROUP that will hold all modified items
view->Add( &m_items );
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() )
{
restore = true; // Cancelling the tool means that items have to be restored
2013-09-09 07:34:52 +00:00
break; // Finish
}
// Dispatch TOOL_ACTIONs
else if( evt->Category() == TC_Command )
{
VECTOR2D cursorPos = getView()->ToWorld( getViewControls()->GetCursorPosition() );
if( evt->IsAction( &COMMON_ACTIONS::rotate ) ) // got rotation event?
{
m_state.Rotate( cursorPos, 900.0 );
m_items.ViewUpdate( VIEW_ITEM::GEOMETRY );
}
else if( evt->IsAction( &COMMON_ACTIONS::flip ) ) // got flip event?
{
m_state.Flip( cursorPos );
m_items.ViewUpdate( VIEW_ITEM::GEOMETRY );
}
}
else if( evt->IsMotion() || evt->IsDrag( MB_Left ) )
2013-09-09 07:34:52 +00:00
{
if( dragging )
{
// Drag items to the current cursor position
2013-09-09 07:34:52 +00:00
VECTOR2D movement = ( evt->Position() - dragPosition );
m_state.Move( movement );
2013-09-09 07:34:52 +00:00
}
else
{
// Prepare to drag
m_selection = m_selectionTool->GetSelection();
if( m_selection.empty() )
break; // there are no items to operate on
2013-09-09 07:34:52 +00:00
std::set<BOARD_ITEM*>::iterator it;
for( it = m_selection.begin(); it != m_selection.end(); ++it )
2013-09-09 07:34:52 +00:00
{
// Save the state of the selected items, in case it has to be restored
m_state.Save( *it );
// Gather all selected items into one VIEW_GROUP
2013-10-01 08:21:32 +00:00
vgAdd( *it, &m_items );
2013-09-09 07:34:52 +00:00
}
// Hide the original items, they are temporarily shown in VIEW_GROUP on overlay
vgSetVisibility( &m_items, false );
vgUpdate( &m_items, VIEW_ITEM::APPEARANCE );
2013-09-09 07:34:52 +00:00
dragging = true;
}
m_items.ViewUpdate( VIEW_ITEM::GEOMETRY );
2013-09-09 07:34:52 +00:00
dragPosition = evt->Position();
}
else if( evt->IsMouseUp( MB_Left ) || evt->IsClick( MB_Left ) )
break; // Finish
2013-09-09 07:34:52 +00:00
}
// Restore visibility of the original items
vgSetVisibility( &m_items, true );
2013-09-09 07:34:52 +00:00
if( restore )
{
// Modifications has to be rollbacked, so restore the previous state of items
vgUpdate( &m_items, VIEW_ITEM::APPEARANCE );
m_state.RestoreAll();
2013-09-09 07:34:52 +00:00
}
else
{
// Changes are applied, so update the items
vgUpdate( &m_items, m_state.GetUpdateFlag() );
m_state.Apply();
2013-09-09 07:34:52 +00:00
}
m_items.Clear();
view->Remove( &m_items );
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;
}
2013-10-01 08:21:32 +00:00
void MOVE_TOOL::vgAdd( BOARD_ITEM* aItem, VIEW_GROUP* aGroup )
2013-09-09 07:34:52 +00:00
{
// Modules are treated in a special way - when they are moved, we have to
// move all the parts that make the module, not the module itself
if( aItem->Type() == PCB_MODULE_T )
{
MODULE* module = static_cast<MODULE*>( aItem );
// Add everything that belongs to the module (besides the module itself)
for( D_PAD* pad = module->Pads().GetFirst(); pad; pad = pad->Next() )
2013-10-01 08:21:32 +00:00
aGroup->Add( pad );
for( BOARD_ITEM* drawing = module->GraphicalItems().GetFirst(); drawing;
drawing = drawing->Next() )
2013-10-01 08:21:32 +00:00
aGroup->Add( drawing );
2013-10-01 08:21:32 +00:00
aGroup->Add( &module->Reference() );
aGroup->Add( &module->Value() );
}
2013-09-09 07:34:52 +00:00
// Add items to the VIEW_GROUP, so they will be displayed on the overlay
// while dragging
aGroup->Add( aItem );
}
void MOVE_TOOL::vgSetVisibility( VIEW_GROUP* aGroup, bool aVisible ) const
{
std::set<VIEW_ITEM*>::const_iterator it, it_end;
for( it = aGroup->Begin(), it_end = aGroup->End(); it != it_end; ++it )
(*it)->ViewSetVisible( aVisible );
}
2013-09-09 07:34:52 +00:00
void MOVE_TOOL::vgUpdate( VIEW_GROUP* aGroup, VIEW_ITEM::ViewUpdateFlags aFlags ) const
{
std::set<VIEW_ITEM*>::const_iterator it, it_end;
for( it = aGroup->Begin(), it_end = aGroup->End(); it != it_end; ++it )
(*it)->ViewUpdate( aFlags );
2013-09-09 07:34:52 +00:00
}