KiCad PCB EDA Suite
PIN_TABLE_DATA_MODEL Class Reference
Inheritance diagram for PIN_TABLE_DATA_MODEL:

Public Member Functions

 PIN_TABLE_DATA_MODEL (SYMBOL_EDIT_FRAME *aFrame)
 
 ~PIN_TABLE_DATA_MODEL ()
 
void onUnitsChanged (wxCommandEvent &aEvent)
 
int GetNumberRows () override
 
int GetNumberCols () override
 
wxString GetColLabelValue (int aCol) override
 
bool IsEmptyCell (int row, int col) override
 
wxString GetValue (int aRow, int aCol) override
 
void SetValue (int aRow, int aCol, const wxString &aValue) override
 
void RebuildRows (const LIB_PINS &aPins, bool groupByName)
 
void SortRows (int aSortCol, bool ascending)
 
void SortPins (LIB_PINS &aRow)
 
void AppendRow (LIB_PIN *aPin)
 
LIB_PINS RemoveRow (int aRow)
 
bool IsEdited ()
 

Static Public Member Functions

static wxString GetValue (const LIB_PINS &pins, int aCol, EDA_UNITS aUserUnits)
 
static int findRow (const std::vector< LIB_PINS > &aRowSet, const wxString &aName)
 
static bool compare (const LIB_PINS &lhs, const LIB_PINS &rhs, int sortCol, bool ascending, EDA_UNITS units)
 

Static Private Member Functions

static wxString StringFromBool (bool aValue)
 
static bool BoolFromString (wxString aValue)
 

Private Attributes

SYMBOL_EDIT_FRAMEm_frame
 
std::vector< LIB_PINSm_rows
 
bool m_edited
 

Detailed Description

Definition at line 37 of file dialog_lib_edit_pin_table.cpp.

Constructor & Destructor Documentation

◆ PIN_TABLE_DATA_MODEL()

PIN_TABLE_DATA_MODEL::PIN_TABLE_DATA_MODEL ( SYMBOL_EDIT_FRAME aFrame)
inline

Definition at line 40 of file dialog_lib_edit_pin_table.cpp.

40  :
41  m_frame( aFrame ),
42  m_edited( false )
43  {
44  m_frame->Bind( UNITS_CHANGED, &PIN_TABLE_DATA_MODEL::onUnitsChanged, this );
45  }
void onUnitsChanged(wxCommandEvent &aEvent)

References m_frame, and onUnitsChanged().

◆ ~PIN_TABLE_DATA_MODEL()

PIN_TABLE_DATA_MODEL::~PIN_TABLE_DATA_MODEL ( )
inline

Definition at line 47 of file dialog_lib_edit_pin_table.cpp.

48  {
49  m_frame->Unbind( UNITS_CHANGED, &PIN_TABLE_DATA_MODEL::onUnitsChanged, this );
50  }
void onUnitsChanged(wxCommandEvent &aEvent)

References m_frame, and onUnitsChanged().

Member Function Documentation

◆ AppendRow()

void PIN_TABLE_DATA_MODEL::AppendRow ( LIB_PIN aPin)
inline

Definition at line 365 of file dialog_lib_edit_pin_table.cpp.

366  {
367  LIB_PINS row;
368  row.push_back( aPin );
369  m_rows.push_back( row );
370 
371  if ( GetView() )
372  {
373  wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
374  GetView()->ProcessTableMessage( msg );
375  }
376  }
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:55
std::vector< LIB_PINS > m_rows

References m_rows.

Referenced by DIALOG_LIB_EDIT_PIN_TABLE::OnAddRow().

◆ BoolFromString()

static bool PIN_TABLE_DATA_MODEL::BoolFromString ( wxString  aValue)
inlinestaticprivate

Definition at line 407 of file dialog_lib_edit_pin_table.cpp.

