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 The 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 wxWindow* aParent, std::list<SCH_SHEET_PATH> aSheetPath,
45 std::shared_ptr<SHEET_SYNCHRONIZATION_AGENT> aAgent ) :
46 DIALOG_SYNC_SHEET_PINS_BASE( aParent ), m_agent( std::move( aAgent ) ),
47 m_lastEditSheet( nullptr ), m_placeItemKind( PlaceItemKind::UNDEFINED ),
48 m_currentTemplate( nullptr )
49{
50 wxImageList* imageList = new wxImageList( SYNC_SHEET_PIN_PREFERENCE::NORMAL_WIDTH,
52
53 for( const auto& [icon_idx, bitmap] : SYNC_SHEET_PIN_PREFERENCE::GetBookctrlPageIcon() )
54 {
55 imageList->Add( KiBitmap( bitmap, SYNC_SHEET_PIN_PREFERENCE::NORMAL_HEIGHT ) );
56 }
57
58 m_notebook->AssignImageList( imageList );
59 int count = -1;
60 std::unordered_map<wxString, std::list<PANEL_SYNC_SHEET_PINS*>> sheet_instances;
61
62 for( const auto& sheet_path : aSheetPath )
63 {
64 auto sheet = sheet_path.Last();
65 wxString fileName = sheet->GetFileName();
67 ++count, *m_agent, sheet_path );
68 m_notebook->AddPage( page, sheet->GetShownName( true ), {}, page->HasUndefinedSheetPing() );
69 page->UpdateForms();
70
71 if( sheet_instances.find( fileName ) == sheet_instances.end() )
72 {
73 sheet_instances.try_emplace( fileName, std::list<PANEL_SYNC_SHEET_PINS*>{ page } );
74 }
75 else
76 {
77 sheet_instances[fileName].push_back( page );
78 }
79
80 m_panels.try_emplace( sheet, page );
81 }
82
83 for( auto& [sheet_name, panel_list] : sheet_instances )
84 {
85 if( panel_list.size() > 1 )
86 {
87 std::list<std::shared_ptr<SHEET_SYNCHRONIZATION_NOTIFIER>> sheet_change_notifiers;
88 std::list<SHEET_SYNCHRONIZATION_MODEL*> sheet_sync_models;
89
90 for( auto& panel : panel_list )
91 {
93 panel->GetModel( SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL ).get();
94 sheet_sync_models.push_back( model );
95 sheet_change_notifiers.push_back(
96 std::make_shared<SHEET_FILE_CHANGE_NOTIFIER>( model, panel ) );
97 }
98
99 for( auto& notifier : sheet_change_notifiers )
100 {
101 for( auto& other : sheet_sync_models )
102 {
103 if( notifier->GetOwner() != other )
104 {
105 other->AddNotifier( notifier );
106 }
107 }
108 }
109 }
110 }
111
112 Bind( wxEVT_CLOSE_WINDOW, &DIALOG_SYNC_SHEET_PINS::OnClose, this );
113
114 // Now all widgets have the size fixed, call FinishDialogSettings
116}
117
118
120
121
122void DIALOG_SYNC_SHEET_PINS::OnCloseBtnClick( wxCommandEvent& event )
123{
124 Close();
125}
126
127
128void DIALOG_SYNC_SHEET_PINS::OnClose( wxCloseEvent& aEvent )
129{
130 aEvent.Skip();
131}
132
133
135{
136 if( !aNewItem )
137 return;
138
139 auto post_end_place_item =
140 std::shared_ptr<std::nullptr_t>( nullptr,
141 [&]( std::nullptr_t )
142 {
144
145 if( m_placementTemplateSet.empty() )
146 {
147 EndPlacement();
148 }
149 else
150 {
151 m_currentTemplate =
152 *m_placementTemplateSet.begin();
153 }
154 } );
155
156
157 if( m_lastEditSheet && m_panels.find( m_lastEditSheet ) != m_panels.end() )
158 {
159 auto& panel = m_panels[m_lastEditSheet];
160 auto template_item = static_cast<SCH_HIERLABEL*>( m_currentTemplate );
161 auto new_item = static_cast<SCH_HIERLABEL*>( aNewItem );
162
163 //Usr may edit the name or shape while placing the new item , do sync if either differs
164 if( template_item->GetText() != new_item->GetText()
165 || template_item->GetShape() != new_item->GetShape() )
166 {
167 m_agent->ModifyItem(
168 template_item,
169 [&]()
170 {
171 template_item->SetText( new_item->GetText() );
172 template_item->SetShape( new_item->GetShape() );
173 },
174 panel->GetSheetPath(),
176 ? SHEET_SYNCHRONIZATION_ITEM_KIND::HIERLABEL
177 : SHEET_SYNCHRONIZATION_ITEM_KIND::SHEET_PIN );
178 }
179
180 panel->UpdateForms();
181
183 panel->GetModel( SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL )->DoNotify();
184 }
185}
187 SCH_SHEET* aSheet, PlaceItemKind aKind, std::set<EDA_ITEM*> const& aPlacementTemplateSet )
188{
189 if( aPlacementTemplateSet.empty() )
190 return;
191
192 m_lastEditSheet = aSheet;
193 m_placeItemKind = aKind;
194 m_placementTemplateSet = aPlacementTemplateSet;
196}
197
199{
200 if( !m_currentTemplate )
201 return {};
202
203 return static_cast<SCH_HIERLABEL*>( m_currentTemplate );
204}
205
207{
208 return !m_placementTemplateSet.empty();
209}
210
212{
215 m_currentTemplate = nullptr;
216}
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
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
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.
std::set< EDA_ITEM * > m_placementTemplateSet
std::unordered_map< SCH_SHEET *, PANEL_SYNC_SHEET_PINS * > m_panels
void EndPlaceItem(EDA_ITEM *aNewItem)
End place a new HIERLABEL/SHEET_PIN , and add the new item to the corresponding table.
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)
void PreparePlacementTemplate(SCH_SHEET *aSheet, PlaceItemKind aKind, std::set< EDA_ITEM * > const &aPlacementTemplateSet)
Either selected HIERLABELs or SHEET_PINs will be used as templates for placing the new ones.
bool CanPlaceMore() const
Check if there are more items to be placed.
~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:89
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.