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 ),
58  m_frame( aParent )
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();
76  auto subgraphs = g_ConnectionGraph->GetBusesNeedingMigration();
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 
125 std::vector<wxString> DIALOG_MIGRATE_BUSES::getProposedLabels( const std::vector<wxString>& aLabelList )
126 {
127  int lowest_start = INT_MAX;
128  int highest_end = -1;
129  int widest_bus = -1;
130 
131  for( const wxString& 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( const wxString& 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( const wxString& 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 }
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:92
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.
void ConfigureFromLabel(wxString aLabel)
Configures the connection given a label.
wxString GetText(GRAPHIC_PINSHAPE shape)
Definition: pin_shape.cpp:33
virtual void SetCrossHairCursorPosition(const VECTOR2D &aPosition, bool aWarpView=true)=0
Moves the graphic crosshair cursor to the requested position expressed in world coordinates.
#define _(s)
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).
size_t i
Definition: json11.cpp:597
const CONNECTION_SUBGRAPH * subgraph
long VectorStart() const
std::vector< const CONNECTION_SUBGRAPH * > GetBusesNeedingMigration()
Determines which subgraphs have more than one conflicting bus label.