KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_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
27#include "sch_item.h"
28#include "sch_label.h"
34
35#include <memory>
36#include <cstddef>
37#include <sch_sheet_pin.h>
38#include <sch_sheet.h>
39#include <unordered_map>
40#include <sch_drawing_tools.h>
41
42
44 std::list<SCH_SHEET_PATH> aSheetPath,
45 std::shared_ptr<SHEET_SYNCHRONIZATION_AGENT> aAgent ) :
47 m_agent( std::move( aAgent ) ),
48 m_lastEditSheet( nullptr ),
49 m_placeItemKind( PlaceItemKind::UNDEFINED ),
50 m_placementTemplate( nullptr )
51{
52 wxImageList* imageList = new wxImageList( SYNC_SHEET_PIN_PREFERENCE::NORMAL_WIDTH,
54
55 for( const auto& [icon_idx, bitmap] : SYNC_SHEET_PIN_PREFERENCE::GetBookctrlPageIcon() )
56 {
57 imageList->Add( KiBitmap( bitmap, SYNC_SHEET_PIN_PREFERENCE::NORMAL_HEIGHT ) );
58 }
59
60 m_notebook->AssignImageList( imageList );
61 int count = -1;
62 std::unordered_map<wxString, std::list<PANEL_SYNC_SHEET_PINS*>> sheet_instances;
63
64 for( const auto& sheet_path : aSheetPath )
65 {
66 auto sheet = sheet_path.Last();
67 wxString fileName = sheet->GetFileName();
69 ++count, *m_agent, sheet_path );
70 m_notebook->AddPage( page, sheet->GetShownName( true ), {}, page->HasUndefinedSheetPing() );
71 page->UpdateForms();
72
73 if( sheet_instances.find( fileName ) == sheet_instances.end() )
74 {
75 sheet_instances.try_emplace( fileName, std::list<PANEL_SYNC_SHEET_PINS*>{ page } );
76 }
77 else
78 {
79 sheet_instances[fileName].push_back( page );
80 }
81
82 m_panels.try_emplace( sheet, page );
83 }
84
85 for( auto& [sheet_name, panel_list] : sheet_instances )
86 {
87 if( panel_list.size() > 1 )
88 {
89 std::list<std::shared_ptr<SHEET_SYNCHRONIZATION_NOTIFIER>> sheet_change_notifiers;
90 std::list<SHEET_SYNCHRONIZATION_MODEL*> sheet_sync_models;
91
92 for( auto& panel : panel_list )
93 {
95 panel->GetModel( SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL ).get();
96 sheet_sync_models.push_back( model );
97 sheet_change_notifiers.push_back(
98 std::make_shared<SHEET_FILE_CHANGE_NOTIFIER>( model, panel ) );
99 }
100
101 for( auto& notifier : sheet_change_notifiers )
102 {
103 for( auto& other : sheet_sync_models )
104 {
105 if( notifier->GetOwner() != other )
106 {
107 other->AddNotifier( notifier );
108 }
109 }
110 }
111 }
112 }
113
114 m_sizerMain->Layout();
115 m_sizerMain->Fit( this );
116 Bind( wxEVT_CLOSE_WINDOW, &DIALOG_SYNC_SHEET_PINS::OnClose, this );
117}
118
119
121
122
123void DIALOG_SYNC_SHEET_PINS::OnCloseBtnClick( wxCommandEvent& event )
124{
125 Close();
126}
127
128
129void DIALOG_SYNC_SHEET_PINS::OnClose( wxCloseEvent& aEvent )
130{
131 aEvent.Skip();
132}
133
134
136{
137 auto post_end_place_item = std::shared_ptr<std::nullptr_t>( nullptr,
138 [&]( std::nullptr_t )
139 {
142 m_placementTemplate = nullptr;
143 } );
144
145 if( !aNewItem )
146 return;
147
148 if( m_lastEditSheet && m_panels.find( m_lastEditSheet ) != m_panels.end() )
149 {
150 auto& panel = m_panels[m_lastEditSheet];
151 auto template_item = static_cast<SCH_HIERLABEL*>( m_placementTemplate );
152 auto new_item = static_cast<SCH_HIERLABEL*>( aNewItem );
153
154 //Usr may edit the name or shape while placing the new item , do sync if either differs
155 if( template_item->GetText() != new_item->GetText()
156 || template_item->GetShape() != new_item->GetShape() )
157 {
158 m_agent->ModifyItem(
159 template_item,
160 [&]()
161 {
162 template_item->SetText( new_item->GetText() );
163 template_item->SetShape( new_item->GetShape() );
164 },
165 panel->GetSheetPath(),
167 ? SHEET_SYNCHRONIZATION_ITEM_KIND::HIERLABEL
168 : SHEET_SYNCHRONIZATION_ITEM_KIND::SHEET_PIN );
169 }
170
171 panel->UpdateForms();
172
174 panel->GetModel( SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL )->DoNotify();
175 }
176}
177
178
180 EDA_ITEM* aTemplate )
181{
182 m_lastEditSheet = aSheet;
183 m_placeItemKind = aKind;
184 m_placementTemplate = aTemplate;
185}
186
187
189{
191 return {};
192
193 return static_cast<SCH_HIERLABEL*>( m_placementTemplate );
194}
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:104
Class DIALOG_SYNC_SHEET_PINS_BASE.
SCH_HIERLABEL * GetPlacementTemplate() const
Get the Placement Template SHEET_PIN / HIERLABEL used for place a new HIERLABEL/SHEET_PIN.
void BeginPlaceItem(SCH_SHEET *aSheet, PlaceItemKind aKind, EDA_ITEM *aTemplate)
Start place a new SHEET_PIN/HIERLABEL.
std::unordered_map< SCH_SHEET *, PANEL_SYNC_SHEET_PINS * > m_panels
void EndPlaceItem(EDA_ITEM *aNewItem)
std::shared_ptr< SHEET_SYNCHRONIZATION_AGENT > m_agent
DIALOG_SYNC_SHEET_PINS(wxWindow *aParent, std::list< SCH_SHEET_PATH > aSheetPath, std::shared_ptr< SHEET_SYNCHRONIZATION_AGENT > aAgent)
void OnClose(wxCloseEvent &aEvent)
~DIALOG_SYNC_SHEET_PINS() override
void OnCloseBtnClick(wxCommandEvent &event) override
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:88
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:57
void AddNotifier(std::shared_ptr< SHEET_SYNCHRONIZATION_NOTIFIER > aNotifier)
static const std::map< BOOKCTRL_ICON_INDEX, BITMAPS > & GetBookctrlPageIcon()
STL namespace.