1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
|
#include "dialog_lib_edit_pin_table.h"
#include "lib_pin.h"
#include <boost/algorithm/string/join.hpp>
#include <queue>
/* Avoid wxWidgets bug #16906 -- http://trac.wxwidgets.org/ticket/16906
*
* If multiple elements live in the root of a wxDataViewCtrl, using
* ItemsAdded() can run into an assertion failure. To avoid this, we avoid
* notifying the widget of changes, but rather reinitialize it.
*
* When a fix for this exists in wxWidgets, this is the place to turn it
* off.
*/
#define REASSOCIATE_HACK
class DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel :
public wxDataViewModel
{
public:
DataViewModel( LIB_PART& aPart );
// wxDataViewModel
virtual unsigned int GetColumnCount() const;
virtual wxString GetColumnType( unsigned int col ) const;
virtual void GetValue( wxVariant&, const wxDataViewItem&, unsigned int ) const;
virtual bool SetValue( const wxVariant&, const wxDataViewItem&, unsigned int );
virtual wxDataViewItem GetParent( const wxDataViewItem& ) const;
virtual bool IsContainer( const wxDataViewItem& ) const;
virtual bool HasContainerColumns( const wxDataViewItem& ) const;
virtual unsigned int GetChildren( const wxDataViewItem&, wxDataViewItemArray& ) const;
virtual int Compare( const wxDataViewItem& lhs,
const wxDataViewItem& rhs,
unsigned int col,
bool ascending ) const;
void SetGroupingColumn( int aCol );
void CalculateGrouping();
void Refresh();
#ifdef REASSOCIATE_HACK
void SetWidget( wxDataViewCtrl* aWidget ) { m_Widget = aWidget; }
#endif
enum
{
NONE = -1,
PIN_NUMBER = 0,
PIN_NAME = 1,
PIN_TYPE = 2,
PIN_POSITION = 3
};
private:
LIB_PART& m_Part;
LIB_PINS m_Backing;
int m_GroupingColumn;
int m_UnitCount;
class Item;
class Group;
class Pin;
mutable std::list<Pin> m_Pins;
mutable std::map<wxString, Group> m_Groups;
#ifdef REASSOCIATE_HACK
wxDataViewCtrl* m_Widget;
#endif
};
class DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::Item
{
public:
virtual void GetValue( wxVariant& aValue, unsigned int aCol ) const = 0;
virtual wxDataViewItem GetParent() const = 0;
virtual bool IsContainer() const = 0;
virtual unsigned int GetChildren( wxDataViewItemArray& ) const = 0;
};
class DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::Group :
public Item
{
public:
Group( unsigned int aGroupingColumn ) : m_GroupingColumn( aGroupingColumn ) {}
virtual void GetValue( wxVariant& aValue, unsigned int aCol ) const;
virtual wxDataViewItem GetParent() const { return wxDataViewItem(); }
virtual bool IsContainer() const { return true; }
virtual unsigned int GetChildren( wxDataViewItemArray& aItems ) const
{
/// @todo C++11
for( std::list<Pin*>::const_iterator i = m_Members.begin(); i != m_Members.end(); ++i )
aItems.push_back( wxDataViewItem( *i ) );
return aItems.size();
}
unsigned int GetCount() const { return m_Members.size(); }
void Add( Pin* aPin );
private:
std::list<Pin*> m_Members;
unsigned int m_GroupingColumn;
};
class DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::Pin :
public Item
{
public:
Pin( DataViewModel& aModel,
LIB_PIN* aBacking ) : m_Model( aModel ), m_Backing( aBacking ), m_Group( 0 ) {}
virtual void GetValue( wxVariant& aValue, unsigned int aCol ) const;
virtual wxDataViewItem GetParent() const { return wxDataViewItem( m_Group ); }
virtual bool IsContainer() const { return false; }
virtual unsigned int GetChildren( wxDataViewItemArray& ) const { return 0; }
void SetGroup( Group* aGroup ) { m_Group = aGroup; }
private:
DataViewModel& m_Model;
LIB_PIN* m_Backing;
Group* m_Group;
};
DIALOG_LIB_EDIT_PIN_TABLE::DIALOG_LIB_EDIT_PIN_TABLE( wxWindow* parent,
LIB_PART& aPart ) :
DIALOG_LIB_EDIT_PIN_TABLE_BASE( parent ),
m_Model( new DataViewModel( aPart ) )
{
#ifdef REASSOCIATE_HACK
m_Model->SetWidget( m_Pins );
#endif
m_Pins->AssociateModel( m_Model.get() );
/// @todo wxFormBuilder bug #61 -- move to base once supported
wxDataViewTextRenderer* rend0 = new wxDataViewTextRenderer( wxT( "string" ), wxDATAVIEW_CELL_INERT );
wxDataViewColumn* col0 = new wxDataViewColumn( _( "Number" ),
rend0,
DataViewModel::PIN_NUMBER,
100,
wxAlignment( wxALIGN_LEFT | wxALIGN_TOP ),
wxDATAVIEW_COL_RESIZABLE | wxDATAVIEW_COL_SORTABLE );
wxDataViewTextRenderer* rend1 = new wxDataViewTextRenderer( wxT( "string" ), wxDATAVIEW_CELL_INERT );
wxDataViewColumn* col1 = new wxDataViewColumn( _( "Name" ),
rend1,
DataViewModel::PIN_NAME,
100,
wxAlignment( wxALIGN_LEFT | wxALIGN_TOP ),
wxDATAVIEW_COL_RESIZABLE | wxDATAVIEW_COL_SORTABLE );
wxDataViewTextRenderer* rend2 = new wxDataViewTextRenderer( wxT( "string" ), wxDATAVIEW_CELL_INERT );
wxDataViewColumn* col2 = new wxDataViewColumn( _( "Type" ),
rend2,
DataViewModel::PIN_TYPE,
100,
wxAlignment( wxALIGN_LEFT | wxALIGN_TOP ),
wxDATAVIEW_COL_RESIZABLE );
wxDataViewTextRenderer* rend3 = new wxDataViewTextRenderer( wxT( "string" ), wxDATAVIEW_CELL_INERT );
wxDataViewColumn* col3 = new wxDataViewColumn( _( "Position" ),
rend3,
DataViewModel::PIN_POSITION,
100,
wxAlignment( wxALIGN_LEFT | wxALIGN_TOP ),
wxDATAVIEW_COL_RESIZABLE );
m_Pins->AppendColumn( col0 );
m_Pins->SetExpanderColumn( col0 );
m_Pins->AppendColumn( col1 );
m_Pins->AppendColumn( col2 );
m_Pins->AppendColumn( col3 );
GetSizer()->SetSizeHints(this);
Centre();
}
DIALOG_LIB_EDIT_PIN_TABLE::~DIALOG_LIB_EDIT_PIN_TABLE()
{
}
void DIALOG_LIB_EDIT_PIN_TABLE::OnColumnHeaderRightClicked( wxDataViewEvent& event )
{
m_Model->SetGroupingColumn( event.GetDataViewColumn()->GetModelColumn() );
event.Skip();
}
DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::DataViewModel( LIB_PART& aPart ) :
m_Part( aPart ),
m_GroupingColumn( 1 ),
m_UnitCount( m_Part.GetUnitCount() )
{
#ifdef REASSOCIATE_HACK
m_Widget = NULL;
#endif
aPart.GetPins( m_Backing );
/// @todo C++11
for( LIB_PINS::const_iterator i = m_Backing.begin(); i != m_Backing.end(); ++i )
m_Pins.push_back( Pin( *this, *i ) );
CalculateGrouping();
}
unsigned int DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::GetColumnCount() const
{
return 4;
}
wxString DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::GetColumnType( unsigned int aCol ) const
{
return wxT( "string" );
}
void DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::GetValue( wxVariant& aVal,
const wxDataViewItem& aItem,
unsigned int aCol ) const
{
assert( aItem.IsOk() );
reinterpret_cast<Item const*>( aItem.GetID() )->GetValue( aVal, aCol );
}
bool DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::SetValue( const wxVariant&,
const wxDataViewItem&,
unsigned int )
{
return false;
}
wxDataViewItem DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::GetParent( const wxDataViewItem& aItem )
const
{
assert( aItem.IsOk() );
return reinterpret_cast<Item const*>( aItem.GetID() )->GetParent();
}
bool DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::IsContainer( const wxDataViewItem& aItem ) const
{
if( aItem.IsOk() )
return reinterpret_cast<Item const*>( aItem.GetID() )->IsContainer();
else
return true;
}
bool DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::HasContainerColumns( const wxDataViewItem& ) const
{
return true;
}
unsigned int DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::GetChildren( const wxDataViewItem& aItem,
wxDataViewItemArray& aItems ) const
{
if( !aItem.IsOk() )
{
for( std::map<wxString, Group>::iterator i = m_Groups.begin(); i != m_Groups.end(); ++i )
if( i->second.GetCount() > 1 )
aItems.push_back( wxDataViewItem( &i->second ) );
for( std::list<Pin>::iterator i = m_Pins.begin(); i != m_Pins.end(); ++i )
if( !i->GetParent().IsOk() )
aItems.push_back( wxDataViewItem( &*i ) );
return aItems.size();
}
else
return reinterpret_cast<Item const*>( aItem.GetID() )->GetChildren( aItems );
}
namespace {
wxString GetNextComponent( const wxString& str, wxString::size_type& cursor )
{
if( str.size() <= cursor )
return wxEmptyString;
wxString::size_type begin = cursor;
wxUniChar c = str[cursor];
if( isdigit( c ) || c == '+' || c == '-' )
{
// number, possibly with sign
while( ++cursor < str.size() )
{
c = str[cursor];
if( isdigit( c ) || c == 'v' || c == 'V' )
continue;
else
break;
}
}
else
{
while( ++cursor < str.size() )
{
c = str[cursor];
if( isdigit( c ) )
break;
else
continue;
}
}
return str.substr( begin, cursor - begin );
}
int ComparePinNames( const wxString& lhs, const wxString& rhs )
{
wxString::size_type cursor1 = 0;
wxString::size_type cursor2 = 0;
wxString comp1, comp2;
for( ; ; )
{
comp1 = GetNextComponent( lhs, cursor1 );
comp2 = GetNextComponent( rhs, cursor2 );
if( comp1.empty() && comp2.empty() )
return 0;
if( comp1.empty() )
return -1;
if( comp2.empty() )
return 1;
wxUniChar c1 = comp1[0];
wxUniChar c2 = comp2[0];
if( isdigit( c1 ) || c1 == '-' || c1 == '+' )
{
if( isdigit( c2 ) || c2 == '-' || c2 == '+' )
{
// numeric comparison
wxString::size_type v1 = comp1.find_first_of( "vV" );
if( v1 != wxString::npos )
comp1[v1] = '.';
wxString::size_type v2 = comp2.find_first_of( "vV" );
if( v2 != wxString::npos )
comp2[v2] = '.';
double val1, val2;
comp1.ToDouble( &val1 );
comp2.ToDouble( &val2 );
if( val1 < val2 )
return -1;
if( val1 > val2 )
return 1;
}
else
return -1;
}
else
{
if( isdigit( c2 ) || c2 == '-' || c2 == '+' )
return 1;
int res = comp1.Cmp( comp2 );
if( res != 0 )
return res;
}
}
}
class CompareLess
{
public:
bool operator()( const wxString& lhs, const wxString& rhs )
{
return ComparePinNames( lhs, rhs ) == -1;
}
};
}
int DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::Compare( const wxDataViewItem& aItem1,
const wxDataViewItem& aItem2,
unsigned int aCol,
bool aAscending ) const
{
wxVariant var1;
GetValue( var1, aItem1, aCol );
wxString str1 = var1.GetString();
wxVariant var2;
GetValue( var2, aItem2, aCol );
wxString str2 = var2.GetString();
int res = ComparePinNames( str1, str2 );
if( res == 0 )
res = ( aItem1.GetID() < aItem2.GetID() ) ? -1 : 1;
return res * ( aAscending ? 1 : -1 );
}
void DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::SetGroupingColumn( int aCol )
{
if( m_GroupingColumn == aCol )
return;
m_GroupingColumn = aCol;
Cleared();
CalculateGrouping();
Refresh();
}
void DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::CalculateGrouping()
{
m_Groups.clear();
if( m_GroupingColumn != -1 )
{
wxVariant value;
for( std::list<Pin>::iterator i = m_Pins.begin(); i != m_Pins.end(); ++i )
{
i->GetValue( value, m_GroupingColumn );
wxString str = value.GetString();
std::map<wxString, Group>::iterator j = m_Groups.find( str );
if( j == m_Groups.end() )
j = m_Groups.insert( std::make_pair( str, m_GroupingColumn ) ).first;
j->second.Add( &*i );
}
}
else
{
for( std::list<Pin>::iterator i = m_Pins.begin(); i != m_Pins.end(); ++i )
i->SetGroup( 0 );
}
}
void DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::Refresh()
{
#ifdef REASSOCIATE_HACK
m_Widget->AssociateModel( this );
#else
std::queue<wxDataViewItem> todo;
todo.push( wxDataViewItem() );
while( !todo.empty() )
{
wxDataViewItem current = todo.front();
wxDataViewItemArray items;
GetChildren( current, items );
ItemsAdded( current, items );
for( wxDataViewItemArray::const_iterator i = items.begin(); i != items.end(); ++i )
{
if( IsContainer( *i ) )
todo.push( *i );
}
todo.pop();
}
#endif
}
void DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::Group::GetValue( wxVariant& aValue,
unsigned int aCol ) const
{
if( aCol == m_GroupingColumn )
{
// shortcut
m_Members.front()->GetValue( aValue, aCol );
}
else
{
std::set<wxString, CompareLess> values;
for( std::list<Pin*>::const_iterator i = m_Members.begin(); i != m_Members.end(); ++i )
{
wxVariant value;
(*i)->GetValue( value, aCol );
values.insert( value.GetString() );
}
aValue = boost::algorithm::join( values, "," );
}
}
void DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::Group::Add( Pin* aPin )
{
switch( GetCount() )
{
case 0:
aPin->SetGroup( 0 );
break;
case 1:
m_Members.front()->SetGroup( this );
// fall through
default:
aPin->SetGroup( this );
}
m_Members.push_back( aPin );
}
void DIALOG_LIB_EDIT_PIN_TABLE::DataViewModel::Pin::GetValue( wxVariant& aValue,
unsigned int aCol ) const
{
switch( aCol )
{
case PIN_NUMBER:
aValue = m_Backing->GetNumberString();
break;
case PIN_NAME:
{
if( m_Model.m_UnitCount > 1 )
{
wxString name;
int unit = m_Backing->GetPartNumber();
if( unit )
name << unit;
else
name << "com";
name << ':';
name << m_Backing->GetName();
aValue = name;
}
else
{
aValue = m_Backing->GetName();
}
}
break;
case PIN_TYPE:
aValue = m_Backing->GetElectricalTypeName();
break;
case PIN_POSITION:
{
wxPoint position = m_Backing->GetPosition();
wxString value;
value << "(" << position.x << "," << position.y << ")";
aValue = value;
}
break;
}
}
|