KiCad PCB EDA Suite
Loading...
Searching...
No Matches
board_text_var_adapter.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
21
22#include <eda_text.h>
23#include <footprint.h>
24#include <pcb_barcode.h>
25#include <pcb_field.h>
26#include <pcb_text.h>
27#include <pcb_textbox.h>
28
29
31 m_board( aBoard )
32{
33 m_tracker.SetSourceKeyExtractor(
34 [this]( EDA_ITEM* aItem ) -> std::vector<TEXT_VAR_REF_KEY>
35 {
36 return ExtractSourceKeys( aItem );
37 } );
38}
39
40
41std::vector<TEXT_VAR_REF_KEY> BOARD_TEXT_VAR_ADAPTER::collectKeys( BOARD_ITEM* aItem )
42{
43 std::vector<TEXT_VAR_REF_KEY> keys;
44
45 auto collect =
46 [&]( BOARD_ITEM* item )
47 {
48 if( EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( item ) )
49 {
50 for( const TEXT_VAR_REF_KEY& key : FilterTrackable( text->GetTextVarReferences() ) )
51 {
52 if( std::find( keys.begin(), keys.end(), key ) == keys.end() )
53 keys.push_back( key );
54 }
55 }
56 // PCB_BARCODE wraps an EDA_TEXT internally
57 else if( PCB_BARCODE* barcode = dynamic_cast<PCB_BARCODE*>( aItem ) )
58 {
59 for( const TEXT_VAR_REF_KEY& key : FilterTrackable( barcode->GetTextVarReferences() ) )
60 {
61 if( std::find( keys.begin(), keys.end(), key ) == keys.end() )
62 keys.push_back( key );
63 }
64 }
65 };
66
67 collect( aItem );
68
69 // Group members participate in the COMMIT architecture as first-class items. FOOTPRINT and
70 // PCB_TABLE children do not, and must therefore be collected into their parent's registration
71 // list
72 if ( aItem->Type() != PCB_GROUP_T )
73 aItem->RunOnChildren( collect, RECURSE_MODE::RECURSE );
74
75 return keys;
76}
77
78
80{
81 if( aItem )
82 m_tracker.RegisterItem( aItem, collectKeys( aItem ) );
83}
84
85
87{
88 if( aItem )
89 m_tracker.UnregisterItem( aItem );
90}
91
92
94{
95 // Register the item, AND handle any fan-out from other items using this item as a source.
96 // You might think no one could yet be referencing something that was just added, but if the
97 // add is the result of an undo of a delete, then any dangling cross-reference should get a
98 // chance to re-link to the item.
99 m_tracker.HandleItemChanged( aItem, collectKeys( aItem ) );
100}
101
102
103void BOARD_TEXT_VAR_ADAPTER::OnBoardItemsAdded( BOARD& aBoard, std::vector<BOARD_ITEM*>& aItems )
104{
105 for( BOARD_ITEM* item : aItems )
106 OnBoardItemAdded( aBoard, item );
107}
108
109
111{
112 // Handle any fan-out from other items using this item as a source, and then unregister it.
113 m_tracker.HandleItemChanged( aItem, collectKeys( aItem ) );
114 unregisterItem( aItem );
115}
116
117
118void BOARD_TEXT_VAR_ADAPTER::OnBoardItemsRemoved( BOARD& aBoard, std::vector<BOARD_ITEM*>& aItems )
119{
120 for( BOARD_ITEM* item : aItems )
121 OnBoardItemRemoved( aBoard, item );
122}
123
124
126{
127 // Re-register the item's own refs (its text may have changed), then fan out any cross-ref keys
128 // this item could source so dependents refresh.
129 m_tracker.HandleItemChanged( aItem, collectKeys( aItem ) );
130}
131
132
133void BOARD_TEXT_VAR_ADAPTER::OnBoardItemsChanged( BOARD& aBoard, std::vector<BOARD_ITEM*>& aItems )
134{
135 for( BOARD_ITEM* item : aItems )
136 OnBoardItemChanged( aBoard, item );
137}
138
139
141 std::vector<BOARD_ITEM*>& aAdded,
142 std::vector<BOARD_ITEM*>& aRemoved,
143 std::vector<BOARD_ITEM*>& aChanged )
144{
145 // Apply in order: remove, add, change. Removals first so re-added items
146 // with recycled pointers don't collide in the index. Changes last so
147 // fan-outs see the updated board state.
148 for( BOARD_ITEM* item : aRemoved )
149 OnBoardItemRemoved( aBoard, item );
150
151 for( BOARD_ITEM* item : aAdded )
152 OnBoardItemAdded( aBoard, item );
153
154 for( BOARD_ITEM* item : aChanged )
155 OnBoardItemChanged( aBoard, item );
156}
157
158
160{
161 m_tracker.Clear();
162
163 for( FOOTPRINT* fp : m_board.Footprints() )
164 registerItem( fp );
165
166 for( BOARD_ITEM* item : m_board.Drawings() )
167 registerItem( item );
168}
169
170
171std::vector<TEXT_VAR_REF_KEY> BOARD_TEXT_VAR_ADAPTER::ExtractSourceKeys( EDA_ITEM* aItem ) const
172{
173 std::vector<TEXT_VAR_REF_KEY> out;
174
175 auto addKey =
176 [&]( const wxString& aPrimary )
177 {
178 if( aPrimary.IsEmpty() )
179 return;
180
183 key.primary = aPrimary;
184
185 if( std::find( out.begin(), out.end(), key ) == out.end() )
186 out.push_back( key );
187 };
188
189 FOOTPRINT* fp = dynamic_cast<FOOTPRINT*>( aItem );
190
191 if( PCB_FIELD* field = dynamic_cast<PCB_FIELD*>( aItem ) )
192 fp = field->GetParentFootprint();
193
194 if( !fp )
195 return out;
196
197 // Any field edit fans out to every ${U1:*} dependent, which is cheaper than coupling to the
198 // commit system for a per-field diff against a pre-image
199 const wxString refdes = fp->GetReference();
200
201 if( !refdes.IsEmpty() )
202 addKey( refdes );
203
204 addKey( fp->m_Uuid.AsString() );
205
206 return out;
207}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:85
FOOTPRINT * GetParentFootprint() const
virtual void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction, RECURSE_MODE aMode) const
Invoke a function on all children.
Definition board_item.h:265
void OnBoardItemsAdded(BOARD &aBoard, std::vector< BOARD_ITEM * > &aItems) override
void OnBoardItemRemoved(BOARD &aBoard, BOARD_ITEM *aItem) override
void OnBoardCompositeUpdate(BOARD &aBoard, std::vector< BOARD_ITEM * > &aAdded, std::vector< BOARD_ITEM * > &aRemoved, std::vector< BOARD_ITEM * > &aChanged) override
void OnBoardItemsChanged(BOARD &aBoard, std::vector< BOARD_ITEM * > &aItems) override
std::vector< TEXT_VAR_REF_KEY > collectKeys(BOARD_ITEM *aItem)
void unregisterItem(BOARD_ITEM *aItem)
void OnBoardItemChanged(BOARD &aBoard, BOARD_ITEM *aItem) override
void OnBoardItemsRemoved(BOARD &aBoard, std::vector< BOARD_ITEM * > &aItems) override
void registerItem(BOARD_ITEM *aItem)
void RebuildIndex()
Scan the whole board and register every text-bearing item.
void OnBoardItemAdded(BOARD &aBoard, BOARD_ITEM *aItem) override
std::vector< TEXT_VAR_REF_KEY > ExtractSourceKeys(EDA_ITEM *aItem) const
Return the keys aItem could source as a cross-reference target.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
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
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:94
const wxString & GetReference() const
Definition footprint.h:793
wxString AsString() const
Definition kiid.cpp:264
@ RECURSE
Definition eda_item.h:51
BARCODE class definition.
Identifies a single resolvable source that a text item's ${...} reference depends on.
std::vector< TEXT_VAR_REF_KEY > FilterTrackable(const std::vector< TEXT_VAR_REF_KEY > &aRefs)
Filter aRefs down to the subset that should be registered in the index (drops OP and any future non-t...
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition typeinfo.h:103