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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
|
/***************************************************************************
* Copyright (C) 2003-2005 by David Saxton *
* david@bluehaze.org *
* *
* 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 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "circuitdocument.h"
#include "connector.h"
#include "conrouter.h"
#include "cnitem.h"
#include "ecnode.h"
#include "itemdocumentdata.h"
#include "wire.h"
#include <kdebug.h>
#include <cmath>
inline static int toCanvas( int pos )
{
return pos*8+4;
}
inline static int fromCanvas( int pos )
{
return (pos-4)/8;
// return (pos>0) ? int((pos-3)/8) : int((pos-5)/8);
}
inline static TQPoint toCanvas( const TQPoint * const pos )
{
return TQPoint( toCanvas(pos->x()), toCanvas(pos->y()) );
}
inline static TQPoint fromCanvas( const TQPoint * const pos )
{
return TQPoint( fromCanvas(pos->x()), fromCanvas(pos->y()) );
}
inline static TQPoint toCanvas( const TQPoint &pos )
{
return TQPoint( toCanvas(pos.x()), toCanvas(pos.y()) );
}
inline static TQPoint fromCanvas( const TQPoint &pos )
{
return TQPoint( fromCanvas(pos.x()), fromCanvas(pos.y()) );
}
//BEGIN class Connector
Connector::Connector( Node * startNode, Node * endNode, ICNDocument *icnDocument, TQString *id )
: TQObject(icnDocument),
TQCanvasPolygon( icnDocument->canvas() )
{
p_icnDocument = icnDocument;
m_conRouter = new ConRouter(p_icnDocument);
p_parentContainer = 0l;
m_startNode = startNode;
m_endNode = endNode;
p_nodeGroup = 0l;
b_semiHidden = false;
b_deleted = false;
b_pointsAdded = false;
b_manualPoints = false;
m_bIsSyncingWires = false;
if (id)
{
m_id = *id;
if ( !p_icnDocument->registerUID(*id) )
{
// kdDebug() << k_funcinfo << "KTechlab: Connector attempted to register given ID, but ID already in use"<<endl;
}
}
else
m_id = p_icnDocument->generateUID("connector");
p_icnDocument->registerItem(this);
p_icnDocument->requestRerouteInvalidatedConnectors();
setVisible(true);
ECNode * startECNode = dynamic_cast<ECNode*>(startNode);
ECNode * endECNode = dynamic_cast<ECNode*>(endNode);
if ( startECNode && endECNode )
{
connect( startECNode, TQ_SIGNAL(numPinsChanged(unsigned)), this, TQ_SLOT(syncWiresWithNodes()) );
connect( endECNode, TQ_SIGNAL(numPinsChanged(unsigned)), this, TQ_SLOT(syncWiresWithNodes()) );
syncWiresWithNodes();
}
}
Connector::~Connector()
{
p_icnDocument->unregisterUID( id() );
delete m_conRouter;
m_conRouter = 0l;
for ( unsigned i = 0; i < m_wires.size(); i++ )
delete m_wires[i];
m_wires.resize(0);
}
int Connector::rtti() const
{
return ItemDocument::RTTI::Connector;
}
void Connector::syncWiresWithNodes()
{
ECNode * startECNode = dynamic_cast<ECNode*>((Node*)m_startNode);
ECNode * endECNode = dynamic_cast<ECNode*>((Node*)m_endNode);
if ( !startECNode || !endECNode )
return;
unsigned newNumWires = 0;
if ( startECNode->type() == Node::ec_junction ||
endECNode->type() == Node::ec_junction )
newNumWires = TQMAX( startECNode->numPins(), endECNode->numPins() );
else
newNumWires = TQMIN( startECNode->numPins(), endECNode->numPins() );
unsigned oldNumWires = m_wires.size();
if ( newNumWires == oldNumWires )
return;
m_bIsSyncingWires = true;
if ( startECNode->type() == Node::ec_junction )
startECNode->setNumPins(newNumWires);
if ( endECNode->type() == Node::ec_junction )
endECNode->setNumPins(newNumWires);
m_bIsSyncingWires = false;
if ( newNumWires > oldNumWires )
{
m_wires.resize(newNumWires);
for ( unsigned i = oldNumWires; i < newNumWires; i++ )
{
if ( startECNode->pin(i) && endECNode->pin(i) )
m_wires[i] = new Wire( startECNode->pin(i), endECNode->pin(i) );
}
}
else
{
for ( unsigned i = newNumWires; i < oldNumWires; i++ )
delete m_wires[i];
m_wires.resize(newNumWires);
}
updateConnectorLines();
emit numWiresChanged(newNumWires);
}
void Connector::setParentContainer( const TQString &cnItemId )
{
// // We only allow the node to be parented once
// if ( p_parentContainer || !ICNDocument->itemWithID(cnItemId) ) return;
p_parentContainer = p_icnDocument->cnItemWithID(cnItemId);
}
void Connector::removeConnector( Node* )
{
if (b_deleted)
return;
b_deleted = true;
// Remove 'penalty' points for this connector from the ICNDocument
updateConnectorPoints(false);
emit selected(false);
emit removed(this);
if ( m_startNode )
m_startNode->removeConnector(this);
if ( m_endNode )
m_endNode->removeConnector(this);
p_icnDocument->appendDeleteList(this);
}
int getSlope( float x1, float y1, float x2, float y2 )
{
enum slope
{
s_n = 0,// .
s_v, // |
s_h, // -
s_s, // /
s_d // \ (backwards slash)
};
if ( x1 == x2 )
{
if ( y1 == y2 ) {
return s_n;
}
return s_v;
}
else if ( y1 == y2 ) {
return s_h;
}
else if ( (y2-y1)/(x2-x1) > 0 ) {
return s_s;
}
else {
return s_d;
}
}
void Connector::updateDrawList()
{
if ( !m_startNode || !m_endNode || !canvas() ) {
return;
}
TQPointList drawLineList;
int prevX = (*m_conRouter->cellPointList()->begin()).x();
int prevY = (*m_conRouter->cellPointList()->begin()).y();
int prevX_canvas = toCanvas(prevX);
int prevY_canvas = toCanvas(prevY);
Cells *cells = p_icnDocument->cells();
bool bumpNow = false;
const TQPointList::const_iterator cplEnd = m_conRouter->cellPointList()->end();
for ( TQPointList::const_iterator it = m_conRouter->cellPointList()->begin(); it != cplEnd; ++it )
{
const int x = (*it).x();
const int y = (*it).y();
const int numCon = p_icnDocument->isValidCellReference(x,y) ? (*cells)[x][y].numCon : 0;
const int y_canvas = toCanvas(y);
const int x_canvas = toCanvas(x);
const bool bumpNext = ( prevX == x &&
numCon > 1 &&
std::abs(y_canvas-m_startNode->y())>8 &&
std::abs(y_canvas-m_endNode->y())>8 );
int x0 = prevX_canvas;
int x2 = x_canvas;
int x1 = (x0+x2)/2;
int y0 = prevY_canvas;
int y3 = y_canvas;
int y1 = ( y0 == y3 ) ? y0 : ((y0<y3) ? y0+3 : y0-3);
int y2 = ( y0 == y3 ) ? y3 : ((y0<y3) ? y3-3 : y3+3);
if (bumpNow) x0 += 3;
if (bumpNext) x2 += 3;
if ( !bumpNow && !bumpNext )
{
drawLineList += TQPoint( x0, y0 );
drawLineList += TQPoint( x2, y3 );
}
else if (bumpNow)
{
drawLineList += TQPoint( x0, y0 );
drawLineList += TQPoint( x1, y1 );
drawLineList += TQPoint( x2, y3 );
}
else if (bumpNext)
{
drawLineList += TQPoint( x0, y0 );
drawLineList += TQPoint( x1, y2 );
drawLineList += TQPoint( x2, y3 );
}
else
{
drawLineList += TQPoint( x0, y0 );
drawLineList += TQPoint( x1, y1 );
drawLineList += TQPoint( x1, y2 );
drawLineList += TQPoint( x2, y3 );
}
prevX = x;
prevY = y;
prevY_canvas = y_canvas;
prevX_canvas = x_canvas;
bumpNow = bumpNext;
}
// Now, remove redundant points (i.e. those that are either repeated or are
// in the same direction as the previous points)
if ( drawLineList.size() < 3 ) {
return;
}
const TQPointList::iterator dllEnd = drawLineList.end();
TQPointList::iterator previous = drawLineList.begin();
TQPointList::iterator current = previous;
current++;
TQPointList::const_iterator next = current;
next++;
while ( previous != dllEnd && current != dllEnd && next != dllEnd )
{
const int slope1 = getSlope( (*previous).x(), (*previous).y(), (*current).x(), (*current).y() );
const int slope2 = getSlope( (*current).x(), (*current).y(), (*next).x(), (*next).y() );
if ( slope1 == slope2 || slope1 == 0 || slope2 == 0 )
{
*current = TQPoint( -1, -1 );
}
else
{
previous = current;
}
current++;
next++;
}
drawLineList.remove( TQPoint( -1, -1 ) );
// Find the bounding rect
{
int x1=-1, y1=-1, x2=-1, y2=-1;
const TQPointList::iterator end = drawLineList.end();
for ( TQPointList::iterator it = drawLineList.begin(); it != end; ++it )
{
const TQPoint p = *it;
if ( p.x() < x1 || x1 == -1 ) {
x1 = p.x();
}
if ( p.x() > x2 || x2 == -1 ) {
x2 = p.x();
}
if ( p.y() < y1 || y1 == -1 ) {
y1 = p.y();
}
if ( p.y() > y2 || y2 == -1 ) {
y2 = p.y();
}
}
TQRect boundRect( x1, y1, x2-x1, y2-y1 );
if ( boundRect != m_oldBoundRect )
{
canvas()->setChanged( boundRect | m_oldBoundRect );
m_oldBoundRect = boundRect;
}
}
//BEGIN build up ConnectorLine list
const ConnectorLineList::iterator ConnectorLineEnd = m_connectorLineList.end();
for ( ConnectorLineList::iterator it = m_connectorLineList.begin(); it != ConnectorLineEnd; ++it )
delete *it;
m_connectorLineList.clear();
if ( drawLineList.size() > 1 )
{
TQPoint prev = drawLineList.first();
const TQPointList::iterator end = drawLineList.end();
for ( TQPointList::iterator it = ++drawLineList.begin(); it != end; ++it )
{
const TQPoint next = *it;
ConnectorLine *line = new ConnectorLine(this);
line->setPoints( prev.x(), prev.y(), next.x(), next.y() );
m_connectorLineList.append(line);
prev = next;
}
}
updateConnectorLines();
//END build up ConnectorPoint list
}
void Connector::setSemiHidden( bool semiHidden )
{
if ( !canvas() || semiHidden == b_semiHidden )
return;
b_semiHidden = semiHidden;
updateConnectorLines();
}
void Connector::updateConnectorPoints( bool add )
{
if (!canvas()) {
return;
}
if ( b_deleted || !isVisible() )
add = false;
// Check we haven't already added/removed the points...
if ( b_pointsAdded == add )
return;
b_pointsAdded = add;
// We don't include the end points in the mapping
if ( m_conRouter->cellPointList()->size() < 3 ) {
return;
}
const int mult = (add)?1:-1;
const TQPointList::iterator end = --m_conRouter->cellPointList()->end();
for ( TQPointList::iterator it = ++m_conRouter->cellPointList()->begin(); it != end; ++it )
{
int x = (*it).x();
int y = (*it).y();
// Add the points of this connector to the cell array in the ICNDocument,
// so that other connectors still to calculate their points know to try
// and avoid this connector
p_icnDocument->addCPenalty( x, y-1, mult*ICNDocument::hs_connector/2 );
p_icnDocument->addCPenalty( x-1, y, mult*ICNDocument::hs_connector/2 );
p_icnDocument->addCPenalty( x, y, mult*ICNDocument::hs_connector );
p_icnDocument->addCPenalty( x+1, y, mult*ICNDocument::hs_connector/2 );
p_icnDocument->addCPenalty( x, y+1, mult*ICNDocument::hs_connector/2 );
if ( p_icnDocument->isValidCellReference( x, y ) ) {
(*p_icnDocument->cells())[x][y].numCon += mult;
}
}
// updateDrawList();
}
void Connector::setRoutePoints( TQPointList pointList, bool setManual, bool checkEndPoints )
{
if (!canvas()) {
return;
}
updateConnectorPoints(false);
bool reversed = pointsAreReverse(pointList);
if (checkEndPoints)
{
if (reversed)
{
pointList.prepend( TQPoint( int(m_endNode->x()), int(m_endNode->y()) ) );
pointList.append( TQPoint( int(m_startNode->x()), int(m_startNode->y()) ) );
}
else
{
pointList.prepend( TQPoint( int(m_startNode->x()), int(m_startNode->y()) ) );
pointList.append( TQPoint( int(m_endNode->x()), int(m_endNode->y()) ) );
}
}
m_conRouter->setPoints( pointList, reversed );
b_manualPoints = setManual;
updateConnectorPoints(true);
}
bool Connector::pointsAreReverse( const TQPointList &pointList ) const
{
if ( !m_startNode || !m_endNode )
{
kdWarning() << k_funcinfo << "Cannot determine orientation as no start and end nodes" << endl;
return false;
}
if ( pointList.isEmpty() )
return false;
int plsx = pointList.first().x();
int plsy = pointList.first().y();
int plex = pointList.last().x();
int pley = pointList.last().y();
double nsx = m_startNode->x();
double nsy = m_startNode->y();
double nex = m_endNode->x();
double ney = m_endNode->y();
double dist_normal = (nsx-plsx)*(nsx-plsx) + (nsy-plsy)*(nsy-plsy) + (nex-plex)*(nex-plex) + (ney-pley)*(ney-pley);
double dist_reverse = (nsx-plex)*(nsx-plex) + (nsy-pley)*(nsy-pley) + (nex-plsx)*(nex-plsx) + (ney-plsy)*(ney-plsy);
return dist_reverse < dist_normal;
}
void Connector::rerouteConnector()
{
if (!isVisible())
return;
if ( nodeGroup() )
{
kdWarning() << k_funcinfo << "Connector is controlled by a NodeGroup! Use that to reroute the connector" << endl;
return;
}
if ( !startNode() || !endNode() )
return;
updateConnectorPoints(false);
m_conRouter->mapRoute( int(startNode()->x()), int(startNode()->y()), int(endNode()->x()), int(endNode()->y()) );
b_manualPoints = false;
updateConnectorPoints(true);
}
void Connector::translateRoute( int dx, int dy )
{
updateConnectorPoints(false);
m_conRouter->translateRoute( dx, dy );
updateConnectorPoints(true);
updateDrawList();
}
void Connector::restoreFromConnectorData( const ConnectorData &connectorData )
{
updateConnectorPoints(false);
b_manualPoints = connectorData.manualRoute;
m_conRouter->setRoutePoints( connectorData.route );
updateConnectorPoints(true);
updateDrawList();
}
ConnectorData Connector::connectorData() const
{
ConnectorData connectorData;
if ( !m_startNode || !m_endNode )
{
kdDebug() << k_funcinfo << " m_startNode="<<m_startNode<<" m_endNode="<<m_endNode<<endl;
return connectorData;
}
connectorData.manualRoute = usesManualPoints();
connectorData.route = *m_conRouter->cellPointList();
if ( m_startNode->isChildNode() )
{
connectorData.startNodeIsChild = true;
connectorData.startNodeCId = m_startNode->childId();
connectorData.startNodeParent = m_startNode->parentItem()->id();
}
else
{
connectorData.startNodeIsChild = false;
connectorData.startNodeId = m_startNode->id();
}
if ( m_endNode->isChildNode() )
{
connectorData.endNodeIsChild = true;
connectorData.endNodeCId = m_endNode->childId();
connectorData.endNodeParent = m_endNode->parentItem()->id();
}
else
{
connectorData.endNodeIsChild = false;
connectorData.endNodeId = m_endNode->id();
}
return connectorData;
}
void Connector::setVisible( bool yes )
{
if ( !canvas() || isVisible() == yes )
return;
TQCanvasPolygon::setVisible(yes);
updateConnectorLines();
}
void Connector::setSelected( bool yes )
{
if ( !canvas() || isSelected() == yes )
return;
TQCanvasPolygon::setSelected(yes);
updateConnectorLines();
emit selected(yes);
}
void Connector::updateConnectorLines()
{
const TQColor color = b_semiHidden ? TQt::gray : (isSelected() ? TQColor( 101, 134, 192 ) : TQt::black);
// const TQColor color = b_semiHidden ? TQt::gray : (isSelected() ? TQColor( 0x7f, 0x7f, 0xff ) : TQt::black);
const int z = ICNDocument::Z::Connector + (isSelected() ? 5 : 0);
TQPen pen( color, (numWires() > 1) ? 2 : 1 );
const ConnectorLineList::iterator end = m_connectorLineList.end();
for ( ConnectorLineList::iterator it = m_connectorLineList.begin(); it != end; ++it )
{
TQCanvasPolygonalItem *item = static_cast<TQCanvasPolygonalItem*>(*it);
item->setZ(z);
item->setPen(pen);
item->setBrush(color);
item->setVisible( isVisible() );
}
}
TQValueList<TQPointList> Connector::splitConnectorPoints( const TQPoint & pos ) const
{
return m_conRouter->splitPoints(pos);
}
TQPointList Connector::connectorPoints( bool reverse ) const
{
bool doReverse = (reverse != pointsAreReverse( m_conRouter->pointList(false) ));
return m_conRouter->pointList(doReverse);
}
//END class Connector
//BEGIN class ConnectorLine
ConnectorLine::ConnectorLine( Connector *connector )
: TQObject(connector), TQCanvasLine( connector->canvas() )
{
p_connector = connector;
}
int ConnectorLine::rtti() const
{
return ICNDocument::RTTI::ConnectorLine;
}
//END class ConnectorLine
#include "connector.moc"
|