KiCad PCB EDA Suite
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  * @author Jon Evans <jon@craftyjon.com>
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 along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <sch_connection.h>
22 #include <schematic.h>
23 #include <connection_graph.h>
24 #include <tool/tool_manager.h>
25 #include <tool/actions.h>
26 #include <dialog_migrate_buses.h>
27 
58  : DIALOG_MIGRATE_BUSES_BASE( aParent ), m_frame( aParent ), m_selected_index( 0 )
59 {
60  m_migration_list->Bind( wxEVT_LIST_ITEM_SELECTED,
62 
63  m_btn_accept->Bind( wxEVT_COMMAND_BUTTON_CLICKED,
65 
66  loadGraphData();
67  updateUi();
68 
69  aParent->GetToolManager()->RunAction( ACTIONS::zoomFitScreen, true );
70 }
71 
72 
74 {
75  m_items.clear();
77 
78  for( auto subgraph : subgraphs )
79  {
80  BUS_MIGRATION_STATUS status;
81 
82  status.subgraph = subgraph;
83  status.approved = false;
84 
85  auto labels = subgraph->GetBusLabels();
86  wxASSERT( labels.size() > 1 );
87 
88  for( auto label : labels )
89  status.labels.push_back( static_cast<SCH_TEXT*>( label )->GetText() );
90 
91  status.possible_labels = getProposedLabels( status.labels );
92  m_items.push_back( status );
93  }
94 }
95 
96 
98 {
99  m_migration_list->DeleteAllItems();
100 
101  m_migration_list->InsertColumn( 0, _( "Sheet" ) );
102  m_migration_list->InsertColumn( 1, _( "Conflicting Labels" ) );
103  m_migration_list->InsertColumn( 2, _( "New Label" ) );
104  m_migration_list->InsertColumn( 3, _( "Status" ) );
105 
106  for( auto item : m_items )
107  {
108  wxString old = item.labels[0];
109  for( unsigned j = 1; j < item.labels.size(); j++ )
110  old << ", " << item.labels[j];
111 
112  auto i = m_migration_list->InsertItem( m_migration_list->GetItemCount(), wxEmptyString );
113 
114  m_migration_list->SetItem( i, 0, item.subgraph->m_sheet.PathHumanReadable() );
115  m_migration_list->SetItem( i, 1, old );
116  m_migration_list->SetItem( i, 2, item.possible_labels[0] );
117  m_migration_list->SetItem( i, 3, "" );
118  }
119 
120  m_migration_list->Select( 0 );
121  m_migration_list->SetColumnWidth( 1, -1 );
122 }
123 
124 
126  const std::vector<wxString>& aLabelList )
127 {
128  int lowest_start = INT_MAX;
129  int highest_end = -1;
130  int widest_bus = -1;
131 
133 
134  for( const wxString& label : aLabelList )
135  {
136  conn.ConfigureFromLabel( label );
137 
138  int start = conn.VectorStart();
139  int end = conn.VectorEnd();
140 
141  if( start < lowest_start )
142  lowest_start = start;
143 
144  if( end > highest_end )
145  highest_end = end;
146 
147  if( end - start + 1 > widest_bus )
148  widest_bus = end - start + 1;
149  }
150 
151  std::vector<wxString> proposals;
152 
153  for( const wxString& label : aLabelList )
154  {
155  conn.ConfigureFromLabel( label );
156  wxString proposal = conn.VectorPrefix();
157  proposal << "[" << highest_end << ".." << lowest_start << "]";
158  proposals.push_back( proposal );
159  }
160 
161  return proposals;
162 }
163 
164 
165 void DIALOG_MIGRATE_BUSES::onItemSelected( wxListEvent& aEvent )
166 {
167  unsigned sel = aEvent.GetIndex();
168  wxASSERT( sel < m_items.size() );
169 
170  m_selected_index = sel;
171 
172  auto subgraph = m_items[sel].subgraph;
173 
174  auto sheet = subgraph->m_sheet;
175  auto driver = subgraph->m_driver;
176 
177  const SCH_SHEET_PATH& current = m_frame->GetCurrentSheet();
178 
179  if( sheet != current )
180  {
182  m_frame->Schematic().SetCurrentSheet( sheet );
183  sheet.LastScreen()->TestDanglingEnds();
184  }
185 
186  auto pos = driver->GetPosition();
187 
189  m_frame->RedrawScreen( pos, false );
190 
191  m_cb_new_name->Clear();
192 
193  for( const wxString& option : m_items[sel].possible_labels )
194  m_cb_new_name->Append( option );
195 
196  m_cb_new_name->Select( 0 );
197 }
198 
199 
200 void DIALOG_MIGRATE_BUSES::onAcceptClicked( wxCommandEvent& aEvent )
201 {
202  wxASSERT( m_selected_index < m_items.size() );
203 
204  auto sel = m_selected_index;
205 
206  m_items[sel].approved_label = m_cb_new_name->GetStringSelection();
207  m_items[sel].approved = true;
208 
209  auto labels = m_items[sel].subgraph->GetBusLabels();
210 
211  for( auto label : labels )
212  static_cast<SCH_TEXT*>( label )->SetText( m_items[sel].approved_label );
213 
214  m_migration_list->SetItem( sel, 2, m_items[sel].approved_label );
215  m_migration_list->SetItem( sel, 3, _( "Updated" ) );
216 
217  if( sel < m_items.size() - 1 )
218  {
219  m_migration_list->Select( sel + 1 );
220  }
221 
222  m_frame->GetCanvas()->Refresh();
223 }
KIGFX::VIEW_CONTROLS * GetViewControls() const
Function GetViewControls() Returns a pointer to the VIEW_CONTROLS instance used in the panel.
virtual void RedrawScreen(const wxPoint &aCenterPoint, bool aWarpPointer)
CONNECTION_GRAPH * ConnectionGraph() const
Definition: schematic.h:129
void onItemSelected(wxListEvent &aEvent)
static TOOL_ACTION zoomFitScreen
Definition: actions.h:94
std::vector< BUS_MIGRATION_STATUS > m_items
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:140
SCH_EDIT_FRAME * m_frame
void ConfigureFromLabel(const wxString &aLabel)
Configures the connection given a label.
Schematic editor (Eeschema) main window.
void UpdateAllScreenReferences()
Function UpdateAllScreenReferences updates the reference and the m_Multi parameter (part selection) f...
std::vector< wxString > labels
std::vector< wxString > getProposedLabels(const std::vector< wxString > &aLabelList)
void onAcceptClicked(wxCommandEvent &aEvent)
DIALOG_MIGRATE_BUSES(SCH_EDIT_FRAME *aParent)
Migrates buses using legacy multi-label joining behavior.
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
SCHEMATIC & Schematic() const
virtual void SetCrossHairCursorPosition(const VECTOR2D &aPosition, bool aWarpView=true)=0
Moves the graphic crosshair cursor to the requested position expressed in world coordinates.
SCH_SHEET_PATH.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
std::vector< wxString > possible_labels
Class DIALOG_MIGRATE_BUSES_BASE.
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
#define _(s)
Definition: 3d_actions.cpp:33
const CONNECTION_SUBGRAPH * subgraph
SCH_SHEET_PATH & GetCurrentSheet() const
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:78
void SetCurrentSheet(const SCH_SHEET_PATH &aPath)
Definition: schematic.h:124
std::vector< const CONNECTION_SUBGRAPH * > GetBusesNeedingMigration()
Determines which subgraphs have more than one conflicting bus label.