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

Public Member Functions

 PIN_TABLE_DATA_MODEL (EDA_UNITS aUserUnits)
 
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

std::vector< LIB_PINSm_rows
 
EDA_UNITS m_userUnits
 
bool m_edited
 

Detailed Description

Definition at line 38 of file dialog_lib_edit_pin_table.cpp.

Constructor & Destructor Documentation

◆ PIN_TABLE_DATA_MODEL()

PIN_TABLE_DATA_MODEL::PIN_TABLE_DATA_MODEL ( EDA_UNITS  aUserUnits)
inline

Definition at line 41 of file dialog_lib_edit_pin_table.cpp.

41  : m_userUnits( aUserUnits ), m_edited( false )
42  {
43  }

Member Function Documentation

◆ AppendRow()

void PIN_TABLE_DATA_MODEL::AppendRow ( LIB_PIN aPin)
inline

Definition at line 349 of file dialog_lib_edit_pin_table.cpp.

350  {
351  LIB_PINS row;
352  row.push_back( aPin );
353  m_rows.push_back( row );
354 
355  if ( GetView() )
356  {
357  wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
358  GetView()->ProcessTableMessage( msg );
359  }
360  }
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:56
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 391 of file dialog_lib_edit_pin_table.cpp.

392  {
393  if( aValue == "1" )
394  {
395  return true;
396  }
397  else if( aValue == "0" )
398  {
399  return false;
400  }
401  else
402  {
403  wxFAIL_MSG( wxString::Format( "string \"%s\" can't be converted to boolean "
404  "correctly, it will have been perceived as FALSE",
405  aValue ) );
406  return false;
407  }
408  }
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 228 of file dialog_lib_edit_pin_table.cpp.

230  {
231  wxString lhStr = GetValue( lhs, sortCol, units );
232  wxString rhStr = GetValue( rhs, sortCol, units );
233 
234  if( lhStr == rhStr )
235  {
236  // Secondary sort key is always COL_NUMBER
237  sortCol = COL_NUMBER;
238  lhStr = GetValue( lhs, sortCol, units );
239  rhStr = GetValue( rhs, sortCol, units );
240  }
241 
242  bool res;
243 
244  // N.B. To meet the iterator sort conditions, we cannot simply invert the truth
245  // to get the opposite sort. i.e. ~(a<b) != (a>b)
246  auto cmp = [ ascending ]( const auto a, const auto b )
247  {
248  if( ascending )
249  return a < b;
250  else
251  return b < a;
252  };
253 
254  switch( sortCol )
255  {
256  case COL_NUMBER:
257  case COL_NAME:
258  res = cmp( PinNumbers::Compare( lhStr, rhStr ), 0 );
259  break;
260  case COL_NUMBER_SIZE:
261  case COL_NAME_SIZE:
262  res = cmp( ValueFromString( units, lhStr ),
263  ValueFromString( units, rhStr ) );
264  break;
265  case COL_LENGTH:
266  case COL_POSX:
267  case COL_POSY:
268  res = cmp( ValueFromString( units, lhStr ), ValueFromString( units, rhStr ) );
269  break;
270  case COL_VISIBLE:
271  default:
272  res = cmp( StrNumCmp( lhStr, rhStr ), 0 );
273  break;
274  }
275 
276  return res;
277  }
static int Compare(PinNumber const &lhs, PinNumber const &rhs)
Definition: pin_number.cpp:111
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
Definition: string.cpp:439
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:437
wxString GetValue(int aRow, int aCol) override

References COL_LENGTH, COL_NAME, COL_NAME_SIZE, COL_NUMBER, COL_NUMBER_SIZE, COL_POSX, COL_POSY, COL_VISIBLE, PinNumbers::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 217 of file dialog_lib_edit_pin_table.cpp.

218  {
219  for( size_t i = 0; i < aRowSet.size(); ++i )
220  {
221  if( aRowSet[ i ][ 0 ] && aRowSet[ i ][ 0 ]->GetName() == aName )
222  return i;
223  }
224 
225  return -1;
226  }