408  {
409  if( aValue == wxT( "1" ) )
410  {
411  return true;
412  }
413  else if( aValue == wxT( "0" ) )
414  {
415  return false;
416  }
417  else
418  {
419  wxFAIL_MSG( wxString::Format( wxT( "string '%s' can't be converted to boolean " )
420  wxT( "correctly, it will have been perceived as FALSE" ),
421  aValue ) );
422  return false;
423  }
424  }
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

References Format().

Referenced by SetValue().

◆ compare()

static bool PIN_TABLE_DATA_MODEL::compare ( const LIB_PINS lhs,
const LIB_PINS rhs,
int  sortCol,
bool  ascending,
EDA_UNITS  units 
)
inlinestatic

Definition at line 245 of file dialog_lib_edit_pin_table.cpp.

247  {
248  wxString lhStr = GetValue( lhs, sortCol, units );
249  wxString rhStr = GetValue( rhs, sortCol, units );
250 
251  if( lhStr == rhStr )
252  {
253  // Secondary sort key is always COL_NUMBER
254  sortCol = COL_NUMBER;
255  lhStr = GetValue( lhs, sortCol, units );
256  rhStr = GetValue( rhs, sortCol, units );
257  }
258 
259  bool res;
260 
261  // N.B. To meet the iterator sort conditions, we cannot simply invert the truth
262  // to get the opposite sort. i.e. ~(a<b) != (a>b)
263  auto cmp = [ ascending ]( const auto a, const auto b )
264  {
265  if( ascending )
266  return a < b;
267  else
268  return b < a;
269  };
270 
271  switch( sortCol )
272  {
273  case COL_NUMBER:
274  case COL_NAME:
275  res = cmp( PIN_NUMBERS::Compare( lhStr, rhStr ), 0 );
276  break;
277  case COL_NUMBER_SIZE:
278  case COL_NAME_SIZE:
279  res = cmp( ValueFromString( units, lhStr ), ValueFromString( units, rhStr ) );
280  break;
281  case COL_LENGTH:
282  case COL_POSX:
283  case COL_POSY:
284  res = cmp( ValueFromString( units, lhStr ), ValueFromString( units, rhStr ) );
285  break;
286  case COL_VISIBLE:
287  default:
288  res = cmp( StrNumCmp( lhStr, rhStr ), 0 );
289  break;
290  }
291 
292  return res;
293  }
static int Compare(const wxString &lhs, const wxString &rhs)
long long int ValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application.
Definition: base_units.cpp:411
wxString GetValue(int aRow, int aCol) override
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.

References COL_LENGTH, COL_NAME, COL_NAME_SIZE, COL_NUMBER, COL_NUMBER_SIZE, COL_POSX, COL_POSY, COL_VISIBLE, PIN_NUMBERS::Compare(), GetValue(), StrNumCmp(), and ValueFromString().

Referenced by SortRows().

◆ findRow()

static int PIN_TABLE_DATA_MODEL::findRow ( const std::vector< LIB_PINS > &  aRowSet,
const wxString &  aName 
)
inlinestatic

Definition at line 234 of file dialog_lib_edit_pin_table.cpp.

235  {
236  for( size_t i = 0; i < aRowSet.size(); ++i )
237  {
238  if( aRowSet[ i ][ 0 ] && aRowSet[ i ][ 0 ]->GetName() == aName )
239  return i;
240  }
241 
242  return -1;
243  }

Referenced by RebuildRows().

◆ GetColLabelValue()

wxString PIN_TABLE_DATA_MODEL::GetColLabelValue ( int  aCol)
inlineoverride

Definition at line 64 of file dialog_lib_edit_pin_table.cpp.

65  {
66  switch( aCol )
67  {
68  case COL_NUMBER: return _( "Number" );
69  case COL_NAME: return _( "Name" );
70  case COL_TYPE: return _( "Electrical Type" );
71  case COL_SHAPE: return _( "Graphic Style" );
72  case COL_ORIENTATION: return _( "Orientation" );
73  case COL_NUMBER_SIZE: return _( "Number Text Size" );
74  case COL_NAME_SIZE: return _( "Name Text Size" );
75  case COL_LENGTH: return _( "Length" );
76  case COL_POSX: return _( "X Position" );
77  case COL_POSY: return _( "Y Position" );
78  case COL_VISIBLE: return _( "Visible" );
79  default: wxFAIL; return wxEmptyString;
80  }
81  }
#define _(s)

