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