Referenced by RebuildRows().

◆ GetColLabelValue()

wxString PIN_TABLE_DATA_MODEL::GetColLabelValue ( int  aCol)
inlineoverride

Definition at line 48 of file dialog_lib_edit_pin_table.cpp.

49  {
50  switch( aCol )
51  {
52  case COL_NUMBER: return _( "Number" );
53  case COL_NAME: return _( "Name" );
54  case COL_TYPE: return _( "Electrical Type" );
55  case COL_SHAPE: return _( "Graphic Style" );
56  case COL_ORIENTATION: return _( "Orientation" );
57  case COL_NUMBER_SIZE: return _( "Number Text Size" );
58  case COL_NAME_SIZE: return _( "Name Text Size" );
59  case COL_LENGTH: return _( "Length" );
60  case COL_POSX: return _( "X Position" );
61  case COL_POSY: return _( "Y Position" );
62  case COL_VISIBLE: return _( "Visible" );
63  default: wxFAIL; return wxEmptyString;
64  }
65  }
#define _(s)
Definition: 3d_actions.cpp:33

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 46 of file dialog_lib_edit_pin_table.cpp.

References COL_COUNT.

◆ GetNumberRows()

int PIN_TABLE_DATA_MODEL::GetNumberRows ( )
inlineoverride

Definition at line 45 of file dialog_lib_edit_pin_table.cpp.

45 { 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 72 of file dialog_lib_edit_pin_table.cpp.

73  {
74  return GetValue( m_rows[ aRow ], aCol, m_userUnits );
75  }
wxString GetValue(int aRow, int aCol) override
std::vector< LIB_PINS > m_rows

References m_rows, and m_userUnits.

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 77 of file dialog_lib_edit_pin_table.cpp.

78  {
79  wxString fieldValue;
80 
81  if( pins.empty() )
82  return fieldValue;
83 
84  for( LIB_PIN* pin : pins )
85  {
86  wxString val;
87 
88  switch( aCol )
89  {
90  case COL_NUMBER:
91  val = pin->GetNumber();
92  break;
93  case COL_NAME:
94  val = pin->GetName();
95  break;
96  case COL_TYPE:
97  val = PinTypeNames()[static_cast<int>( pin->GetType() )];
98  break;
99  case COL_SHAPE:
100  val = PinShapeNames()[static_cast<int>( pin->GetShape() )];
101  break;
102  case COL_ORIENTATION:
103  if( PinOrientationIndex( pin->GetOrientation() ) >= 0 )
104  val = PinOrientationNames()[ PinOrientationIndex( pin->GetOrientation() ) ];
105  break;
106  case COL_NUMBER_SIZE:
107  val = StringFromValue( aUserUnits, pin->GetNumberTextSize(), true );
108  break;
109  case COL_NAME_SIZE:
110  val = StringFromValue( aUserUnits, pin->GetNameTextSize(), true );
111  break;
112  case COL_LENGTH:
113  val = StringFromValue( aUserUnits, pin->GetLength() );
114  break;
115  case COL_POSX:
116  val = StringFromValue( aUserUnits, pin->GetPosition().x );
117  break;
118  case COL_POSY:
119  val = StringFromValue( aUserUnits, pin->GetPosition().y );
120  break;
121  case COL_VISIBLE:
122  val = StringFromBool( pin->IsVisible() );
123  break;
124  default:
125  wxFAIL;
126  break;
127  }
128 
129  if( aCol == COL_NUMBER )
130  {
131  if( fieldValue.length() )
132  fieldValue += wxT( ", " );
133  fieldValue += val;
134  }
135  else
136  {
137  if( !fieldValue.Length() )
138  fieldValue = val;
139  else if( val != fieldValue )
140  fieldValue = INDETERMINATE_STATE;
141  }
142  }
143 
144  return fieldValue;
145  }
int PinOrientationIndex(int code)
Definition: pin_type.cpp:146
const wxArrayString & PinShapeNames()
Definition: pin_type.cpp:200
const wxArrayString & PinTypeNames()
Definition: pin_type.cpp:182
const wxArrayString & PinOrientationNames()
Definition: pin_type.cpp:218
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:225
#define INDETERMINATE_STATE
Used for holding indeterminate values, such as with multiple selections holding different values or c...
Definition: base_units.h:48
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 377 of file dialog_lib_edit_pin_table.cpp.

378  {
379  return m_edited;
380  }

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 67 of file dialog_lib_edit_pin_table.cpp.

68  {
69  return false; // don't allow adjacent cell overflow, even if we are actually empty
70  }

◆ RebuildRows()

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

Definition at line 279 of file dialog_lib_edit_pin_table.cpp.

280  {
281  if ( GetView() )
282  {
283  // Commit any pending in-place edits before the row gets moved out from under
284  // the editor.
285  if( auto grid = dynamic_cast<WX_GRID*>( GetView() ) )
286  grid->CommitPendingChanges( true );
287 
288  wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, 0, m_rows.size() );
289  GetView()->ProcessTableMessage( msg );
290  }
291 
292  m_rows.clear();
293 
294  for( LIB_PIN* pin : aPins )
295  {
296  int rowIndex = -1;
297 
298  if( groupByName )
299  rowIndex = findRow( m_rows, pin->GetName() );
300 
301  if( rowIndex < 0 )
302  {
303  m_rows.emplace_back( LIB_PINS() );
304  rowIndex = m_rows.size() - 1;
305  }
306 
307  m_rows[ rowIndex ].push_back( pin );
308  }
309 
310  int sortCol = 0;
311  bool ascending = true;
312 
313  if( GetView() && GetView()->GetSortingColumn() != wxNOT_FOUND )
314  {
315  sortCol = GetView()->GetSortingColumn();
316  ascending = GetView()->IsSortOrderAscending();
317  }
318 
319  for( LIB_PINS& row : m_rows )
320  SortPins( row );
321 
322  SortRows( sortCol, ascending );
323 
324  if ( GetView() )
325  {
326  wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, m_rows.size() );
327  GetView()->ProcessTableMessage( msg );
328  }
329  }
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:56
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 362 of file dialog_lib_edit_pin_table.cpp.

