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 Bind( wxEVT_CLOSE_WINDOW, &DIALOG_SYNC_SHEET_PINS::OnClose, this );
115
116 // Now all widgets have the size fixed, call FinishDialogSettings
118}
119
120
122
123
124void DIALOG_SYNC_SHEET_PINS::OnCloseBtnClick( wxCommandEvent& event )
125{
126 Close();
127}
128
129
130void DIALOG_SYNC_SHEET_PINS::OnClose( wxCloseEvent& aEvent )
131{
132 aEvent.Skip();
133}
134
135
137{
138 auto post_end_place_item = std::shared_ptr<std::nullptr_t>( nullptr,
139 [&]( std::nullptr_t )
140 {
143 m_placementTemplate = nullptr;
144 } );
145
146 if( !aNewItem )
147 return;
148
149 if( m_lastEditSheet && m_panels.find( m_lastEditSheet ) != m_panels.end() )
150 {
151 auto& panel = m_panels[m_lastEditSheet];
152 auto template_item = static_cast<SCH_HIERLABEL*>( m_placementTemplate );
153 auto new_item = static_cast<SCH_HIERLABEL*>( aNewItem );
154
155 //Usr may edit the name or shape while placing the new item , do sync if either differs
156 if( template_item->GetText() != new_item->GetText()
157 || template_item->GetShape() != new_item->GetShape() )
158 {
159 m_agent->ModifyItem(
160 template_item,
161 [&]()
162 {
163 template_item->SetText( new_item->GetText() );
164 template_item->SetShape( new_item->GetShape() );
165 },
166 panel->GetSheetPath(),
168 ? SHEET_SYNCHRONIZATION_ITEM_KIND::HIERLABEL
169 : SHEET_SYNCHRONIZATION_ITEM_KIND::SHEET_PIN );
170 }
171
172 panel->UpdateForms();
173
175 panel->GetModel( SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL )->DoNotify();
176 }
177}
178
179
181 EDA_ITEM* aTemplate )
182{
183 m_lastEditSheet = aSheet;
184 m_placeItemKind = aKind;
185 m_placementTemplate = aTemplate;
186}
187
188
190{
192 return {};
193
194 return static_cast<SCH_HIERLABEL*>( m_placementTemplate );
195}
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.
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: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.