References _, COL_LENGTH, COL_NAME, COL_NAME_SIZE, COL_NUMBER, COL_NUMBER_SIZE, COL_ORIENTATION, COL_POSX, COL_POSY, COL_SHAPE, COL_TYPE, and COL_VISIBLE.

◆ GetNumberCols()

int PIN_TABLE_DATA_MODEL::GetNumberCols ( )
inlineoverride

Definition at line 62 of file dialog_lib_edit_pin_table.cpp.

References COL_COUNT.

◆ GetNumberRows()

int PIN_TABLE_DATA_MODEL::GetNumberRows ( )
inlineoverride

Definition at line 61 of file dialog_lib_edit_pin_table.cpp.

61 { return (int) m_rows.size(); }
std::vector< LIB_PINS > m_rows

References m_rows.

◆ GetValue() [1/2]

wxString PIN_TABLE_DATA_MODEL::GetValue ( int  aRow,
int  aCol 
)
inlineoverride

Definition at line 88 of file dialog_lib_edit_pin_table.cpp.

89  {
90  return GetValue( m_rows[ aRow ], aCol, m_frame->GetUserUnits() );
91  }
wxString GetValue(int aRow, int aCol) override
std::vector< LIB_PINS > m_rows
EDA_UNITS GetUserUnits() const
Return the user units currently in use.

References EDA_BASE_FRAME::GetUserUnits(), m_frame, and m_rows.

Referenced by compare().

◆ GetValue() [2/2]

static wxString PIN_TABLE_DATA_MODEL::GetValue ( const LIB_PINS pins,
int  aCol,
EDA_UNITS  aUserUnits 
)
inlinestatic

Definition at line 93 of file dialog_lib_edit_pin_table.cpp.

94  {
95  wxString fieldValue;
96 
97  if( pins.empty() )
98  return fieldValue;
99 
100  for( LIB_PIN* pin : pins )
101  {
102  wxString val;
103 
104  switch( aCol )
105  {
106  case COL_NUMBER:
107  val = pin->GetNumber();
108  break;
109  case COL_NAME:
110  val = pin->GetName();
111  break;
112  case COL_TYPE:
113  val = PinTypeNames()[static_cast<int>( pin->GetType() )];
114  break;
115  case COL_SHAPE:
116  val = PinShapeNames()[static_cast<int>( pin->GetShape() )];
117  break;
118  case COL_ORIENTATION:
119  if( PinOrientationIndex( pin->GetOrientation() ) >= 0 )
120  val = PinOrientationNames()[ PinOrientationIndex( pin->GetOrientation() ) ];
121  break;
122  case COL_NUMBER_SIZE:
123  val = StringFromValue( aUserUnits, pin->GetNumberTextSize(), true );
124  break;
125  case COL_NAME_SIZE:
126  val = StringFromValue( aUserUnits, pin->GetNameTextSize(), true );
127  break;
128  case COL_LENGTH:
129  val = StringFromValue( aUserUnits, pin->GetLength(), true );
130  break;
131  case COL_POSX:
132  val = StringFromValue( aUserUnits, pin->GetPosition().x, true );
133  break;
134  case COL_POSY:
135  val = StringFromValue( aUserUnits, pin->GetPosition().y, true );
136  break;
137  case COL_VISIBLE:
138  val = StringFromBool( pin->IsVisible() );
139  break;
140  default:
141  wxFAIL;
142  break;
143  }
144 
145  if( aCol == COL_NUMBER )
146  {
147  if( fieldValue.length() )
148  fieldValue += wxT( ", " );
149  fieldValue += val;
150  }
151  else
152  {
153  if( !fieldValue.Length() )
154  fieldValue = val;
155  else if( val != fieldValue )
156  fieldValue = INDETERMINATE_STATE;
157  }
158  }
159 
160  return fieldValue;
161  }
int PinOrientationIndex(int code)
Definition: pin_type.cpp:150
const wxArrayString & PinShapeNames()
Definition: pin_type.cpp:204
const wxArrayString & PinTypeNames()
Definition: pin_type.cpp:186
const wxArrayString & PinOrientationNames()
Definition: pin_type.cpp:222
wxString StringFromValue(EDA_UNITS aUnits, double aValue, bool aAddUnitSymbol, EDA_DATA_TYPE aType)
Convert a value to a string using double notation.
Definition: base_units.cpp:204
#define INDETERMINATE_STATE
Used for holding indeterminate values, such as with multiple selections holding different values or c...
Definition: base_units.h:47
static wxString StringFromBool(bool aValue)