363  {
364  LIB_PINS removedRow = m_rows[ aRow ];
365 
366  m_rows.erase( m_rows.begin() + aRow );
367 
368  if ( GetView() )
369  {
370  wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aRow, 1 );
371  GetView()->ProcessTableMessage( msg );
372  }
373 
374  return removedRow;
375  }
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:56
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 147 of file dialog_lib_edit_pin_table.cpp.

148  {
149  if( aValue == INDETERMINATE_STATE )
150  return;
151 
152  LIB_PINS pins = m_rows[ aRow ];
153 
154  for( LIB_PIN* pin : pins )
155  {
156  switch( aCol )
157  {
158  case COL_NUMBER:
159  pin->SetNumber( aValue );
160  break;
161 
162  case COL_NAME:
163  pin->SetName( aValue );
164  break;
165 
166  case COL_TYPE:
167  if( PinTypeNames().Index( aValue ) != wxNOT_FOUND )
168  pin->SetType( (ELECTRICAL_PINTYPE) PinTypeNames().Index( aValue ) );
169 
170  break;
171 
172  case COL_SHAPE:
173  if( PinShapeNames().Index( aValue ) != wxNOT_FOUND )
174  pin->SetShape( (GRAPHIC_PINSHAPE) PinShapeNames().Index( aValue ) );
175 
176  break;
177 
178  case COL_ORIENTATION:
179  if( PinOrientationNames().Index( aValue ) != wxNOT_FOUND )
180  pin->SetOrientation( PinOrientationCode( PinOrientationNames().Index( aValue ) ) );
181  break;
182 
183  case COL_NUMBER_SIZE:
184  pin->SetNumberTextSize( ValueFromString( m_userUnits, aValue ) );
185  break;
186 
187  case COL_NAME_SIZE:
188  pin->SetNameTextSize( ValueFromString( m_userUnits, aValue ) );
189  break;
190 
191  case COL_LENGTH:
192  pin->SetLength( ValueFromString( m_userUnits, aValue ) );
193  break;
194 
195  case COL_POSX:
196  pin->SetPosition( wxPoint( ValueFromString( m_userUnits, aValue ),
197  pin->GetPosition().y ) );
198  break;
199 
200  case COL_POSY:
201  pin->SetPosition( wxPoint( pin->GetPosition().x,
202  ValueFromString( m_userUnits, aValue ) ) );
203  break;
204 
205  case COL_VISIBLE:
206  pin->SetVisible(BoolFromString( aValue ));
207  break;
208  default:
209  wxFAIL;
210  break;
211  }
212  }
213 
214  m_edited = true;
215  }
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:56
int PinOrientationCode(int index)
Definition: pin_type.cpp:137
GRAPHIC_PINSHAPE
Definition: pin_type.h:55
const wxArrayString & PinShapeNames()
Definition: pin_type.cpp:200
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:437
const wxArrayString & PinTypeNames()
Definition: pin_type.cpp:182
const wxArrayString & PinOrientationNames()
Definition: pin_type.cpp:218
ELECTRICAL_PINTYPE
The component 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:48
std::vector< LIB_PINS > m_rows

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, INDETERMINATE_STATE, m_edited, m_rows, m_userUnits, pin, PinOrientationCode(), PinOrientationNames(), PinShapeNames(), PinTypeNames(), and ValueFromString().

