KiCad PCB EDA Suite
dialog_edit_components_libid.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 2017 Jean-Pierre Charras, jp.charras@wanadoo.fr
5  * Copyright 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
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
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
31 #include <fctsys.h>
32 #include <sch_edit_frame.h>
33 #include <sch_draw_panel.h>
34 #include <sch_component.h>
35 #include <sch_reference_list.h>
36 #include <pgm_base.h>
37 #include <symbol_lib_table.h>
38 #include <widgets/wx_grid.h>
39 
41 #include <wx/tokenzr.h>
42 #include <grid_tricks.h>
44 
45 #define COL_REFS 0
46 #define COL_CURR_LIBID 1
47 #define COL_NEW_LIBID 2
48 
49 // a re-implementation of wxGridCellAutoWrapStringRenderer to allow workaround to autorowsize bug
50 class GRIDCELL_AUTOWRAP_STRINGRENDERER : public wxGridCellAutoWrapStringRenderer
51 {
52 public:
53  int GetHeight( wxDC& aDC, wxGrid* aGrid, int aRow, int aCol );
54 
55  wxGridCellRenderer *Clone() const override
56  { return new GRIDCELL_AUTOWRAP_STRINGRENDERER; }
57 
58 private:
59  // HELPER ROUTINES UNCHANGED FROM wxWidgets IMPLEMENTATION
60 
61  wxArrayString GetTextLines( wxGrid& grid,
62  wxDC& dc,
63  const wxGridCellAttr& attr,
64  const wxRect& rect,
65  int row, int col);
66 
67  // Helper methods of GetTextLines()
68 
69  // Break a single logical line of text into several physical lines, all of
70  // which are added to the lines array. The lines are broken at maxWidth and
71  // the dc is used for measuring text extent only.
72  void BreakLine(wxDC& dc,
73  const wxString& logicalLine,
74  wxCoord maxWidth,
75  wxArrayString& lines);
76 
77  // Break a word, which is supposed to be wider than maxWidth, into several
78  // lines, which are added to lines array and the last, incomplete, of which
79  // is returned in line output parameter.
80  //
81  // Returns the width of the last line.
82  wxCoord BreakWord(wxDC& dc,
83  const wxString& word,
84  wxCoord maxWidth,
85  wxArrayString& lines,
86  wxString& line);
87 };
88 
89 
90 // PRIVATE METHOD UNCHANGED FROM wxWidgets IMPLEMENTATION
91 wxArrayString
93  wxDC& dc,
94  const wxGridCellAttr& attr,
95  const wxRect& rect,
96  int row, int col)
97 {
98  dc.SetFont(attr.GetFont());
99  const wxCoord maxWidth = rect.GetWidth();
100 
101  // Transform logical lines into physical ones, wrapping the longer ones.
102  const wxArrayString
103  logicalLines = wxSplit(grid.GetCellValue(row, col), '\n', '\0');
104 
105  // Trying to do anything if the column is hidden anyhow doesn't make sense
106  // and we run into problems in BreakLine() in this case.
107  if ( maxWidth <= 0 )
108  return logicalLines;
109 
110  wxArrayString physicalLines;
111  for ( wxArrayString::const_iterator it = logicalLines.begin();
112  it != logicalLines.end();
113  ++it )
114  {
115  const wxString& line = *it;
116 
117  if ( dc.GetTextExtent(line).x > maxWidth )
118  {
119  // Line does not fit, break it up.
120  BreakLine(dc, line, maxWidth, physicalLines);
121  }
122  else // The entire line fits as is
123  {
124  physicalLines.push_back(line);
125  }
126  }
127 
128  return physicalLines;
129 }
130 
131 
132 // PRIVATE METHOD UNCHANGED FROM wxWidgets IMPLEMENTATION
133 void
135  const wxString& logicalLine,
136  wxCoord maxWidth,
137  wxArrayString& lines)
138 {
139  wxCoord lineWidth = 0;
140  wxString line;
141 
142  // For each word
143  wxStringTokenizer wordTokenizer(logicalLine, wxS(" \t"), wxTOKEN_RET_DELIMS);
144  while ( wordTokenizer.HasMoreTokens() )
145  {
146  const wxString word = wordTokenizer.GetNextToken();
147  const wxCoord wordWidth = dc.GetTextExtent(word).x;
148  if ( lineWidth + wordWidth < maxWidth )
149  {
150  // Word fits, just add it to this line.
151  line += word;
152  lineWidth += wordWidth;
153  }
154  else
155  {
156  // Word does not fit, check whether the word is itself wider that
157  // available width
158  if ( wordWidth < maxWidth )
159  {
160  // Word can fit in a new line, put it at the beginning
161  // of the new line.
162  lines.push_back(line);
163  line = word;
164  lineWidth = wordWidth;
165  }
166  else // Word cannot fit in available width at all.
167  {
168  if ( !line.empty() )
169  {
170  lines.push_back(line);
171  line.clear();
172  lineWidth = 0;
173  }
174 
175  // Break it up in several lines.
176  lineWidth = BreakWord(dc, word, maxWidth, lines, line);
177  }
178  }
179  }
180 
181  if ( !line.empty() )
182  lines.push_back(line);
183 }
184 
185 
186 // PRIVATE METHOD UNCHANGED FROM wxWidgets IMPLEMENTATION
187 wxCoord
189  const wxString& word,
190  wxCoord maxWidth,
191  wxArrayString& lines,
192  wxString& line)
193 {
194  wxArrayInt widths;
195  dc.GetPartialTextExtents(word, widths);
196 
197  // TODO: Use binary search to find the first element > maxWidth.
198  const unsigned count = widths.size();
199  unsigned n;
200  for ( n = 0; n < count; n++ )
201  {
202  if ( widths[n] > maxWidth )
203  break;
204  }
205 
206  if ( n == 0 )
207  {
208  // This is a degenerate case: the first character of the word is
209  // already wider than the available space, so we just can't show it
210  // completely and have to put the first character in this line.
211  n = 1;
212  }
213 
214  lines.push_back(word.substr(0, n));
215 
216  // Check if the remainder of the string fits in one line.
217  //
218  // Unfortunately we can't use the existing partial text extents as the
219  // extent of the remainder may be different when it's rendered in a
220  // separate line instead of as part of the same one, so we have to
221  // recompute it.
222  const wxString rest = word.substr(n);
223  const wxCoord restWidth = dc.GetTextExtent(rest).x;
224  if ( restWidth <= maxWidth )
225  {
226  line = rest;
227  return restWidth;
228  }
229 
230  // Break the rest of the word into lines.
231  //
232  // TODO: Perhaps avoid recursion? The code is simpler like this but using a
233  // loop in this function would probably be more efficient.
234  return BreakWord(dc, rest, maxWidth, lines, line);
235 }
236 
237 
238 #define GRID_CELL_MARGIN 4
239 
240 int GRIDCELL_AUTOWRAP_STRINGRENDERER::GetHeight( wxDC& aDC, wxGrid* aGrid, int aRow, int aCol )
241 {
242  wxGridCellAttr* attr = aGrid->GetOrCreateCellAttr( aRow, aCol );
243  wxRect rect;
244 
245  aDC.SetFont( attr->GetFont() );
246  rect.SetWidth( aGrid->GetColSize( aCol ) - ( 2 * GRID_CELL_MARGIN ) );
247 
248  const size_t numLines = GetTextLines( *aGrid, aDC, *attr, rect, aRow, aCol ).size();
249  const int textHeight = numLines * aDC.GetCharHeight();
250 
251  attr->DecRef();
252 
253  return textHeight + ( 2 * GRID_CELL_MARGIN );
254 }
255 
256 
257 // a helper class to handle components to edit
259 {
260 public:
261  SCH_COMPONENT* m_Component; // the schematic component
262  int m_Row; // the row index in m_grid
263  SCH_SCREEN* m_Screen; // the screen where m_Component lives
264  wxString m_Reference; // the schematic reference, only to display it in list
265  wxString m_InitialLibId; // the Lib Id of the component before any change
266  bool m_IsOrphan; // true if a component has no corresponding symbol found in libs
267 
269  {
270  m_Component = aComponent;
272  m_Row = -1;
273  m_IsOrphan = false;
274  m_Screen = nullptr;
275  }
276 
277  // Returns a string like mylib:symbol_name from the LIB_ID of the component
278  wxString GetStringLibId()
279  {
281  }
282 
283  // Returns a string containing the reference of the component
285  {
286  return m_Reference;
287  }
288 };
289 
290 
300 {
301 public:
304 
306 
307 private:
309  bool m_isModified; // set to true if the schematic is modified
310  std::vector<int> m_OrphansRowIndexes; // list of rows containing orphan lib_id
311 
312  std::vector<CMP_CANDIDATE> m_components;
313 
315 
316  void initDlg();
317 
324  void AddRowToGrid( bool aMarkRow, const wxString& aReferences, const wxString& aStrLibId );
325 
327  bool validateLibIds();
328 
330  void revertChanges();
331 
336  bool setLibIdByBrowser( int aRow );
337 
338  // Events handlers
339 
340  // called on a right click or a left double click:
341  void onCellBrowseLib( wxGridEvent& event ) override;
342 
343  // Apply changes, but do not close the dialog
344  void onApplyButton( wxCommandEvent& event ) override;
345 
346  // Cancel all changes, and close the dialog
347  void onCancel( wxCommandEvent& event ) override
348  {
349  if( m_isModified )
350  revertChanges();
351 
352  // Just skipping the event doesn't work after the library browser was run
353  if( IsQuasiModal() )
354  EndQuasiModal( wxID_CANCEL );
355  else
356  event.Skip();
357  }
358 
359  // Undo all changes, and clear the list of new lib_ids
360  void onUndoChangesButton( wxCommandEvent& event ) override;
361 
362  // Try to find a candidate for non existing symbols
363  void onClickOrphansButton( wxCommandEvent& event ) override;
364 
365  // UI event, to enable/disable buttons
366  void updateUIChangesButton( wxUpdateUIEvent& event ) override
367  {
368  m_buttonUndo->Enable( m_isModified );
369  }
370 
371  // Automatically called when click on OK button
372  bool TransferDataFromWindow() override;
373 
374  void AdjustGridColumns( int aWidth );
375 
376  void OnSizeGrid( wxSizeEvent& event ) override;
377 };
378 
379 
382 {
383  m_parent = aParent;
385 
386  m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
387 
388  initDlg();
389 
391 }
392 
393 
395 {
396  // Delete the GRID_TRICKS.
397  m_grid->PopEventHandler( true );
398 
399  m_autoWrapRenderer->DecRef();
400 }
401 
402 
403 // A sort compare function to sort components list by LIB_ID and then reference
404 static bool sort_by_libid( const CMP_CANDIDATE& cmp1, const CMP_CANDIDATE& cmp2 )
405 {
406  if( cmp1.m_Component->GetLibId() == cmp2.m_Component->GetLibId() )
407  return cmp1.m_Reference.Cmp( cmp2.m_Reference ) < 0;
408 
409  return cmp1.m_Component->GetLibId() < cmp2.m_Component->GetLibId();
410 }
411 
412 
414 {
415  // Clear the FormBuilder rows
416  m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
417 
418  m_isModified = false;
419 
420  // This option build the full component list
421  // In complex hierarchies, the same component is in fact duplicated, but
422  // it is listed with different references (one by sheet instance)
423  // the list is larger and looks like it contains all components
424  SCH_SHEET_LIST sheets( g_RootSheet );
425  SCH_REFERENCE_LIST references;
426  // build the full list of components including component having no symbol in loaded libs
427  // (orphan components)
428  sheets.GetComponents( references, /* include power symbols */true,
429  /* include orphan components */true );
430 
431  for( unsigned ii = 0; ii < references.GetCount(); ii++ )
432  {
433  SCH_REFERENCE& item = references[ii];
434  CMP_CANDIDATE candidate( item.GetComp() );
435  candidate.m_Screen = item.GetSheetPath().LastScreen();
436  SCH_SHEET_PATH sheetpath = item.GetSheetPath();
437  candidate.m_Reference = candidate.m_Component->GetRef( &sheetpath );
438  int unitcount = candidate.m_Component->GetUnitCount();
439  candidate.m_IsOrphan = ( unitcount == 0 );
440  m_components.push_back( candidate );
441  }
442 
443  if( m_components.size() == 0 )
444  return;
445 
446  // now sort by lib id to create groups of items having the same lib id
447  std::sort( m_components.begin(), m_components.end(), sort_by_libid );
448 
449  // Now, fill m_grid
450  wxString last_str_libid = m_components.front().GetStringLibId();
451  int row = 0;
452  wxString refs;
453  wxString last_ref;
454  bool mark_cell = m_components.front().m_IsOrphan;
455 
456  for( auto& cmp : m_components )
457  {
458  wxString str_libid = cmp.GetStringLibId();
459 
460  if( last_str_libid != str_libid )
461  {
462  // Add last group to grid
463  AddRowToGrid( mark_cell, refs, last_str_libid );
464 
465  // prepare next entry
466  mark_cell = cmp.m_IsOrphan;
467  last_str_libid = str_libid;
468  refs.Empty();
469  row++;
470  }
471  else if( cmp.GetSchematicReference() == last_ref )
472  {
473  cmp.m_Row = row;
474  continue;
475  }
476 
477  last_ref = cmp.GetSchematicReference();
478 
479  if( !refs.IsEmpty() )
480  refs += wxT( ", " );
481 
482  refs += cmp.GetSchematicReference();
483  cmp.m_Row = row;
484  }
485 
486  // Add last component group:
487  AddRowToGrid( mark_cell, refs, last_str_libid );
488 
489  // Allows only the selection by row
490  m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
491 
492  m_buttonOrphanItems->Enable( m_OrphansRowIndexes.size() > 0 );
493  Layout();
494 }
495 
496 
497 void DIALOG_EDIT_COMPONENTS_LIBID::AddRowToGrid( bool aMarkRow, const wxString& aReferences,
498  const wxString& aStrLibId )
499 {
500  int row = m_grid->GetNumberRows();
501 
502  if( aMarkRow ) // a orphan component exists, set m_AsOrphanCmp as true
503  m_OrphansRowIndexes.push_back( row );
504 
505  m_grid->AppendRows( 1 );
506 
507  m_grid->SetCellValue( row, COL_REFS, aReferences );
508  m_grid->SetReadOnly( row, COL_REFS );
509 
510  m_grid->SetCellValue( row, COL_CURR_LIBID, aStrLibId );
511  m_grid->SetReadOnly( row, COL_CURR_LIBID );
512 
513  if( aMarkRow ) // A symbol is not existing in libraries: mark the cell
514  {
515  wxFont font = m_grid->GetDefaultCellFont();
516  font.MakeBold();
517  font.MakeItalic();
518  m_grid->SetCellFont( row, COL_CURR_LIBID, font );
519  }
520 
521  m_grid->SetCellRenderer( row, COL_REFS, m_autoWrapRenderer->Clone() );
522 
523  // wxWidgets' AutoRowHeight fails when used with wxGridCellAutoWrapStringRenderer
524  // (fixed in 2014, but didn't get in to wxWidgets 3.0.2)
525  wxClientDC dc( this );
526  m_grid->SetRowSize( row, m_autoWrapRenderer->GetHeight( dc, m_grid, row, COL_REFS ) );
527 
528  // set new libid column browse button
529  wxGridCellAttr* attr = new wxGridCellAttr;
530  attr->SetEditor( new GRID_CELL_SYMBOL_ID_EDITOR( this, aStrLibId ) );
531  m_grid->SetAttr( row, COL_NEW_LIBID, attr );
532 }
533 
534 
536 {
537  if( !m_grid->CommitPendingChanges() )
538  return false;
539 
540  int row_max = m_grid->GetNumberRows() - 1;
541 
542  for( int row = 0; row <= row_max; row++ )
543  {
544  wxString new_libid = m_grid->GetCellValue( row, COL_NEW_LIBID );
545 
546  if( new_libid.IsEmpty() )
547  continue;
548 
549  // a new lib id is found. validate this new value
550  LIB_ID id;
551  id.Parse( new_libid, LIB_ID::ID_SCH );
552 
553  if( !id.IsValid() )
554  {
555  wxString msg;
556  msg.Printf( _( "Symbol library identifier \"%s\" is not valid." ), new_libid );
557  wxMessageBox( msg );
558 
559  m_grid->SetFocus();
560  m_grid->MakeCellVisible( row, COL_NEW_LIBID );
561  m_grid->SetGridCursor( row, COL_NEW_LIBID );
562 
563  m_grid->EnableCellEditControl( true );
564  m_grid->ShowCellEditControl();
565 
566  return false;
567  }
568  }
569 
570  return true;
571 }
572 
573 
574 void DIALOG_EDIT_COMPONENTS_LIBID::onApplyButton( wxCommandEvent& event )
575 {
576  if( TransferDataFromWindow() )
577  m_parent->GetCanvas()->Refresh();
578 }
579 
580 
582 {
583  revertChanges();
584 
585  int row_max = m_grid->GetNumberRows() - 1;
586 
587  for( int row = 0; row <= row_max; row++ )
588  {
589  m_grid->SetCellValue( row, COL_NEW_LIBID, wxEmptyString );
590  }
591 
592  m_isModified = false;
593 }
594 
595 
597 {
598  int row = event.GetRow();
599  m_grid->SelectRow( row ); // only for user, to show the selected line
600 
601  setLibIdByBrowser( row );
602 
603 }
604 
605 
607 {
608  std::vector< wxString > libs = Prj().SchSymbolLibTable()->GetLogicalLibs();
609  wxArrayString aliasNames;
610  wxArrayString candidateSymbNames;
611 
612  unsigned fixesCount = 0;
613 
614  // Try to find a candidate for non existing symbols in any loaded library
615  for( unsigned ii = 0; ii < m_OrphansRowIndexes.size(); ii++ )
616  {
617  wxString orphanLibid = m_grid->GetCellValue( m_OrphansRowIndexes[ii], COL_CURR_LIBID );
618  int grid_row_idx = m_OrphansRowIndexes[ii]; //row index in m_grid for the current item
619 
620  LIB_ID curr_libid;
621  curr_libid.Parse( orphanLibid, LIB_ID::ID_SCH, true );
622  wxString symbName = curr_libid.GetLibItemName();
623  // number of full LIB_ID candidates (because we search for a symbol name
624  // inside all avaiable libraries, perhaps the same symbol name can be found
625  // in more than one library, giving ambiguity
626  int libIdCandidateCount = 0;
627  candidateSymbNames.Clear();
628 
629  // now try to find a candidate
630  for( auto &lib : libs )
631  {
632  aliasNames.Clear();
633 
634  try
635  {
636  Prj().SchSymbolLibTable()->EnumerateSymbolLib( lib, aliasNames );
637  }
638  catch( const IO_ERROR& ) {} // ignore, it is handled below
639 
640  if( aliasNames.IsEmpty() )
641  continue;
642 
643  // Find a symbol name in symbols inside this library:
644  int index = aliasNames.Index( symbName );
645 
646  if( index != wxNOT_FOUND )
647  {
648  // a candidate is found!
649  libIdCandidateCount++;
650  wxString newLibid = lib + ':' + symbName;
651 
652  // Uses the first found. Most of time, it is alone.
653  // Others will be stored in a candidate list
654  if( libIdCandidateCount <= 1 )
655  {
656  m_grid->SetCellValue( grid_row_idx, COL_NEW_LIBID, newLibid );
657  candidateSymbNames.Add( m_grid->GetCellValue( grid_row_idx, COL_NEW_LIBID ) );
658  fixesCount++;
659  }
660  else // Store other candidates for later selection
661  {
662  candidateSymbNames.Add( newLibid );
663  }
664  }
665  }
666 
667  // If more than one LIB_ID candidate, ask for selection between candidates:
668  if( libIdCandidateCount > 1 )
669  {
670  // Mainly for user: select the row being edited
671  m_grid->SelectRow( grid_row_idx );
672 
673  wxString msg;
674  msg.Printf( _( "Available Candidates for %s " ),
675  m_grid->GetCellValue( grid_row_idx, COL_CURR_LIBID ) );
676 
677  wxSingleChoiceDialog dlg ( this, msg,
678  wxString::Format( _( "Candidates count %d " ), libIdCandidateCount ),
679  candidateSymbNames );
680 
681  if( dlg.ShowModal() == wxID_OK )
682  m_grid->SetCellValue( grid_row_idx, COL_NEW_LIBID, dlg.GetStringSelection() );
683  }
684  }
685 
686  if( fixesCount < m_OrphansRowIndexes.size() ) // Not all orphan components are fixed
687  {
688  wxMessageBox( wxString::Format( _( "%u link(s) mapped, %u not found" ),
689  fixesCount,
690  (unsigned) m_OrphansRowIndexes.size() - fixesCount ) );
691  }
692  else
693  wxMessageBox( wxString::Format( _( "All %u link(s) resolved" ), fixesCount ) );
694 }
695 
696 
698 {
699 #if 0
700  // Use dialog symbol selector to choose a symbol
702  SCH_BASE_FRAME::COMPONENT_SELECTION sel =
703  m_frame->SelectComponentFromLibrary( NULL, dummy, true, 0, 0, false );
704 #else
705  // Use library viewer to choose a symbol
706  LIB_ID aPreselectedLibid;
707  wxString current = m_grid->GetCellValue( aRow, COL_NEW_LIBID );
708 
709  if( current.IsEmpty() )
710  current = m_grid->GetCellValue( aRow, COL_CURR_LIBID );
711 
712  if( !current.IsEmpty() )
713  aPreselectedLibid.Parse( current, LIB_ID::ID_SCH, true );
714 
715  COMPONENT_SELECTION sel =
716  m_parent->SelectComponentFromLibBrowser( this, NULL, aPreselectedLibid, 0, 0 );
717 #endif
718 
719  if( sel.LibId.empty() ) // command aborted
720  return false;
721 
722  if( !sel.LibId.IsValid() ) // Should not occur
723  {
724  wxMessageBox( _( "Invalid symbol library identifier" ) );
725  return false;
726  }
727 
728  wxString new_libid;
729  new_libid = sel.LibId.Format();
730 
731  m_grid->SetCellValue( aRow, COL_NEW_LIBID, new_libid );
732 
733  return true;
734 }
735 
736 
738 {
739  if( !validateLibIds() )
740  return false;
741 
742  bool change = false;
743  int row_max = m_grid->GetNumberRows() - 1;
744 
745  for( int row = 0; row <= row_max; row++ )
746  {
747  wxString new_libid = m_grid->GetCellValue( row, COL_NEW_LIBID );
748 
749  if( new_libid.IsEmpty() || new_libid == m_grid->GetCellValue( row, COL_CURR_LIBID ) )
750  continue;
751 
752  // A new lib id is found and was already validated.
753  LIB_ID id;
754  id.Parse( new_libid, LIB_ID::ID_SCH, true );
755 
756  for( CMP_CANDIDATE& cmp : m_components )
757  {
758  if( cmp.m_Row != row )
759  continue;
760 
761  SCH_FIELD* value = cmp.m_Component->GetField( VALUE );
762 
763  // If value is a proxy for the itemName then make sure it gets updated
764  if( cmp.m_Component->GetLibId().GetLibItemName().wx_str() == value->GetText() )
765  value->SetText( id.GetLibItemName().wx_str() );
766 
767  cmp.m_Component->SetLibId( id );
768  change = true;
769  cmp.m_Screen->SetModify();
770  }
771  }
772 
773  if( change )
774  {
775  m_isModified = true;
776  SCH_SCREENS schematic;
777  schematic.UpdateSymbolLinks( true );
778  }
779 
780  return true;
781 }
782 
783 
785 {
786  bool change = false;
787  int row_max = m_grid->GetNumberRows() - 1;
788 
789  for( int row = 0; row <= row_max; row++ )
790  {
791  for( CMP_CANDIDATE& cmp : m_components )
792  {
793  if( cmp.m_Row != row )
794  continue;
795 
796  LIB_ID id;
797  id.Parse( cmp.m_InitialLibId, LIB_ID::ID_SCH, true );
798 
799  if( cmp.m_Component->GetLibId() != id )
800  {
801  cmp.m_Component->SetLibId( id );
802  change = true;
803  }
804  }
805  }
806 
807  if( change )
808  {
809  SCH_SCREENS schematic;
810  schematic.UpdateSymbolLinks( true );
811  m_parent->GetCanvas()->Refresh();
812  }
813 }
814 
815 
817 {
818  // Account for scroll bars
819  aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
820 
821  int colWidth = aWidth / 3;
822 
823  m_grid->SetColSize( COL_REFS, colWidth );
824  aWidth -= colWidth;
825 
826  colWidth = 0;
827  for( int row = 0; row < m_grid->GetNumberRows(); ++row )
828  {
829  wxString cellValue = m_grid->GetCellValue( row, COL_CURR_LIBID );
830  colWidth = std::max( colWidth, GetTextSize( cellValue, m_grid ).x );
831  }
832 
833  colWidth += 20;
834  m_grid->SetColSize( COL_CURR_LIBID, colWidth );
835  aWidth -= colWidth;
836 
837  colWidth = 0;
838  for( int row = 0; row < m_grid->GetNumberRows(); ++row )
839  {
840  wxString cellValue = m_grid->GetCellValue( row, COL_NEW_LIBID );
841  colWidth = std::max( colWidth, GetTextSize( cellValue, m_grid ).x );
842  }
843 
844  colWidth += 20;
845  m_grid->SetColSize( COL_NEW_LIBID, std::max( colWidth, aWidth ) );
846 }
847 
848 
850 {
851  AdjustGridColumns( event.GetSize().GetX() );
852 
853  wxClientDC dc( this );
854 
855  // wxWidgets' AutoRowHeight fails when used with wxGridCellAutoWrapStringRenderer
856  for( int row = 0; row < m_grid->GetNumberRows(); ++row )
857  m_grid->SetRowSize( row, m_autoWrapRenderer->GetHeight( dc, m_grid, row, COL_REFS ) );
858 
859  event.Skip();
860 }
861 
862 
864 {
865  // This dialog itself subsequently can invoke a KIWAY_PLAYER as a quasimodal
866  // frame. Therefore this dialog as a modal frame parent, MUST be run under
867  // quasimodal mode for the quasimodal frame support to work. So don't use
868  // the QUASIMODAL macros here.
869  DIALOG_EDIT_COMPONENTS_LIBID dlg( aCaller );
870  // DO NOT use ShowModal() here, otherwise the library browser will not work
871  // properly.
872  dlg.ShowQuasiModal();
873 
874  return dlg.IsSchematicModified();
875 }
DIALOG_EDIT_COMPONENTS_LIBID(SCH_EDIT_FRAME *aParent)
SCH_SHEET_LIST.
void revertChanges()
Reverts all changes already made.
SCH_FIELD instances are attached to a component and provide a place for the component's value,...
Definition: sch_field.h:52
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
std::vector< CMP_CANDIDATE > m_components
const SCH_SHEET_PATH & GetSheetPath() const
wxCoord BreakWord(wxDC &dc, const wxString &word, wxCoord maxWidth, wxArrayString &lines, wxString &line)
bool setLibIdByBrowser(int aRow)
run the lib browser and set the selected LIB_ID for row aRow
SCH_COMPONENT * GetComp() const
void OnSizeGrid(wxSizeEvent &event) override
GRIDCELL_AUTOWRAP_STRINGRENDERER * m_autoWrapRenderer
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
void BreakLine(wxDC &dc, const wxString &logicalLine, wxCoord maxWidth, wxArrayString &lines)
GRID_TRICKS is used to add mouse and command handling (such as cut, copy, and paste) to a WX_GRID ins...
Definition: grid_tricks.h:51
std::vector< COMPONENT_SELECTION > HISTORY_LIST
Schematic editor (Eeschema) main window.
wxArrayString GetTextLines(wxGrid &grid, wxDC &dc, const wxGridCellAttr &attr, const wxRect &rect, int row, int col)
#define COL_CURR_LIBID
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
bool IsValid() const
Definition: lib_id.h:171
bool empty() const
Definition: lib_id.h:186
SCH_REFERENCE_LIST is used to create a flattened list of components because in a complex hierarchy,...
wxSize GetTextSize(const wxString &aSingleLine, wxWindow *aWindow)
Return the size of aSingleLine of text when it is rendered in aWindow using whatever font is currentl...
Definition: common.cpp:263
CMP_CANDIDATE(SCH_COMPONENT *aComponent)
#define GRID_CELL_MARGIN
DIALOG_EDIT_COMPONENTS_LIBID is a dialog to globally edit the LIB_ID of groups if components having t...
#define VALUE
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:111
#define NULL
int ShowQuasiModal()
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
int GetHeight(wxDC &aDC, wxGrid *aGrid, int aRow, int aCol)
static bool sort_by_libid(const CMP_CANDIDATE &cmp1, const CMP_CANDIDATE &cmp2)
void onCancel(wxCommandEvent &event) override
bool IsQuasiModal()
Definition: dialog_shim.h:124
bool InvokeDialogEditComponentsLibId(SCH_EDIT_FRAME *aCaller)
Run a dialog to modify the LIB_ID of components for instance when a symbol has moved from a symbol li...
SCH_SHEET_PATH.
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:172
UTF8 Format() const
Definition: lib_id.cpp:237
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...
COMPONENT_SELECTION SelectComponentFromLibBrowser(wxTopLevelWindow *aParent, const SCHLIB_FILTER *aFilter, const LIB_ID &aPreselectedLibid, int aUnit, int aConvert)
Function SelectComponentFromLibBrowser Calls the library viewer to select component to import into sc...
Definition: getpart.cpp:50
wxGridCellRenderer * Clone() const override
void EndQuasiModal(int retCode)
void onCellBrowseLib(wxGridEvent &event) override
void onApplyButton(wxCommandEvent &event) override
see class PGM_BASE
void UpdateSymbolLinks(bool aForce=false)
Initialize or reinitialize the weak reference to the LIB_PART for each SCH_COMPONENT found in the ful...
SCH_SCREEN * LastScreen()
Function LastScreen.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
#define _(s)
Definition: 3d_actions.cpp:33
#define COL_NEW_LIBID
void AddRowToGrid(bool aMarkRow, const wxString &aReferences, const wxString &aStrLibId)
Add a new row (new entry) in m_grid.
SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:99
Class DIALOG_EDIT_COMPONENTS_LIBID_BASE.
wxString GetUniStringLibId() const
Definition: lib_id.h:148
SCH_SHEET * g_RootSheet
void GetComponents(SCH_REFERENCE_LIST &aReferences, bool aIncludePowerSymbols=true, bool aForceIncludeOrphanComponents=false)
Function GetComponents adds a SCH_REFERENCE() object to aReferences for each component in the list of...
int Parse(const UTF8 &aId, LIB_ID_TYPE aType, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:122
unsigned GetCount()
Function GetCount.
const LIB_ID & GetLibId() const
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
void onUndoChangesButton(wxCommandEvent &event) override
void onClickOrphansButton(wxCommandEvent &event) override
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:123
SCH_REFERENCE is used as a helper to define a component's reference designator in a schematic.
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:491
void updateUIChangesButton(wxUpdateUIEvent &event) override
bool validateLibIds()
returns true if all new lib id are valid