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