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, SCH_SHEET* aInitialSheet ) :
46 DIALOG_SYNC_SHEET_PINS_BASE( aParent ), m_agent( std::move( aAgent ) ),
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 int initialSelection = -1;
61 int firstUnsyncedPage = -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 bool hasUndefined = page->HasUndefinedSheetPing();
71 m_notebook->AddPage( page, sheet->GetShownName( true ), {}, hasUndefined );
72 page->UpdateForms();
73
74 if( aInitialSheet && sheet == aInitialSheet )
75 initialSelection = count;
76
77 if( firstUnsyncedPage < 0 && hasUndefined )
78 firstUnsyncedPage = count;
79
80 if( sheet_instances.find( fileName ) == sheet_instances.end() )
81 {
82 sheet_instances.try_emplace( fileName, std::list<PANEL_SYNC_SHEET_PINS*>{ page } );
83 }
84 else
85 {
86 sheet_instances[fileName].push_back( page );
87 }
88
89 m_panels.try_emplace( sheet, page );
90 }
91
92 for( auto& [sheet_name, panel_list] : sheet_instances )
93 {
94 if( panel_list.size() > 1 )
95 {
96 std::list<std::shared_ptr<SHEET_SYNCHRONIZATION_NOTIFIER>> sheet_change_notifiers;
97 std::list<SHEET_SYNCHRONIZATION_MODEL*> sheet_sync_models;
98
99 for( auto& panel : panel_list )
100 {
102 panel->GetModel( SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL ).get();
103 sheet_sync_models.push_back( model );
104 sheet_change_notifiers.push_back(
105 std::make_shared<SHEET_FILE_CHANGE_NOTIFIER>( model, panel ) );
106 }
107
108 for( auto& notifier : sheet_change_notifiers )
109 {
110 for( auto& other : sheet_sync_models )
111 {
112 if( notifier->GetOwner() != other )
113 {
114 other->AddNotifier( notifier );
115 }
116 }
117 }
118 }
119 }
120
121 // Select initial page: prefer the explicitly requested sheet, fall back to the first page
122 // with unsynced pins, or stay on the first page if all are synced
123 if( initialSelection >= 0 )
124 m_notebook->SetSelection( initialSelection );
125 else if( firstUnsyncedPage >= 0 )
126 m_notebook->SetSelection( firstUnsyncedPage );
127
128 Bind( wxEVT_CLOSE_WINDOW, &DIALOG_SYNC_SHEET_PINS::OnClose, this );
129
130 // Now all widgets have the size fixed, call FinishDialogSettings
132}
133
134
136
137
138void DIALOG_SYNC_SHEET_PINS::OnClose( wxCloseEvent& aEvent )
139{
140 aEvent.Skip();
141}
142
143
145{
146 if( !aNewItem )
147 return;
148
149 auto post_end_place_item =
150 std::shared_ptr<std::nullptr_t>( nullptr,
151 [&]( std::nullptr_t )
152 {
154
155 if( m_placementTemplateSet.empty() )
156 {
157 EndPlacement();
158 }
159 else
160 {
162 }
163 } );
164
165
166 if( m_lastEditSheet && m_panels.find( m_lastEditSheet ) != m_panels.end() )
167 {
168 auto& panel = m_panels[m_lastEditSheet];
169 auto template_item = static_cast<SCH_HIERLABEL*>( m_currentTemplate );
170 auto new_item = static_cast<SCH_HIERLABEL*>( aNewItem );
171
172 //Usr may edit the name or shape while placing the new item , do sync if either differs
173 if( template_item->GetText() != new_item->GetText()
174 || template_item->GetShape() != new_item->GetShape() )
175 {
176 m_agent->ModifyItem(
177 template_item,
178 [&]()
179 {
180 template_item->SetText( new_item->GetText() );
181 template_item->SetShape( new_item->GetShape() );
182 },
183 panel->GetSheetPath(),
187 }
188
189 panel->UpdateForms();
190
192 panel->GetModel( SHEET_SYNCHRONIZATION_MODEL::HIRE_LABEL )->DoNotify();
193 }
194}
195
196
198 std::set<EDA_ITEM*> const& aPlacementTemplateSet )
199{
200 if( aPlacementTemplateSet.empty() )
201 return;
202
203 m_lastEditSheet = aSheet;
204 m_placeItemKind = aKind;
205 m_placementTemplateSet = aPlacementTemplateSet;
207}
208
209
211{
212 if( !m_currentTemplate )
213 return {};
214
215 return static_cast<SCH_HIERLABEL*>( m_currentTemplate );
216}
217
218
220{
221 return !m_placementTemplateSet.empty();
222}
223
224
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...
DIALOG_SYNC_SHEET_PINS_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Synchronize sheet pins and hierarchical labels"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
DIALOG_SYNC_SHEET_PINS(wxWindow *aParent, std::list< SCH_SHEET_PATH > aSheetPath, std::shared_ptr< SHEET_SYNCHRONIZATION_AGENT > aAgent, SCH_SHEET *aInitialSheet=nullptr)
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
The same sheet may have multiple instances.
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
It's the agent that performs modification and placement.
void OnClose(wxCloseEvent &aEvent)
void PreparePlacementTemplate(SCH_SHEET *aSheet, PlaceItemKind aKind, std::set< EDA_ITEM * > const &aPlacementTemplateSet)
Either the selected HIERLABEL or SHEET_PIN 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
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
static const std::map< BOOKCTRL_ICON_INDEX, BITMAPS > & GetBookctrlPageIcon()
STL namespace.
KIBIS_MODEL * model