References COL_LENGTH, COL_NAME, COL_NAME_SIZE, COL_NUMBER, COL_NUMBER_SIZE, COL_ORIENTATION, COL_POSX, COL_POSY, COL_SHAPE, COL_TYPE, COL_VISIBLE, INDETERMINATE_STATE, pin, PinOrientationIndex(), PinOrientationNames(), PinShapeNames(), PinTypeNames(), StringFromBool(), and StringFromValue().

◆ IsEdited()

bool PIN_TABLE_DATA_MODEL::IsEdited ( )
inline

Definition at line 393 of file dialog_lib_edit_pin_table.cpp.

394  {
395  return m_edited;
396  }

References m_edited.

Referenced by DIALOG_LIB_EDIT_PIN_TABLE::OnClose().

◆ IsEmptyCell()

bool PIN_TABLE_DATA_MODEL::IsEmptyCell ( int  row,
int  col 
)
inlineoverride

Definition at line 83 of file dialog_lib_edit_pin_table.cpp.

84  {
85  return false; // don't allow adjacent cell overflow, even if we are actually empty
86  }

◆ onUnitsChanged()

void PIN_TABLE_DATA_MODEL::onUnitsChanged ( wxCommandEvent &  aEvent)
inline

Definition at line 52 of file dialog_lib_edit_pin_table.cpp.

53  {
54  if( GetView() )
55  GetView()->ForceRefresh();
56 
57  aEvent.Skip();
58  }

Referenced by PIN_TABLE_DATA_MODEL(), and ~PIN_TABLE_DATA_MODEL().

◆ RebuildRows()

void PIN_TABLE_DATA_MODEL::RebuildRows ( const LIB_PINS aPins,
bool  groupByName 
)
inline

Definition at line 295 of file dialog_lib_edit_pin_table.cpp.

296  {
297  if ( GetView() )
298  {
299  // Commit any pending in-place edits before the row gets moved out from under
300  // the editor.
301  if( auto grid = dynamic_cast<WX_GRID*>( GetView() ) )
302  grid->CommitPendingChanges( true );
303 
304  wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, 0, m_rows.size() );
305  GetView()->ProcessTableMessage( msg );
306  }
307 
308  m_rows.clear();
309 
310  for( LIB_PIN* pin : aPins )
311  {
312  int rowIndex = -1;
313 
314  if( groupByName )
315  rowIndex = findRow( m_rows, pin->GetName() );
316 
317  if( rowIndex < 0 )
318  {
319  m_rows.emplace_back( LIB_PINS() );
320  rowIndex = m_rows.size() - 1;
321  }
322 
323  m_rows[ rowIndex ].push_back( pin );
324  }
325 
326  int sortCol = 0;
327  bool ascending = true;
328 
329  if( GetView() && GetView()->GetSortingColumn() != wxNOT_FOUND )
330  {
331  sortCol = GetView()->GetSortingColumn();
332  ascending = GetView()->IsSortOrderAscending();
333  }
334 
335  for( LIB_PINS& row : m_rows )
336  SortPins( row );
337 
338  SortRows( sortCol, ascending );
339 
340  if ( GetView() )
341  {
342  wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, m_rows.size() );
343  GetView()->ProcessTableMessage( msg );
344  }
345  }
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:55
void SortRows(int aSortCol, bool ascending)
static int findRow(const std::vector< LIB_PINS > &aRowSet, const wxString &aName)
std::vector< LIB_PINS > m_rows

References findRow(), grid, m_rows, pin, SortPins(), and SortRows().

Referenced by DIALOG_LIB_EDIT_PIN_TABLE::OnRebuildRows(), and DIALOG_LIB_EDIT_PIN_TABLE::TransferDataToWindow().

◆ RemoveRow()

LIB_PINS PIN_TABLE_DATA_MODEL::RemoveRow ( int  aRow)
inline

Definition at line 378 of file dialog_lib_edit_pin_table.cpp.

379  {
380  LIB_PINS removedRow = m_rows[ aRow ];
381 
382  m_rows.erase( m_rows.begin() + aRow );
383 
384  if ( GetView() )
385  {
386  wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aRow, 1 );
387  GetView()->ProcessTableMessage( msg );
388  }
389 
390  return removedRow;
391  }
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:55
std::vector< LIB_PINS > m_rows

References m_rows.

Referenced by DIALOG_LIB_EDIT_PIN_TABLE::OnDeleteRow().

◆ SetValue()

void PIN_TABLE_DATA_MODEL::SetValue ( int  aRow,
int  aCol,
const wxString &  aValue 
)
inlineoverride

Definition at line 163 of file dialog_lib_edit_pin_table.cpp.

164  {
165  if( aValue == INDETERMINATE_STATE )
166  return;
167 
168  LIB_PINS pins = m_rows[ aRow ];
169 
170  for( LIB_PIN* pin : pins )
171  {
172  switch( aCol )
173  {
174  case COL_NUMBER:
175  if( pins.size() == 1 )
176  pin->SetNumber( aValue );
177  break;
178 
179  case COL_NAME:
180  pin->SetName( aValue );
181  break;
182 
183  case COL_TYPE:
184  if( PinTypeNames().Index( aValue ) != wxNOT_FOUND )
185  pin->SetType( (ELECTRICAL_PINTYPE) PinTypeNames().Index( aValue ) );
186 
187  break;
188 
189  case COL_SHAPE:
190  if( PinShapeNames().Index( aValue ) != wxNOT_FOUND )
191  pin->SetShape( (GRAPHIC_PINSHAPE) PinShapeNames().Index( aValue ) );
192 
193  break;
194 
195  case COL_ORIENTATION:
196  if( PinOrientationNames().Index( aValue ) != wxNOT_FOUND )
197  pin->SetOrientation( PinOrientationCode( PinOrientationNames().Index( aValue ) ) );
198  break;
199 
200  case COL_NUMBER_SIZE:
201  pin->SetNumberTextSize( ValueFromString( m_frame->GetUserUnits(), aValue ) );
202  break;
203 
204  case COL_NAME_SIZE:
205  pin->SetNameTextSize( ValueFromString( m_frame->GetUserUnits(), aValue ) );
206  break;
207 
208  case COL_LENGTH:
209  pin->SetLength( ValueFromString( m_frame->GetUserUnits(), aValue ) );
210  break;
211 
212  case COL_POSX:
213  pin->SetPosition( wxPoint( ValueFromString( m_frame->GetUserUnits(), aValue ),
214  pin->GetPosition().y ) );
215  break;
216 
217  case COL_POSY:
218  pin->SetPosition( wxPoint( pin->GetPosition().x,
219  ValueFromString( m_frame->GetUserUnits(), aValue ) ) );
220  break;
221 
222  case COL_VISIBLE:
223  pin->SetVisible(BoolFromString( aValue ));
224  break;
225  default:
226  wxFAIL;
227  break;
228  }
229  }
230 
231  m_edited = true;
232  }
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:55
int PinOrientationCode(int index)
Definition: pin_type.cpp:141
GRAPHIC_PINSHAPE
Definition: pin_type.h:55
const wxArrayString & PinShapeNames()
Definition: pin_type.cpp:204
static bool BoolFromString(wxString aValue)
long long int ValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application.
Definition: base_units.cpp:411
const wxArrayString & PinTypeNames()
Definition: pin_type.cpp:186
const wxArrayString & PinOrientationNames()
Definition: pin_type.cpp:222
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition: pin_type.h:35
#define INDETERMINATE_STATE
Used for holding indeterminate values, such as with multiple selections holding different values or c...
Definition: base_units.h:47
std::vector< LIB_PINS > m_rows
EDA_UNITS GetUserUnits() const
Return the user units currently in use.

References BoolFromString(), COL_LENGTH, COL_NAME, COL_NAME_SIZE, COL_NUMBER, COL_NUMBER_SIZE, COL_ORIENTATION, COL_POSX, COL_POSY, COL_SHAPE, COL_TYPE, COL_VISIBLE, EDA_BASE_FRAME::GetUserUnits(), INDETERMINATE_STATE, m_edited, m_frame, m_rows, pin, PinOrientationCode(), PinOrientationNames(), PinShapeNames(), PinTypeNames(), and ValueFromString().

◆ SortPins()

void PIN_TABLE_DATA_MODEL::SortPins ( LIB_PINS aRow)
inline

Definition at line 356 of file dialog_lib_edit_pin_table.cpp.

357  {
358  std::sort( aRow.begin(), aRow.end(),
359  []( LIB_PIN* lhs, LIB_PIN* rhs ) -> bool
360  {
361  return PIN_NUMBERS::Compare( lhs->GetNumber(), rhs->GetNumber() ) < 0;
362  } );
363  }
static int Compare(const wxString &lhs, const wxString &rhs)
const wxString & GetNumber() const
Definition: lib_pin.h:116

References PIN_NUMBERS::Compare(), and LIB_PIN::GetNumber().

Referenced by RebuildRows().

◆ SortRows()

void PIN_TABLE_DATA_MODEL::SortRows ( int  aSortCol,
bool  ascending 
)
inline

Definition at line 347 of file dialog_lib_edit_pin_table.cpp.

348  {
349  std::sort( m_rows.begin(), m_rows.end(),
350  [ aSortCol, ascending, this ]( const LIB_PINS& lhs, const LIB_PINS& rhs ) -> bool
351  {
352  return compare( lhs, rhs, aSortCol, ascending, m_frame->GetUserUnits() );
353  } );
354  }
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:55
static bool compare(const LIB_PINS &lhs, const LIB_PINS &rhs, int sortCol, bool ascending, EDA_UNITS units)
std::vector< LIB_PINS > m_rows
EDA_UNITS GetUserUnits() const
Return the user units currently in use.

References compare(), EDA_BASE_FRAME::GetUserUnits(), m_frame, and m_rows.

Referenced by DIALOG_LIB_EDIT_PIN_TABLE::OnColSort(), and RebuildRows().

◆ StringFromBool()

static wxString PIN_TABLE_DATA_MODEL::StringFromBool ( bool  aValue)
inlinestaticprivate

Definition at line 399 of file dialog_lib_edit_pin_table.cpp.

400  {
401  if( aValue )
402  return wxT( "1" );
403  else
404  return wxT( "0" );
405  }

Referenced by GetValue().

Member Data Documentation

◆ m_edited

bool PIN_TABLE_DATA_MODEL::m_edited
private

Definition at line 434 of file dialog_lib_edit_pin_table.cpp.

Referenced by IsEdited(), and SetValue().

◆ m_frame

SYMBOL_EDIT_FRAME* PIN_TABLE_DATA_MODEL::m_frame
private

◆ m_rows

std::vector<LIB_PINS> PIN_TABLE_DATA_MODEL::m_rows
private

The documentation for this class was generated from the following file: