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

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

Member Function Documentation

◆ AppendRow()

void PIN_TABLE_DATA_MODEL::AppendRow ( LIB_PIN aPin)
inline

Definition at line 348 of file dialog_lib_edit_pin_table.cpp.

349  {
350  LIB_PINS row;
351  row.push_back( aPin );
352  m_rows.push_back( row );
353 
354  if ( GetView() )
355  {
356  wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
357  GetView()->ProcessTableMessage( msg );
358  }
359  }
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 390 of file dialog_lib_edit_pin_table.cpp.

391  {
392  if( aValue == "1" )
393  {
394  return true;
395  }
396  else if( aValue == "0" )
397  {
398  return false;
399  }
400  else
401  {
402  wxFAIL_MSG( wxString::Format( "string '%s' can't be converted to boolean "
403  "correctly, it will have been perceived as FALSE",
404  aValue ) );
405  return false;
406  }
407  }
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( PIN_NUMBERS::Compare( lhStr, rhStr ), 0 );
259  break;
260  case COL_NUMBER_SIZE:
261  case COL_NAME_SIZE:
262  res = cmp( ValueFromString( units, lhStr ), ValueFromString( units, rhStr ) );
263  break;
264  case COL_LENGTH:
265  case COL_POSX:
266  case COL_POSY:
267  res = cmp( ValueFromString( units, lhStr ), ValueFromString( units, rhStr ) );
268  break;
269  case COL_VISIBLE:
270  default:
271  res = cmp( StrNumCmp( lhStr, rhStr ), 0 );
272  break;
273  }
274 
275  return res;
276  }
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:416
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 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 47 of file dialog_lib_edit_pin_table.cpp.

48  {
49  switch( aCol )
50  {
51  case COL_NUMBER: return _( "Number" );
52  case COL_NAME: return _( "Name" );
53  case COL_TYPE: return _( "Electrical Type" );
54  case COL_SHAPE: return _( "Graphic Style" );
55  case COL_ORIENTATION: return _( "Orientation" );
56  case COL_NUMBER_SIZE: return _( "Number Text Size" );
57  case COL_NAME_SIZE: return _( "Name Text Size" );
58  case COL_LENGTH: return _( "Length" );
59  case COL_POSX: return _( "X Position" );
60  case COL_POSY: return _( "Y Position" );
61  case COL_VISIBLE: return _( "Visible" );
62  default: wxFAIL; return wxEmptyString;
63  }
64  }
#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 45 of file dialog_lib_edit_pin_table.cpp.

References COL_COUNT.

◆ GetNumberRows()

int PIN_TABLE_DATA_MODEL::GetNumberRows ( )
inlineoverride

Definition at line 44 of file dialog_lib_edit_pin_table.cpp.

44 { 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 71 of file dialog_lib_edit_pin_table.cpp.

72  {
73  return GetValue( m_rows[ aRow ], aCol, m_userUnits );
74  }
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 76 of file dialog_lib_edit_pin_table.cpp.

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

377  {
378  return m_edited;
379  }

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

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

◆ RebuildRows()

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

Definition at line 278 of file dialog_lib_edit_pin_table.cpp.

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

362  {
363  LIB_PINS removedRow = m_rows[ aRow ];
364 
365  m_rows.erase( m_rows.begin() + aRow );
366 
367  if ( GetView() )
368  {
369  wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aRow, 1 );
370  GetView()->ProcessTableMessage( msg );
371  }
372 
373  return removedRow;
374  }
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 146 of file dialog_lib_edit_pin_table.cpp.

147  {
148  if( aValue == INDETERMINATE_STATE )
149  return;
150 
151  LIB_PINS pins = m_rows[ aRow ];
152 
153  for( LIB_PIN* pin : pins )
154  {
155  switch( aCol )
156  {
157  case COL_NUMBER:
158  if( pins.size() == 1 )
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:55
int PinOrientationCode(int index)
Definition: pin_type.cpp:138
GRAPHIC_PINSHAPE
Definition: pin_type.h:55
const wxArrayString & PinShapeNames()
Definition: pin_type.cpp:201
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:416
const wxArrayString & PinTypeNames()
Definition: pin_type.cpp:183
const wxArrayString & PinOrientationNames()
Definition: pin_type.cpp:219
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

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

340  {
341  std::sort( aRow.begin(), aRow.end(),
342  []( LIB_PIN* lhs, LIB_PIN* rhs ) -> bool
343  {
344  return PIN_NUMBERS::Compare( lhs->GetNumber(), rhs->GetNumber() ) < 0;
345  } );
346  }
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 330 of file dialog_lib_edit_pin_table.cpp.

331  {
332  std::sort( m_rows.begin(), m_rows.end(),
333  [ aSortCol, ascending, this ]( const LIB_PINS& lhs, const LIB_PINS& rhs ) -> bool
334  {
335  return compare( lhs, rhs, aSortCol, ascending, m_userUnits );
336  } );
337  }
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

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

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

Referenced by GetValue().

Member Data Documentation

◆ m_edited

bool PIN_TABLE_DATA_MODEL::m_edited
private

Definition at line 415 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 414 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: