selection_tool.cpp 17 KB
Newer Older
1 2 3 4 5
/*
 * 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>
6
 * @author Maciej Suminski <maciej.suminski@cern.ch>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * 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>
28
#include <cassert>
29 30 31 32

#include <class_drawpanel_gal.h>
#include <class_board.h>
#include <class_board_item.h>
33
#include <class_track.h>
34 35 36 37
#include <class_module.h>

#include <wxPcbStruct.h>
#include <collectors.h>
38
#include <view/view_controls.h>
39
#include <view/view_group.h>
40
#include <painter.h>
41

Maciej Suminski's avatar
Maciej Suminski committed
42 43
#include <tool/tool_event.h>
#include <tool/tool_manager.h>
44 45 46

#include "selection_tool.h"
#include "selection_area.h"
47
#include "bright_box.h"
48
#include "common_actions.h"
49

50
using namespace KIGFX;
51 52 53
using boost::optional;

SELECTION_TOOL::SELECTION_TOOL() :
54
    TOOL_INTERACTIVE( "pcbnew.InteractiveSelection" ), m_multiple( false )
55 56
{
    m_selArea = new SELECTION_AREA;
57
    m_selection.group = new KIGFX::VIEW_GROUP;
58
}
59 60 61 62


SELECTION_TOOL::~SELECTION_TOOL()
{
63 64
    delete m_selArea;
    delete m_selection.group;
65 66 67 68 69
}


void SELECTION_TOOL::Reset()
{
70 71 72 73
    // Reinsert the VIEW_GROUP, in case it was removed from the VIEW
    getView()->Remove( m_selection.group );
    getView()->Add( m_selection.group );

74
    // The tool launches upon reception of action event ("pcbnew.InteractiveSelection")
75
    Go( &SELECTION_TOOL::Main, COMMON_ACTIONS::selectionActivate.MakeEvent() );
76 77 78
}


79
int SELECTION_TOOL::Main( TOOL_EVENT& aEvent )
80
{
81 82 83
    VIEW*   view = getView();

    assert( getModel<BOARD>( PCB_T ) != NULL );
84

85 86
    view->Add( m_selection.group );

87 88 89
    // Main loop: keep receiving events
    while( OPT_TOOL_EVENT evt = Wait() )
    {
90 91
        // Should selected items be added to the current selection or
        // become the new selection (discarding previously selected items)
Maciej Suminski's avatar
Maciej Suminski committed
92
        m_additive = evt->Modifier( MD_SHIFT );
93

94
        if( evt->IsCancel() )
95
        {
96
            if( !m_selection.Empty() )  // Cancel event deselects items...
97
                clearSelection();
98 99
            else                        // ...unless there is nothing selected
                break;                  // then exit the tool
100
        }
101 102

        // single click? Select single object
103
        if( evt->IsClick( BUT_LEFT ) )
104
            selectSingle( evt->Position() );
105

106
        // drag with LMB? Select multiple objects (or at least draw a selection box) or drag them
107
        if( evt->IsDrag( BUT_LEFT ) )
108
        {
109
            if( m_selection.Empty() || m_additive )
110 111 112
            {
                // If nothings has been selected or user wants to select more
                // draw the selection box
113
                selectMultiple();
114 115 116
            }
            else
            {
117
                // Check if dragging has started within any of selected items bounding box
118
                if( containsSelected( evt->Position() ) )
119
                {
120
                    // Yes -> run the move tool and wait till it finishes
121
                    m_toolMgr->InvokeTool( "pcbnew.InteractiveMove" );
122
                }
123
                else
124
                {
125
                    // No -> clear the selection list
126
                    clearSelection();
127
                }
128 129
            }
        }
130 131
    }

132 133 134
    m_selection.group->Clear();
    view->Remove( m_selection.group );

135
    return 0;
136 137
}

138

139 140
void SELECTION_TOOL::AddMenuItem( const TOOL_ACTION& aAction )
{
141
    assert( aAction.GetId() > 0 );    // Check if the action was registered before in ACTION_MANAGER
142 143 144 145 146

    m_menu.Add( aAction );
}


147
void SELECTION_TOOL::toggleSelection( BOARD_ITEM* aItem )
148
{
149
    if( m_selection.items.find( aItem ) != m_selection.items.end() )
150
    {
151
        deselectItem( aItem );
152 153 154 155

        // If there is nothing selected, disable the context menu
        if( m_selection.Empty() )
            SetContextMenu( &m_menu, CMENU_OFF );
156 157
    }
    else
158
    {
159
        if( !m_additive )
160
            clearSelection();
161

162
        // Prevent selection of invisible or inactive items
163
        if( selectable( aItem ) )
164
        {
165
            selectItem( aItem );
166 167 168 169

            // Now the context menu should be enabled
            SetContextMenu( &m_menu, CMENU_BUTTON );
        }
170 171 172
    }
}

173 174

void SELECTION_TOOL::clearSelection()
175
{
176
    VIEW_GROUP::const_iter it, it_end;
177

178
    for( it = m_selection.group->Begin(), it_end = m_selection.group->End(); it != it_end; ++it )
179
    {
180 181 182
        BOARD_ITEM* item = static_cast<BOARD_ITEM*>( *it );

        item->ViewSetVisible( true );
183 184
        item->ClearSelected();
    }
185

186 187
    m_selection.group->Clear();
    m_selection.items.clear();
188 189 190

    // Do not show the context menu when there is nothing selected
    SetContextMenu( &m_menu, CMENU_OFF );
191 192 193
}


194
void SELECTION_TOOL::selectSingle( const VECTOR2I& aWhere )
195
{
Maciej Suminski's avatar
Maciej Suminski committed
196 197
    BOARD* pcb = getModel<BOARD>( PCB_T );
    BOARD_ITEM* item;
198 199 200
    GENERAL_COLLECTORS_GUIDE guide = getEditFrame<PCB_EDIT_FRAME>()->GetCollectorsGuide();
    GENERAL_COLLECTOR collector;

Maciej Suminski's avatar
Maciej Suminski committed
201 202
    collector.Collect( pcb, GENERAL_COLLECTOR::AllBoardItems,
                       wxPoint( aWhere.x, aWhere.y ), guide );
203

Maciej Suminski's avatar
Maciej Suminski committed
204
    switch( collector.GetCount() )
205
    {
206
    case 0:
207
        if( !m_additive )
208
            clearSelection();
209

210 211 212
        break;

    case 1:
213
        toggleSelection( collector[0] );
214 215 216
        break;

    default:
217
        // Remove modules, they have to be selected by clicking on area that does not
218 219
        // contain anything but module footprint and not selectable items
        for( int i = collector.GetCount() - 1; i >= 0 ; --i )
220 221
        {
            BOARD_ITEM* boardItem = ( collector )[i];
222

223
            if( boardItem->Type() == PCB_MODULE_T || !selectable( boardItem ) )
224 225 226 227 228 229 230 231
                collector.Remove( i );
        }

        // Let's see if there is still disambiguation in selection..
        if( collector.GetCount() == 1 )
        {
            toggleSelection( collector[0] );
        }
232
        else if( collector.GetCount() > 1 )
233 234
        {
            item = disambiguationMenu( &collector );
235

236 237 238
            if( item )
                toggleSelection( item );
        }
239

240
        break;
241 242 243 244 245 246 247
    }
}


BOARD_ITEM* SELECTION_TOOL::pickSmallestComponent( GENERAL_COLLECTOR* aCollector )
{
    int count = aCollector->GetPrimaryCount();     // try to use preferred layer
248

249 250
    if( 0 == count )
        count = aCollector->GetCount();
251

252
    for( int i = 0; i < count; ++i )
253
    {
254
        if( ( *aCollector )[i]->Type() != PCB_MODULE_T )
255 256 257
            return NULL;
    }

258
    // All are modules, now find smallest MODULE
259 260 261
    int minDim = 0x7FFFFFFF;
    int minNdx = 0;

262
    for( int i = 0; i < count; ++i )
263
    {
264
        MODULE* module = (MODULE*) ( *aCollector )[i];
265

266 267
        int lx = module->GetBoundingBox().GetWidth();
        int ly = module->GetBoundingBox().GetHeight();
268

269
        int lmin = std::min( lx, ly );
270 271 272 273 274 275 276 277

        if( lmin < minDim )
        {
            minDim = lmin;
            minNdx = i;
        }
    }

Maciej Suminski's avatar
Maciej Suminski committed
278
    return (*aCollector)[minNdx];
279 280
}

281

282
bool SELECTION_TOOL::selectMultiple()
283
{
284
    bool cancelled = false;     // Was the tool cancelled while it was running?
285
    m_multiple = true;          // Multiple selection mode is active
286
    VIEW* view = getView();
287
    getViewControls()->SetAutoPan( true );
288

289
    view->Add( m_selArea );
290

291
    while( OPT_TOOL_EVENT evt = Wait() )
292 293
    {
        if( evt->IsCancel() )
294 295
        {
            cancelled = true;
296
            break;
297
        }
298

299
        if( evt->IsDrag( BUT_LEFT ) )
300
        {
301 302 303
            if( !m_additive )
                clearSelection();

304
            // Start drawing a selection box
305 306 307
            m_selArea->SetOrigin( evt->DragOrigin() );
            m_selArea->SetEnd( evt->Position() );
            m_selArea->ViewSetVisible( true );
308
            m_selArea->ViewUpdate( VIEW_ITEM::GEOMETRY );
309 310
        }

311
        if( evt->IsMouseUp( BUT_LEFT ) )
312
        {
313
            // End drawing the selection box
314
            m_selArea->ViewSetVisible( false );
315

316
            // Mark items within the selection box as selected
Maciej Suminski's avatar
Maciej Suminski committed
317
            std::vector<VIEW::LAYER_ITEM_PAIR> selectedItems;
318
            BOX2I selectionBox = m_selArea->ViewBBox();
319
            view->Query( selectionBox, selectedItems );         // Get the list of selected items
320

Maciej Suminski's avatar
Maciej Suminski committed
321
            std::vector<VIEW::LAYER_ITEM_PAIR>::iterator it, it_end;
322

323 324 325 326
            for( it = selectedItems.begin(), it_end = selectedItems.end(); it != it_end; ++it )
            {
                BOARD_ITEM* item = static_cast<BOARD_ITEM*>( it->first );

327
                // Add only those items that are visible and fully within the selection box
328
                if( selectable( item ) && selectionBox.Contains( item->ViewBBox() ) )
329
                    selectItem( item );
330
            }
331 332

            // Now the context menu should be enabled
333
            if( !m_selection.Empty() )
334
                SetContextMenu( &m_menu, CMENU_BUTTON );
335

336 337 338 339
            break;
        }
    }

340 341 342
    view->Remove( m_selArea );
    m_multiple = false;         // Multiple selection mode is inactive
    getViewControls()->SetAutoPan( false );
343 344

    return cancelled;
345 346 347
}


348
BOARD_ITEM* SELECTION_TOOL::disambiguationMenu( GENERAL_COLLECTOR* aCollector )
349
{
Maciej Suminski's avatar
Maciej Suminski committed
350
    BOARD_ITEM* current = NULL;
351
    boost::shared_ptr<BRIGHT_BOX> brightBox;
352
    CONTEXT_MENU menu;
353 354

    int limit = std::min( 10, aCollector->GetCount() );
355

356 357 358
    for( int i = 0; i < limit; ++i )
    {
        wxString text;
359
        BOARD_ITEM* item = ( *aCollector )[i];
360
        text = item->GetSelectMenuText();
361
        menu.Add( text, i );
362 363
    }

364 365
    menu.SetTitle( _( "Clarify selection" ) );
    SetContextMenu( &menu, CMENU_NOW );
366

367
    while( OPT_TOOL_EVENT evt = Wait() )
368
    {
Maciej Suminski's avatar
Maciej Suminski committed
369
        if( evt->Action() == TA_CONTEXT_MENU_UPDATE )
370 371
        {
            if( current )
372
                current->ClearBrightened();
373 374

            int id = *evt->GetCommandId();
375

376
            // User has pointed an item, so show it in a different way
377
            if( id >= 0 && id < limit )
378 379
            {
                current = ( *aCollector )[id];
380 381 382
                current->SetBrightened();
            }
            else
383
                current = NULL;
384
        }
Maciej Suminski's avatar
Maciej Suminski committed
385
        else if( evt->Action() == TA_CONTEXT_MENU_CHOICE )
386 387 388
        {
            optional<int> id = evt->GetCommandId();

389
            // User has selected an item, so this one will be returned
390 391
            if( id && ( *id >= 0 ) )
                current = ( *aCollector )[*id];
392

393 394 395
            break;
        }

396
        // Draw a mark to show which item is available to be selected
397 398 399 400
        if( current && current->IsBrightened() )
        {
            brightBox.reset( new BRIGHT_BOX( current ) );
            getView()->Add( brightBox.get() );
401 402 403
        }
    }

404
    // Removes possible brighten mark
405
    getView()->MarkTargetDirty( TARGET_OVERLAY );
406

407 408 409
    // Restore the original menu
    SetContextMenu( &m_menu, CMENU_BUTTON );

410
    return current;
411
}
412 413


414
bool SELECTION_TOOL::selectable( const BOARD_ITEM* aItem ) const
415
{
416
    // Is high contrast mode enabled?
417 418 419 420
    bool highContrast = getView()->GetPainter()->GetSettings()->GetHighContrast();

    if( highContrast )
    {
421
        bool onActive = false;          // Is the item on any of active layers?
422
        int layers[KIGFX::VIEW::VIEW_MAX_LAYERS], layers_count;
423 424 425

        // Filter out items that do not belong to active layers
        std::set<unsigned int> activeLayers = getView()->GetPainter()->
426
                                              GetSettings()->GetActiveLayers();
427 428 429 430
        aItem->ViewGetLayers( layers, layers_count );

        for( int i = 0; i < layers_count; ++i )
        {
431
            if( activeLayers.count( layers[i] ) > 0 )    // Item is on at least one of active layers
432 433 434 435 436 437
            {
                onActive = true;
                break;
            }
        }

438
        if( !onActive ) // We do not want to select items that are in the background
439 440
            return false;
    }
441

442
    BOARD* board = getModel<BOARD>( PCB_T );
443

444 445 446 447 448 449 450 451
    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 );

452
        return board->IsLayerVisible( top ) || board->IsLayerVisible( bottom );
453 454 455 456
    }
    break;

    case PCB_PAD_T:
457 458 459 460 461
    {
        // Pads are not selectable in multiple selection mode
        if( m_multiple )
            return false;

462
        // Pads are supposed to be on top, bottom or both at the same time (THT)
463
        if( aItem->IsOnLayer( LAYER_N_FRONT ) && board->IsLayerVisible( LAYER_N_FRONT ) )
464 465
            return true;

466
        if( aItem->IsOnLayer( LAYER_N_BACK ) && board->IsLayerVisible( LAYER_N_BACK ) )
467 468 469
            return true;

        return false;
470 471 472 473 474 475 476
    }
    break;

    case PCB_MODULE_TEXT_T:
        // Module texts are not selectable in multiple selection mode
        if( m_multiple )
            return false;
477

478 479
        break;

480
    // These are not selectable, otherwise silkscreen drawings would be easily destroyed
481
    case PCB_MODULE_EDGE_T:
482 483 484
    // and some other stuff that should be selected
    case NOT_USED:
    case TYPE_NOT_INIT:
485
        return false;
486 487 488

    default:    // Suppress warnings
        break;
489 490
    }

491 492 493
    // All other items are selected only if the layer on which they exist is visible
    return board->IsLayerVisible( aItem->GetLayer() );
}
494 495


496 497 498 499 500 501 502
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;

503
    public:
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
        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 );
}


581 582
bool SELECTION_TOOL::containsSelected( const VECTOR2I& aPoint ) const
{
583 584
    const unsigned GRIP_MARGIN = 500000;

585 586
    // Check if the point is located within any of the currently selected items bounding boxes
    std::set<BOARD_ITEM*>::iterator it, it_end;
587

588
    for( it = m_selection.items.begin(), it_end = m_selection.items.end(); it != it_end; ++it )
589 590
    {
        BOX2I itemBox = (*it)->ViewBBox();
591
        itemBox.Inflate( GRIP_MARGIN );    // Give some margin for gripping an item
592 593 594 595 596 597 598

        if( itemBox.Contains( aPoint ) )
            return true;
    }

    return false;
}