KiCad PCB EDA Suite
Loading...
Searching...
No Matches
block_drag_pick.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "block_drag_pick.h"
21
22#include <board_item.h>
24#include <pcb_generator.h>
25#include <pcb_shape.h>
26#include <tool/edit_points.h>
28
29
30bool BlockDragHandlePickable( const EDIT_POINTS& aCollection, const EDIT_POINT& aPoint )
31{
32 const EDA_ITEM* parent = aCollection.GetParent();
33
34 if( !parent || !parent->IsBOARD_ITEM() )
35 return false;
36
37 const BOARD_ITEM& item = static_cast<const BOARD_ITEM&>( *parent );
38
40 return false;
41
42 // A block drag displaces the handles it picked and then asks the behavior to rebuild the
43 // item once per handle. A handle that rebuilds the whole shape reads the shape as the
44 // other handles have already left it, so picking both compounds
45 if( aPoint.DrivesWholeShape() )
46 return false;
47
48 if( dynamic_cast<const EDIT_LINE*>( &aPoint ) )
49 {
50 if( item.Type() == PCB_ZONE_T || dynamic_cast<const PCB_GENERATOR_POLY*>( &item ) )
51 return true;
52
53 return item.Type() == PCB_SHAPE_T && static_cast<const PCB_SHAPE&>( item ).GetShape() == SHAPE_T::POLY;
54 }
55
56 return true;
57}
58
59
60void BlockDragApplyPick( std::set<PCB_HANDLE_KEY>& aPicked, const PCB_HANDLE_KEY& aKey, bool aAccumulate )
61{
62 if( !aAccumulate )
63 {
64 aPicked.clear();
65 aPicked.insert( aKey );
66 return;
67 }
68
69 if( !aPicked.erase( aKey ) )
70 aPicked.insert( aKey );
71}
72
73
74SHAPE_LINE_CHAIN BlockDragPickRect( const VECTOR2I& aFirst, const VECTOR2I& aSecond )
75{
76 return SHAPE_LINE_CHAIN( { aFirst, VECTOR2I( aSecond.x, aFirst.y ), aSecond, VECTOR2I( aFirst.x, aSecond.y ) },
77 true );
78}
79
80
81void BlockDragLassoPick( const std::vector<EDIT_POINTS*>& aCollections, const SHAPE_LINE_CHAIN& aLasso,
82 bool aAccumulate, std::set<PCB_HANDLE_KEY>& aPicked )
83{
84 if( !aAccumulate )
85 aPicked.clear();
86
87 if( aLasso.PointCount() < 3 )
88 return;
89
90 aLasso.GenerateBBoxCache();
91
92 for( EDIT_POINTS* collection : aCollections )
93 {
94 for( unsigned ii = 0; ii < collection->PointsSize(); ++ii )
95 {
96 const EDIT_POINT& point = collection->Point( ii );
97
98 if( !aLasso.PointInside( point.GetPosition(), 0, true ) && !aLasso.PointOnEdge( point.GetPosition() ) )
99 continue;
100
101 if( !BlockDragHandlePickable( *collection, point ) )
102 continue;
103
104 if( std::optional<PCB_HANDLE_KEY> key = PCB_HANDLE_KEY::Make( *collection, &point ) )
105 aPicked.insert( *key );
106 }
107 }
108}
BLOCK_DRAG_CAPABILITY BlockDragCapability(const BOARD_ITEM &aItem)
Classify one board item for a block drag.
@ DEFORMABLE
Shows handles, and reshapes as the user drags the ones they picked.
SHAPE_LINE_CHAIN BlockDragPickRect(const VECTOR2I &aFirst, const VECTOR2I &aSecond)
The closed rectangle a box pick covers, built from two opposite corners.
bool BlockDragHandlePickable(const EDIT_POINTS &aCollection, const EDIT_POINT &aPoint)
True when the user is allowed to pick this handle.
void BlockDragApplyPick(std::set< PCB_HANDLE_KEY > &aPicked, const PCB_HANDLE_KEY &aKey, bool aAccumulate)
Add one clicked handle to the picked set.
void BlockDragLassoPick(const std::vector< EDIT_POINTS * > &aCollections, const SHAPE_LINE_CHAIN &aLasso, bool aAccumulate, std::set< PCB_HANDLE_KEY > &aPicked)
Add the handles a lasso surrounds to the picked set.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:99
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
Represent a line connecting two EDIT_POINTs.
EDIT_POINTS is a VIEW_ITEM that manages EDIT_POINTs and EDIT_LINEs and draws them.
EDA_ITEM * GetParent() const
Return parent of the EDIT_POINTS.
Represent a single point that can be used for modifying items.
Definition edit_points.h:44
bool DrivesWholeShape() const
Whether dragging this handle rebuilds the whole shape instead of placing one of its points,...
virtual VECTOR2I GetPosition() const
Return coordinates of an EDIT_POINT.
Definition edit_points.h:72
bool IsBOARD_ITEM() const
Definition view_item.h:98
Lasting name for one handle in an EDIT_POINTS collection.
static std::optional< PCB_HANDLE_KEY > Make(const EDIT_POINTS &aPoints, const EDIT_POINT *aPoint)
Build the key for a handle in a collection.
bool PointOnEdge(const VECTOR2I &aP, int aAccuracy=0) const
Check if point aP lies on an edge or vertex of the line chain.
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void GenerateBBoxCache() const
int PointCount() const
Return the number of points (vertices) in this line chain.
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const override
Check if point aP lies inside a closed shape.
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:80
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:100
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708