KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_migrate_buses.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) 2018 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Jon Evans <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <sch_connection.h>
23#include <schematic.h>
24#include <connection_graph.h>
25#include <tool/tool_manager.h>
26#include <tool/actions.h>
28#include <sch_label.h>
29#include <view/view_controls.h>
30
58
59
60bool DIALOG_MIGRATE_BUSES::ShouldPrompt( int aFileFormatVersionAtLoad, size_t aBusesNeedingMigration )
61{
62 // Legacy .sch files report format version 0 and no s-expression schematic predates 20200310,
63 // so anything at or above that boundary already went through the one-time bus migration.
64 return aBusesNeedingMigration > 0 && aFileFormatVersionAtLoad < 20200310;
65}
66
67
69 : DIALOG_MIGRATE_BUSES_BASE( aParent ), m_frame( aParent ), m_selected_index( 0 )
70{
71 OptOut( this ); // No control state save/restore
72
73 m_migration_list->Bind( wxEVT_LIST_ITEM_SELECTED, &DIALOG_MIGRATE_BUSES::onItemSelected, this );
74 m_btn_accept->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &DIALOG_MIGRATE_BUSES::onAcceptClicked, this );
75
77 updateUi();
78
80}
81
82
84{
85 m_migration_list->Unbind( wxEVT_LIST_ITEM_SELECTED, &DIALOG_MIGRATE_BUSES::onItemSelected, this );
86 m_btn_accept->Unbind( wxEVT_COMMAND_BUTTON_CLICKED, &DIALOG_MIGRATE_BUSES::onAcceptClicked, this );
87}
88
89
91{
92 m_items.clear();
93 auto subgraphs = m_frame->Schematic().ConnectionGraph()->GetBusesNeedingMigration();
94
95 for( const CONNECTION_SUBGRAPH* subgraph : subgraphs )
96 {
98
99 status.subgraph = subgraph;
100 status.approved = false;
101
102 std::vector<SCH_ITEM*> labels = subgraph->GetVectorBusLabels();
103 wxASSERT( labels.size() > 1 );
104
105 for( SCH_ITEM* label : labels )
106 status.labels.push_back( static_cast<SCH_LABEL_BASE*>( label )->GetText() );
107
108 status.possible_labels = getProposedLabels( status.labels );
109 m_items.push_back( status );
110 }
111}
112
113
115{
116 m_migration_list->DeleteAllItems();
117
118 m_migration_list->InsertColumn( 0, _( "Sheet" ) );
119 m_migration_list->InsertColumn( 1, _( "Conflicting Labels" ) );
120 m_migration_list->InsertColumn( 2, _( "New Label" ) );
121 m_migration_list->InsertColumn( 3, _( "Status" ) );
122
123 for( auto& item : m_items )
124 {
125 wxString old = item.labels[0];
126
127 for( unsigned j = 1; j < item.labels.size(); j++ )
128 old << ", " << item.labels[j];
129
130 auto i = m_migration_list->InsertItem( m_migration_list->GetItemCount(), wxEmptyString );
131
132 m_migration_list->SetItem( i, 0, item.subgraph->GetSheet().PathHumanReadable() );
133 m_migration_list->SetItem( i, 1, old );
134 m_migration_list->SetItem( i, 2, item.possible_labels[0] );
135 m_migration_list->SetItem( i, 3, "" );
136 }
137
138 m_migration_list->Select( 0 );
139 m_migration_list->SetColumnWidth( 1, -1 );
140}
141
142
143std::vector<wxString> DIALOG_MIGRATE_BUSES::getProposedLabels( const std::vector<wxString>& aLabelList )
144{
145 int lowest_start = INT_MAX;
146 int highest_end = -1;
147 int widest_bus = -1;
148
149 SCH_CONNECTION conn( m_frame->Schematic().ConnectionGraph() );
150
151 for( const wxString& label : aLabelList )
152 {
153 conn.ConfigureFromLabel( label );
154
155 int start = conn.VectorStart();
156 int end = conn.VectorEnd();
157
158 if( start < lowest_start )
159 lowest_start = start;
160
161 if( end > highest_end )
162 highest_end = end;
163
164 if( end - start + 1 > widest_bus )
165 widest_bus = end - start + 1;
166 }
167
168 std::vector<wxString> proposals;
169
170 for( const wxString& label : aLabelList )
171 {
172 conn.ConfigureFromLabel( label );
173 wxString proposal = conn.VectorPrefix();
174 proposal << "[" << highest_end << ".." << lowest_start << "]";
175 proposals.push_back( proposal );
176 }
177
178 return proposals;
179}
180
181
182void DIALOG_MIGRATE_BUSES::onItemSelected( wxListEvent& aEvent )
183{
184 unsigned sel = aEvent.GetIndex();
185 wxASSERT( sel < m_items.size() );
186
187 m_selected_index = sel;
188
189 const CONNECTION_SUBGRAPH* subgraph = m_items[sel].subgraph;
190 const SCH_SHEET_PATH& sheet = subgraph->GetSheet();
191 const SCH_ITEM* driver = subgraph->GetDriver();
192
193 if( sheet != m_frame->GetCurrentSheet() )
194 {
196 m_frame->Schematic().SetCurrentSheet( sheet );
197 m_frame->TestDanglingEnds();
198 }
199
200 VECTOR2I pos = driver->GetPosition();
201
202 m_frame->GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( pos, false );
203 m_frame->RedrawScreen( pos, false );
204
205 m_cb_new_name->Clear();
206
207 for( const wxString& option : m_items[sel].possible_labels )
208 m_cb_new_name->Append( option );
209
210 m_cb_new_name->Select( 0 );
211}
212
213
214void DIALOG_MIGRATE_BUSES::onAcceptClicked( wxCommandEvent& aEvent )
215{
216 wxASSERT( m_selected_index < m_items.size() );
217
218 unsigned sel = m_selected_index;
219
220 m_items[sel].approved_label = m_cb_new_name->GetStringSelection();
221 m_items[sel].approved = true;
222
223 std::vector<SCH_ITEM*> labels = m_items[sel].subgraph->GetVectorBusLabels();
224
225 for( SCH_ITEM* label : labels )
226 static_cast<SCH_LABEL_BASE*>( label )->SetText( m_items[sel].approved_label );
227
228 m_migration_list->SetItem( sel, 2, m_items[sel].approved_label );
229 m_migration_list->SetItem( sel, 3, _( "Updated" ) );
230
231 if( sel < m_items.size() - 1 )
232 m_migration_list->Select( sel + 1 );
233
234 m_frame->GetCanvas()->Refresh();
235}
static TOOL_ACTION zoomFitScreen
Definition actions.h:138
A subgraph is a set of items that are electrically connected on a single sheet.
const SCH_ITEM * GetDriver() const
const SCH_SHEET_PATH & GetSheet() const
DIALOG_MIGRATE_BUSES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Migrate Buses"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_DIALOG_STYLE)
void onAcceptClicked(wxCommandEvent &aEvent)
void onItemSelected(wxListEvent &aEvent)
std::vector< wxString > getProposedLabels(const std::vector< wxString > &aLabelList)
std::vector< BUS_MIGRATION_STATUS > m_items
static bool ShouldPrompt(int aFileFormatVersionAtLoad, size_t aBusesNeedingMigration)
Multiple differently-named labels on a single bus subgraph were only permitted before KiCad 6....
DIALOG_MIGRATE_BUSES(SCH_EDIT_FRAME *aParent)
void OptOut(wxWindow *aWindow)
Opt out of control state saving.
virtual VECTOR2I GetPosition() const
Definition eda_item.h:282
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
long VectorEnd() const
void ConfigureFromLabel(const wxString &aLabel)
Configures the connection given a label.
long VectorStart() const
wxString VectorPrefix() const
Schematic editor (Eeschema) main window.
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
void UpdateAllScreenReferences() const
Update all the symbol references for this sheet path.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
#define _(s)
std::vector< wxString > possible_labels
const CONNECTION_SUBGRAPH * subgraph
std::vector< wxString > labels
VECTOR2I end
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683