KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_handle_key.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 "pcb_handle_key.h"
21
22#include <tool/edit_points.h>
23
24
25PCB_HANDLE_KEY::PCB_HANDLE_KEY( const KIID& aParent, bool aIsLine, int aIndex, unsigned aPointCount,
26 unsigned aLineCount ) :
27 m_parent( aParent ),
28 m_isLine( aIsLine ),
29 m_index( aIndex ),
30 m_pointCount( aPointCount ),
31 m_lineCount( aLineCount )
32{
33}
34
35
36std::optional<PCB_HANDLE_KEY> PCB_HANDLE_KEY::Make( const EDIT_POINTS& aPoints, const EDIT_POINT* aPoint )
37{
38 const EDA_ITEM* parent = aPoints.GetParent();
39
40 if( !aPoint || !parent )
41 return std::nullopt;
42
43 for( unsigned ii = 0; ii < aPoints.PointsSize(); ++ii )
44 {
45 if( &aPoints.Point( ii ) == aPoint )
46 {
47 return PCB_HANDLE_KEY( parent->m_Uuid, false, ii, aPoints.PointsSize(), aPoints.LinesSize() );
48 }
49 }
50
51 for( unsigned ii = 0; ii < aPoints.LinesSize(); ++ii )
52 {
53 if( &aPoints.Line( ii ) == aPoint )
54 {
55 return PCB_HANDLE_KEY( parent->m_Uuid, true, ii, aPoints.PointsSize(), aPoints.LinesSize() );
56 }
57 }
58
59 return std::nullopt;
60}
61
62
64{
65 const EDA_ITEM* parent = aPoints.GetParent();
66
67 if( !parent || parent->m_Uuid != m_parent )
68 return nullptr;
69
70 if( aPoints.PointsSize() != m_pointCount || aPoints.LinesSize() != m_lineCount )
71 return nullptr;
72
73 if( m_isLine )
74 {
75 if( (unsigned) m_index >= aPoints.LinesSize() )
76 return nullptr;
77
78 return &aPoints.Line( m_index );
79 }
80
81 if( (unsigned) m_index >= aPoints.PointsSize() )
82 return nullptr;
83
84 return &aPoints.Point( m_index );
85}
86
87
88bool PCB_HANDLE_KEY::operator==( const PCB_HANDLE_KEY& aOther ) const
89{
90 return m_parent == aOther.m_parent && m_isLine == aOther.m_isLine && m_index == aOther.m_index;
91}
92
93
94bool PCB_HANDLE_KEY::operator<( const PCB_HANDLE_KEY& aOther ) const
95{
96 if( m_parent != aOther.m_parent )
97 return m_parent < aOther.m_parent;
98
99 if( m_isLine != aOther.m_isLine )
100 return m_isLine < aOther.m_isLine;
101
102 return m_index < aOther.m_index;
103}
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
const KIID m_Uuid
Definition eda_item.h:599
EDIT_POINTS is a VIEW_ITEM that manages EDIT_POINTs and EDIT_LINEs and draws them.
unsigned int PointsSize() const
Return number of stored EDIT_POINTs.
EDIT_LINE & Line(unsigned int aIndex)
EDA_ITEM * GetParent() const
Return parent of the EDIT_POINTS.
unsigned int LinesSize() const
Return number of stored EDIT_LINEs.
EDIT_POINT & Point(unsigned int aIndex)
Represent a single point that can be used for modifying items.
Definition edit_points.h:44
Definition kiid.h:46
bool operator<(const PCB_HANDLE_KEY &aOther) const
unsigned m_lineCount
bool operator==(const PCB_HANDLE_KEY &aOther) const
EDIT_POINT * Resolve(EDIT_POINTS &aPoints) const
Find this key's handle in a collection, usually after it was rebuilt.
static std::optional< PCB_HANDLE_KEY > Make(const EDIT_POINTS &aPoints, const EDIT_POINT *aPoint)
Build the key for a handle in a collection.
PCB_HANDLE_KEY(const KIID &aParent, bool aIsLine, int aIndex, unsigned aPointCount, unsigned aLineCount)
unsigned m_pointCount