◆ SortPins()

void PIN_TABLE_DATA_MODEL::SortPins ( LIB_PINS aRow)
inline

Definition at line 340 of file dialog_lib_edit_pin_table.cpp.

341  {
342  std::sort( aRow.begin(), aRow.end(),
343  []( LIB_PIN* lhs, LIB_PIN* rhs ) -> bool
344  {
345  return PinNumbers::Compare( lhs->GetNumber(), rhs->GetNumber() ) < 0;
346  } );
347  }
static int Compare(PinNumber const &lhs, PinNumber const &rhs)
Definition: pin_number.cpp:111
const wxString & GetNumber() const
Definition: lib_pin.h:117

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

Referenced by RebuildRows().

◆ SortRows()

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

Definition at line 331 of file dialog_lib_edit_pin_table.cpp.

332  {
333  std::sort( m_rows.begin(), m_rows.end(),
334  [ aSortCol, ascending, this ]( const LIB_PINS& lhs, const LIB_PINS& rhs ) -> bool
335  {
336  return compare( lhs, rhs, aSortCol, ascending, m_userUnits );
337  } );
338  }
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:56
static bool compare(const LIB_PINS &lhs, const LIB_PINS &rhs, int sortCol, bool ascending, EDA_UNITS units)
std::vector< LIB_PINS > m_rows

References compare(), m_rows, and m_userUnits.

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

◆ StringFromBool()

static wxString PIN_TABLE_DATA_MODEL::StringFromBool ( bool  aValue)
inlinestaticprivate

Definition at line 383 of file dialog_lib_edit_pin_table.cpp.

384  {
385  if( aValue )
386  return wxT( "1" );
387  else
388  return wxT( "0" );
389  }

Referenced by GetValue().

Member Data Documentation

◆ m_edited

bool PIN_TABLE_DATA_MODEL::m_edited
private

Definition at line 416 of file dialog_lib_edit_pin_table.cpp.

Referenced by IsEdited(), and SetValue().

◆ m_rows

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

◆ m_userUnits

EDA_UNITS PIN_TABLE_DATA_MODEL::m_userUnits
private

Definition at line 415 of file dialog_lib_edit_pin_table.cpp.

Referenced by GetValue(), SetValue(), and SortRows().


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