KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_sync_sheet_pins.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 (C) 2023 Ethan Chien <[email protected]>
5 * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
26#include "sch_sheet_pin.h"
31
32#include <bitmaps.h>
33#include <map>
34#include <memory>
35#include <sch_label.h>
36#include <sch_sheet.h>
37#include <sch_pin.h>
38#include <sch_screen.h>
39#include <wx/bookctrl.h>
40#include <eda_item.h>
41
43 wxNotebook* aNoteBook, int aIndex,
45 const SCH_SHEET_PATH& aPath ) :
47 m_sheet( aSheet ),
48 m_noteBook( aNoteBook ),
49 m_index( aIndex ),
50 m_sheetFileName( aSheet->GetFileName() ),
51 m_agent( aAgent ),
52 m_path( std::move( aPath ) )
53{
54 m_btnUsePinAsTemplate->SetBitmap( KiBitmapBundle( BITMAPS::add_hierar_pin ) );
55 m_btnUseLabelAsTemplate->SetBitmap( KiBitmapBundle( BITMAPS::add_hierarchical_label ) );
56 m_btnUndo->SetBitmap( KiBitmapBundle( BITMAPS::left ) );
57
58 m_labelSheetName->SetLabel( aSheet->GetFileName() );
59 m_labelSymName->SetLabel( aSheet->GetShownName( true ) );
60
61
62 for( wxDataViewCtrl* view : { m_viewAssociated, m_viewSheetLabels, m_viewSheetPins } )
63 {
65 {
66 switch( col )
67 {
69 view->AppendIconTextColumn( SHEET_SYNCHRONIZATION_MODEL::GetColName( col ), col );
70 break;
72 view->AppendTextColumn( SHEET_SYNCHRONIZATION_MODEL::GetColName( col ), col );
73 break;
74 }
75 }
76 }
77
78 std::map<int, wxDataViewCtrl*> view_idx = std::map<int, wxDataViewCtrl*>{
82 };
83
84 for( auto& [idx, view] : view_idx )
85 {
86 auto model = wxObjectDataPtr<SHEET_SYNCHRONIZATION_MODEL>(
88 view->AssociateModel( model.get() );
89 m_models.try_emplace( idx, std::move( model ) );
90 }
91
92 for( auto& [idx, view] : view_idx )
94}
95
96
98{
99 SHEET_SYNCHRONIZATION_ITEM_LIST labels_list, pins_list, associated_list;
100 auto labels_ori = m_sheet->GetScreen()->Items().OfType( SCH_HIER_LABEL_T );
101 std::vector<SCH_SHEET_PIN*> pins_ori = m_sheet->GetPins();
102
103 // De-duplicate the hierarchical labels list
104 std::set<wxString> dedup_labels_ori_text;
105 std::vector<SCH_HIERLABEL*> dedup_labels_ori;
106
107 for( const auto& item : labels_ori )
108 {
109 SCH_HIERLABEL* label = static_cast<SCH_HIERLABEL*>( item );
110
111 if( dedup_labels_ori_text.count( label->GetText() ) == 0 )
112 {
113 dedup_labels_ori_text.insert( label->GetText() );
114 dedup_labels_ori.push_back( label );
115 }
116 }
117
118 auto check_matched = [&]( SCH_HIERLABEL* label )
119 {
120 for( size_t i = 0; i < pins_ori.size(); i++ )
121 {
122 SCH_SHEET_PIN* cur_pin = pins_ori[i];
123
124 if( label->GetText() == cur_pin->GetText() && label->GetShape() == cur_pin->GetShape() )
125 {
126 associated_list.push_back(
127 std::make_shared<ASSOCIATED_SCH_LABEL_PIN>( label, cur_pin ) );
128 pins_ori.erase( pins_ori.begin() + i );
129 return;
130 }
131 }
132
133 labels_list.push_back(
134 std::make_shared<SCH_HIERLABEL_SYNCHRONIZATION_ITEM>( label, m_sheet ) );
135 };
136
137 for( const auto& item : dedup_labels_ori )
138 check_matched( static_cast<SCH_HIERLABEL*>( item ) );
139
140 for( const auto& pin : pins_ori )
141 pins_list.push_back( std::make_shared<SCH_SHEET_PIN_SYNCHRONIZATION_ITEM>(
142 static_cast<SCH_SHEET_PIN*>( pin ), m_sheet ) );
143
144 m_models[SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL]->UpdateItems( std::move( labels_list ) );
145 m_models[SHEET_SYNCHRONIZATION_MODEL::SHEET_PIN]->UpdateItems( std::move( pins_list ) );
146 m_models[SHEET_SYNCHRONIZATION_MODEL::ASSOCIATED]->UpdateItems( std::move( associated_list ) );
147
149}
150
151
153{
154 return m_models.at( aKind );
155}
156
157
159{
160 return m_sheetFileName;
161}
162
163
165{
166}
167
168
170{
171 return !m_models.at( SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL )->GetCount()
173}
174
175
177{
178 WXUNUSED( aEvent )
179
180 if( auto idx = m_models[SHEET_SYNCHRONIZATION_MODEL::SHEET_PIN]->GetSelectedIndex();
181 idx.has_value() )
182 {
184 m_models[SHEET_SYNCHRONIZATION_MODEL::SHEET_PIN]->GetSynchronizationItem(
185 *idx ) )
186 {
188 static_cast<SCH_SHEET_PIN*>( item->GetItem() ) );
189 }
190 }
191}
192
193
195{
196 WXUNUSED( aEvent )
197
198 if( auto idx = m_models[SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL]->GetSelectedIndex();
199 idx.has_value() )
200 {
202 m_models[SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL]->GetSynchronizationItem(
203 *idx ) )
204 {
206 static_cast<SCH_HIERLABEL*>( item->GetItem() ) );
207 }
208 }
209}
210
211
213{
214 wxDataViewItem labelIdx = m_viewSheetLabels->GetSelection();
215 wxDataViewItem pinIdx = m_viewSheetPins->GetSelection();
216
217 for( auto& idx : { labelIdx, pinIdx } )
218 {
219 if( !idx.IsOk() )
220 return;
221 }
222
227
228 for( const auto& item : { labelItem, pinItem } )
229 {
230 if( !item )
231 return;
232 }
233
234 auto label_ptr =
235 std::static_pointer_cast<SCH_HIERLABEL_SYNCHRONIZATION_ITEM>( labelItem )->GetLabel();
236 auto pin_ptr =
237 std::static_pointer_cast<SCH_SHEET_PIN_SYNCHRONIZATION_ITEM>( pinItem )->GetPin();
238
239 switch( direction )
240 {
243 *pinItem,
244 [&]()
245 {
246 pin_ptr->SetText( label_ptr->GetText() );
247 pin_ptr->SetShape( label_ptr->GetShape() );
248 },
249 m_path );
250 break;
253 *labelItem,
254 [&]()
255 {
256 label_ptr->SetText( pin_ptr->GetText() );
257 label_ptr->SetShape( pin_ptr->GetShape() );
258 },
259 m_path );
260 break;
261 }
262
264 std::make_shared<ASSOCIATED_SCH_LABEL_PIN>( label_ptr, pin_ptr ) );
266
267 for( auto idx :
269 {
270 PostProcessModelSelection( idx, {} );
271 }
272
274}
275
276
278{
282}
283
284
286{
287 WXUNUSED( aEvent )
289}
290
291
293{
294 WXUNUSED( aEvent )
296}
297
298
299void PANEL_SYNC_SHEET_PINS::OnBtnRmPinsClicked( wxCommandEvent& aEvent )
300{
301 WXUNUSED( aEvent )
302 wxDataViewItemArray array;
303 m_viewSheetPins->GetSelections( array );
307}
308
309
311{
312 WXUNUSED( aEvent )
313 wxDataViewItemArray array;
314 m_viewSheetLabels->GetSelections( array );
318}
319
320
321void PANEL_SYNC_SHEET_PINS::OnBtnUndoClicked( wxCommandEvent& aEvent )
322{
323 WXUNUSED( aEvent )
324 wxDataViewItemArray indexes;
325 m_viewAssociated->GetSelections( indexes );
326 auto items = m_models[SHEET_SYNCHRONIZATION_MODEL::ASSOCIATED]->TakeItems( indexes );
327
328 if( !items.size() )
329 return;
330
331 for( auto& item : items )
332 {
333 auto associated = std::static_pointer_cast<ASSOCIATED_SCH_LABEL_PIN>( item );
335 std::make_shared<SCH_HIERLABEL_SYNCHRONIZATION_ITEM>( associated->GetLabel(),
336 m_sheet ) );
338 std::make_shared<SCH_SHEET_PIN_SYNCHRONIZATION_ITEM>( associated->GetPin(),
339 m_sheet ) );
340 }
341
344}
345
346
347void PANEL_SYNC_SHEET_PINS::PostProcessModelSelection( int aIdex, wxDataViewItem const& aItem )
348{
349 if( aItem.IsOk() )
350 m_models[aIdex]->OnRowSelected( m_models[aIdex]->GetRow( aItem ) );
351 else
352 m_models[aIdex]->OnRowSelected( {} );
353
354 switch( aIdex )
355 {
357 {
358 for( auto btn : { m_btnAddLabels, m_btnRmPins } )
359 btn->Enable( m_models[SHEET_SYNCHRONIZATION_MODEL::SHEET_PIN]->HasSelectedIndex() );
360
361 break;
362 }
364 {
365 for( auto btn : { m_btnAddSheetPins, m_btnRmLabels } )
366 btn->Enable( m_models[SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL]->HasSelectedIndex() );
367
368 break;
369 }
371 {
372 m_btnUndo->Enable( m_models[SHEET_SYNCHRONIZATION_MODEL::ASSOCIATED]->HasSelectedIndex() );
373 break;
374 }
375 default:
376 break;
377 }
378
380 {
382 {
383 btn->Enable( m_models[SHEET_SYNCHRONIZATION_MODEL::SHEET_PIN]->HasSelectedIndex()
384 && m_models[SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL]->HasSelectedIndex() );
385 }
386 }
387}
388
389
391{
393}
394
395
397{
399}
400
401
403{
405}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:94
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:238
Class PANEL_SYNC_SHEET_PINS_BASE.
SHEET_SYNCHRONIZATION_MODEL_PTR GetModel(int aKind) const
void PostProcessModelSelection(int aIdex, wxDataViewItem const &aItem)
void OnBtnUsePinAsTemplateClicked(wxCommandEvent &event) override
void OnBtnRmPinsClicked(wxCommandEvent &event) override
void GenericSync(SYNC_DIRECTION direction)
void OnBtnAddLabelsClicked(wxCommandEvent &event) override
void OnViewSheetLabelCellClicked(wxDataViewEvent &event) override
void OnViewMatchedCellClicked(wxDataViewEvent &event) override
const wxString & GetSheetFileName() const
void OnBtnUseLabelAsTemplateClicked(wxCommandEvent &event) override
void OnBtnRmLabelsClicked(wxCommandEvent &event) override
void OnBtnAddSheetPinsClicked(wxCommandEvent &event) override
void OnBtnUndoClicked(wxCommandEvent &event) override
SHEET_SYNCHRONIZATION_AGENT & m_agent
PANEL_SYNC_SHEET_PINS(wxWindow *aParent, SCH_SHEET *aSheet, wxNotebook *aNoteBook, int aIndex, SHEET_SYNCHRONIZATION_AGENT &aAgent, const SCH_SHEET_PATH &aPath)
void OnViewSheetPinCellClicked(wxDataViewEvent &event) override
SYNC_SHEET_PINT_MODELS m_models
LABEL_FLAG_SHAPE GetShape() const
Definition: sch_label.h:177
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:108
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet_pin.h:66
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:57
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition: sch_sheet.h:306
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:110
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition: sch_sheet.h:181
wxString GetShownName(bool aAllowExtraText) const
Definition: sch_sheet.h:103
Agent for all the modifications while syncing the sheet pin and hierarchical label.
void PlaceHieraLable(SCH_SHEET *aSheet, SCH_SHEET_PATH const &aPath, SCH_SHEET_PIN *aPin)
void ModifyItem(SHEET_SYNCHRONIZATION_ITEM &aItem, std::function< void()> const &aDoModify, const SCH_SHEET_PATH &aPath)
void PlaceSheetPin(SCH_SHEET *aSheet, SCH_SHEET_PATH const &aPath, SCH_HIERLABEL *aLabel)
STL namespace.
wxObjectDataPtr< SHEET_SYNCHRONIZATION_MODEL > SHEET_SYNCHRONIZATION_MODEL_PTR
std::vector< SHEET_SYNCHRONIZATION_ITE_PTR > SHEET_SYNCHRONIZATION_ITEM_LIST
std::shared_ptr< SHEET_SYNCHRONIZATION_ITEM > SHEET_SYNCHRONIZATION_ITE_PTR
@ SCH_HIER_LABEL_T
Definition: typeinfo.h:169