-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathcomponent.cpp
More file actions
593 lines (532 loc) · 11.9 KB
/
component.cpp
File metadata and controls
593 lines (532 loc) · 11.9 KB
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
585
586
587
588
589
590
591
592
593
/**
** This file is part of the uConfig project.
** Copyright 2017-2020 Robotips, Sebastien CAUX (sebcaux)
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
#include "component.h"
#include "drawrect.h"
#include <QDebug>
#include <QFontMetrics>
#include <qmath.h>
/**
* @brief Base constructor for component
* @param name optionally specify the component name at the creation
*/
Component::Component(const QString &name)
: _prefix("U")
{
_showPinName = true;
_showPadName = true;
_unitCount = 1;
_refText = new DrawText("U");
_nameText = new DrawText();
_packageText = new DrawText();
_packageText->setVisible(false);
_docText = new DrawText();
_docText->setVisible(false);
setName(name);
}
/**
* @brief Component copy constructor. Also copy all the pins.
* @param other component to copy
*/
Component::Component(const Component &other)
{
_name = other._name;
_prefix = other._prefix;
_alias = other._alias;
_footPrints = other._footPrints;
_showPinName = other._showPinName;
_showPadName = other._showPadName;
_unitCount = other._unitCount;
for (auto pin : other._pins)
{
addPin(new Pin(*pin));
}
for (auto draw : other._draws)
{
addDraw(draw->clone());
}
_refText = new DrawText(*other._refText);
_nameText = new DrawText(*other._nameText);
_packageText = new DrawText(*other._packageText);
_docText = new DrawText(*other._nameText);
}
/**
* @brief Component destructor. Delete also all the internal pins.
*/
Component::~Component()
{
for (auto &pin : _pins)
{
delete pin;
}
for (auto &draw : _draws)
{
delete draw;
}
delete _refText;
delete _nameText;
delete _packageText;
delete _docText;
}
/**
* @brief Component name getter.
* @return component name
*/
const QString &Component::name() const
{
return _name;
}
/**
* @brief Component name setter.
* @param name name to give
* @warning replace all the space contained in the name by underscore
*/
void Component::setName(const QString &name)
{
_name = name;
_name.replace(" ", "_");
_nameText->setText(name);
}
/**
* @brief Component pins list getter
* @return list of pins
*/
QList<Pin *> &Component::pins()
{
return _pins;
}
/**
* @brief Constant component pins list getter
* @return list of pins
*/
const QList<Pin *> &Component::pins() const
{
return _pins;
}
/**
* @brief Adds a pin to the component
* @param pin pin pointer to add
*/
void Component::addPin(Pin *pin)
{
pin->setComponent(this);
_pins.append(pin);
}
/**
* @brief Removes a pin from his pointer
* @param pin pin pointer to remove
*/
void Component::removePin(Pin *pin)
{
if (_pins.removeOne(pin))
{
delete pin;
}
}
/**
* @brief Removes and deletes all pins
*/
void Component::clearPins()
{
for (auto &pin : _pins)
{
delete pin;
}
_pins.clear();
}
/**
* @brief Component draw items list getter
* @return list of draws
*/
QList<Draw *> &Component::draws()
{
return _draws;
}
/**
* @brief Constant component draw items list getter
* @return list of draws
*/
const QList<Draw *> &Component::draws() const
{
return _draws;
}
/**
* @brief Adds a item draw to the component at the first position
* @param draw draw pointer to add
*/
void Component::prependDraw(Draw *draw)
{
draw->setComponent(this);
_draws.prepend(draw);
}
/**
* @brief Adds a item draw to the component
* @param draw draw pointer to add
*/
void Component::addDraw(Draw *draw)
{
draw->setComponent(this);
_draws.append(draw);
}
/**
* @brief Removes a draw item from his pointer
* @param draw draw pointer to remove
*/
void Component::removeDraw(Draw *draw)
{
if (_draws.removeOne(draw))
{
delete draw;
}
}
/**
* @brief Removes and deletes all draws
*/
void Component::clearDraws()
{
for (auto &draw : _draws)
{
delete draw;
}
_draws.clear();
}
/**
* @brief Component prefix letter(s) getter
* @return prefix letter(s)
*/
const QString &Component::prefix() const
{
return _prefix;
}
/**
* @brief Sets one or more letters prefix for the component. For ex: "U", "IC"
* @param prefix prefix letter(s)
*/
void Component::setPrefix(const QString &prefix)
{
_prefix = prefix;
}
/**
* @brief Returns a reference to the list of component aliases name for identical pin package configuration
* @return aliases list
*/
QStringList &Component::aliases()
{
return _alias;
}
/**
* @brief Returns the list of component aliases name for identical pin package configuration
* @return aliases list
*/
const QStringList &Component::aliases() const
{
return _alias;
}
/**
* @brief Adds an alias name to the component
* @param alias alias name to add
*/
void Component::addAlias(const QString &alias)
{
_alias.append(alias);
}
/**
* @brief Adds an alias list name to the component
* @param aliases aliases names to add
*/
void Component::addAlias(const QStringList &aliases)
{
_alias.append(aliases);
}
/**
* @brief Returns a reference to possible footprint list
* @return non const footprint list
*/
QStringList &Component::footPrints()
{
return _footPrints;
}
/**
* @brief Returns possible footprint list
* @return footprint list
*/
const QStringList &Component::footPrints() const
{
return _footPrints;
}
/**
* @brief Adds a footprint name to the list of possible footprints for this component
* @param footprint name to add
*/
void Component::addFootPrint(const QString &footprint)
{
_footPrints.append(footprint);
}
/**
* @brief Adds a footprint list to the list of possible footprints for this component
* @param footprint list to add
*/
void Component::addFootPrint(const QStringList &footprints)
{
_footPrints.append(footprints);
}
/**
* @brief Returns the bounding rect of component with pin lenght
* @return bounding rect
*/
QRect Component::boundingRect() const
{
QRect mrect(0, 0, 1, 1);
for (auto pin : _pins)
{
mrect = mrect.united(QRect(pin->pos(), QSize(1, 1)));
}
return mrect;
}
/**
* @brief Component rect
* @return component rect
*/
QRect Component::rect() const
{
return _rect;
}
/**
* @brief Sets rect of component
* @param rect component rect
*/
void Component::setRect(const QRect &rect)
{
_rect = rect;
}
bool pinPadLessThan(const Pin *pin1, const Pin *pin2)
{
return (pin1->padName().rightJustified(4, '0') < pin2->padName().rightJustified(4, '0'));
}
/**
* @brief Sorts the component by pad name
*/
void Component::sort()
{
std::sort(_pins.begin(), _pins.end(), pinPadLessThan);
}
/**
* @brief Pin name is shown if true
* @return true if pin name is shown
*/
bool Component::showPinName() const
{
return _showPinName;
}
/**
* @brief Makes pin name visible
* @param showPinName Pin name is shown if true
*/
void Component::setShowPinName(bool showPinName)
{
_showPinName = showPinName;
}
/**
* @brief Pad name is shown if true
* @return true if pad name is shown
*/
bool Component::showPadName() const
{
return _showPadName;
}
/**
* @brief Makes pad name visible
* @param showPadName Pad name is shown if true
*/
void Component::setShowPadName(bool showPadName)
{
_showPadName = showPadName;
}
/**
* @brief Returns the number of units declared in the component
* @return unit count
*/
int Component::unitCount() const
{
return _unitCount;
}
/**
* @brief Sets the number of units declared in the component
* @param unitCount Count of units
*/
void Component::setUnitCount(int unitCount)
{
_unitCount = unitCount;
}
/**
* @brief Image of debugger for extraction from PDF
* @return image view of PDF with color rects
*/
const QImage &Component::debugInfo() const
{
return _debugInfo;
}
/**
* @brief Sets the debug image view
* @param debugInfo debug image view
*/
void Component::setDebugInfo(const QImage &debugInfo)
{
_debugInfo = debugInfo;
}
/**
* @brief Reference draw text
* @return Pointer to reference draw text
*/
DrawText *Component::refText() const
{
return _refText;
}
/**
* @brief Sets reference draw text
* @param refText Pointer to reference draw text
*/
void Component::setRefText(DrawText *refText)
{
delete _refText;
_refText = refText;
}
/**
* @brief Reference name text
* @return Pointer to name draw text
*/
DrawText *Component::nameText() const
{
return _nameText;
}
/**
* @brief Sets name draw text
* @param refText Pointer to name draw text
*/
void Component::setNameText(DrawText *nameText)
{
delete _nameText;
_nameText = nameText;
}
/**
* @brief Reference package text
* @return Pointer to package draw text
*/
DrawText *Component::packageText() const
{
return _packageText;
}
/**
* @brief Sets package draw text
* @param refText Pointer to package draw text
*/
void Component::setPackageText(DrawText *packageText)
{
delete _packageText;
_packageText = packageText;
}
/**
* @brief Reference doc text
* @return Pointer to doc draw text
*/
DrawText *Component::docText() const
{
return _docText;
}
/**
* @brief Sets doc draw text
* @param refText Pointer to doc draw text
*/
void Component::setDocText(DrawText *docText)
{
delete _docText;
_docText = docText;
}
/**
* @brief Reorganizes component pins like a simple DIP/SOIC package
*/
void Component::reorganizeToPackageStyle()
{
QPoint pos;
int i;
int rightCount = _pins.count() / 2;
int leftCount = _pins.count() - rightCount;
int leftOffset = (static_cast<int>(ceil(leftCount / 2.0)) - 1) * 100;
int rightOffset = (static_cast<int>(ceil(rightCount / 2.0)) - 1) * 100;
if (leftCount % 2 == 0)
{
leftOffset += 100;
}
int margin = 0;
int leftMargin = 0;
int rightMargin = 0;
sort();
// compute leftMargin and rightMargin
for (i = 0; i < leftCount; i++)
{
Pin *pin = _pins[i];
pin->setPinType(Pin::Normal);
pin->setLength(200);
int width = pin->name().count() * 50 + 250;
if (width > leftMargin)
{
leftMargin = width;
}
}
for (; i < leftCount + rightCount; i++)
{
Pin *pin = _pins[i];
pin->setPinType(Pin::Normal);
pin->setLength(200);
int width = pin->name().count() * 50 + 250;
if (width > rightMargin)
{
rightMargin = width;
}
}
margin = (leftMargin + rightMargin) / 2;
margin = (qCeil(margin / 100.0)) * 100; // grid align
// place pins
pos = QPoint(-margin, -leftOffset);
for (i = 0; i < leftCount; i++)
{
Pin *pin = _pins[i];
pin->setAngle(0);
pin->setPos(pos);
pos += QPoint(0, 100);
}
pos = QPoint(margin, rightOffset);
for (; i < leftCount + rightCount; i++)
{
Pin *pin = _pins[i];
pin->setAngle(180);
pin->setPos(pos);
pos += QPoint(0, -100);
}
clearDraws();
// added draw outside rect
QRect rect = QRect(QPoint(-margin + 200, -qMax(leftOffset, rightOffset) - 100), QPoint(margin - 200, qMax(leftOffset, rightOffset) + 100));
addDraw(new DrawRect(rect));
// set position of ref and name text
_nameText->setPos(QPoint(rect.right(), rect.top() - 50));
_nameText->setTextHJustify(DrawText::TextHRight);
_nameText->setAngle(0.0);
_refText->setPos(QPoint(rect.left(), rect.bottom() + 50));
_refText->setTextHJustify(DrawText::TextHLeft);
_refText->setAngle(0.0);
;
}