2013-08-02 14:53:50 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
2013-09-19 15:02:57 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
2013-08-02 14:53:50 +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 <boost/foreach.hpp>
|
|
|
|
#include <boost/optional.hpp>
|
2013-09-26 16:38:58 +00:00
|
|
|
#include <cassert>
|
2013-08-02 14:53:50 +00:00
|
|
|
|
|
|
|
#include <class_drawpanel_gal.h>
|
|
|
|
#include <class_board.h>
|
|
|
|
#include <class_board_item.h>
|
2013-09-04 08:56:06 +00:00
|
|
|
#include <class_track.h>
|
2013-08-02 14:53:50 +00:00
|
|
|
#include <class_module.h>
|
|
|
|
|
|
|
|
#include <wxPcbStruct.h>
|
|
|
|
#include <collectors.h>
|
2013-09-02 14:29:10 +00:00
|
|
|
#include <view/view_controls.h>
|
2013-10-02 08:21:05 +00:00
|
|
|
#include <view/view_group.h>
|
2013-09-17 09:32:47 +00:00
|
|
|
#include <painter.h>
|
2013-08-02 14:53:50 +00:00
|
|
|
|
2013-09-16 07:52:47 +00:00
|
|
|
#include <tool/tool_event.h>
|
|
|
|
#include <tool/tool_manager.h>
|
2013-08-02 14:53:50 +00:00
|
|
|
|
|
|
|
#include "selection_tool.h"
|
|
|
|
#include "selection_area.h"
|
2013-09-16 09:00:59 +00:00
|
|
|
#include "bright_box.h"
|
2013-09-27 18:52:34 +00:00
|
|
|
#include "common_actions.h"
|
2013-08-02 14:53:50 +00:00
|
|
|
|
|
|
|
using namespace KiGfx;
|
|
|
|
using boost::optional;
|
|
|
|
|
|
|
|
SELECTION_TOOL::SELECTION_TOOL() :
|
2013-09-27 18:52:34 +00:00
|
|
|
TOOL_INTERACTIVE( "pcbnew.InteractiveSelection" ), m_multiple( false )
|
2013-08-06 07:31:08 +00:00
|
|
|
{
|
|
|
|
m_selArea = new SELECTION_AREA;
|
2013-10-02 08:21:05 +00:00
|
|
|
m_selection.group = new KiGfx::VIEW_GROUP;
|
2013-08-06 07:31:08 +00:00
|
|
|
}
|
2013-08-02 14:53:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
SELECTION_TOOL::~SELECTION_TOOL()
|
|
|
|
{
|
2013-10-02 08:21:05 +00:00
|
|
|
delete m_selArea;
|
|
|
|
delete m_selection.group;
|
2013-08-02 14:53:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SELECTION_TOOL::Reset()
|
|
|
|
{
|
2013-10-02 09:21:17 +00:00
|
|
|
m_selection.group->Clear();
|
|
|
|
m_selection.items.clear();
|
|
|
|
|
|
|
|
// Reinsert the VIEW_GROUP, in case it was removed from the VIEW
|
|
|
|
getView()->Remove( m_selection.group );
|
|
|
|
getView()->Add( m_selection.group );
|
|
|
|
|
2013-09-19 15:02:57 +00:00
|
|
|
// The tool launches upon reception of action event ("pcbnew.InteractiveSelection")
|
2013-09-27 18:52:34 +00:00
|
|
|
Go( &SELECTION_TOOL::Main, COMMON_ACTIONS::selectionActivate.MakeEvent() );
|
2013-09-26 16:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-06 07:31:08 +00:00
|
|
|
int SELECTION_TOOL::Main( TOOL_EVENT& aEvent )
|
2013-08-02 14:53:50 +00:00
|
|
|
{
|
2013-09-06 14:04:12 +00:00
|
|
|
BOARD* board = getModel<BOARD>( PCB_T );
|
2013-10-02 08:21:05 +00:00
|
|
|
VIEW* view = getView();
|
2013-09-26 16:38:58 +00:00
|
|
|
assert( board != NULL );
|
2013-09-02 14:29:10 +00:00
|
|
|
|
2013-10-02 08:21:05 +00:00
|
|
|
view->Add( m_selection.group );
|
|
|
|
|
2013-08-06 07:31:08 +00:00
|
|
|
// Main loop: keep receiving events
|
|
|
|
while( OPT_TOOL_EVENT evt = Wait() )
|
|
|
|
{
|
2013-09-19 15:02:57 +00:00
|
|
|
// Should selected items be added to the current selection or
|
|
|
|
// become the new selection (discarding previously selected items)
|
2013-08-21 15:37:27 +00:00
|
|
|
m_additive = evt->Modifier( MD_ModShift );
|
2013-08-09 08:18:48 +00:00
|
|
|
|
2013-08-06 07:31:08 +00:00
|
|
|
if( evt->IsCancel() )
|
2013-08-22 13:05:37 +00:00
|
|
|
{
|
2013-10-02 08:21:05 +00:00
|
|
|
if( !m_selection.Empty() ) // Cancel event deselects items...
|
2013-08-22 13:05:37 +00:00
|
|
|
clearSelection();
|
2013-09-19 15:02:57 +00:00
|
|
|
else // ...unless there is nothing selected
|
2013-09-26 16:38:58 +00:00
|
|
|
break; // then exit the tool
|
2013-08-22 13:05:37 +00:00
|
|
|
}
|
2013-08-06 07:31:08 +00:00
|
|
|
|
|
|
|
// single click? Select single object
|
|
|
|
if( evt->IsClick( MB_Left ) )
|
2013-08-09 08:18:48 +00:00
|
|
|
selectSingle( evt->Position() );
|
2013-08-06 07:31:08 +00:00
|
|
|
|
2013-09-02 14:29:10 +00:00
|
|
|
// drag with LMB? Select multiple objects (or at least draw a selection box) or drag them
|
2013-08-06 07:31:08 +00:00
|
|
|
if( evt->IsDrag( MB_Left ) )
|
2013-09-02 14:29:10 +00:00
|
|
|
{
|
2013-10-02 08:21:05 +00:00
|
|
|
if( m_selection.Empty() || m_additive )
|
2013-09-02 14:29:10 +00:00
|
|
|
{
|
|
|
|
// If nothings has been selected or user wants to select more
|
|
|
|
// draw the selection box
|
2013-09-19 15:02:57 +00:00
|
|
|
selectMultiple();
|
2013-09-02 14:29:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
// Check if dragging has started within any of selected items bounding box
|
2013-09-19 15:02:57 +00:00
|
|
|
if( containsSelected( evt->Position() ) )
|
2013-09-12 08:24:23 +00:00
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
// Yes -> run the move tool and wait till it finishes
|
2013-09-12 08:24:23 +00:00
|
|
|
m_toolMgr->InvokeTool( "pcbnew.InteractiveMove" );
|
2013-09-19 15:02:57 +00:00
|
|
|
}
|
2013-09-12 08:24:23 +00:00
|
|
|
else
|
2013-09-19 15:02:57 +00:00
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
// No -> clear the selection list
|
2013-09-12 08:24:23 +00:00
|
|
|
clearSelection();
|
2013-09-19 15:02:57 +00:00
|
|
|
}
|
2013-09-02 14:29:10 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-06 07:31:08 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 08:21:05 +00:00
|
|
|
m_selection.group->Clear();
|
|
|
|
view->Remove( m_selection.group );
|
|
|
|
|
2013-08-06 07:31:08 +00:00
|
|
|
return 0;
|
2013-08-02 14:53:50 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 07:31:08 +00:00
|
|
|
|
2013-09-26 16:38:58 +00:00
|
|
|
void SELECTION_TOOL::AddMenuItem( const TOOL_ACTION& aAction )
|
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
assert( aAction.GetId() > 0 ); // Check if the action was registered before in ACTION_MANAGER
|
2013-09-26 16:38:58 +00:00
|
|
|
|
|
|
|
m_menu.Add( aAction );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-09 08:18:48 +00:00
|
|
|
void SELECTION_TOOL::toggleSelection( BOARD_ITEM* aItem )
|
2013-08-02 14:53:50 +00:00
|
|
|
{
|
2013-10-02 08:21:05 +00:00
|
|
|
if( m_selection.items.find( aItem ) != m_selection.items.end() )
|
2013-08-06 07:31:08 +00:00
|
|
|
{
|
2013-09-26 16:38:58 +00:00
|
|
|
deselectItem( aItem );
|
2013-10-02 08:21:05 +00:00
|
|
|
|
|
|
|
// If there is nothing selected, disable the context menu
|
|
|
|
if( m_selection.Empty() )
|
|
|
|
SetContextMenu( &m_menu, CMENU_OFF );
|
2013-08-08 10:30:00 +00:00
|
|
|
}
|
|
|
|
else
|
2013-08-02 14:53:50 +00:00
|
|
|
{
|
2013-08-09 08:18:48 +00:00
|
|
|
if( !m_additive )
|
2013-08-06 07:31:08 +00:00
|
|
|
clearSelection();
|
2013-08-08 10:30:00 +00:00
|
|
|
|
2013-09-19 15:02:57 +00:00
|
|
|
// Prevent selection of invisible or inactive items
|
2013-09-04 08:56:06 +00:00
|
|
|
if( selectable( aItem ) )
|
2013-10-02 08:21:05 +00:00
|
|
|
{
|
2013-09-26 16:38:58 +00:00
|
|
|
selectItem( aItem );
|
2013-10-02 08:21:05 +00:00
|
|
|
|
|
|
|
// Now the context menu should be enabled
|
|
|
|
SetContextMenu( &m_menu, CMENU_BUTTON );
|
|
|
|
}
|
2013-08-02 14:53:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 07:31:08 +00:00
|
|
|
|
|
|
|
void SELECTION_TOOL::clearSelection()
|
2013-08-02 14:53:50 +00:00
|
|
|
{
|
2013-10-02 08:21:05 +00:00
|
|
|
VIEW_GROUP::const_iter it, it_end;
|
|
|
|
for( it = m_selection.group->Begin(), it_end = m_selection.group->End(); it != it_end; ++it )
|
2013-08-02 14:53:50 +00:00
|
|
|
{
|
2013-10-02 08:21:05 +00:00
|
|
|
BOARD_ITEM* item = static_cast<BOARD_ITEM*>( *it );
|
|
|
|
|
|
|
|
item->ViewSetVisible( true );
|
2013-08-06 07:31:08 +00:00
|
|
|
item->ClearSelected();
|
|
|
|
}
|
2013-08-02 14:53:50 +00:00
|
|
|
|
2013-10-02 08:21:05 +00:00
|
|
|
m_selection.group->Clear();
|
|
|
|
m_selection.items.clear();
|
2013-09-26 16:38:58 +00:00
|
|
|
|
|
|
|
// Do not show the context menu when there is nothing selected
|
|
|
|
SetContextMenu( &m_menu, CMENU_OFF );
|
2013-08-02 14:53:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-09 08:18:48 +00:00
|
|
|
void SELECTION_TOOL::selectSingle( const VECTOR2I& aWhere )
|
2013-08-02 14:53:50 +00:00
|
|
|
{
|
2013-08-06 08:30:09 +00:00
|
|
|
BOARD* pcb = getModel<BOARD>( PCB_T );
|
|
|
|
BOARD_ITEM* item;
|
2013-08-02 14:53:50 +00:00
|
|
|
GENERAL_COLLECTORS_GUIDE guide = getEditFrame<PCB_EDIT_FRAME>()->GetCollectorsGuide();
|
|
|
|
GENERAL_COLLECTOR collector;
|
|
|
|
|
2013-08-08 17:41:20 +00:00
|
|
|
collector.Collect( pcb, GENERAL_COLLECTOR::AllBoardItems,
|
|
|
|
wxPoint( aWhere.x, aWhere.y ), guide );
|
2013-08-06 07:31:08 +00:00
|
|
|
|
2013-08-06 08:30:09 +00:00
|
|
|
switch( collector.GetCount() )
|
2013-08-02 14:53:50 +00:00
|
|
|
{
|
2013-08-06 07:31:08 +00:00
|
|
|
case 0:
|
2013-08-09 08:18:48 +00:00
|
|
|
if( !m_additive )
|
2013-08-06 07:31:08 +00:00
|
|
|
clearSelection();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2013-08-09 08:18:48 +00:00
|
|
|
toggleSelection( collector[0] );
|
2013-08-06 07:31:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-09-17 09:32:47 +00:00
|
|
|
// Remove modules, they have to be selected by clicking on area that does not
|
2013-09-17 11:47:33 +00:00
|
|
|
// contain anything but module footprint and not selectable items
|
|
|
|
for( int i = collector.GetCount() - 1; i >= 0 ; --i )
|
2013-09-12 08:46:22 +00:00
|
|
|
{
|
|
|
|
BOARD_ITEM* boardItem = ( collector )[i];
|
2013-09-17 11:47:33 +00:00
|
|
|
if( boardItem->Type() == PCB_MODULE_T || !selectable( boardItem ) )
|
2013-09-12 08:46:22 +00:00
|
|
|
collector.Remove( i );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's see if there is still disambiguation in selection..
|
|
|
|
if( collector.GetCount() == 1 )
|
|
|
|
{
|
|
|
|
toggleSelection( collector[0] );
|
|
|
|
}
|
2013-09-17 11:47:33 +00:00
|
|
|
else if( collector.GetCount() > 1 )
|
2013-09-12 08:46:22 +00:00
|
|
|
{
|
|
|
|
item = disambiguationMenu( &collector );
|
2013-09-26 16:38:58 +00:00
|
|
|
|
2013-09-12 08:46:22 +00:00
|
|
|
if( item )
|
|
|
|
toggleSelection( item );
|
|
|
|
}
|
2013-08-06 07:31:08 +00:00
|
|
|
break;
|
2013-08-02 14:53:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOARD_ITEM* SELECTION_TOOL::pickSmallestComponent( GENERAL_COLLECTOR* aCollector )
|
|
|
|
{
|
|
|
|
int count = aCollector->GetPrimaryCount(); // try to use preferred layer
|
2013-08-06 07:31:08 +00:00
|
|
|
if( 0 == count )
|
|
|
|
count = aCollector->GetCount();
|
2013-08-02 14:53:50 +00:00
|
|
|
|
2013-08-06 07:31:08 +00:00
|
|
|
for( int i = 0; i < count; ++i )
|
2013-08-02 14:53:50 +00:00
|
|
|
{
|
2013-08-06 07:31:08 +00:00
|
|
|
if( ( *aCollector )[i]->Type() != PCB_MODULE_T )
|
2013-08-02 14:53:50 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-08-06 07:31:08 +00:00
|
|
|
// All are modules, now find smallest MODULE
|
2013-08-02 14:53:50 +00:00
|
|
|
int minDim = 0x7FFFFFFF;
|
|
|
|
int minNdx = 0;
|
|
|
|
|
2013-08-06 07:31:08 +00:00
|
|
|
for( int i = 0; i < count; ++i )
|
2013-08-02 14:53:50 +00:00
|
|
|
{
|
2013-08-06 08:30:09 +00:00
|
|
|
MODULE* module = (MODULE*)( *aCollector )[i];
|
2013-08-02 14:53:50 +00:00
|
|
|
|
2013-08-06 07:31:08 +00:00
|
|
|
int lx = module->GetBoundingBox().GetWidth();
|
|
|
|
int ly = module->GetBoundingBox().GetHeight();
|
2013-08-02 14:53:50 +00:00
|
|
|
|
2013-08-06 07:31:08 +00:00
|
|
|
int lmin = std::min( lx, ly );
|
2013-08-02 14:53:50 +00:00
|
|
|
|
|
|
|
if( lmin < minDim )
|
|
|
|
{
|
|
|
|
minDim = lmin;
|
|
|
|
minNdx = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:30:09 +00:00
|
|
|
return (*aCollector)[minNdx];
|
2013-08-02 14:53:50 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 07:31:08 +00:00
|
|
|
|
2013-09-06 14:04:12 +00:00
|
|
|
bool SELECTION_TOOL::selectMultiple()
|
2013-08-02 14:53:50 +00:00
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
bool cancelled = false; // Was the tool cancelled while it was running?
|
2013-09-19 15:02:57 +00:00
|
|
|
m_multiple = true; // Multiple selection mode is active
|
2013-09-27 14:23:43 +00:00
|
|
|
VIEW* view = getView();
|
2013-09-19 15:02:57 +00:00
|
|
|
getViewControls()->SetAutoPan( true );
|
2013-08-06 07:31:08 +00:00
|
|
|
|
2013-09-19 15:02:57 +00:00
|
|
|
view->Add( m_selArea );
|
2013-08-06 07:31:08 +00:00
|
|
|
|
2013-09-19 15:02:57 +00:00
|
|
|
while( OPT_TOOL_EVENT evt = Wait() )
|
2013-08-06 07:31:08 +00:00
|
|
|
{
|
|
|
|
if( evt->IsCancel() )
|
2013-09-06 14:04:12 +00:00
|
|
|
{
|
|
|
|
cancelled = true;
|
2013-08-06 07:31:08 +00:00
|
|
|
break;
|
2013-09-06 14:04:12 +00:00
|
|
|
}
|
2013-08-06 07:31:08 +00:00
|
|
|
|
|
|
|
if( evt->IsDrag( MB_Left ) )
|
|
|
|
{
|
2013-08-09 08:18:48 +00:00
|
|
|
if( !m_additive )
|
|
|
|
clearSelection();
|
|
|
|
|
2013-08-08 17:42:19 +00:00
|
|
|
// Start drawing a selection box
|
2013-08-06 07:31:08 +00:00
|
|
|
m_selArea->SetOrigin( evt->DragOrigin() );
|
|
|
|
m_selArea->SetEnd( evt->Position() );
|
|
|
|
m_selArea->ViewSetVisible( true );
|
2013-09-02 14:29:10 +00:00
|
|
|
m_selArea->ViewUpdate( VIEW_ITEM::GEOMETRY );
|
2013-08-06 07:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( evt->IsMouseUp( MB_Left ) )
|
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
// End drawing the selection box
|
2013-08-06 07:31:08 +00:00
|
|
|
m_selArea->ViewSetVisible( false );
|
2013-08-08 17:42:19 +00:00
|
|
|
|
2013-09-19 15:02:57 +00:00
|
|
|
// Mark items within the selection box as selected
|
2013-08-08 17:42:19 +00:00
|
|
|
std::vector<VIEW::LayerItemPair> selectedItems;
|
|
|
|
BOX2I selectionBox = m_selArea->ViewBBox();
|
2013-09-19 15:02:57 +00:00
|
|
|
view->Query( selectionBox, selectedItems ); // Get the list of selected items
|
2013-08-08 17:42:19 +00:00
|
|
|
|
|
|
|
std::vector<VIEW::LayerItemPair>::iterator it, it_end;
|
|
|
|
for( it = selectedItems.begin(), it_end = selectedItems.end(); it != it_end; ++it )
|
|
|
|
{
|
|
|
|
BOARD_ITEM* item = static_cast<BOARD_ITEM*>( it->first );
|
|
|
|
|
2013-09-19 15:02:57 +00:00
|
|
|
// Add only those items that are visible and fully within the selection box
|
2013-09-04 08:56:06 +00:00
|
|
|
if( selectable( item ) && selectionBox.Contains( item->ViewBBox() ) )
|
2013-10-02 08:21:05 +00:00
|
|
|
selectItem( item );
|
2013-08-08 17:42:19 +00:00
|
|
|
}
|
2013-09-26 16:38:58 +00:00
|
|
|
|
|
|
|
// Now the context menu should be enabled
|
2013-10-02 08:21:05 +00:00
|
|
|
if( !m_selection.Empty() )
|
2013-09-26 16:38:58 +00:00
|
|
|
SetContextMenu( &m_menu, CMENU_BUTTON );
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2013-08-06 07:31:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-19 15:02:57 +00:00
|
|
|
view->Remove( m_selArea );
|
|
|
|
m_multiple = false; // Multiple selection mode is inactive
|
|
|
|
getViewControls()->SetAutoPan( false );
|
2013-09-06 14:04:12 +00:00
|
|
|
|
|
|
|
return cancelled;
|
2013-08-02 14:53:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-22 13:05:37 +00:00
|
|
|
BOARD_ITEM* SELECTION_TOOL::disambiguationMenu( GENERAL_COLLECTOR* aCollector )
|
2013-08-02 14:53:50 +00:00
|
|
|
{
|
2013-08-08 17:41:20 +00:00
|
|
|
BOARD_ITEM* current = NULL;
|
2013-09-16 09:00:59 +00:00
|
|
|
boost::shared_ptr<BRIGHT_BOX> brightBox;
|
2013-09-26 16:38:58 +00:00
|
|
|
CONTEXT_MENU menu;
|
2013-08-06 07:31:08 +00:00
|
|
|
|
|
|
|
int limit = std::min( 10, aCollector->GetCount() );
|
|
|
|
for( int i = 0; i < limit; ++i )
|
|
|
|
{
|
|
|
|
wxString text;
|
2013-09-12 08:46:22 +00:00
|
|
|
BOARD_ITEM* item = ( *aCollector )[i];
|
2013-09-17 11:47:33 +00:00
|
|
|
text = item->GetSelectMenuText();
|
2013-09-26 16:38:58 +00:00
|
|
|
menu.Add( text, i );
|
2013-08-06 07:31:08 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 16:38:58 +00:00
|
|
|
menu.SetTitle( _( "Clarify selection" ) );
|
|
|
|
SetContextMenu( &menu, CMENU_NOW );
|
2013-08-06 07:31:08 +00:00
|
|
|
|
2013-09-16 09:00:59 +00:00
|
|
|
while( OPT_TOOL_EVENT evt = Wait() )
|
2013-08-06 07:31:08 +00:00
|
|
|
{
|
|
|
|
if( evt->Action() == TA_ContextMenuUpdate )
|
|
|
|
{
|
|
|
|
if( current )
|
2013-08-08 10:30:00 +00:00
|
|
|
current->ClearBrightened();
|
2013-08-06 07:31:08 +00:00
|
|
|
|
|
|
|
int id = *evt->GetCommandId();
|
2013-08-08 10:30:00 +00:00
|
|
|
|
2013-09-19 15:02:57 +00:00
|
|
|
// User has pointed an item, so show it in a different way
|
2013-09-29 19:23:45 +00:00
|
|
|
if( id >= 0 && id < limit )
|
2013-08-06 07:31:08 +00:00
|
|
|
{
|
|
|
|
current = ( *aCollector )[id];
|
2013-08-08 10:30:00 +00:00
|
|
|
current->SetBrightened();
|
|
|
|
}
|
|
|
|
else
|
2013-08-06 07:31:08 +00:00
|
|
|
current = NULL;
|
2013-08-08 10:30:00 +00:00
|
|
|
}
|
|
|
|
else if( evt->Action() == TA_ContextMenuChoice )
|
2013-08-06 07:31:08 +00:00
|
|
|
{
|
|
|
|
optional<int> id = evt->GetCommandId();
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
// User has selected an item, so this one will be returned
|
2013-08-06 07:31:08 +00:00
|
|
|
if( id && ( *id >= 0 ) )
|
|
|
|
current = ( *aCollector )[*id];
|
2013-08-08 10:30:00 +00:00
|
|
|
|
2013-09-16 09:00:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-19 15:02:57 +00:00
|
|
|
// Draw a mark to show which item is available to be selected
|
2013-09-16 09:00:59 +00:00
|
|
|
if( current && current->IsBrightened() )
|
|
|
|
{
|
|
|
|
brightBox.reset( new BRIGHT_BOX( current ) );
|
|
|
|
getView()->Add( brightBox.get() );
|
2013-08-06 07:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 08:21:05 +00:00
|
|
|
// Removes possible brighten mark
|
2013-09-16 09:00:59 +00:00
|
|
|
getView()->MarkTargetDirty( TARGET_OVERLAY );
|
2013-09-26 12:09:56 +00:00
|
|
|
|
2013-09-26 16:38:58 +00:00
|
|
|
// Restore the original menu
|
|
|
|
SetContextMenu( &m_menu, CMENU_BUTTON );
|
|
|
|
|
2013-09-16 09:00:59 +00:00
|
|
|
return current;
|
2013-08-06 07:31:08 +00:00
|
|
|
}
|
2013-09-04 08:56:06 +00:00
|
|
|
|
|
|
|
|
2013-09-26 12:09:56 +00:00
|
|
|
bool SELECTION_TOOL::selectable( const BOARD_ITEM* aItem ) const
|
2013-09-04 08:56:06 +00:00
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
// Is high contrast mode enabled?
|
2013-09-17 09:32:47 +00:00
|
|
|
bool highContrast = getView()->GetPainter()->GetSettings()->GetHighContrast();
|
|
|
|
|
|
|
|
if( highContrast )
|
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
bool onActive = false; // Is the item on any of active layers?
|
2013-09-17 09:32:47 +00:00
|
|
|
int layers[KiGfx::VIEW::VIEW_MAX_LAYERS], layers_count;
|
|
|
|
|
|
|
|
// Filter out items that do not belong to active layers
|
|
|
|
std::set<unsigned int> activeLayers = getView()->GetPainter()->
|
|
|
|
GetSettings()->GetActiveLayers();
|
|
|
|
aItem->ViewGetLayers( layers, layers_count );
|
|
|
|
|
|
|
|
for( int i = 0; i < layers_count; ++i )
|
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
if( activeLayers.count( layers[i] ) > 0 ) // Item is on at least one of active layers
|
2013-09-17 09:32:47 +00:00
|
|
|
{
|
|
|
|
onActive = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
if( !onActive ) // We do not want to select items that are in the background
|
2013-09-17 09:32:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-09-06 14:04:12 +00:00
|
|
|
|
2013-09-17 11:47:33 +00:00
|
|
|
BOARD* board = getModel<BOARD>( PCB_T );
|
2013-09-04 08:56:06 +00:00
|
|
|
switch( aItem->Type() )
|
|
|
|
{
|
|
|
|
case PCB_VIA_T:
|
|
|
|
{
|
|
|
|
// For vias it is enough if only one of layers is visible
|
|
|
|
LAYER_NUM top, bottom;
|
|
|
|
static_cast<const SEGVIA*>( aItem )->ReturnLayerPair( &top, &bottom );
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
return ( board->IsLayerVisible( top ) || board->IsLayerVisible( bottom ) );
|
2013-09-04 08:56:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PCB_PAD_T:
|
2013-09-09 08:10:02 +00:00
|
|
|
{
|
|
|
|
// Pads are not selectable in multiple selection mode
|
|
|
|
if( m_multiple )
|
|
|
|
return false;
|
|
|
|
|
2013-09-04 08:56:06 +00:00
|
|
|
// Pads are supposed to be on top, bottom or both at the same time (THT)
|
2013-09-06 14:04:12 +00:00
|
|
|
if( aItem->IsOnLayer( LAYER_N_FRONT ) && board->IsLayerVisible( LAYER_N_FRONT ) )
|
2013-09-04 08:56:06 +00:00
|
|
|
return true;
|
|
|
|
|
2013-09-06 14:04:12 +00:00
|
|
|
if( aItem->IsOnLayer( LAYER_N_BACK ) && board->IsLayerVisible( LAYER_N_BACK ) )
|
2013-09-04 08:56:06 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2013-09-09 08:10:02 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PCB_MODULE_TEXT_T:
|
|
|
|
// Module texts are not selectable in multiple selection mode
|
|
|
|
if( m_multiple )
|
|
|
|
return false;
|
2013-09-04 08:56:06 +00:00
|
|
|
break;
|
|
|
|
|
2013-10-02 10:02:25 +00:00
|
|
|
// These are not selectable, otherwise silkscreen drawings would be easily destroyed
|
2013-09-04 08:56:06 +00:00
|
|
|
case PCB_MODULE_EDGE_T:
|
2013-10-02 10:02:25 +00:00
|
|
|
// and some other stuff that should be selected
|
|
|
|
case NOT_USED:
|
|
|
|
case TYPE_NOT_INIT:
|
2013-09-04 08:56:06 +00:00
|
|
|
return false;
|
2013-09-26 12:09:56 +00:00
|
|
|
|
|
|
|
default: // Suppress warnings
|
|
|
|
break;
|
2013-09-04 08:56:06 +00:00
|
|
|
}
|
|
|
|
|
2013-09-06 14:04:12 +00:00
|
|
|
// All other items are selected only if the layer on which they exist is visible
|
|
|
|
return board->IsLayerVisible( aItem->GetLayer() );
|
|
|
|
}
|
2013-09-19 15:02:57 +00:00
|
|
|
|
|
|
|
|
2013-10-02 08:21:05 +00:00
|
|
|
void SELECTION_TOOL::selectItem( BOARD_ITEM* aItem )
|
|
|
|
{
|
|
|
|
/// Selecting an item needs a few operations, so they are wrapped in a functor
|
|
|
|
class selectBase_
|
|
|
|
{
|
|
|
|
SELECTION& s;
|
|
|
|
|
|
|
|
public:
|
|
|
|
selectBase_( SELECTION& s_ ) : s( s_ ) {}
|
|
|
|
|
|
|
|
void operator()( BOARD_ITEM* item )
|
|
|
|
{
|
|
|
|
s.group->Add( item );
|
|
|
|
// Hide the original item, so it is shown only on overlay
|
|
|
|
item->ViewSetVisible( false );
|
|
|
|
item->SetSelected();
|
|
|
|
}
|
|
|
|
} selectBase( m_selection );
|
|
|
|
|
|
|
|
// 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() )
|
|
|
|
selectBase( pad );
|
|
|
|
|
|
|
|
for( BOARD_ITEM* drawing = module->GraphicalItems().GetFirst(); drawing;
|
|
|
|
drawing = drawing->Next() )
|
|
|
|
selectBase( drawing );
|
|
|
|
|
|
|
|
selectBase( &module->Reference() );
|
|
|
|
selectBase( &module->Value() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add items to the VIEW_GROUP, so they will be displayed on the overlay
|
|
|
|
selectBase( aItem );
|
|
|
|
m_selection.items.insert( aItem );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SELECTION_TOOL::deselectItem( BOARD_ITEM* aItem )
|
|
|
|
{
|
|
|
|
/// Deselecting an item needs a few operations, so they are wrapped in a functor
|
|
|
|
class deselectBase_
|
|
|
|
{
|
|
|
|
SELECTION& s;
|
|
|
|
|
|
|
|
public:
|
|
|
|
deselectBase_( SELECTION& s_ ) : s( s_ ) {}
|
|
|
|
|
|
|
|
void operator()( BOARD_ITEM* item )
|
|
|
|
{
|
|
|
|
s.group->Remove( item );
|
|
|
|
// Restore original item visibility
|
|
|
|
item->ViewSetVisible( true );
|
|
|
|
item->ClearSelected();
|
|
|
|
}
|
|
|
|
} deselectBase( m_selection );
|
|
|
|
|
|
|
|
// 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() )
|
|
|
|
deselectBase( pad );
|
|
|
|
|
|
|
|
for( BOARD_ITEM* drawing = module->GraphicalItems().GetFirst(); drawing;
|
|
|
|
drawing = drawing->Next() )
|
|
|
|
deselectBase( drawing );
|
|
|
|
|
|
|
|
deselectBase( &module->Reference() );
|
|
|
|
deselectBase( &module->Value() );
|
|
|
|
}
|
|
|
|
|
|
|
|
deselectBase( aItem );
|
|
|
|
m_selection.items.erase( aItem );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-19 15:02:57 +00:00
|
|
|
bool SELECTION_TOOL::containsSelected( const VECTOR2I& aPoint ) const
|
|
|
|
{
|
2013-09-27 16:51:21 +00:00
|
|
|
const unsigned GRIP_MARGIN = 500000;
|
|
|
|
|
2013-09-19 15:02:57 +00:00
|
|
|
// Check if the point is located within any of the currently selected items bounding boxes
|
|
|
|
std::set<BOARD_ITEM*>::iterator it, it_end;
|
2013-10-02 08:21:05 +00:00
|
|
|
for( it = m_selection.items.begin(), it_end = m_selection.items.end(); it != it_end; ++it )
|
2013-09-19 15:02:57 +00:00
|
|
|
{
|
|
|
|
BOX2I itemBox = (*it)->ViewBBox();
|
2013-09-27 16:51:21 +00:00
|
|
|
itemBox.Inflate( GRIP_MARGIN ); // Give some margin for gripping an item
|
2013-09-19 15:02:57 +00:00
|
|
|
|
|
|
|
if( itemBox.Contains( aPoint ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|