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 <tool/actions.h>
25 #include <dialog_migrate_buses.h>
26 
57  : DIALOG_MIGRATE_BUSES_BASE( aParent ), m_frame( aParent ), m_selected_index( 0 )
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( ACTIONS::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(), wxEmptyString );
112 
113  m_migration_list->SetItem( i, 0, item.subgraph->m_sheet.PathHumanReadable() );
114  m_migration_list->SetItem( i, 1, old );
115  m_migration_list->SetItem( i, 2, item.possible_labels[0] );
116  m_migration_list->SetItem( i, 3, "" );
117  }
118 
119  m_migration_list->Select( 0 );
120  m_migration_list->SetColumnWidth( 1, -1 );
121 }
122 
123 
124 std::vector<wxString> DIALOG_MIGRATE_BUSES::getProposedLabels( const std::vector<wxString>& aLabelList )
125 {
126  int lowest_start = INT_MAX;
127  int highest_end = -1;
128  int widest_bus = -1;
129 
130  for( const wxString& label : aLabelList )
131  {
132  SCH_CONNECTION conn;
133  conn.ConfigureFromLabel( label );
134 
135  int start = conn.VectorStart();
136  int end = conn.VectorEnd();
137 
138  if( start < lowest_start )
139  lowest_start = start;
140 
141  if( end > highest_end )
142  highest_end = end;
143 
144  if( end - start + 1 > widest_bus )
145  widest_bus = end - start + 1;
146  }
147 
148  SCH_CONNECTION conn;
149  std::vector<wxString> proposals;
150 
151  for( const wxString& label : aLabelList )
152  {
153  conn.ConfigureFromLabel( label );
154  wxString proposal = conn.VectorPrefix();
155  proposal << "[" << highest_end << ".." << lowest_start << "]";
156  proposals.push_back( proposal );
157  }
158 
159  return proposals;
160 }
161 
162 
163 void DIALOG_MIGRATE_BUSES::onItemSelected( wxListEvent& aEvent )
164 {
165  unsigned sel = aEvent.GetIndex();
166  wxASSERT( sel < m_items.size() );
167 
168  m_selected_index = sel;
169 
170  auto subgraph = m_items[sel].subgraph;
171 
172  auto sheet = subgraph->m_sheet;
173  auto driver = subgraph->m_driver;
174 
175  if( sheet != *g_CurrentSheet )
176  {
177  sheet.LastScreen()->SetZoom( m_frame->GetScreen()->GetZoom() );
178  *g_CurrentSheet = sheet;
180  sheet.LastScreen()->TestDanglingEnds();
181  }
182 
183  auto pos = driver->GetPosition();
184 
186  m_frame->RedrawScreen( pos, false );
187 
188  m_cb_new_name->Clear();
189 
190  for( const wxString& option : m_items[sel].possible_labels )
191  m_cb_new_name->Append( option );
192 
193  m_cb_new_name->Select( 0 );
194 }
195 
196 
197 void DIALOG_MIGRATE_BUSES::onAcceptClicked( wxCommandEvent& aEvent )
198 {
199  wxASSERT( m_selected_index < m_items.size() );
200 
201  auto sel = m_selected_index;
202 
203  m_items[sel].approved_label = m_cb_new_name->GetStringSelection();
204  m_items[sel].approved = true;
205 
206  auto labels = m_items[sel].subgraph->GetBusLabels();
207 
208  for( auto label : labels )
209  static_cast<SCH_TEXT*>( label )->SetText( m_items[sel].approved_label );
210 
211  m_migration_list->SetItem( sel, 2, m_items[sel].approved_label );
212  m_migration_list->SetItem( sel, 3, _( "Updated" ) );
213 
214  if( sel < m_items.size() - 1 )
215  {
216  m_migration_list->Select( sel + 1 );
217  }
218 
219  m_frame->GetCanvas()->Refresh();
220 }
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
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)
double GetZoom() const
Function GetZoom returns the current "zoom factor", which is a measure of "internal units per device ...
Definition: base_screen.h:240
void onItemSelected(wxListEvent &aEvent)
static TOOL_ACTION zoomFitScreen
Definition: actions.h:93
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:109
SCH_EDIT_FRAME * m_frame
Schematic editor (Eeschema) main window.
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...
std::vector< wxString > getProposedLabels(const std::vector< wxString > &aLabelList)
long VectorEnd() const
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.
wxString GetText(GRAPHIC_PINSHAPE aShape)
Definition: pin_shape.cpp:58
void ConfigureFromLabel(wxString aLabel)
Configures the connection given a label.
virtual void SetCrossHairCursorPosition(const VECTOR2D &aPosition, bool aWarpView=true)=0
Moves the graphic crosshair cursor to the requested position expressed in world coordinates.
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.
wxString VectorPrefix() const
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
#define _(s)
Definition: 3d_actions.cpp:31
const CONNECTION_SUBGRAPH * subgraph
long VectorStart() const
std::vector< const CONNECTION_SUBGRAPH * > GetBusesNeedingMigration()
Determines which subgraphs have more than one conflicting bus label.