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 <connection_graph.h>
23 #include <tool/tool_manager.h>
24 #include <dialog_migrate_buses.h>
25 
56  DIALOG_MIGRATE_BUSES_BASE( aParent ),
57  m_frame( aParent )
58 {
59  m_migration_list->Bind( wxEVT_LIST_ITEM_SELECTED,
61 
62  m_btn_accept->Bind( wxEVT_COMMAND_BUTTON_CLICKED,
64 
65  loadGraphData();
66  updateUi();
67 
68  aParent->GetToolManager()->RunAction( "common.Control.zoomFitScreen", true );
69 }
70 
71 
73 {
74  m_items.clear();
75  auto subgraphs = g_ConnectionGraph->GetBusesNeedingMigration();
76 
77  for( auto subgraph : subgraphs )
78  {
79  BUS_MIGRATION_STATUS status;
80 
81  status.subgraph = subgraph;
82  status.approved = false;
83 
84  auto labels = subgraph->GetBusLabels();
85  wxASSERT( labels.size() > 1 );
86 
87  for( auto label : labels )
88  status.labels.push_back( static_cast<SCH_TEXT*>( label )->GetText() );
89 
90  status.possible_labels = getProposedLabels( status.labels );
91  m_items.push_back( status );
92  }
93 }
94 
95 
97 {
98  m_migration_list->DeleteAllItems();
99 
100  m_migration_list->InsertColumn( 0, _( "Sheet" ) );
101  m_migration_list->InsertColumn( 1, _( "Conflicting Labels" ) );
102  m_migration_list->InsertColumn( 2, _( "New Label" ) );
103  m_migration_list->InsertColumn( 3, _( "Status" ) );
104 
105  for( auto item : m_items )
106  {
107  wxString old = item.labels[0];
108  for( unsigned j = 1; j < item.labels.size(); j++ )
109  old << ", " << item.labels[j];
110 
111  auto i = m_migration_list->InsertItem( m_migration_list->GetItemCount(),
112  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 
125 std::vector<wxString> DIALOG_MIGRATE_BUSES::getProposedLabels( std::vector<wxString> aLabelList )
126 {
127  int lowest_start = INT_MAX;
128  int highest_end = -1;
129  int widest_bus = -1;
130 
131  for( auto label : aLabelList )
132  {
133  SCH_CONNECTION conn;
134  conn.ConfigureFromLabel( label );
135 
136  int start = conn.VectorStart();
137  int end = conn.VectorEnd();
138 
139  if( start < lowest_start )
140  lowest_start = start;
141 
142  if( end > highest_end )
143  highest_end = end;
144 
145  if( end - start + 1 > widest_bus )
146  widest_bus = end - start + 1;
147  }
148 
149  SCH_CONNECTION conn;
150  std::vector<wxString> proposals;
151 
152  for( auto label : aLabelList )
153  {
154  conn.ConfigureFromLabel( label );
155  wxString proposal = conn.VectorPrefix();
156  proposal << "[" << highest_end << ".." << lowest_start << "]";
157  proposals.push_back( proposal );
158  }
159 
160  return proposals;
161 }
162 
163 
164 void DIALOG_MIGRATE_BUSES::onItemSelected( wxListEvent& aEvent )
165 {
166  unsigned sel = aEvent.GetIndex();
167  wxASSERT( sel < m_items.size() );
168 
169  m_selected_index = sel;
170 
171  auto subgraph = m_items[sel].subgraph;
172 
173  auto sheet = subgraph->m_sheet;
174  auto driver = subgraph->m_driver;
175 
176  if( sheet != *g_CurrentSheet )
177  {
178  sheet.LastScreen()->SetZoom( m_frame->GetScreen()->GetZoom() );
179  *g_CurrentSheet = sheet;
181  sheet.LastScreen()->TestDanglingEnds();
182  }
183 
184  auto pos = driver->GetPosition();
185 
187  m_frame->RedrawScreen( pos, false );
188 
189  m_cb_new_name->Clear();
190 
191  for( auto option : m_items[sel].possible_labels )
192  m_cb_new_name->Append( option );
193 
194  m_cb_new_name->Select( 0 );
195 }
196 
197 
198 void DIALOG_MIGRATE_BUSES::onAcceptClicked( wxCommandEvent& aEvent )
199 {
200  wxASSERT( m_selected_index < m_items.size() );
201 
202  auto sel = m_selected_index;
203 
204  m_items[sel].approved_label = m_cb_new_name->GetStringSelection();
205  m_items[sel].approved = true;
206 
207  auto labels = m_items[sel].subgraph->GetBusLabels();
208 
209  for( auto label : labels )
210  static_cast<SCH_TEXT*>( label )->SetText( m_items[sel].approved_label );
211 
212  m_migration_list->SetItem( sel, 2, m_items[sel].approved_label );
213  m_migration_list->SetItem( sel, 3, _( "Updated" ) );
214 
215  if( sel < m_items.size() - 1 )
216  {
217  m_migration_list->Select( sel + 1 );
218  }
219 
220  m_frame->GetCanvas()->Refresh();
221 }
double GetZoom() const
Function GetZoom returns the current "zoom factor", which is a measure of "internal units per device ...
Definition: base_screen.h:340
void onItemSelected(wxListEvent &aEvent)
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:125
SCH_EDIT_FRAME * m_frame
Schematic editor (Eeschema) main window.
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...
void UpdateAllScreenReferences()
Function UpdateAllScreenReferences updates the reference and the m_Multi parameter (part selection) f...
std::vector< wxString > labels
CONNECTION_GRAPH * g_ConnectionGraph
This also wants to live in the eventual SCHEMATIC object.
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
SCH_SHEET_PATH * g_CurrentSheet
With the new connectivity algorithm, many more places than before want to know what the current sheet...
long VectorEnd() const
void onAcceptClicked(wxCommandEvent &aEvent)
TOOL_MANAGER * GetToolManager() const override
Return the tool manager instance, if any.
Definition: draw_frame.h:937
DIALOG_MIGRATE_BUSES(SCH_EDIT_FRAME *aParent)
Migrates buses using legacy multi-label joining behavior.
SCH_DRAW_PANEL * GetCanvas() const override
void ConfigureFromLabel(wxString aLabel)
Configures the connection given a label.
wxString GetText(GRAPHIC_PINSHAPE shape)
Definition: pin_shape.cpp:33
std::vector< wxString > possible_labels
virtual void RedrawScreen(const wxPoint &aCenterPoint, bool aWarpPointer) override
Redraw the entire screen area by updating the scroll bars and mouse pointer in order to have aCenterP...
Class DIALOG_MIGRATE_BUSES_BASE.
wxString VectorPrefix() const
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
size_t i
Definition: json11.cpp:597
const CONNECTION_SUBGRAPH * subgraph
long VectorStart() const
void SetCrossHairPosition(const wxPoint &aPosition, bool aSnapToGrid=true)
Set the screen cross hair position to aPosition in logical (drawing) units.
std::vector< wxString > getProposedLabels(std::vector< wxString > aLabelList)
std::vector< const CONNECTION_SUBGRAPH * > GetBusesNeedingMigration()
Determines which subgraphs have more than one conflicting bus label.