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 auto check_matched = [&]( SCH_HIERLABEL* label )
104 {
105 for( size_t i = 0; i < pins_ori.size(); i++ )
106 {
107 SCH_SHEET_PIN* cur_pin = pins_ori[i];
108
109 if( label->GetText() == cur_pin->GetText() && label->GetShape() == cur_pin->GetShape() )
110 {
111 associated_list.push_back(
112 std::make_shared<ASSOCIATED_SCH_LABEL_PIN>( label, cur_pin ) );
113 pins_ori.erase( pins_ori.begin() + i );
114 return;
115 }
116 }
117
118 labels_list.push_back(
119 std::make_shared<SCH_HIERLABEL_SYNCHRONIZATION_ITEM>( label, m_sheet ) );
120 };
121
122 for( const auto& item : labels_ori )
123 check_matched( static_cast<SCH_HIERLABEL*>( item ) );
124
125 for( const auto& pin : pins_ori )
126 pins_list.push_back( std::make_shared<SCH_SHEET_PIN_SYNCHRONIZATION_ITEM>(
127 static_cast<SCH_SHEET_PIN*>( pin ), m_sheet ) );
128
129 m_models[SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL]->UpdateItems( std::move( labels_list ) );
130 m_models[SHEET_SYNCHRONIZATION_MODEL::SHEET_PIN]->UpdateItems( std::move( pins_list ) );
131 m_models[SHEET_SYNCHRONIZATION_MODEL::ASSOCIATED]->UpdateItems( std::move( associated_list ) );
132
134}
135
136
138{
139 return m_models.at( aKind );
140}
141
142
144{
145 return m_sheetFileName;
146}
147
148
150{
151}
152
153
155{
156 return !m_models.at( SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL )->GetCount()
158}
159
160
162{
163 WXUNUSED( aEvent )
164
165 if( auto idx = m_models[SHEET_SYNCHRONIZATION_MODEL::SHEET_PIN]->GetSelectedIndex();
166 idx.has_value() )
167 {
169 m_models[SHEET_SYNCHRONIZATION_MODEL::SHEET_PIN]->GetSynchronizationItem(
170 *idx ) )
171 {
173 static_cast<SCH_SHEET_PIN*>( item->GetItem() ) );
174 }
175 }
176}
177
178
180{
181 WXUNUSED( aEvent )
182
183 if( auto idx = m_models[SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL]->GetSelectedIndex();
184 idx.has_value() )
185 {
187 m_models[SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL]->GetSynchronizationItem(
188 *idx ) )
189 {
191 static_cast<SCH_HIERLABEL*>( item->GetItem() ) );
192 }
193 }
194}
195
196
198{
199 wxDataViewItem labelIdx = m_viewSheetLabels->GetSelection();
200 wxDataViewItem pinIdx = m_viewSheetPins->GetSelection();
201
202 for( auto& idx : { labelIdx, pinIdx } )
203 {
204 if( !idx.IsOk() )
205 return;
206 }
207
212
213 for( const auto& item : { labelItem, pinItem } )
214 {
215 if( !item )
216 return;
217 }
218
219 auto label_ptr =
220 std::static_pointer_cast<SCH_HIERLABEL_SYNCHRONIZATION_ITEM>( labelItem )->GetLabel();
221 auto pin_ptr =
222 std::static_pointer_cast<SCH_SHEET_PIN_SYNCHRONIZATION_ITEM>( pinItem )->GetPin();
223
224 switch( direction )
225 {
228 *pinItem,
229 [&]()
230 {
231 pin_ptr->SetText( label_ptr->GetText() );
232 pin_ptr->SetShape( label_ptr->GetShape() );
233 },
234 m_path );
235 break;
238 *labelItem,
239 [&]()
240 {
241 label_ptr->SetText( pin_ptr->GetText() );
242 label_ptr->SetShape( pin_ptr->GetShape() );
243 },
244 m_path );
245 break;
246 }
247
249 std::make_shared<ASSOCIATED_SCH_LABEL_PIN>( label_ptr, pin_ptr ) );
251
252 for( auto idx :
254 {
255 PostProcessModelSelection( idx, {} );
256 }
257
259}
260
261
263{
267}
268
269
271{
272 WXUNUSED( aEvent )
274}
275
276
278{
279 WXUNUSED( aEvent )
281}
282
283
284void PANEL_SYNC_SHEET_PINS::OnBtnRmPinsClicked( wxCommandEvent& aEvent )
285{
286 WXUNUSED( aEvent )
287 wxDataViewItemArray array;
288 m_viewSheetPins->GetSelections( array );
292}
293
294
296{
297 WXUNUSED( aEvent )
298 wxDataViewItemArray array;
299 m_viewSheetLabels->GetSelections( array );
303}
304
305
306void PANEL_SYNC_SHEET_PINS::OnBtnUndoClicked( wxCommandEvent& aEvent )
307{
308 WXUNUSED( aEvent )
309 wxDataViewItemArray indexes;
310 m_viewAssociated->GetSelections( indexes );
311 auto items = m_models[SHEET_SYNCHRONIZATION_MODEL::ASSOCIATED]->TakeItems( indexes );
312
313 if( !items.size() )
314 return;
315
316 for( auto& item : items )
317 {
318 auto associated = std::static_pointer_cast<ASSOCIATED_SCH_LABEL_PIN>( item );
320 std::make_shared<SCH_HIERLABEL_SYNCHRONIZATION_ITEM>( associated->GetLabel(),
321 m_sheet ) );
323 std::make_shared<SCH_SHEET_PIN_SYNCHRONIZATION_ITEM>( associated->GetPin(),
324 m_sheet ) );
325 }
326
329}
330
331
332void PANEL_SYNC_SHEET_PINS::PostProcessModelSelection( int aIdex, wxDataViewItem const& aItem )
333{
334 if( aItem.IsOk() )
335 m_models[aIdex]->OnRowSelected( m_models[aIdex]->GetRow( aItem ) );
336 else
337 m_models[aIdex]->OnRowSelected( {} );
338
339 switch( aIdex )
340 {
342 {
343 for( auto btn : { m_btnAddLabels, m_btnRmPins } )
344 btn->Enable( m_models[SHEET_SYNCHRONIZATION_MODEL::SHEET_PIN]->HasSelectedIndex() );
345
346 break;
347 }
349 {
350 for( auto btn : { m_btnAddSheetPins, m_btnRmLabels } )
351 btn->Enable( m_models[SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL]->HasSelectedIndex() );
352
353 break;
354 }
356 {
357 m_btnUndo->Enable( m_models[SHEET_SYNCHRONIZATION_MODEL::ASSOCIATED]->HasSelectedIndex() );
358 break;
359 }
360 default:
361 break;
362 }
363
365 {
367 {
368 btn->Enable( m_models[SHEET_SYNCHRONIZATION_MODEL::SHEET_PIN]->HasSelectedIndex()
369 && m_models[SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL]->HasSelectedIndex() );
370 }
371 }
372}
373
374
376{
378}
379
380
382{
384}
385
386
388{
390}
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:98
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:172
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:109
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