1
/***************************************************************************
2
* Copyright (C) 2012 by santiago González *
3
* santigoro@gmail.com *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 3 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, see <http://www.gnu.org/licenses/>. *
18
***************************************************************************/
20
#include "connector.h"
21
#include "connectorline.h"
22
#include "circuitwidget.h"
23
#include "simulator.h"
30
Connector::Connector( QObject* parent, QString type, QString id, Pin* startpin, Pin* endpin )
31
: Component( parent, type, id )
42
m_startPin = startpin;
43
m_startpinid = startpin->objectName();
44
setPos( startpin->scenePos() );
45
if( m_startPin->isBus() ) setIsBus( true );
51
m_endpinid = endpin->objectName();
52
m_startPin->setConnector( this );
53
m_endPin->setConnector( this );
54
m_startPin->setConPin( m_endPin );
55
m_endPin->setConPin( m_startPin );
56
if( m_isBus ) m_endPin->setIsBus( true );
63
m_idLabel->setVisible( false );
65
Connector::~Connector() { }
67
void Connector::remNullLines() // Remove lines with leght = 0 or aligned
69
if( m_conLineList.length() < 2 )
75
for( ConnectorLine* line : m_conLineList )
77
if( line->isDiagonal() ) continue;
79
int index = m_conLineList.indexOf( line );
80
if( index < m_conLineList.length()-1 ) //
82
ConnectorLine* line2 = m_conLineList.at( index+1 );
84
if( line2->isDiagonal() ) continue;
86
if( line->dx() == line2->dx() || line->dy() == line2->dy() ) // Lines aligned or null line
88
line2->sSetP1( line->p1() );
94
if( m_conLineList.length() < 2 )
102
void Connector::remConLine( ConnectorLine* line )
104
int index = m_conLineList.indexOf( line );
106
connectLines( index-1, index+1 );
107
Circuit::self()->removeItem( line );
108
m_conLineList.removeOne( line );
109
if( m_actLine > 0 ) m_actLine -= 1;
112
void Connector::refreshPointList()
114
if( m_conLineList.isEmpty() ) return;
119
data.setNum( m_conLineList.at(0)->p1().x() );
121
data.setNum( m_conLineList.at(0)->p1().y() );
123
int count = m_conLineList.size();
124
for( int i=0; i<count; i++ )
126
data.setNum( m_conLineList.at(i)->p2().x() );
128
data.setNum( m_conLineList.at(i)->p2().y() );
131
setPos( m_conLineList.first()->scenePos() );
133
setPointList( list );
134
//qDebug() << "lines " << count << "connector poinlist" << m_conLineList;
137
void Connector::addConLine( ConnectorLine* line, int index )
139
if( index > 0 && index < m_conLineList.size() ) disconnectLines( index-1, index );
141
m_conLineList.insert( index, line );
143
Circuit::self()->addItem(line);
147
connectLines( index-1, index );
148
m_conLineList.at( index-1 )->sSetP2( line->p1() );
151
if( index < m_conLineList.size()-1 )
153
if( m_conLineList.size() < 2 ) return;
155
connectLines( index, index+1 );
156
m_conLineList.at( index+1 )->sSetP1( line->p2() );
158
line->setIsBus( m_isBus );
159
if( Circuit::self()->is_constarted() ) line->setCursor( Qt::ArrowCursor );
162
ConnectorLine* Connector::addConLine( int x1, int y1, int x2, int y2, int index )
164
ConnectorLine* line = new ConnectorLine( x1, y1, x2, y2, this );
166
addConLine( line, index );
171
void Connector::connectLines( int index1, int index2 )
173
if( index1 < 0 || index2 < 0 || index2 > m_conLineList.length()-1 )
176
ConnectorLine* line1 = m_conLineList.at( index1 );
177
ConnectorLine* line2 = m_conLineList.at( index2 );
179
line1->setNextLine( line2 );
180
line2->setPrevLine( line1 );
183
void Connector::disconnectLines( int index1, int index2 )
185
if( index1 < 0 || index2 < 0 || index2 > m_conLineList.length()-1 )
188
ConnectorLine* line1 = m_conLineList.at( index1 );
189
ConnectorLine* line2 = m_conLineList.at( index2 );
191
line1->setNextLine( 0l );
192
line2->setPrevLine( 0l );
195
void Connector::updateConRoute( Pin* pin, QPointF thisPoint )
197
if( !this->isVisible() ) return;
198
if( Circuit::self()->pasting() )
204
bool diagonal = false;
205
int length = m_conLineList.length();
207
ConnectorLine* preline = 0l;
209
if( pin == m_startPin )
211
line = m_conLineList.first();
212
diagonal = line->isDiagonal();
213
//qDebug() << "Connector::updateConRoute StartPin";
215
line->sSetP1( thisPoint.toPoint() );
221
preline = m_conLineList.at(1);
234
line = m_conLineList.last();
236
diagonal = line->isDiagonal();
237
//qDebug() << "Connector::updateConRoute EndPin";
239
line->sSetP2( togrid( thisPoint ).toPoint() );
241
m_lastindex = length-1;
245
preline = m_conLineList.at( m_lastindex-1 );
246
if( pin != 0l ) m_actLine = m_lastindex-1;
248
if( diagonal || m_freeLine )
251
if( m_lastindex == m_actLine ) // Add new corner
253
QPoint point = line->p2();
255
ConnectorLine* newLine = addConLine( point.x(), point.y(), point.x()+4, point.y()+4, m_lastindex + 1 );
257
if( line->isSelected() ) newLine->setSelected( true );
264
if( (line->dx() == 0) && (line->dy() == 0) && (length > 1) ) // Null Line
266
//if( preline && preline->isDiagonal() ) return;
268
Circuit::self()->removeItem( line );
269
m_conLineList.removeOne( line );
271
if( m_actLine > 0 ) m_actLine -= 1;
273
else if( line->dx() != 0 && line->dy() != 0 )
277
if( m_lastindex == m_actLine ) // Add new corner
281
if( abs(line->dx()) > abs(line->dy()) ) point.setY( line->p1().y() );
282
else point.setX( line->p1().x() );
284
ConnectorLine* newLine = addConLine( point.x(), point.y(), line->p2().x(), line->p2().y(), m_lastindex + 1 );
286
if( line->isSelected() ) newLine->setSelected( true );
288
line->setP2( point );
291
else if( m_lastindex < m_actLine ) // Update first corner
295
if ( preline->dx() == 0 ) point.setY( line->p1().y() );
296
else /*if( preline->dy() == 0 )*/ point.setX( line->p1().x() );
298
line->setP2( point );
300
if( line->dx() == preline->dx() || line->dy() == preline->dy() ) // Lines aligned or null line
302
if( line->isSelected() || preline->isSelected())
304
preline->sSetP1( line->p1() );
309
else // Update last corner
313
if ( preline->dx() == 0 ) point.setY( line->p2().y() );
314
else /*if( preline->dy() == 0 )*/ point.setX( line->p2().x() );
316
line->setP1( point );
318
if( line->dx() == preline->dx() || line->dy() == preline->dy() ) // Lines aligned or null line
320
if( line->isSelected() || preline->isSelected())
322
preline->sSetP2( line->p2() );
328
//qDebug() << "Connector::updateConRoute"<<line->p1()<<line->p2();
329
//if( preline ) qDebug() << "Connector::updateConRoute"<<preline->p1()<<preline->p2();
333
void Connector::remLines()
335
while( !m_conLineList.isEmpty() )
337
ConnectorLine* line = m_conLineList.takeLast();
338
Circuit::self()->removeItem( line );
343
void Connector::move( QPointF delta )
345
//qDebug() << "Connector::move ..........................";
346
if( Circuit::self()->pasting() )
348
for( ConnectorLine* line : m_conLineList )
355
//Component::move( delta );
358
void Connector::setSelected( bool selected )
360
//qDebug() <<"\nConnector::setSelected"<<selected;
361
for( ConnectorLine* line : m_conLineList )
363
line->setSelected( selected );
364
//qDebug() << line->isSelected();
367
Component::setSelected( selected );
370
void Connector::setVisib( bool vis )
372
//qDebug() <<"\nConnector::setSelected"<<selected;
373
for( ConnectorLine* line : m_conLineList )
375
line->setVisible( vis );
376
//qDebug() << line->isSelected();
379
this->setVisible( vis );
382
void Connector::remove()
384
//qDebug() << "Connector::remove simulator running: " << Simulator::self()->isRunning();
385
//qDebug()<<"Connector::remove" << this->objectName();
386
if( Simulator::self()->isRunning() ) CircuitWidget::self()->powerCircOff();
388
if( m_startPin ) m_startPin->reset();
389
if( m_endPin ) m_endPin->reset();
391
Circuit::self()->conList()->removeOne( this );
392
Circuit::self()->removeItem( this );
396
void Connector::closeCon( Pin* endpin, bool connect )
398
if( Simulator::self()->isRunning() ) CircuitWidget::self()->powerCircOff();
401
m_endpinid = endpin->objectName();
405
QString enodid = "enode";
406
enodid.append( m_id );
407
enodid.remove( "Connector" );
408
eNode* newEnode = new eNode( enodid );
409
if( m_isBus ) newEnode->setIsBus( true );
411
// We will get all ePins from stratPin and endPin nets an add to new eNode
412
m_startPin->setConPin( 0l );
413
m_endPin->setConPin( 0l );
415
m_startPin->registerPins( newEnode );
416
m_endPin->registerPins( newEnode );
418
m_startPin->setConnector( this );
419
m_endPin->setConnector( this );
423
m_startPin->setIsBus( true );
424
m_endPin->setIsBus( true );
426
m_startPin->setConPin( m_endPin );
427
m_endPin->setConPin( m_startPin );
429
updateConRoute( m_endPin, m_endPin->scenePos() );
432
for( ConnectorLine* line : m_conLineList ) line->setCursor( Qt::CrossCursor );
435
void Connector::splitCon( int index, Pin* pin1, Pin* pin2 )
437
if( !m_endPin ) return;
439
pin2->setEnode( enode() );
440
pin1->setEnode( enode() );
442
disconnectLines( index-1, index );
444
QString type = QString("Connector");
447
id.append( Circuit::self()->newSceneId() );
449
Connector* new_connector = new Connector( Circuit::self(), type, id, pin2 );
450
Circuit::self()->addItem(new_connector);
453
int size = m_conLineList.size();
454
for( int i = index; i < size; ++i)
456
ConnectorLine* lline = m_conLineList.takeAt( index );
457
new_connector->lineList()->insert( newindex, lline );
459
lline->setParent( new_connector );
460
lline->setConnector( new_connector );
462
if( newindex > 1 ) new_connector->incActLine();
466
if( index > 1 ) m_actLine = index-2;
469
new_connector->closeCon( m_endPin ); // Close new_connector first please
470
closeCon( pin1 ); // Close this
473
void Connector::updateLines()
475
eNode* enode = startPin()->getEnode();
476
if( enode && enode->voltchanged() )
478
for( ConnectorLine* line : m_conLineList ) line->update();
482
QStringList Connector::pointList() { refreshPointList(); return m_pointList; }
483
void Connector::setPointList( QStringList pl ) { /*m_pointList.clear(); */m_pointList = pl;}
485
QString Connector::startPinId() { return m_startpinid;}
486
void Connector::setStartPinId( QString pinid) { m_startpinid = pinid; }
487
QString Connector::endPinId() { return m_endpinid; }
488
void Connector::setEndPinId( QString pinid) { m_endpinid = pinid; }
490
QString Connector::enodId()
492
eNode *node = m_startPin->getEnode();
494
if( node ) return node->itemId();
497
//void Connector::setEnodId( QString enodid ) { m_enodid = enodid; }
499
Pin* Connector::startPin() { return m_startPin;}
500
void Connector::setStartPin( Pin* pin) { m_startPin = pin; }
501
Pin* Connector::endPin() { return m_endPin; }
502
void Connector::setEndPin( Pin* pin) { m_endPin = pin; }
504
eNode* Connector::enode() { return m_startPin->getEnode(); }
506
void Connector::setEnode( eNode* enode )
508
if( m_startPin ) m_startPin->setEnode( enode );
509
if( m_endPin ) m_endPin->setEnode( enode );
510
if( m_isBus ) enode->setIsBus( true );
513
double Connector::getVolt()
515
//if( !m_eNode ) return 0;
516
return m_startPin->getVolt();
519
QList<ConnectorLine*>* Connector::lineList() { return &m_conLineList; }
521
void Connector::incActLine()
523
//qDebug() << "Connector::incActLine"<< m_actLine << m_conLineList.size()-1;
525
if( m_actLine < m_conLineList.size()-1 ) m_actLine += 1;
528
void Connector::setIsBus( bool bus )
530
if( m_isBus == bus ) return;
533
for( ConnectorLine* line : m_conLineList ) line->setIsBus( bus );
538
bool Connector::isBus()
543
#include "moc_connector.cpp"