~ubuntu-branches/ubuntu/precise/fritzing/precise

« back to all changes in this revision

Viewing changes to src/items/wire.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Georges Khaznadar
  • Date: 2011-08-26 10:11:05 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110826101105-w5hmn7zcf93ig5v6
Tags: 0.6.3b-1
* upgrapded to the newer upstream version
* parameters of the function GraphicsUtils::distanceFromLine in 
  src/svg/groundplanegenerator.cpp:767 are now declared as doubles,
  which Closes: #636441
* the new version fixes src/utils/folderutils.cpp, which
  Closes: #636061

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*******************************************************************
2
 
 
3
 
Part of the Fritzing project - http://fritzing.org
4
 
Copyright (c) 2007-2011 Fachhochschule Potsdam - http://fh-potsdam.de
5
 
 
6
 
Fritzing is free software: you can redistribute it and/or modify
7
 
it under the terms of the GNU General Public License as published by
8
 
the Free Software Foundation, either version 3 of the License, or
9
 
(at your option) any later version.
10
 
 
11
 
Fritzing is distributed in the hope that it will be useful,
12
 
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
GNU General Public License for more details.
15
 
 
16
 
You should have received a copy of the GNU General Public License
17
 
along with Fritzing.  If not, see <http://www.gnu.org/licenses/>.
18
 
 
19
 
********************************************************************
20
 
 
21
 
$Revision: 5176 $:
22
 
$Author: cohen@irascible.com $:
23
 
$Date: 2011-07-05 11:33:03 +0200 (Tue, 05 Jul 2011) $
24
 
 
25
 
********************************************************************/
26
 
 
27
 
#include "wire.h"
28
 
 
29
 
#include <QLineF>
30
 
#include <QPen>
31
 
#include <QRadialGradient>
32
 
#include <QBrush>
33
 
#include <QPen>
34
 
#include <QGraphicsScene>
35
 
#include <QList>
36
 
#include <QGraphicsItem>
37
 
#include <QSet>
38
 
#include <QComboBox>
39
 
#include <QToolTip>
40
 
 
41
 
#include "../debugdialog.h"
42
 
#include "../sketch/infographicsview.h"
43
 
#include "../connectors/connectoritem.h"
44
 
#include "../connectors/svgidlayer.h"
45
 
#include "../fsvgrenderer.h"
46
 
#include "partlabel.h"
47
 
#include "../model/modelpart.h"
48
 
#include "../utils/graphicsutils.h"
49
 
 
50
 
#include <stdlib.h>
51
 
 
52
 
QHash<QString, QString> Wire::colors;
53
 
QHash<QString, QString> Wire::shadowColors;
54
 
QHash<QString, QString> Wire::colorTrans;
55
 
QStringList Wire::colorNames;
56
 
QHash<int, QString> Wire::widthTrans;
57
 
QList<int> Wire::widths;
58
 
qreal Wire::STANDARD_TRACE_WIDTH;
59
 
qreal Wire::HALF_STANDARD_TRACE_WIDTH;
60
 
 
61
 
////////////////////////////////////////////////////////////
62
 
 
63
 
bool alphaLessThan(QColor * c1, QColor * c2)
64
 
{
65
 
        return c1->alpha() < c2->alpha();
66
 
}
67
 
 
68
 
/////////////////////////////////////////////////////////////
69
 
 
70
 
 
71
 
WireMenu::WireMenu(const QString & title, QWidget * parent) : QMenu(title, parent) 
72
 
{
73
 
        m_wire = NULL;
74
 
}
75
 
 
76
 
void WireMenu::setWire(Wire * w) {
77
 
        m_wire = w;
78
 
}
79
 
 
80
 
Wire * WireMenu::wire() {
81
 
        return m_wire;
82
 
}
83
 
 
84
 
/////////////////////////////////////////////////////////////
85
 
 
86
 
WireAction::WireAction(QAction * action) : QAction(action) {
87
 
        m_wire = NULL;
88
 
        this->setText(action->text());
89
 
        this->setStatusTip(action->statusTip());
90
 
        this->setCheckable(action->isCheckable());
91
 
}
92
 
 
93
 
WireAction::WireAction(const QString & title, QObject * parent) : QAction(title, parent) {
94
 
        m_wire = NULL;
95
 
}
96
 
 
97
 
void WireAction::setWire(Wire * w) {
98
 
        m_wire = w;
99
 
}
100
 
 
101
 
Wire * WireAction::wire() {
102
 
        return m_wire;
103
 
}
104
 
 
105
 
/////////////////////////////////////////////////////////////
106
 
 
107
 
Wire::Wire( ModelPart * modelPart, ViewIdentifierClass::ViewIdentifier viewIdentifier,  const ViewGeometry & viewGeometry, long id, QMenu* itemMenu, bool initLabel)
108
 
        : ItemBase(modelPart, viewIdentifier, viewGeometry, id, itemMenu)
109
 
{
110
 
        m_connector0 = m_connector1 = NULL;
111
 
        m_partLabel = initLabel ? new PartLabel(this, NULL) : NULL;
112
 
        m_canChainMultiple = false;
113
 
    setFlag(QGraphicsItem::ItemIsSelectable, true );
114
 
        m_connectorHover = NULL;
115
 
        m_opacity = 1.0;
116
 
        m_ignoreSelectionChange = false;
117
 
 
118
 
        //DebugDialog::debug(QString("aix line %1 %2 %3 %4").arg(this->viewGeometry().line().x1())
119
 
                                                                                                        //.arg(this->viewGeometry().line().y1())
120
 
                                                                                                        //.arg(this->viewGeometry().line().x2())
121
 
                                                                                                        //.arg(this->viewGeometry().line().y2()) );
122
 
        //DebugDialog::debug(QString("aix loc %1 %2").arg(this->viewGeometry().loc().x())
123
 
                                                                                                                //.arg(this->viewGeometry().loc().y()) );
124
 
 
125
 
        setPos(m_viewGeometry.loc());
126
 
 
127
 
        m_dragEnd = false;
128
 
}
129
 
 
130
 
Wire::~Wire() {
131
 
}
132
 
 
133
 
FSvgRenderer * Wire::setUp(ViewLayer::ViewLayerID viewLayerID, const LayerHash &  viewLayers, InfoGraphicsView * infoGraphicsView ) {
134
 
        ItemBase::setViewLayerID(viewLayerID, viewLayers);
135
 
        FSvgRenderer * svgRenderer = setUpConnectors(m_modelPart, m_viewIdentifier);
136
 
        if (svgRenderer != NULL) {
137
 
                initEnds(m_viewGeometry, svgRenderer->viewBox(), infoGraphicsView);
138
 
                setConnectorTooltips();
139
 
        }
140
 
        setZValue(this->z());
141
 
 
142
 
        return svgRenderer;
143
 
}
144
 
 
145
 
void Wire::saveGeometry() {
146
 
        m_viewGeometry.setSelected(this->isSelected());
147
 
        m_viewGeometry.setLine(this->line());
148
 
        m_viewGeometry.setLoc(this->pos());
149
 
        m_viewGeometry.setZ(this->zValue());
150
 
}
151
 
 
152
 
 
153
 
bool Wire::itemMoved() {
154
 
        if  (m_viewGeometry.loc() != this->pos()) return true;
155
 
 
156
 
        if (this->line().dx() != m_viewGeometry.line().dx()) return false;
157
 
        if (this->line().dy() != m_viewGeometry.line().dy()) return false;
158
 
 
159
 
        return (this->line() != m_viewGeometry.line());
160
 
}
161
 
 
162
 
void Wire::moveItem(ViewGeometry & viewGeometry) {
163
 
        this->setPos(viewGeometry.loc());
164
 
        this->setLine(viewGeometry.line());
165
 
}
166
 
 
167
 
void Wire::initEnds(const ViewGeometry & vg, QRectF defaultRect, InfoGraphicsView * infoGraphicsView) {
168
 
        bool gotOne = false;
169
 
        bool gotTwo = false;
170
 
        qreal penWidth = 1;
171
 
        foreach (QGraphicsItem * childItem, childItems()) {
172
 
                ConnectorItem * item = dynamic_cast<ConnectorItem *>(childItem);
173
 
                if (item == NULL) continue;
174
 
 
175
 
                // check the name or is order good enough?
176
 
 
177
 
                if (gotOne) {
178
 
                        gotTwo = true;
179
 
                        m_connector1 = item;
180
 
                        break;
181
 
                }
182
 
                else {
183
 
                        penWidth = item->rect().width();
184
 
                        m_connector0 = item;
185
 
                        gotOne = true;
186
 
                }
187
 
        }
188
 
 
189
 
        if (!gotTwo) {
190
 
                return;
191
 
        }
192
 
 
193
 
        if ((vg.line().length() == 0) && (vg.line().x1() == -1)) {
194
 
                this->setLine(defaultRect.left(), defaultRect.top(), defaultRect.right(), defaultRect.bottom());
195
 
        }
196
 
        else {
197
 
                this->setLine(vg.line());
198
 
        }
199
 
 
200
 
        setConnector0Rect();
201
 
        setConnector1Rect();
202
 
        m_viewGeometry.setLine(this->line());
203
 
        
204
 
        QBrush brush(QColor(0, 0, 0));
205
 
        QPen pen(brush, penWidth, Qt::SolidLine, Qt::RoundCap);
206
 
        this->setPen(pen);
207
 
 
208
 
        m_pen.setCapStyle(Qt::RoundCap);
209
 
        m_shadowPen.setCapStyle(Qt::RoundCap);
210
 
        if (infoGraphicsView != NULL) {
211
 
                infoGraphicsView->initWire(this, penWidth);
212
 
        }
213
 
 
214
 
        prepareGeometryChange();
215
 
}
216
 
 
217
 
void Wire::paint (QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget ) {
218
 
        if (m_hidden) return;
219
 
 
220
 
        painter->setOpacity(m_inactive ? m_opacity  / 2 : m_opacity);
221
 
        if (!getRatsnest() && !getTrace()) {
222
 
                painter->save();
223
 
                painter->setPen(m_shadowPen);
224
 
                QLineF line = this->line();
225
 
                painter->drawLine(line);
226
 
                painter->restore();
227
 
        }
228
 
        ItemBase::paint(painter, option, widget);
229
 
}
230
 
 
231
 
void Wire::paintHover(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 
232
 
{
233
 
        Q_UNUSED(widget);
234
 
        Q_UNUSED(option);
235
 
        painter->save();
236
 
        if ((m_connectorHoverCount > 0 && !m_dragEnd) || m_connectorHoverCount2 > 0) {
237
 
                painter->setOpacity(.50);
238
 
                painter->fillPath(this->hoverShape(), QBrush(connectorHoverColor));
239
 
        }
240
 
        else {
241
 
                painter->setOpacity(hoverOpacity);
242
 
                painter->fillPath(this->hoverShape(), QBrush(hoverColor));
243
 
        }
244
 
        painter->restore();
245
 
}
246
 
 
247
 
void Wire::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
248
 
        //DebugDialog::debug("checking press event");
249
 
        emit wireSplitSignal(this, event->scenePos(), this->pos(), this->line());
250
 
}
251
 
 
252
 
void Wire::mousePressEvent(QGraphicsSceneMouseEvent *event)
253
 
{
254
 
        WireMenu * wireMenu = qobject_cast<WireMenu *>(m_itemMenu);
255
 
        if (wireMenu) {
256
 
                wireMenu->setWire(this);
257
 
        }
258
 
        ItemBase::mousePressEvent(event);
259
 
}
260
 
 
261
 
void Wire::initDragEnd(ConnectorItem * connectorItem, QPointF scenePos) {
262
 
        Q_UNUSED(scenePos);
263
 
        saveGeometry();
264
 
        QLineF line = this->line();
265
 
        m_drag0 = (connectorItem == m_connector0);
266
 
        m_dragEnd = true;
267
 
        if (m_drag0) {
268
 
                m_wireDragOrigin = line.p2();
269
 
                //DebugDialog::debug(QString("drag near origin %1 %2").arg(m_wireDragOrigin.x()).arg(m_wireDragOrigin.y()) );
270
 
        }
271
 
        else {
272
 
                m_wireDragOrigin = line.p1();
273
 
                //DebugDialog::debug(QString("drag far origin %1 %2").arg(m_wireDragOrigin.x()).arg(m_wireDragOrigin.y()) );
274
 
                //DebugDialog::debug(QString("drag far other %1 %2").arg(line.p2().x()).arg(line.p2().y()) );
275
 
        }
276
 
 
277
 
        if (connectorItem->chained()) {
278
 
                QList<Wire *> chained;
279
 
                QList<ConnectorItem *> ends;
280
 
                collectChained(chained, ends);
281
 
                // already saved the first one
282
 
                for (int i = 1; i < chained.count(); i++) {
283
 
                        chained[i]->saveGeometry();
284
 
                }
285
 
        }
286
 
}
287
 
 
288
 
 
289
 
void Wire::mouseReleaseConnectorEvent(ConnectorItem * connectorItem, QGraphicsSceneMouseEvent * event) {
290
 
        Q_UNUSED(event);
291
 
        Q_UNUSED(connectorItem);
292
 
        releaseDrag();
293
 
}
294
 
 
295
 
void Wire::mouseMoveConnectorEvent(ConnectorItem * connectorItem, QGraphicsSceneMouseEvent * event) {
296
 
        mouseMoveEventAux(this->mapFromItem(connectorItem, event->pos()), (event->modifiers() & Qt::ShiftModifier) != 0);
297
 
}
298
 
 
299
 
 
300
 
void Wire::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
301
 
        mouseMoveEventAux(event->pos(), (event->modifiers() & Qt::ShiftModifier) != 0);
302
 
}
303
 
 
304
 
void Wire::mouseMoveEventAux(QPointF eventPos, bool shiftModifier) {
305
 
        if (m_spaceBarWasPressed) {
306
 
                return;
307
 
        }
308
 
 
309
 
        if (m_dragEnd == false) {
310
 
                return;
311
 
        }
312
 
 
313
 
        ConnectorItem * whichConnectorItem;
314
 
        ConnectorItem * otherConnectorItem;
315
 
        if (m_drag0) {
316
 
                whichConnectorItem = m_connector0;
317
 
                otherConnectorItem = m_connector1;
318
 
        }
319
 
        else {
320
 
                whichConnectorItem = m_connector1;
321
 
                otherConnectorItem = m_connector0;
322
 
        }
323
 
 
324
 
        if (shiftModifier) {
325
 
                QPointF initialPos = mapFromScene(otherConnectorItem->sceneAdjustedTerminalPoint(NULL));  
326
 
                bool bendpoint = whichConnectorItem->connectionsCount() > 0;
327
 
                if (bendpoint) {
328
 
                        foreach (ConnectorItem * ci, whichConnectorItem->connectedToItems()) {
329
 
                                if (ci->attachedToItemType() != ModelPart::Wire) {
330
 
                                        bendpoint = false;
331
 
                                        break;
332
 
                                }
333
 
                        }
334
 
                }
335
 
                if (bendpoint) {
336
 
                        bendpoint = false;
337
 
                        foreach (ConnectorItem * ci, whichConnectorItem->connectedToItems()) {
338
 
                                Wire * w = dynamic_cast<Wire *>(ci->attachedTo());
339
 
                                ConnectorItem * oci = w->otherConnector(ci);
340
 
                                QPointF otherInitialPos = mapFromScene(oci->sceneAdjustedTerminalPoint(NULL));
341
 
                                QPointF p1(initialPos.x(), otherInitialPos.y());
342
 
                                qreal d = ((p1.x() - eventPos.x()) * (p1.x() - eventPos.x())) +  ((p1.y() - eventPos.y()) * (p1.y() - eventPos.y()));
343
 
                                if (d <= 144) {
344
 
                                        bendpoint = true;
345
 
                                        eventPos = p1;
346
 
                                        break;
347
 
                                }
348
 
                                p1.setX(otherInitialPos.x());
349
 
                                p1.setY(initialPos.y());
350
 
                                d = ((p1.x() - eventPos.x()) * (p1.x() - eventPos.x())) +  ((p1.y() - eventPos.y()) * (p1.y() - eventPos.y()));
351
 
                                if (d <= 144) {
352
 
                                        bendpoint = true;
353
 
                                        eventPos = p1;
354
 
                                        break;
355
 
                                }                               
356
 
 
357
 
                        }
358
 
                }
359
 
 
360
 
                if (!bendpoint) {
361
 
                        eventPos = GraphicsUtils::calcConstraint(initialPos, eventPos);
362
 
                }
363
 
 
364
 
        }
365
 
 
366
 
        QPointF temp = this->mapToScene(eventPos);
367
 
        //DebugDialog::debug(QString("wire move event %1,%2  %3").arg(temp.x()).arg(temp.y()).arg(m_drag0));
368
 
 
369
 
 
370
 
        if (m_drag0) {
371
 
                QPointF r = this->mapToScene(eventPos);
372
 
                QGraphicsSvgItem::setPos(r.x(), r.y());
373
 
                this->setLine(0, 0, m_wireDragOrigin.x() - r.x() + m_viewGeometry.loc().x(),
374
 
                                                        m_wireDragOrigin.y() - r.y() + m_viewGeometry.loc().y() );
375
 
        }
376
 
        else {
377
 
                this->setLine(m_wireDragOrigin.x(), m_wireDragOrigin.y(), eventPos.x(), eventPos.y());
378
 
        }
379
 
        setConnector1Rect();
380
 
 
381
 
        bool chained = false;
382
 
        foreach (ConnectorItem * toConnectorItem, whichConnectorItem->connectedToItems()) {
383
 
                Wire * chainedWire = dynamic_cast<Wire *>(toConnectorItem->attachedTo());
384
 
                if (chainedWire == NULL) continue;
385
 
 
386
 
                chainedWire->simpleConnectedMoved(whichConnectorItem, toConnectorItem);
387
 
                chained = true;
388
 
        }
389
 
 
390
 
        if (!chained) {
391
 
                // don't allow wire to connect back to something the other end is already directly connected to
392
 
                QList<Wire *> wires;
393
 
                QList<ConnectorItem *> ends;
394
 
                collectChained(otherConnectorItem, wires, ends);
395
 
                for (int i = 0; i < wires.count(); i++) {
396
 
                        Wire * w = wires[i];
397
 
                        collectChained(w->m_connector1, wires, ends);
398
 
                        collectChained(w->m_connector0, wires, ends);
399
 
                }
400
 
                ends.append(otherConnectorItem);
401
 
                foreach (Wire * w, wires) {
402
 
                        ends.append(w->connector0());
403
 
                        ends.append(w->connector1());
404
 
                }
405
 
                foreach (ConnectorItem * toConnectorItem, whichConnectorItem->connectedToItems()) {
406
 
                        ends.removeOne(toConnectorItem);
407
 
                }
408
 
 
409
 
                ConnectorItem * originatingConnector = NULL;
410
 
                if (otherConnectorItem && otherConnectorItem->connectionsCount() > 0) {
411
 
                        originatingConnector = otherConnectorItem->connectedToItems()[0];
412
 
                }
413
 
                whichConnectorItem->findConnectorUnder(false, true, ends, true, originatingConnector);
414
 
        }
415
 
}
416
 
 
417
 
void Wire::setConnector0Rect() {
418
 
        QRectF rect = m_connector0->rect();
419
 
        rect.moveTo(0 - (rect.width()  / 2.0)  ,
420
 
                                0 - (rect.height()  / 2.0) );
421
 
        //DebugDialog::debug(QString("set connector rect %1 %2").arg(rect.width()).arg(rect.height()));
422
 
        m_connector0->setRect(rect);
423
 
}
424
 
 
425
 
 
426
 
void Wire::setConnector1Rect() {
427
 
        QRectF rect = m_connector1->rect();
428
 
        rect.moveTo(this->line().dx() - (rect.width()  / 2.0)  ,
429
 
                                this->line().dy() - (rect.height()  / 2.0) );
430
 
        m_connector1->setRect(rect);
431
 
}
432
 
 
433
 
void Wire::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
434
 
        if (m_spaceBarWasPressed) {
435
 
                return;
436
 
        }
437
 
 
438
 
        if (releaseDrag()) return;
439
 
 
440
 
        ItemBase::mouseReleaseEvent(event);
441
 
}
442
 
 
443
 
bool Wire::releaseDrag() {
444
 
        if (m_dragEnd == false) return false;
445
 
 
446
 
        m_dragEnd = false;
447
 
 
448
 
        ConnectorItem * from = (m_drag0) ? m_connector0 : m_connector1;
449
 
        ConnectorItem * to = from->releaseDrag();
450
 
 
451
 
        QLineF newLine = this->line();
452
 
        QLineF oldLine = m_viewGeometry.line();
453
 
        QPointF oldPos = m_viewGeometry.loc();
454
 
        QPointF newPos = this->pos();
455
 
        if (newLine != oldLine || oldPos != newPos) {
456
 
                emit wireChangedSignal(this, oldLine, newLine, oldPos, newPos, from, to);
457
 
        }
458
 
 
459
 
        return true;
460
 
}
461
 
 
462
 
 
463
 
void Wire::saveInstanceLocation(QXmlStreamWriter & streamWriter)
464
 
{
465
 
        QLineF line = m_viewGeometry.line();
466
 
        QPointF loc = m_viewGeometry.loc();
467
 
        streamWriter.writeAttribute("x", QString::number(loc.x()));
468
 
        streamWriter.writeAttribute("y", QString::number(loc.y()));
469
 
        streamWriter.writeAttribute("x1", QString::number(line.x1()));
470
 
        streamWriter.writeAttribute("y1", QString::number(line.y1()));
471
 
        streamWriter.writeAttribute("x2", QString::number(line.x2()));
472
 
        streamWriter.writeAttribute("y2", QString::number(line.y2()));
473
 
        streamWriter.writeAttribute("wireFlags", QString::number(m_viewGeometry.flagsAsInt()));
474
 
}
475
 
 
476
 
void Wire::writeGeometry(QXmlStreamWriter & streamWriter) {
477
 
        ItemBase::writeGeometry(streamWriter);
478
 
        streamWriter.writeStartElement("wireExtras");
479
 
        streamWriter.writeAttribute("mils", QString::number(mils()));
480
 
        streamWriter.writeAttribute("color", m_pen.brush().color().name());
481
 
        streamWriter.writeAttribute("opacity", QString::number(m_opacity));
482
 
        streamWriter.writeEndElement();
483
 
}
484
 
 
485
 
void Wire::setExtras(QDomElement & element, InfoGraphicsView * infoGraphicsView)
486
 
{
487
 
        if (element.isNull()) return;
488
 
 
489
 
        bool ok;
490
 
        qreal w = element.attribute("width").toDouble(&ok);
491
 
        if (ok) {
492
 
                setWireWidth(w, infoGraphicsView);
493
 
        }
494
 
        else {
495
 
                w = element.attribute("mils").toDouble(&ok);
496
 
                if (ok) {
497
 
                        setWireWidth(GraphicsUtils::mils2pixels(w, FSvgRenderer::printerScale()), infoGraphicsView);
498
 
                }
499
 
        }
500
 
 
501
 
        setColorFromElement(element);
502
 
}
503
 
 
504
 
void Wire::setColorFromElement(QDomElement & element) {
505
 
        QString colorString = element.attribute("color");
506
 
        if (colorString.isNull() || colorString.isEmpty()) return;
507
 
 
508
 
        bool ok;
509
 
        qreal op = element.attribute("opacity").toDouble(&ok);
510
 
        if (!ok) {
511
 
                op = 1.0;
512
 
        }
513
 
 
514
 
        setColorString(colorString, op);
515
 
}
516
 
 
517
 
void Wire::hoverEnterConnectorItem(QGraphicsSceneHoverEvent * event , ConnectorItem * item) {
518
 
        m_connectorHover = item;
519
 
        ItemBase::hoverEnterConnectorItem(event, item);
520
 
}
521
 
 
522
 
void Wire::hoverLeaveConnectorItem(QGraphicsSceneHoverEvent * event, ConnectorItem * item) {
523
 
        m_connectorHover = NULL;
524
 
        ItemBase::hoverLeaveConnectorItem(event, item);
525
 
}
526
 
 
527
 
void Wire::connectionChange(ConnectorItem * onMe, ConnectorItem * onIt, bool connect) {
528
 
        checkVisibility(onMe, onIt, connect);
529
 
 
530
 
        bool movable = true;
531
 
        foreach (ConnectorItem * connectedTo, m_connector0->connectedToItems()) {
532
 
                if (connectedTo->attachedToItemType() != ModelPart::Wire) {
533
 
                        movable = false;
534
 
                        break;
535
 
                }
536
 
        }
537
 
        if (movable) {
538
 
                foreach (ConnectorItem * connectedTo, m_connector1->connectedToItems()) {
539
 
                        if (connectedTo->attachedToItemType() != ModelPart::Wire) {
540
 
                                movable = false;
541
 
                                break;
542
 
                        }
543
 
                }
544
 
        }
545
 
}
546
 
 
547
 
void Wire::mouseDoubleClickConnectorEvent(ConnectorItem * connectorItem, QGraphicsSceneMouseEvent * event) {
548
 
        Q_UNUSED(event);
549
 
        int chained = 0;
550
 
        foreach (ConnectorItem * toConnectorItem, connectorItem->connectedToItems()) {
551
 
                if (toConnectorItem->attachedToItemType() == ModelPart::Wire) {
552
 
                        chained++;
553
 
                }
554
 
                else {
555
 
                        return;
556
 
                }
557
 
        }
558
 
 
559
 
        if (chained == 1) {
560
 
                emit wireJoinSignal(this, connectorItem);
561
 
        }
562
 
}
563
 
 
564
 
void Wire::mousePressConnectorEvent(ConnectorItem * connectorItem, QGraphicsSceneMouseEvent * event) {
565
 
        //DebugDialog::debug("checking press connector event");
566
 
 
567
 
        if (m_canChainMultiple && event->modifiers() & altOrMetaModifier()) {
568
 
                // dragging a wire out of a bendpoint
569
 
                InfoGraphicsView * infoGraphicsView = InfoGraphicsView::getInfoGraphicsView(this);
570
 
                if (infoGraphicsView != NULL) {
571
 
                        infoGraphicsView->mousePressConnectorEvent(connectorItem, event);
572
 
                }
573
 
 
574
 
                return;
575
 
        }
576
 
 
577
 
 
578
 
        connectorItem->setOverConnectorItem(NULL);
579
 
        initDragEnd(connectorItem, event->scenePos());
580
 
 
581
 
}
582
 
 
583
 
void Wire::simpleConnectedMoved(ConnectorItem * to) {
584
 
        // to is this wire, from is something else
585
 
        simpleConnectedMoved(to->firstConnectedToIsh(), to);
586
 
}
587
 
 
588
 
void Wire::simpleConnectedMoved(ConnectorItem * from, ConnectorItem * to)
589
 
{
590
 
        if (from == NULL) return;
591
 
 
592
 
        // to is this wire, from is something else
593
 
        QPointF p1, p2;
594
 
        calcNewLine(from, to, p1, p2);
595
 
 
596
 
        /*
597
 
        QPointF oldPos = this->pos();
598
 
        QPointF newPos = p1;
599
 
        QLineF oldLine = this->line();
600
 
        QLineF newLine(0, 0,  p2.x() - p1.x(), p2.y() - p1.y());
601
 
        if (qAbs(oldPos.x() - newPos.x()) > 1.75 ||
602
 
                qAbs(oldPos.y() - newPos.y()) > 1.75 ||
603
 
                qAbs(oldLine.x1() - newLine.x1()) > 1.75 ||
604
 
                qAbs(oldLine.x2() - newLine.x2()) > 1.75 ||
605
 
                qAbs(oldLine.y1() - newLine.y1()) > 1.75 ||
606
 
                qAbs(oldLine.y2() - newLine.y2()) > 1.75
607
 
                )
608
 
        {
609
 
                DebugDialog::debug("line changed");
610
 
                calcNewLine(from,to,p1,p2);
611
 
        }
612
 
        */
613
 
 
614
 
        this->setPos(p1);
615
 
        this->setLine(0,0, p2.x() - p1.x(), p2.y() - p1.y() );
616
 
        //DebugDialog::debug(QString("set line %5: %1 %2, %3 %4, vis:%6 lyr:%7").arg(p1.x()).arg(p1.y()).arg(p2.x()).arg(p2.y()).arg(id()).arg(isVisible()).arg(m_viewIdentifier) );
617
 
        setConnector1Rect();
618
 
}
619
 
 
620
 
void Wire::calcNewLine(ConnectorItem * from, ConnectorItem * to, QPointF & p1, QPointF & p2) {
621
 
        // to is this wire, from is something else
622
 
        if (to == m_connector0) {
623
 
                p1 = from->sceneAdjustedTerminalPoint(to);
624
 
                ConnectorItem * otherFrom = m_connector1->firstConnectedToIsh();
625
 
                if (otherFrom == NULL) {
626
 
                        p2 = m_connector1->mapToScene(m_connector1->rect().center());
627
 
                }
628
 
                else {
629
 
                        p2 = otherFrom->sceneAdjustedTerminalPoint(m_connector1);
630
 
                }
631
 
        }
632
 
        else {
633
 
                p2 = from->sceneAdjustedTerminalPoint(to);
634
 
                ConnectorItem * otherFrom = m_connector0->firstConnectedToIsh();
635
 
                if (otherFrom == NULL) {
636
 
                        p1 = m_connector0->mapToScene(m_connector0->rect().center());
637
 
                }
638
 
                else {
639
 
                        p1 = otherFrom->sceneAdjustedTerminalPoint(m_connector0);
640
 
                }
641
 
 
642
 
        }
643
 
}
644
 
 
645
 
void Wire::connectedMoved(ConnectorItem * from, ConnectorItem * to) {
646
 
        // "from" is the connector on the part
647
 
        // "to" is the connector on the wire
648
 
 
649
 
        simpleConnectedMoved(from, to);
650
 
        return;
651
 
 
652
 
        /*
653
 
        DebugDialog::debug(QString("connected moved %1 %2, %3 %4")
654
 
                .arg(from->attachedToID())
655
 
                .arg(from->attachedToTitle())
656
 
                .arg(to->attachedToID())
657
 
                .arg(to->attachedToTitle())
658
 
                );
659
 
        */
660
 
 
661
 
        ConnectorItem * otherEnd = otherConnector(to);
662
 
        bool chained = otherEnd->chained();
663
 
        QPointF p1, p2;
664
 
        if (chained) {
665
 
                // move both ends
666
 
                if (to == m_connector0) {
667
 
                        p1 = from->sceneAdjustedTerminalPoint(m_connector0);
668
 
                        p2 = this->line().p2() + p1;
669
 
                }
670
 
                else {
671
 
                        p2 = from->sceneAdjustedTerminalPoint(m_connector1);
672
 
                        p1 = p2 - this->line().p2();
673
 
                }
674
 
        }
675
 
        else {
676
 
                calcNewLine(from, to, p1, p2);
677
 
        }
678
 
        this->setPos(p1);
679
 
        this->setLine(0,0, p2.x() - p1.x(), p2.y() - p1.y() );
680
 
        //DebugDialog::debug(QString("set line %5: %1 %2, %3 %4, vis:%6 lyr:%7").arg(p1.x()).arg(p1.y()).arg(p2.x()).arg(p2.y()).arg(id()).arg(isVisible()).arg(m_viewIdentifier) );
681
 
        setConnector1Rect();
682
 
 
683
 
        if (chained) {
684
 
                foreach (ConnectorItem * otherEndTo, otherEnd->connectedToItems()) {
685
 
                        if (otherEndTo->attachedToItemType() == ModelPart::Wire) {
686
 
                                otherEndTo->attachedTo()->connectedMoved(otherEnd, otherEndTo);
687
 
                        }
688
 
                }
689
 
        }
690
 
}
691
 
 
692
 
 
693
 
FSvgRenderer * Wire::setUpConnectors(ModelPart * modelPart, ViewIdentifierClass::ViewIdentifier viewIdentifier) {
694
 
 
695
 
        QString error;
696
 
        FSvgRenderer * renderer = ItemBase::setUpImage(modelPart, viewIdentifier, m_viewLayerID, m_viewLayerSpec, error);
697
 
        if (renderer == NULL) {
698
 
                return NULL;
699
 
        }
700
 
 
701
 
        foreach (Connector * connector, m_modelPart->connectors().values()) {
702
 
                if (connector == NULL) continue;
703
 
 
704
 
                SvgIdLayer * svgIdLayer = connector->fullPinInfo(viewIdentifier, m_viewLayerID);
705
 
                if (svgIdLayer == NULL) continue;
706
 
 
707
 
                bool result = renderer->setUpConnector(svgIdLayer, false);
708
 
                if (!result) continue;
709
 
 
710
 
                ConnectorItem * connectorItem = newConnectorItem(connector);
711
 
                connectorItem->setRect(svgIdLayer->m_rect);
712
 
                connectorItem->setTerminalPoint(svgIdLayer->m_point);
713
 
 
714
 
                connectorItem->setCircular(true);
715
 
                //DebugDialog::debug(QString("terminal point %1 %2").arg(terminalPoint.x()).arg(terminalPoint.y()) );
716
 
        }
717
 
 
718
 
        return renderer;
719
 
}
720
 
 
721
 
/*
722
 
void Wire::setPos(const QPointF & pos) {
723
 
        GraphicsSvgLineItem::setPos(pos);
724
 
}
725
 
 
726
 
// helpful for debugging
727
 
void Wire::setLine(QLineF line) {
728
 
        GraphicsSvgLineItem::setLine(line);
729
 
        DebugDialog::debug(QString("set line %5 %6, %7 %8, %1 %2 %3 %4").arg(line.x1()).arg(line.y1()).arg(line.x2()).arg(line.y2())
730
 
                .arg(id()).arg(m_viewIdentifier).arg(this->pos().x()).arg(this->pos().y()) );
731
 
}
732
 
 
733
 
void Wire::setLine(qreal x1, qreal y1, qreal x2, qreal y2) {
734
 
        GraphicsSvgLineItem::setLine(x1, y1, x2, y2);
735
 
        DebugDialog::debug(QString("set line %5 %6, %7 %8, %1 %2 %3 %4").arg(x1).arg(y1).arg(x2).arg(y2)
736
 
                .arg(id()).arg(m_viewIdentifier).arg(this->pos().x()).arg(this->pos().y()) );
737
 
}
738
 
 
739
 
*/
740
 
 
741
 
 
742
 
void Wire::setLineAnd(QLineF line, QPointF pos, bool useLine) {
743
 
        this->setPos(pos);
744
 
        if (useLine) this->setLine(line);
745
 
 
746
 
        setConnector1Rect();
747
 
}
748
 
 
749
 
ConnectorItem * Wire::otherConnector(ConnectorItem * oneConnector) {
750
 
        if (oneConnector == m_connector0) return m_connector1;
751
 
 
752
 
        return m_connector0;
753
 
}
754
 
 
755
 
ConnectorItem * Wire::connector0() {
756
 
        return m_connector0;
757
 
}
758
 
 
759
 
ConnectorItem * Wire::connector1() {
760
 
        return m_connector1;
761
 
}
762
 
 
763
 
void Wire::findConnectorsUnder() {
764
 
        for (int i = 0; i < childItems().count(); i++) {
765
 
                ConnectorItem * connectorItem = dynamic_cast<ConnectorItem *>(childItems()[i]);
766
 
                if (connectorItem == NULL) continue;
767
 
 
768
 
                connectorItem->findConnectorUnder(true, false, ConnectorItem::emptyConnectorItemList, false, NULL);
769
 
        }
770
 
}
771
 
 
772
 
void Wire::collectChained(QList<Wire *> & chained, QList<ConnectorItem *> & ends ) {
773
 
        chained.append(this);
774
 
        for (int i = 0; i < chained.count(); i++) {
775
 
                Wire * wire = chained[i];
776
 
                collectChained(wire->m_connector1, chained, ends);
777
 
                collectChained(wire->m_connector0, chained, ends);
778
 
        }
779
 
}
780
 
 
781
 
void Wire::collectChained(ConnectorItem * connectorItem, QList<Wire *> & chained, QList<ConnectorItem *> & ends) {
782
 
        if (connectorItem == NULL) return;
783
 
 
784
 
        foreach (ConnectorItem * connectedToItem, connectorItem->connectedToItems()) {
785
 
                Wire * wire = dynamic_cast<Wire *>(connectedToItem->attachedTo());
786
 
                if (wire == NULL) {
787
 
                        if (!ends.contains(connectedToItem)) {
788
 
                                ends.append(connectedToItem);
789
 
                        }
790
 
                        continue;
791
 
                }
792
 
 
793
 
                if (chained.contains(wire)) continue;
794
 
                chained.append(wire);
795
 
        }
796
 
}
797
 
 
798
 
void Wire::collectWires(QList<Wire *> & wires) {
799
 
        if (wires.contains(this)) return;
800
 
 
801
 
        wires.append(this);
802
 
        //DebugDialog::debug(QString("collecting wire %1").arg(this->id()) );
803
 
        collectWiresAux(wires, m_connector0);
804
 
        collectWiresAux(wires, m_connector1);
805
 
}
806
 
 
807
 
void Wire::collectWiresAux(QList<Wire *> & wires, ConnectorItem * start) {
808
 
        foreach (ConnectorItem * toConnectorItem, start->connectedToItems()) {
809
 
                if (toConnectorItem->attachedToItemType() == ModelPart::Wire) {
810
 
                        dynamic_cast<Wire *>(toConnectorItem->attachedTo())->collectWires(wires);
811
 
                }
812
 
        }
813
 
 
814
 
}
815
 
 
816
 
bool Wire::stickyEnabled()
817
 
{
818
 
        return (connector0()->connectionsCount() <= 0) && (connector1()->connectionsCount() <= 0);
819
 
}
820
 
 
821
 
bool Wire::getTrace() {
822
 
        return m_viewGeometry.getAnyTrace();
823
 
}
824
 
 
825
 
bool Wire::getRouted() {
826
 
        return m_viewGeometry.getRouted();
827
 
}
828
 
 
829
 
void Wire::setRouted(bool routed) {
830
 
        m_viewGeometry.setRouted(routed);
831
 
}
832
 
 
833
 
void Wire::setRatsnest(bool ratsnest) {
834
 
        m_viewGeometry.setRatsnest(ratsnest);
835
 
}
836
 
 
837
 
bool Wire::getRatsnest() {
838
 
        return m_viewGeometry.getRatsnest();
839
 
}
840
 
 
841
 
void Wire::setAutoroutable(bool ar) {
842
 
        m_viewGeometry.setAutoroutable(ar);
843
 
}
844
 
 
845
 
bool Wire::getAutoroutable() {
846
 
        return m_viewGeometry.getAutoroutable();
847
 
}
848
 
 
849
 
void Wire::setNormal(bool normal) {
850
 
        m_viewGeometry.setNormal(normal);
851
 
}
852
 
 
853
 
bool Wire::getNormal() {
854
 
        return m_viewGeometry.getNormal();
855
 
}
856
 
 
857
 
void Wire::setColor(const QColor & color, qreal op) {
858
 
        m_pen.setBrush(QBrush(color));
859
 
        m_opacity = op;
860
 
        m_colorName = color.name();
861
 
        this->update();
862
 
}
863
 
 
864
 
void Wire::setShadowColor(QColor & color) {
865
 
        m_shadowBrush = QBrush(color);
866
 
        m_shadowPen.setBrush(m_shadowBrush);
867
 
        m_bendpointPen.setBrush(m_shadowBrush);
868
 
        m_bendpoint2Pen.setBrush(m_shadowBrush);
869
 
        if (m_connector0) m_connector0->restoreColor(false, 0, false);
870
 
        if (m_connector0) m_connector1->restoreColor(false, 0, false);
871
 
        this->update();
872
 
}
873
 
 
874
 
const QColor & Wire::color() {
875
 
        return m_pen.brush().color();
876
 
}
877
 
 
878
 
void Wire::setWireWidth(qreal width, InfoGraphicsView * infoGraphicsView) {
879
 
        if (m_pen.widthF() == width) return;
880
 
 
881
 
        prepareGeometryChange();
882
 
        setPenWidth(width, infoGraphicsView);
883
 
        if (m_connector0) m_connector0->restoreColor(false, 0, false);
884
 
        if (m_connector1) m_connector1->restoreColor(false, 0, false);
885
 
        update();
886
 
}
887
 
 
888
 
qreal Wire::width() {
889
 
        return m_pen.widthF();
890
 
}
891
 
 
892
 
qreal Wire::mils() {
893
 
        return 1000 * m_pen.widthF() / FSvgRenderer::printerScale();
894
 
}
895
 
 
896
 
void Wire::setColorString(QString colorName, qreal op) {
897
 
        // sets a color using the name (.e. "red")
898
 
        // note: colorName is associated with a Fritzing color, not a Qt color
899
 
 
900
 
        QString colorString = colors.value(colorName, "");
901
 
        if (colorString.isEmpty()) {
902
 
                colorString = colorName;
903
 
 
904
 
                foreach (QString c, colors.keys()) {
905
 
                        if (colors.value(c).compare(colorName, Qt::CaseInsensitive) == 0) {
906
 
                                colorName = c;
907
 
                                break;
908
 
                        }
909
 
                }
910
 
        }
911
 
 
912
 
        QColor c;
913
 
        c.setNamedColor(colorString);
914
 
        setColor(c, op);
915
 
        m_colorName = colorName;
916
 
 
917
 
        colorString = shadowColors.value(colorName, "");
918
 
        if (colorString.isEmpty()) {
919
 
                colorString = colorName;
920
 
        }
921
 
 
922
 
        c.setNamedColor(colorString);
923
 
        setShadowColor(c);
924
 
}
925
 
 
926
 
QString Wire::hexString() {
927
 
        return m_pen.brush().color().name();
928
 
}
929
 
 
930
 
QString Wire::colorString() {
931
 
        return m_colorName;
932
 
}
933
 
 
934
 
void Wire::initNames() {
935
 
        if (colors.count() > 0) return;
936
 
 
937
 
        widths << 16 << 24 << 32 << 48;
938
 
        widthTrans.insert(widths[0], tr("thin (16 mil)"));
939
 
        widthTrans.insert(widths[1], tr("standard (24 mil)"));
940
 
        widthTrans.insert(widths[2], tr("thick (32 mil)"));
941
 
        widthTrans.insert(widths[3], tr("extra thick (48 mil)"));
942
 
 
943
 
        STANDARD_TRACE_WIDTH = GraphicsUtils::mils2pixels(widths[1], FSvgRenderer::printerScale());
944
 
        HALF_STANDARD_TRACE_WIDTH = STANDARD_TRACE_WIDTH / 2.0;
945
 
 
946
 
    // need a list because a hash table doesn't guarantee order 
947
 
    colorNames.append(tr("blue"));
948
 
        colorNames.append(tr("red"));
949
 
    colorNames.append(tr("black"));
950
 
        colorNames.append(tr("yellow"));
951
 
        colorNames.append(tr("green"));
952
 
        colorNames.append(tr("grey"));
953
 
        colorNames.append(tr("white"));
954
 
        colorNames.append(tr("orange"));
955
 
    colorNames.append(tr("brown"));
956
 
    colorNames.append(tr("purple"));
957
 
    colorNames.append(tr("schematic black"));
958
 
 
959
 
        // need this hash table to translate from user's language to internal color name
960
 
    colorTrans.insert(tr("blue"), "blue");
961
 
        colorTrans.insert(tr("red"), "red");
962
 
    colorTrans.insert(tr("black"), "black");
963
 
        colorTrans.insert(tr("yellow"), "yellow");
964
 
        colorTrans.insert(tr("green"), "green");
965
 
        colorTrans.insert(tr("grey"), "grey");
966
 
        colorTrans.insert(tr("white"), "white");
967
 
        colorTrans.insert(tr("orange"), "orange");
968
 
        colorTrans.insert(tr("brown"), "brown");
969
 
    colorTrans.insert(tr("purple"), "purple");
970
 
    colorTrans.insert(tr("schematic black"), "schematic black");
971
 
 
972
 
    colors.insert("blue",       "#418dd9");
973
 
        colors.insert("red",    "#cc1414");
974
 
    colors.insert("black",      "#404040");
975
 
        colors.insert("yellow", "#ffe24d");
976
 
        colors.insert("green",  "#47cc79");
977
 
        colors.insert("grey",   "#999999");
978
 
        colors.insert("white",  "#ffffff");
979
 
        colors.insert("orange", "#ff7033");
980
 
        colors.insert("jumper", ViewLayer::JumperColor);
981
 
        colors.insert("trace",  ViewLayer::Copper0Color);    
982
 
        colors.insert("trace1",  ViewLayer::Copper1Color);    
983
 
        //colors.insert("unrouted", "#000000");
984
 
        //colors.insert("blackblack", "#000000");
985
 
        colors.insert("schematicGrey", "#9d9d9d");
986
 
    colors.insert("purple", "#ab58a2");
987
 
        colors.insert("brown", "#8c3b00");
988
 
        colors.insert("schematic black", "#000000");
989
 
 
990
 
    shadowColors.insert("blue",         "#1b5bb3");
991
 
        shadowColors.insert("red",              "#8c0000");
992
 
    shadowColors.insert("black",        "#000000");
993
 
        shadowColors.insert("yellow",   "#e6ab00");
994
 
        shadowColors.insert("green",    "#00a63d");
995
 
        shadowColors.insert("grey",             "#666666");
996
 
        shadowColors.insert("white",    "#999999");
997
 
        shadowColors.insert("orange",   "#d95821");
998
 
    shadowColors.insert("jumper",       "#2d6563");
999
 
        shadowColors.insert("trace",    "#d69b00");
1000
 
        shadowColors.insert("trace1",   "#d69b00");    
1001
 
        //shadowColors.insert("unrouted", "#000000");
1002
 
    shadowColors.insert("purple",       "#7a3a73");
1003
 
    shadowColors.insert("brown",        "#6c2710");
1004
 
        shadowColors.insert("schematicGrey", "#1d1d1d");
1005
 
        //shadowColors.insert("blackblack", "#000000");
1006
 
        shadowColors.insert("schematic black", "#000000");
1007
 
}
1008
 
 
1009
 
bool Wire::hasFlag(ViewGeometry::WireFlag flag)
1010
 
{
1011
 
        return m_viewGeometry.hasFlag(flag);
1012
 
}
1013
 
 
1014
 
bool Wire::hasAnyFlag(ViewGeometry::WireFlags flags)
1015
 
{
1016
 
        return m_viewGeometry.hasAnyFlag(flags);
1017
 
}
1018
 
 
1019
 
Wire * Wire::findTraced(ViewGeometry::WireFlags flags, QList<ConnectorItem *>  & ends) {
1020
 
        QList<Wire *> chainedWires;
1021
 
        this->collectChained(chainedWires, ends);
1022
 
        if (ends.count() != 2) {
1023
 
                DebugDialog::debug(QString("wire in jumper or trace must have two ends") );
1024
 
                return NULL;
1025
 
        }
1026
 
 
1027
 
        return ConnectorItem::directlyWiredTo(ends[0], ends[1], flags);
1028
 
}
1029
 
 
1030
 
QRgb Wire::getRgb(const QString & name) {
1031
 
        QString str = colors.value(name);
1032
 
        QColor c;
1033
 
        c.setNamedColor(str);
1034
 
        return c.rgb();
1035
 
}
1036
 
 
1037
 
ViewGeometry::WireFlags Wire::wireFlags() {
1038
 
        return m_viewGeometry.wireFlags();
1039
 
}
1040
 
 
1041
 
void Wire::setWireFlags(ViewGeometry::WireFlags wireFlags) {
1042
 
        m_viewGeometry.setWireFlags(wireFlags);
1043
 
}
1044
 
 
1045
 
qreal Wire::opacity() {
1046
 
        return m_opacity;
1047
 
}
1048
 
 
1049
 
void Wire::setOpacity(qreal opacity) {
1050
 
        m_opacity = opacity;
1051
 
        this->update();
1052
 
}
1053
 
 
1054
 
bool Wire::draggingEnd() {
1055
 
        return m_dragEnd;
1056
 
}
1057
 
 
1058
 
void Wire::setCanChainMultiple(bool can) {
1059
 
        m_canChainMultiple = can;
1060
 
}
1061
 
 
1062
 
bool Wire::canChangeColor() {
1063
 
        if (getRatsnest()) return false;
1064
 
        if (!getTrace()) return true;
1065
 
 
1066
 
        return (this->m_viewIdentifier == ViewIdentifierClass::SchematicView);
1067
 
}
1068
 
 
1069
 
void Wire::collectDirectWires(QList<Wire *> & wires) {
1070
 
        if (!wires.contains(this)) {
1071
 
                wires.append(this);
1072
 
        }
1073
 
 
1074
 
        collectDirectWires(m_connector0, wires);
1075
 
        collectDirectWires(m_connector1, wires);
1076
 
}
1077
 
 
1078
 
void Wire::collectDirectWires(ConnectorItem * connectorItem, QList<Wire *> & wires) {
1079
 
        if (connectorItem->connectionsCount() != 1) return;
1080
 
 
1081
 
        ConnectorItem * toConnectorItem = connectorItem->connectedToItems()[0];
1082
 
        if (toConnectorItem->attachedToItemType() != ModelPart::Wire) return;
1083
 
 
1084
 
        Wire * nextWire = dynamic_cast<Wire *>(toConnectorItem->attachedTo());
1085
 
        if (wires.contains(nextWire)) return;
1086
 
 
1087
 
        wires.append(nextWire);
1088
 
        nextWire->collectDirectWires(nextWire->otherConnector(toConnectorItem), wires);
1089
 
}
1090
 
 
1091
 
QVariant Wire::itemChange(GraphicsItemChange change, const QVariant &value)
1092
 
{
1093
 
    if (change == ItemSelectedChange) {
1094
 
                if (m_partLabel) {
1095
 
                        m_partLabel->update();
1096
 
                }
1097
 
 
1098
 
                if (!m_ignoreSelectionChange) {
1099
 
                        QList<Wire *> chained;
1100
 
                        QList<ConnectorItem *> ends;
1101
 
                        collectChained(chained, ends);
1102
 
                        InfoGraphicsView * infoGraphicsView = InfoGraphicsView::getInfoGraphicsView(this);
1103
 
                        if (infoGraphicsView) {
1104
 
                                infoGraphicsView->setIgnoreSelectionChangeEvents(true);
1105
 
                        }
1106
 
                        // DebugDialog::debug(QString("original wire selected %1 %2").arg(value.toBool()).arg(this->id()));
1107
 
                        foreach (Wire * wire, chained) {
1108
 
                                if (wire != this ) {
1109
 
                                        wire->setIgnoreSelectionChange(true);
1110
 
                                        wire->setSelected(value.toBool());
1111
 
                                        wire->setIgnoreSelectionChange(false);
1112
 
                                        // DebugDialog::debug(QString("wire selected %1 %2").arg(value.toBool()).arg(wire->id()));
1113
 
                                }
1114
 
                        }
1115
 
                        if (infoGraphicsView) {
1116
 
                                infoGraphicsView->setIgnoreSelectionChangeEvents(false);
1117
 
                        }
1118
 
                }
1119
 
    }
1120
 
    return ItemBase::itemChange(change, value);
1121
 
}
1122
 
 
1123
 
void Wire::cleanup() {
1124
 
}
1125
 
 
1126
 
void Wire::getConnectedColor(ConnectorItem * connectorItem, QBrush * &brush, QPen * &pen, qreal & opacity, qreal & negativePenWidth, bool & negativeOffsetRect) {
1127
 
 
1128
 
        connectorItem->setBigDot(false);
1129
 
        int count = 0;
1130
 
        bool bendpoint = true;
1131
 
        foreach (ConnectorItem * toConnectorItem, connectorItem->connectedToItems()) {
1132
 
                if (toConnectorItem->attachedToItemType() != ModelPart::Wire) {
1133
 
                        // for drawing a big dot on the end of a part connector in schematic view if the part is connected to more than one trace
1134
 
                        bendpoint = false;
1135
 
                        if (toConnectorItem->connectionsCount() > 1) {  
1136
 
                                InfoGraphicsView * infoGraphicsView = InfoGraphicsView::getInfoGraphicsView(this);
1137
 
                                if (infoGraphicsView != NULL && infoGraphicsView->hasBigDots()) {
1138
 
                                        int c = 0;
1139
 
                                        foreach (ConnectorItem * totoConnectorItem, toConnectorItem->connectedToItems()) {
1140
 
                                                if (totoConnectorItem->attachedToItemType() == ModelPart::Wire) {
1141
 
                                                        Wire * w = dynamic_cast<Wire *>(totoConnectorItem->attachedTo());
1142
 
                                                        if (w && w->getTrace()) {
1143
 
                                                                c++;
1144
 
                                                        }
1145
 
                                                }
1146
 
                                        }
1147
 
                                        if (c > 1) {
1148
 
                                                count = 2;
1149
 
                                                break;
1150
 
                                        }
1151
 
                                }
1152
 
                        }
1153
 
 
1154
 
                        ItemBase::getConnectedColor(connectorItem, brush, pen, opacity, negativePenWidth, negativeOffsetRect);
1155
 
                        return;
1156
 
                }
1157
 
 
1158
 
                count++;
1159
 
        }
1160
 
 
1161
 
        if (count == 0) {
1162
 
                ItemBase::getConnectedColor(connectorItem, brush, pen, opacity, negativePenWidth, negativeOffsetRect);
1163
 
                return;
1164
 
        }
1165
 
        
1166
 
        // connectorItem is a bendpoint or connects to a multiply connected connector
1167
 
 
1168
 
        //if (!bendpoint) {
1169
 
                //DebugDialog::debug(QString("big dot %1 %2 %3").arg(this->id()).arg(connectorItem->connectorSharedID()).arg(count));
1170
 
        //}
1171
 
 
1172
 
        brush = &m_shadowBrush;
1173
 
        opacity = 1.0;
1174
 
        if (count > 1) {
1175
 
                // only ever reach here when drawing a connector that is connected to more than one trace
1176
 
                pen = &m_bendpoint2Pen;
1177
 
                negativePenWidth = m_bendpoint2Width;
1178
 
                negativeOffsetRect = m_negativeOffsetRect;
1179
 
                connectorItem->setBigDot(true);
1180
 
        }
1181
 
        else {
1182
 
                negativeOffsetRect = m_negativeOffsetRect;
1183
 
                negativePenWidth = m_bendpointWidth;
1184
 
                pen = &m_bendpointPen;
1185
 
        }
1186
 
}
1187
 
 
1188
 
void Wire::setPenWidth(qreal w, InfoGraphicsView * infoGraphicsView) {
1189
 
        m_pen.setWidthF(w);
1190
 
        infoGraphicsView->getBendpointWidths(this, w, m_bendpointWidth, m_bendpoint2Width, m_negativeOffsetRect);
1191
 
        m_bendpointPen.setWidthF(qAbs(m_bendpointWidth));
1192
 
        m_bendpoint2Pen.setWidthF(qAbs(m_bendpoint2Width));
1193
 
        m_shadowPen.setWidthF(w + 2);
1194
 
}
1195
 
 
1196
 
bool Wire::connectionIsAllowed(ConnectorItem * to) {
1197
 
        if (!ItemBase::connectionIsAllowed(to)) return false;
1198
 
 
1199
 
        Wire * w = dynamic_cast<Wire *>(to->attachedTo());
1200
 
        if (w == NULL) return true;
1201
 
 
1202
 
        if (w->getRatsnest()) return false;
1203
 
 
1204
 
        return m_viewIdentifier != ViewIdentifierClass::BreadboardView;
1205
 
}
1206
 
 
1207
 
bool Wire::isGrounded() {
1208
 
        return ConnectorItem::isGrounded(connector0(), connector1());
1209
 
}
1210
 
 
1211
 
bool Wire::acceptsMouseDoubleClickConnectorEvent(ConnectorItem *, QGraphicsSceneMouseEvent *) {
1212
 
        return true;
1213
 
}
1214
 
 
1215
 
bool Wire::acceptsMouseMoveConnectorEvent(ConnectorItem *, QGraphicsSceneMouseEvent *) {
1216
 
        return true;
1217
 
}
1218
 
 
1219
 
bool Wire::acceptsMouseReleaseConnectorEvent(ConnectorItem *, QGraphicsSceneMouseEvent *) {
1220
 
        return true;
1221
 
}
1222
 
 
1223
 
void Wire::setIgnoreSelectionChange(bool ignore) {
1224
 
        m_ignoreSelectionChange = ignore;
1225
 
}
1226
 
 
1227
 
bool Wire::collectExtraInfo(QWidget * parent, const QString & family, const QString & prop, const QString & value, bool swappingEnabled, QString & returnProp, QString & returnValue, QWidget * & returnWidget)
1228
 
{
1229
 
        if (prop.compare("width", Qt::CaseInsensitive) == 0) {
1230
 
                // don't display width property
1231
 
                return false;
1232
 
        }
1233
 
 
1234
 
        if (prop.compare("color", Qt::CaseInsensitive) == 0) {
1235
 
                returnProp = tr("color");
1236
 
                if (canChangeColor()) {
1237
 
                        QComboBox * comboBox = new QComboBox(parent);
1238
 
                        comboBox->setEditable(false);
1239
 
                        comboBox->setEnabled(swappingEnabled);
1240
 
                        comboBox->setObjectName("infoViewComboBox");
1241
 
                        
1242
 
                        int ix = 0;
1243
 
                        QString englishCurrColor = colorString();
1244
 
                        foreach(QString transColorName, Wire::colorNames) {
1245
 
                                QString englishColorName = Wire::colorTrans.value(transColorName);
1246
 
                                comboBox->addItem(transColorName, QVariant(englishColorName));
1247
 
                                if (englishColorName.compare(englishCurrColor, Qt::CaseInsensitive) == 0) {
1248
 
                                        comboBox->setCurrentIndex(ix);
1249
 
                                }
1250
 
                                ix++;
1251
 
                        }
1252
 
 
1253
 
                        connect(comboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(colorEntry(const QString &)));
1254
 
                        returnWidget = comboBox;
1255
 
                        returnValue = comboBox->currentText();
1256
 
                        return true;
1257
 
                }
1258
 
                else {
1259
 
                        returnWidget = NULL;
1260
 
                        returnValue = colorString();
1261
 
                        return true;
1262
 
                }
1263
 
        }
1264
 
 
1265
 
        return ItemBase::collectExtraInfo(parent, family, prop, value, swappingEnabled, returnProp, returnValue, returnWidget);
1266
 
}
1267
 
 
1268
 
void Wire::colorEntry(const QString & text) {
1269
 
        Q_UNUSED(text);
1270
 
 
1271
 
        QComboBox * comboBox = dynamic_cast<QComboBox *>(sender());
1272
 
        if (comboBox == NULL) return;
1273
 
 
1274
 
        QString color = comboBox->itemData(comboBox->currentIndex()).toString();
1275
 
 
1276
 
        InfoGraphicsView * infoGraphicsView = InfoGraphicsView::getInfoGraphicsView(this);
1277
 
        if (infoGraphicsView != NULL) {
1278
 
                infoGraphicsView->changeWireColor(color);
1279
 
        }
1280
 
}
1281
 
 
1282
 
bool Wire::hasPartLabel() {
1283
 
        
1284
 
        return false;
1285
 
}
1286
 
 
1287
 
ItemBase::PluralType Wire::isPlural() {
1288
 
        return Plural;
1289
 
}
1290
 
 
1291
 
void Wire::checkVisibility(ConnectorItem * onMe, ConnectorItem * onIt, bool connect) {
1292
 
        if (connect) {
1293
 
                if (!onIt->attachedTo()->isVisible()) {
1294
 
                        this->setVisible(false);
1295
 
                }
1296
 
                else {
1297
 
                        ConnectorItem * other = otherConnector(onMe);
1298
 
                        foreach (ConnectorItem * toConnectorItem, other->connectedToItems()) {
1299
 
                                if (toConnectorItem->attachedToItemType() == ModelPart::Wire) continue;
1300
 
 
1301
 
                                if (!toConnectorItem->attachedTo()->isVisible()) {
1302
 
                                        this->setVisible(false);
1303
 
                                        break;
1304
 
                                }
1305
 
                        }
1306
 
                }
1307
 
        }
1308
 
}
1309
 
 
1310
 
bool Wire::canSwitchLayers() {
1311
 
        return false;
1312
 
}
1313
 
 
1314
 
bool Wire::hasPartNumberProperty()
1315
 
{
1316
 
        return false;
1317
 
}
1318
 
 
1319
 
bool Wire::rotationAllowed() {
1320
 
        return false;
1321
 
}
1322
 
 
1323
 
bool Wire::rotation45Allowed() {
1324
 
        return false;
1325
 
}
1326
 
 
1327
 
void Wire::addedToScene(bool temporary) {
1328
 
        ItemBase::addedToScene(temporary);
1329
 
 
1330
 
        InfoGraphicsView * infoGraphicsView = InfoGraphicsView::getInfoGraphicsView(this);
1331
 
        if (infoGraphicsView == NULL) return;
1332
 
 
1333
 
        bool succeeded = connect(this, SIGNAL(wireChangedSignal(Wire*, QLineF, QLineF, QPointF, QPointF, ConnectorItem *, ConnectorItem *)      ),
1334
 
                        infoGraphicsView, SLOT(wire_wireChanged(Wire*, QLineF, QLineF, QPointF, QPointF, ConnectorItem *, ConnectorItem *)),
1335
 
                        Qt::DirectConnection);          // DirectConnection means call the slot directly like a subroutine, without waiting for a thread or queue
1336
 
        succeeded = succeeded && connect(this, SIGNAL(wireSplitSignal(Wire*, QPointF, QPointF, QLineF)),
1337
 
                        infoGraphicsView, SLOT(wire_wireSplit(Wire*, QPointF, QPointF, QLineF)));
1338
 
        succeeded = succeeded && connect(this, SIGNAL(wireJoinSignal(Wire*, ConnectorItem *)),
1339
 
                        infoGraphicsView, SLOT(wire_wireJoin(Wire*, ConnectorItem*)));
1340
 
        if (!succeeded) {
1341
 
                DebugDialog::debug("wire signal connect failed");
1342
 
        }
1343
 
}
 
1
/*******************************************************************
 
2
 
 
3
Part of the Fritzing project - http://fritzing.org
 
4
Copyright (c) 2007-2011 Fachhochschule Potsdam - http://fh-potsdam.de
 
5
 
 
6
Fritzing is free software: you can redistribute it and/or modify
 
7
it under the terms of the GNU General Public License as published by
 
8
the Free Software Foundation, either version 3 of the License, or
 
9
(at your option) any later version.
 
10
 
 
11
Fritzing is distributed in the hope that it will be useful,
 
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
GNU General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License
 
17
along with Fritzing.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
********************************************************************
 
20
 
 
21
$Revision: 5417 $:
 
22
$Author: cohen@irascible.com $:
 
23
$Date: 2011-08-18 12:47:41 +0200 (Thu, 18 Aug 2011) $
 
24
 
 
25
********************************************************************/
 
26
 
 
27
/* 
 
28
 
 
29
curvy To Do
 
30
 
 
31
        * pixel turds
 
32
 
 
33
        * undo/redo
 
34
 
 
35
        * save/load
 
36
 
 
37
        * copy/paste
 
38
 
 
39
        * make-straight function
 
40
 
 
41
        * export
 
42
 
 
43
        curvy to begin with? would have to vary with some function of angle and distance
 
44
                could convert control points to t values?
 
45
 
 
46
        turn curvature on/off per view
 
47
 
 
48
---------------------------------------------------------
 
49
 
 
50
later:
 
51
 
 
52
        clippable wire
 
53
 
 
54
        gerber
 
55
 
 
56
        autorouter warning in PCB view
 
57
 
 
58
        modify parameters (tension, unit area)?
 
59
 
 
60
*/
 
61
 
 
62
/////////////////////////////////////////////////////////////////
 
63
 
 
64
#include "wire.h"
 
65
 
 
66
#include <QLineF>
 
67
#include <QPen>
 
68
#include <QRadialGradient>
 
69
#include <QBrush>
 
70
#include <QPen>
 
71
#include <QGraphicsScene>
 
72
#include <QList>
 
73
#include <QGraphicsItem>
 
74
#include <QSet>
 
75
#include <QComboBox>
 
76
#include <QToolTip>
 
77
#include <QApplication>
 
78
 
 
79
#include "../debugdialog.h"
 
80
#include "../sketch/infographicsview.h"
 
81
#include "../connectors/connectoritem.h"
 
82
#include "../connectors/svgidlayer.h"
 
83
#include "../fsvgrenderer.h"
 
84
#include "partlabel.h"
 
85
#include "../model/modelpart.h"
 
86
#include "../utils/graphicsutils.h"
 
87
#include "../utils/textutils.h"
 
88
#include "../utils/bezier.h"
 
89
#include "../utils/bezierdisplay.h"
 
90
#include "../layerattributes.h"
 
91
 
 
92
#include <stdlib.h>
 
93
 
 
94
QHash<QString, QString> Wire::colors;
 
95
QHash<QString, QString> Wire::shadowColors;
 
96
QHash<QString, QString> Wire::colorTrans;
 
97
QStringList Wire::colorNames;
 
98
QHash<int, QString> Wire::widthTrans;
 
99
QList<int> Wire::widths;
 
100
double Wire::STANDARD_TRACE_WIDTH;
 
101
double Wire::HALF_STANDARD_TRACE_WIDTH;
 
102
double Wire::THIN_TRACE_WIDTH;
 
103
 
 
104
const double DefaultHoverStrokeWidth = 4;
 
105
 
 
106
static Bezier UndoBezier;
 
107
static BezierDisplay * TheBezierDisplay = NULL;
 
108
 
 
109
////////////////////////////////////////////////////////////
 
110
 
 
111
bool alphaLessThan(QColor * c1, QColor * c2)
 
112
{
 
113
        return c1->alpha() < c2->alpha();
 
114
}
 
115
 
 
116
/////////////////////////////////////////////////////////////
 
117
 
 
118
 
 
119
WireMenu::WireMenu(const QString & title, QWidget * parent) : QMenu(title, parent) 
 
120
{
 
121
        m_wire = NULL;
 
122
}
 
123
 
 
124
void WireMenu::setWire(Wire * w) {
 
125
        m_wire = w;
 
126
}
 
127
 
 
128
Wire * WireMenu::wire() {
 
129
        return m_wire;
 
130
}
 
131
 
 
132
/////////////////////////////////////////////////////////////
 
133
 
 
134
WireAction::WireAction(QAction * action) : QAction(action) {
 
135
        m_wire = NULL;
 
136
        this->setText(action->text());
 
137
        this->setStatusTip(action->statusTip());
 
138
        this->setCheckable(action->isCheckable());
 
139
}
 
140
 
 
141
WireAction::WireAction(const QString & title, QObject * parent) : QAction(title, parent) {
 
142
        m_wire = NULL;
 
143
}
 
144
 
 
145
void WireAction::setWire(Wire * w) {
 
146
        m_wire = w;
 
147
}
 
148
 
 
149
Wire * WireAction::wire() {
 
150
        return m_wire;
 
151
}
 
152
 
 
153
/////////////////////////////////////////////////////////////
 
154
 
 
155
Wire::Wire( ModelPart * modelPart, ViewIdentifierClass::ViewIdentifier viewIdentifier,  const ViewGeometry & viewGeometry, long id, QMenu* itemMenu, bool initLabel)
 
156
        : ItemBase(modelPart, viewIdentifier, viewGeometry, id, itemMenu)
 
157
{
 
158
        m_bezier = NULL;
 
159
        m_canHaveCurve = true;
 
160
        m_hoverStrokeWidth = DefaultHoverStrokeWidth;
 
161
        m_connector0 = m_connector1 = NULL;
 
162
        m_partLabel = initLabel ? new PartLabel(this, NULL) : NULL;
 
163
        m_canChainMultiple = false;
 
164
    setFlag(QGraphicsItem::ItemIsSelectable, true );
 
165
        m_connectorHover = NULL;
 
166
        m_opacity = 1.0;
 
167
        m_ignoreSelectionChange = false;
 
168
 
 
169
        //DebugDialog::debug(QString("aix line %1 %2 %3 %4").arg(this->viewGeometry().line().x1())
 
170
                                                                                                        //.arg(this->viewGeometry().line().y1())
 
171
                                                                                                        //.arg(this->viewGeometry().line().x2())
 
172
                                                                                                        //.arg(this->viewGeometry().line().y2()) );
 
173
        //DebugDialog::debug(QString("aix loc %1 %2").arg(this->viewGeometry().loc().x())
 
174
                                                                                                                //.arg(this->viewGeometry().loc().y()) );
 
175
 
 
176
        setPos(m_viewGeometry.loc());
 
177
 
 
178
        m_dragCurve = m_dragEnd = false;
 
179
}
 
180
 
 
181
Wire::~Wire() {
 
182
        if (m_bezier) {
 
183
                delete m_bezier;
 
184
        }
 
185
}
 
186
 
 
187
FSvgRenderer * Wire::setUp(ViewLayer::ViewLayerID viewLayerID, const LayerHash &  viewLayers, InfoGraphicsView * infoGraphicsView) {
 
188
        ItemBase::setViewLayerID(viewLayerID, viewLayers);
 
189
        FSvgRenderer * svgRenderer = setUpConnectors(m_modelPart, m_viewIdentifier);
 
190
        if (svgRenderer != NULL) {
 
191
                initEnds(m_viewGeometry, svgRenderer->viewBox(), infoGraphicsView);
 
192
                setConnectorTooltips();
 
193
        }
 
194
        setZValue(this->z());
 
195
 
 
196
        return svgRenderer;
 
197
}
 
198
 
 
199
void Wire::saveGeometry() {
 
200
        m_viewGeometry.setSelected(this->isSelected());
 
201
        m_viewGeometry.setLine(this->line());
 
202
        m_viewGeometry.setLoc(this->pos());
 
203
        m_viewGeometry.setZ(this->zValue());
 
204
}
 
205
 
 
206
 
 
207
bool Wire::itemMoved() {
 
208
        if  (m_viewGeometry.loc() != this->pos()) return true;
 
209
 
 
210
        if (this->line().dx() != m_viewGeometry.line().dx()) return false;
 
211
        if (this->line().dy() != m_viewGeometry.line().dy()) return false;
 
212
 
 
213
        return (this->line() != m_viewGeometry.line());
 
214
}
 
215
 
 
216
void Wire::moveItem(ViewGeometry & viewGeometry) {
 
217
        this->setPos(viewGeometry.loc());
 
218
        this->setLine(viewGeometry.line());
 
219
}
 
220
 
 
221
void Wire::initEnds(const ViewGeometry & vg, QRectF defaultRect, InfoGraphicsView * infoGraphicsView) {
 
222
        bool gotOne = false;
 
223
        bool gotTwo = false;
 
224
        double penWidth = 1;
 
225
        foreach (ConnectorItem * item, cachedConnectorItems()) {
 
226
                // check the name or is order good enough?
 
227
 
 
228
                if (gotOne) {
 
229
                        gotTwo = true;
 
230
                        m_connector1 = item;
 
231
                        break;
 
232
                }
 
233
                else {
 
234
                        penWidth = item->rect().width();
 
235
                        m_connector0 = item;
 
236
                        gotOne = true;
 
237
                }
 
238
        }
 
239
 
 
240
        if (!gotTwo) {
 
241
                return;
 
242
        }
 
243
 
 
244
        if ((vg.line().length() == 0) && (vg.line().x1() == -1)) {
 
245
                this->setLine(defaultRect.left(), defaultRect.top(), defaultRect.right(), defaultRect.bottom());
 
246
        }
 
247
        else {
 
248
                this->setLine(vg.line());
 
249
        }
 
250
 
 
251
        setConnector0Rect();
 
252
        setConnector1Rect();
 
253
        m_viewGeometry.setLine(this->line());
 
254
        
 
255
        QBrush brush(QColor(0, 0, 0));
 
256
        QPen pen(brush, penWidth, Qt::SolidLine, Qt::RoundCap);
 
257
        this->setPen(pen);
 
258
 
 
259
        m_pen.setCapStyle(Qt::RoundCap);
 
260
        m_shadowPen.setCapStyle(Qt::RoundCap);
 
261
        if (infoGraphicsView != NULL) {
 
262
                infoGraphicsView->initWire(this, penWidth);
 
263
        }
 
264
 
 
265
        prepareGeometryChange();
 
266
}
 
267
 
 
268
void Wire::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget ) {
 
269
        if (m_hidden) return;
 
270
 
 
271
        ItemBase::paint(painter, option, widget);
 
272
}
 
273
 
 
274
void Wire::paintBody(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget ) 
 
275
{
 
276
        Q_UNUSED(option);
 
277
        Q_UNUSED(widget);
 
278
 
 
279
        QPainterPath painterPath;
 
280
        if (m_bezier && !m_bezier->isEmpty()) {
 
281
                QLineF line = this->line();
 
282
                painterPath.moveTo(line.p1());
 
283
                painterPath.cubicTo(m_bezier->cp0(), m_bezier->cp1(), line.p2());
 
284
                
 
285
                /*
 
286
                DebugDialog::debug(QString("c0x:%1,c0y:%2 c1x:%3,c1y:%4 p0x:%5,p0y:%6 p1x:%7,p1y:%8 px:%9,py:%10")
 
287
                                                        .arg(m_controlPoints.at(0).x())
 
288
                                                        .arg(m_controlPoints.at(0).y())
 
289
                                                        .arg(m_controlPoints.at(1).x())
 
290
                                                        .arg(m_controlPoints.at(1).y())
 
291
                                                        .arg(m_line.p1().x())
 
292
                                                        .arg(m_line.p1().y())
 
293
                                                        .arg(m_line.p2().x())
 
294
                                                        .arg(m_line.p2().y())
 
295
                                                        .arg(pos().x())
 
296
                                                        .arg(pos().y())
 
297
 
 
298
                                                        );
 
299
                */
 
300
        }
 
301
 
 
302
 
 
303
        painter->setOpacity(m_inactive ? m_opacity  / 2 : m_opacity);
 
304
        if (hasShadow()) {
 
305
                painter->save();
 
306
                painter->setPen(m_shadowPen);
 
307
                if (painterPath.isEmpty()) {
 
308
                        QLineF line = this->line();
 
309
                        painter->drawLine(line);
 
310
                }
 
311
                else {
 
312
                        painter->drawPath(painterPath);
 
313
                }
 
314
                painter->restore();
 
315
        }
 
316
           
 
317
        painter->setPen(m_pen);
 
318
        if (painterPath.isEmpty()) {
 
319
                painter->drawLine(getPaintLine());      
 
320
        }
 
321
        else {
 
322
                painter->drawPath(painterPath);
 
323
        }
 
324
}
 
325
 
 
326
void Wire::paintHover(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 
 
327
{
 
328
        Q_UNUSED(widget);
 
329
        Q_UNUSED(option);
 
330
        painter->save();
 
331
        if ((m_connectorHoverCount > 0 && !(m_dragEnd || m_dragCurve)) || m_connectorHoverCount2 > 0) {
 
332
                painter->setOpacity(.50);
 
333
                painter->fillPath(this->hoverShape(), QBrush(connectorHoverColor));
 
334
        }
 
335
        else {
 
336
                painter->setOpacity(hoverOpacity);
 
337
                painter->fillPath(this->hoverShape(), QBrush(hoverColor));
 
338
        }
 
339
        painter->restore();
 
340
}
 
341
 
 
342
QPainterPath Wire::hoverShape() const
 
343
{
 
344
        return shapeAux(m_hoverStrokeWidth);
 
345
}
 
346
 
 
347
QPainterPath Wire::shape() const
 
348
{
 
349
        return shapeAux(m_pen.widthF());
 
350
}
 
351
 
 
352
QPainterPath Wire::shapeAux(double width) const
 
353
{
 
354
        QPainterPath path;
 
355
        if (m_line == QLineF()) {
 
356
            return path;
 
357
        }
 
358
                                
 
359
        path.moveTo(m_line.p1());
 
360
        if (m_bezier == NULL || m_bezier->isEmpty()) {
 
361
                path.lineTo(m_line.p2());
 
362
        }
 
363
        else {
 
364
                path.cubicTo(m_bezier->cp0(), m_bezier->cp1(), m_line.p2());
 
365
        }
 
366
        //DebugDialog::debug(QString("using hoverstrokewidth %1 %2").arg(m_id).arg(m_hoverStrokeWidth));
 
367
        return GraphicsUtils::shapeFromPath(path, m_pen, width, false);
 
368
}
 
369
 
 
370
QRectF Wire::boundingRect() const
 
371
{
 
372
        if (m_pen.widthF() == 0.0) {
 
373
            const double x1 = m_line.p1().x();
 
374
            const double x2 = m_line.p2().x();
 
375
            const double y1 = m_line.p1().y();
 
376
            const double y2 = m_line.p2().y();
 
377
            double lx = qMin(x1, x2);
 
378
            double rx = qMax(x1, x2);
 
379
            double ty = qMin(y1, y2);
 
380
            double by = qMax(y1, y2);
 
381
            return QRectF(lx, ty, rx - lx, by - ty);
 
382
        }
 
383
        return hoverShape().controlPointRect();
 
384
}
 
385
 
 
386
void Wire::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
 
387
        //DebugDialog::debug("checking press event");
 
388
        emit wireSplitSignal(this, event->scenePos(), this->pos(), this->line());
 
389
}
 
390
 
 
391
void Wire::mousePressEvent(QGraphicsSceneMouseEvent *event)
 
392
{
 
393
        WireMenu * wireMenu = qobject_cast<WireMenu *>(m_itemMenu);
 
394
        if (wireMenu) {
 
395
                wireMenu->setWire(this);
 
396
        }
 
397
        ItemBase::mousePressEvent(event);
 
398
}
 
399
 
 
400
void Wire::initDragCurve(QPointF scenePos) {
 
401
        if (m_bezier == NULL) {
 
402
                m_bezier = new Bezier();
 
403
        }
 
404
 
 
405
        UndoBezier.copy(m_bezier);
 
406
 
 
407
        m_dragCurve = true;
 
408
        m_dragEnd = false;
 
409
 
 
410
        QPointF p0 = connector0()->sceneAdjustedTerminalPoint(NULL);
 
411
        QPointF p1 = connector1()->sceneAdjustedTerminalPoint(NULL);
 
412
        if (m_bezier->isEmpty()) {
 
413
                m_bezier->initToEnds(mapFromScene(p0), mapFromScene(p1));
 
414
        }
 
415
        else {
 
416
                m_bezier->set_endpoints(mapFromScene(p0), mapFromScene(p1));
 
417
        }
 
418
 
 
419
        m_bezier->initControlIndex(mapFromScene(scenePos), m_pen.widthF());
 
420
        TheBezierDisplay = new BezierDisplay;
 
421
        TheBezierDisplay->initDisplay(this, m_bezier);
 
422
}
 
423
 
 
424
bool Wire::initNewBendpoint(QPointF scenePos, Bezier & left, Bezier & right) {
 
425
        if (m_bezier == NULL || m_bezier->isEmpty()) {
 
426
                UndoBezier.clear();
 
427
                return false;
 
428
        }
 
429
 
 
430
        QPointF p0 = connector0()->sceneAdjustedTerminalPoint(NULL);
 
431
        QPointF p1 = connector1()->sceneAdjustedTerminalPoint(NULL);
 
432
        m_bezier->set_endpoints(mapFromScene(p0), mapFromScene(p1));
 
433
        UndoBezier.copy(m_bezier);
 
434
 
 
435
        double t = m_bezier->findSplit(mapFromScene(scenePos), m_pen.widthF());
 
436
        m_bezier->split(t, left, right);
 
437
        return true;
 
438
}
 
439
 
 
440
void Wire::initDragEnd(ConnectorItem * connectorItem, QPointF scenePos) {
 
441
        Q_UNUSED(scenePos);
 
442
        saveGeometry();
 
443
        QLineF line = this->line();
 
444
        m_drag0 = (connectorItem == m_connector0);
 
445
        m_dragEnd = true;
 
446
        m_dragCurve = false;
 
447
        if (m_drag0) {
 
448
                m_wireDragOrigin = line.p2();
 
449
                //DebugDialog::debug(QString("drag near origin %1 %2").arg(m_wireDragOrigin.x()).arg(m_wireDragOrigin.y()) );
 
450
        }
 
451
        else {
 
452
                m_wireDragOrigin = line.p1();
 
453
                //DebugDialog::debug(QString("drag far origin %1 %2").arg(m_wireDragOrigin.x()).arg(m_wireDragOrigin.y()) );
 
454
                //DebugDialog::debug(QString("drag far other %1 %2").arg(line.p2().x()).arg(line.p2().y()) );
 
455
        }
 
456
 
 
457
        if (connectorItem->chained()) {
 
458
                QList<Wire *> chained;
 
459
                QList<ConnectorItem *> ends;
 
460
                collectChained(chained, ends);
 
461
                // already saved the first one
 
462
                for (int i = 1; i < chained.count(); i++) {
 
463
                        chained[i]->saveGeometry();
 
464
                }
 
465
        }
 
466
}
 
467
 
 
468
 
 
469
void Wire::mouseReleaseConnectorEvent(ConnectorItem * connectorItem, QGraphicsSceneMouseEvent * event) {
 
470
        Q_UNUSED(event);
 
471
        Q_UNUSED(connectorItem);
 
472
        releaseDrag();
 
473
}
 
474
 
 
475
void Wire::mouseMoveConnectorEvent(ConnectorItem * connectorItem, QGraphicsSceneMouseEvent * event) {
 
476
        mouseMoveEventAux(this->mapFromItem(connectorItem, event->pos()), event->modifiers());
 
477
}
 
478
 
 
479
 
 
480
void Wire::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
 
481
        mouseMoveEventAux(event->pos(), event->modifiers());
 
482
}
 
483
 
 
484
void Wire::mouseMoveEventAux(QPointF eventPos, Qt::KeyboardModifiers modifiers) {
 
485
        if (m_spaceBarWasPressed) {
 
486
                return;
 
487
        }
 
488
 
 
489
        if (m_dragCurve) {
 
490
                prepareGeometryChange();
 
491
                dragCurve(eventPos, modifiers);
 
492
                update();
 
493
                if (TheBezierDisplay) TheBezierDisplay->updateDisplay(this, m_bezier);
 
494
                return;
 
495
        }
 
496
 
 
497
        if (m_dragEnd == false) {
 
498
                return;
 
499
        }
 
500
 
 
501
        ConnectorItem * whichConnectorItem;
 
502
        ConnectorItem * otherConnectorItem;
 
503
        if (m_drag0) {
 
504
                whichConnectorItem = m_connector0;
 
505
                otherConnectorItem = m_connector1;
 
506
        }
 
507
        else {
 
508
                whichConnectorItem = m_connector1;
 
509
                otherConnectorItem = m_connector0;
 
510
        }
 
511
 
 
512
        if ((modifiers & Qt::ShiftModifier) != 0) {
 
513
                QPointF initialPos = mapFromScene(otherConnectorItem->sceneAdjustedTerminalPoint(NULL)); 
 
514
                bool bendpoint = isBendpoint(whichConnectorItem);
 
515
                if (bendpoint) {
 
516
                        bendpoint = false;
 
517
                        foreach (ConnectorItem * ci, whichConnectorItem->connectedToItems()) {
 
518
                                Wire * w = qobject_cast<Wire *>(ci->attachedTo());
 
519
                                ConnectorItem * oci = w->otherConnector(ci);
 
520
                                QPointF otherInitialPos = mapFromScene(oci->sceneAdjustedTerminalPoint(NULL));
 
521
                                QPointF p1(initialPos.x(), otherInitialPos.y());
 
522
                                double d = GraphicsUtils::distanceSqd(p1, eventPos);
 
523
                                if (d <= 144) {
 
524
                                        bendpoint = true;
 
525
                                        eventPos = p1;
 
526
                                        break;
 
527
                                }
 
528
                                p1.setX(otherInitialPos.x());
 
529
                                p1.setY(initialPos.y());
 
530
                                d = GraphicsUtils::distanceSqd(p1, eventPos);
 
531
                                if (d <= 144) {
 
532
                                        bendpoint = true;
 
533
                                        eventPos = p1;
 
534
                                        break;
 
535
                                }                               
 
536
                        }
 
537
                }
 
538
 
 
539
                if (!bendpoint) {
 
540
                        eventPos = GraphicsUtils::calcConstraint(initialPos, eventPos);
 
541
                }
 
542
 
 
543
        }
 
544
 
 
545
        if (m_drag0) {
 
546
                QPointF p = this->mapToScene(eventPos);
 
547
                QGraphicsSvgItem::setPos(p.x(), p.y());
 
548
                this->setLine(0, 0, m_wireDragOrigin.x() - p.x() + m_viewGeometry.loc().x(),
 
549
                                                        m_wireDragOrigin.y() - p.y() + m_viewGeometry.loc().y() );
 
550
                //DebugDialog::debug(QString("drag0 wdo:(%1,%2) p:(%3,%4) vg:(%5,%6) l:(%7,%8)")
 
551
                //                      .arg(m_wireDragOrigin.x()).arg(m_wireDragOrigin.y())
 
552
                //                      .arg(p.x()).arg(p.y())
 
553
                //                      .arg(m_viewGeometry.loc().x()).arg(m_viewGeometry.loc().y())
 
554
                //                      .arg(line().p2().x()).arg(line().p2().y())
 
555
                //      );
 
556
        }
 
557
        else {
 
558
                this->setLine(m_wireDragOrigin.x(), m_wireDragOrigin.y(), eventPos.x(), eventPos.y());
 
559
                //DebugDialog::debug(QString("drag1 wdo:(%1,%2) ep:(%3,%4) p:(%5,%6) l:(%7,%8)")
 
560
                //                      .arg(m_wireDragOrigin.x()).arg(m_wireDragOrigin.y())
 
561
                //                      .arg(eventPos.x()).arg(eventPos.y())
 
562
                //                      .arg(pos().x()).arg(pos().y())
 
563
                //                      .arg(line().p2().x()).arg(line().p2().y())
 
564
                //      );
 
565
        }
 
566
        setConnector1Rect();
 
567
 
 
568
        bool chained = false;
 
569
        foreach (ConnectorItem * toConnectorItem, whichConnectorItem->connectedToItems()) {
 
570
                Wire * chainedWire = qobject_cast<Wire *>(toConnectorItem->attachedTo());
 
571
                if (chainedWire == NULL) continue;
 
572
 
 
573
                chainedWire->simpleConnectedMoved(whichConnectorItem, toConnectorItem);
 
574
                chained = true;
 
575
        }
 
576
 
 
577
        if (!chained) {
 
578
                // don't allow wire to connect back to something the other end is already directly connected to
 
579
                QList<Wire *> wires;
 
580
                QList<ConnectorItem *> ends;
 
581
                collectChained(otherConnectorItem, wires, ends);
 
582
                for (int i = 0; i < wires.count(); i++) {
 
583
                        Wire * w = wires[i];
 
584
                        collectChained(w->m_connector1, wires, ends);
 
585
                        collectChained(w->m_connector0, wires, ends);
 
586
                }
 
587
                ends.append(otherConnectorItem);
 
588
                foreach (Wire * w, wires) {
 
589
                        ends.append(w->connector0());
 
590
                        ends.append(w->connector1());
 
591
                }
 
592
                foreach (ConnectorItem * toConnectorItem, whichConnectorItem->connectedToItems()) {
 
593
                        ends.removeOne(toConnectorItem);
 
594
                }
 
595
 
 
596
                ConnectorItem * originatingConnector = NULL;
 
597
                if (otherConnectorItem && otherConnectorItem->connectionsCount() > 0) {
 
598
                        originatingConnector = otherConnectorItem->connectedToItems()[0];
 
599
                }
 
600
                whichConnectorItem->findConnectorUnder(false, true, ends, true, originatingConnector);
 
601
        }
 
602
}
 
603
 
 
604
void Wire::setConnector0Rect() {
 
605
        QRectF rect = m_connector0->rect();
 
606
        rect.moveTo(0 - (rect.width()  / 2.0)  ,
 
607
                                0 - (rect.height()  / 2.0) );
 
608
        //DebugDialog::debug(QString("set connector rect %1 %2").arg(rect.width()).arg(rect.height()));
 
609
        m_connector0->setRect(rect);
 
610
}
 
611
 
 
612
 
 
613
void Wire::setConnector1Rect() {
 
614
        QRectF rect = m_connector1->rect();
 
615
        rect.moveTo(this->line().dx() - (rect.width()  / 2.0)  ,
 
616
                                this->line().dy() - (rect.height()  / 2.0) );
 
617
        m_connector1->setRect(rect);
 
618
}
 
619
 
 
620
void Wire::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
 
621
        if (m_spaceBarWasPressed) {
 
622
                return;
 
623
        }
 
624
 
 
625
        if (releaseDrag()) return;
 
626
 
 
627
        ItemBase::mouseReleaseEvent(event);
 
628
}
 
629
 
 
630
bool Wire::releaseDrag() {
 
631
        if (m_dragEnd == false && m_dragCurve == false) return false;
 
632
 
 
633
        if (m_dragCurve) {
 
634
                delete TheBezierDisplay;
 
635
                TheBezierDisplay = NULL;
 
636
                m_dragCurve = false;
 
637
                ungrabMouse();
 
638
                if (UndoBezier != *m_bezier) {
 
639
                        emit wireChangedCurveSignal(this, &UndoBezier, m_bezier, false);
 
640
                }
 
641
                return true;
 
642
        }
 
643
 
 
644
        m_dragEnd = false;
 
645
 
 
646
        ConnectorItem * from = (m_drag0) ? m_connector0 : m_connector1;
 
647
        ConnectorItem * to = from->releaseDrag();
 
648
 
 
649
        QLineF newLine = this->line();
 
650
        QLineF oldLine = m_viewGeometry.line();
 
651
        QPointF oldPos = m_viewGeometry.loc();
 
652
        QPointF newPos = this->pos();
 
653
        if (newLine != oldLine || oldPos != newPos) {
 
654
                emit wireChangedSignal(this, oldLine, newLine, oldPos, newPos, from, to);
 
655
        }
 
656
 
 
657
        return true;
 
658
}
 
659
 
 
660
 
 
661
void Wire::saveInstanceLocation(QXmlStreamWriter & streamWriter)
 
662
{
 
663
        QLineF line = m_viewGeometry.line();
 
664
        QPointF loc = m_viewGeometry.loc();
 
665
        streamWriter.writeAttribute("x", QString::number(loc.x()));
 
666
        streamWriter.writeAttribute("y", QString::number(loc.y()));
 
667
        streamWriter.writeAttribute("x1", QString::number(line.x1()));
 
668
        streamWriter.writeAttribute("y1", QString::number(line.y1()));
 
669
        streamWriter.writeAttribute("x2", QString::number(line.x2()));
 
670
        streamWriter.writeAttribute("y2", QString::number(line.y2()));
 
671
        streamWriter.writeAttribute("wireFlags", QString::number(m_viewGeometry.flagsAsInt()));
 
672
}
 
673
 
 
674
void Wire::writeGeometry(QXmlStreamWriter & streamWriter) {
 
675
        ItemBase::writeGeometry(streamWriter);
 
676
        streamWriter.writeStartElement("wireExtras");
 
677
        streamWriter.writeAttribute("mils", QString::number(mils()));
 
678
        streamWriter.writeAttribute("color", m_pen.brush().color().name());
 
679
        streamWriter.writeAttribute("opacity", QString::number(m_opacity));
 
680
        if (m_bezier) m_bezier->write(streamWriter);
 
681
        streamWriter.writeEndElement();
 
682
}
 
683
 
 
684
void Wire::setExtras(QDomElement & element, InfoGraphicsView * infoGraphicsView)
 
685
{
 
686
        if (element.isNull()) return;
 
687
 
 
688
        bool ok;
 
689
        double w = element.attribute("width").toDouble(&ok);
 
690
        if (ok) {
 
691
                setWireWidth(w, infoGraphicsView, infoGraphicsView->getWireStrokeWidth(this, w));
 
692
        }
 
693
        else {
 
694
                w = element.attribute("mils").toDouble(&ok);
 
695
                if (ok) {
 
696
                        double wpix = GraphicsUtils::mils2pixels(w, FSvgRenderer::printerScale());
 
697
                        setWireWidth(wpix, infoGraphicsView, infoGraphicsView->getWireStrokeWidth(this, wpix));
 
698
                }
 
699
        }
 
700
 
 
701
        setColorFromElement(element);
 
702
        QDomElement bElement = element.firstChildElement("bezier");
 
703
        Bezier bezier = Bezier::fromElement(bElement);
 
704
        if (!bezier.isEmpty()) {
 
705
                prepareGeometryChange();
 
706
                m_bezier = new Bezier;
 
707
                m_bezier->copy(&bezier);
 
708
                QPointF p0 = connector0()->sceneAdjustedTerminalPoint(NULL);
 
709
                QPointF p1 = connector1()->sceneAdjustedTerminalPoint(NULL);
 
710
                m_bezier->set_endpoints(mapFromScene(p0), mapFromScene(p1));
 
711
        }
 
712
 
 
713
}
 
714
 
 
715
void Wire::setColorFromElement(QDomElement & element) {
 
716
        QString colorString = element.attribute("color");
 
717
        if (colorString.isNull() || colorString.isEmpty()) return;
 
718
 
 
719
        bool ok;
 
720
        double op = element.attribute("opacity").toDouble(&ok);
 
721
        if (!ok) {
 
722
                op = 1.0;
 
723
        }
 
724
 
 
725
        setColorString(colorString, op);
 
726
}
 
727
 
 
728
void Wire::hoverEnterConnectorItem(QGraphicsSceneHoverEvent * event , ConnectorItem * item) {
 
729
        m_connectorHover = item;
 
730
        ItemBase::hoverEnterConnectorItem(event, item);
 
731
}
 
732
 
 
733
void Wire::hoverLeaveConnectorItem(QGraphicsSceneHoverEvent * event, ConnectorItem * item) {
 
734
        m_connectorHover = NULL;
 
735
        ItemBase::hoverLeaveConnectorItem(event, item);
 
736
}
 
737
 
 
738
void Wire::hoverEnterEvent ( QGraphicsSceneHoverEvent * event ) {
 
739
        ItemBase::hoverEnterEvent(event);
 
740
        QApplication::instance()->installEventFilter(this);
 
741
        QApplication::setOverrideCursor(cursor());
 
742
        //DebugDialog::debug("---wire set override cursor");
 
743
        updateCursor(event->modifiers());
 
744
}
 
745
 
 
746
void Wire::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event ) {
 
747
        QApplication::instance()->removeEventFilter(this);
 
748
        ItemBase::hoverLeaveEvent(event);
 
749
        //DebugDialog::debug("------wire restore override cursor");
 
750
        QApplication::restoreOverrideCursor();
 
751
}
 
752
 
 
753
 
 
754
void Wire::connectionChange(ConnectorItem * onMe, ConnectorItem * onIt, bool connect) {
 
755
        checkVisibility(onMe, onIt, connect);
 
756
 
 
757
        bool movable = true;
 
758
        foreach (ConnectorItem * connectedTo, m_connector0->connectedToItems()) {
 
759
                if (connectedTo->attachedToItemType() != ModelPart::Wire) {
 
760
                        movable = false;
 
761
                        break;
 
762
                }
 
763
        }
 
764
        if (movable) {
 
765
                foreach (ConnectorItem * connectedTo, m_connector1->connectedToItems()) {
 
766
                        if (connectedTo->attachedToItemType() != ModelPart::Wire) {
 
767
                                movable = false;
 
768
                                break;
 
769
                        }
 
770
                }
 
771
        }
 
772
}
 
773
 
 
774
void Wire::mouseDoubleClickConnectorEvent(ConnectorItem * connectorItem) {
 
775
        int chained = 0;
 
776
        foreach (ConnectorItem * toConnectorItem, connectorItem->connectedToItems()) {
 
777
                if (toConnectorItem->attachedToItemType() == ModelPart::Wire) {
 
778
                        chained++;
 
779
                }
 
780
                else {
 
781
                        return;
 
782
                }
 
783
        }
 
784
 
 
785
 
 
786
        if (chained == 1) {
 
787
                // near as I can tell, this is to eliminate the overrides from the connectorItem and then from the wire itself
 
788
                QApplication::restoreOverrideCursor();
 
789
                QApplication::restoreOverrideCursor();
 
790
                emit wireJoinSignal(this, connectorItem);
 
791
        }
 
792
}
 
793
 
 
794
void Wire::mousePressConnectorEvent(ConnectorItem * connectorItem, QGraphicsSceneMouseEvent * event) {
 
795
        //DebugDialog::debug("checking press connector event");
 
796
 
 
797
        if (m_canChainMultiple && event->modifiers() & altOrMetaModifier()) {
 
798
                // dragging a wire out of a bendpoint
 
799
                InfoGraphicsView * infoGraphicsView = InfoGraphicsView::getInfoGraphicsView(this);
 
800
                if (infoGraphicsView != NULL) {
 
801
                        infoGraphicsView->mousePressConnectorEvent(connectorItem, event);
 
802
                }
 
803
 
 
804
                return;
 
805
        }
 
806
 
 
807
 
 
808
        connectorItem->setOverConnectorItem(NULL);
 
809
        initDragEnd(connectorItem, event->scenePos());
 
810
 
 
811
}
 
812
 
 
813
void Wire::simpleConnectedMoved(ConnectorItem * to) {
 
814
        // to is this wire, from is something else
 
815
        simpleConnectedMoved(to->firstConnectedToIsh(), to);
 
816
}
 
817
 
 
818
void Wire::simpleConnectedMoved(ConnectorItem * from, ConnectorItem * to)
 
819
{
 
820
        if (from == NULL) return;
 
821
 
 
822
        // to is this wire, from is something else
 
823
        QPointF p1, p2;
 
824
        calcNewLine(from, to, p1, p2);
 
825
 
 
826
        /*
 
827
        QPointF oldPos = this->pos();
 
828
        QPointF newPos = p1;
 
829
        QLineF oldLine = this->line();
 
830
        QLineF newLine(0, 0,  p2.x() - p1.x(), p2.y() - p1.y());
 
831
        if (qAbs(oldPos.x() - newPos.x()) > 1.75 ||
 
832
                qAbs(oldPos.y() - newPos.y()) > 1.75 ||
 
833
                qAbs(oldLine.x1() - newLine.x1()) > 1.75 ||
 
834
                qAbs(oldLine.x2() - newLine.x2()) > 1.75 ||
 
835
                qAbs(oldLine.y1() - newLine.y1()) > 1.75 ||
 
836
                qAbs(oldLine.y2() - newLine.y2()) > 1.75
 
837
                )
 
838
        {
 
839
                DebugDialog::debug("line changed");
 
840
                calcNewLine(from,to,p1,p2);
 
841
        }
 
842
        */
 
843
 
 
844
        this->setPos(p1);
 
845
        this->setLine(0,0, p2.x() - p1.x(), p2.y() - p1.y() );
 
846
        //DebugDialog::debug(QString("set line %5: %1 %2, %3 %4, vis:%6 lyr:%7").arg(p1.x()).arg(p1.y()).arg(p2.x()).arg(p2.y()).arg(id()).arg(isVisible()).arg(m_viewIdentifier) );
 
847
        setConnector1Rect();
 
848
}
 
849
 
 
850
void Wire::calcNewLine(ConnectorItem * from, ConnectorItem * to, QPointF & p1, QPointF & p2) {
 
851
        // to is this wire, from is something else
 
852
        if (to == m_connector0) {
 
853
                p1 = from->sceneAdjustedTerminalPoint(to);
 
854
                ConnectorItem * otherFrom = m_connector1->firstConnectedToIsh();
 
855
                if (otherFrom == NULL) {
 
856
                        p2 = m_connector1->mapToScene(m_connector1->rect().center());
 
857
                }
 
858
                else {
 
859
                        p2 = otherFrom->sceneAdjustedTerminalPoint(m_connector1);
 
860
                }
 
861
        }
 
862
        else {
 
863
                p2 = from->sceneAdjustedTerminalPoint(to);
 
864
                ConnectorItem * otherFrom = m_connector0->firstConnectedToIsh();
 
865
                if (otherFrom == NULL) {
 
866
                        p1 = m_connector0->mapToScene(m_connector0->rect().center());
 
867
                }
 
868
                else {
 
869
                        p1 = otherFrom->sceneAdjustedTerminalPoint(m_connector0);
 
870
                }
 
871
 
 
872
        }
 
873
}
 
874
 
 
875
void Wire::connectedMoved(ConnectorItem * from, ConnectorItem * to) {
 
876
        // "from" is the connector on the part
 
877
        // "to" is the connector on the wire
 
878
 
 
879
        simpleConnectedMoved(from, to);
 
880
        return;
 
881
 
 
882
        /*
 
883
        DebugDialog::debug(QString("connected moved %1 %2, %3 %4")
 
884
                .arg(from->attachedToID())
 
885
                .arg(from->attachedToTitle())
 
886
                .arg(to->attachedToID())
 
887
                .arg(to->attachedToTitle())
 
888
                );
 
889
        */
 
890
 
 
891
        ConnectorItem * otherEnd = otherConnector(to);
 
892
        bool chained = otherEnd->chained();
 
893
        QPointF p1, p2;
 
894
        if (chained) {
 
895
                // move both ends
 
896
                if (to == m_connector0) {
 
897
                        p1 = from->sceneAdjustedTerminalPoint(m_connector0);
 
898
                        p2 = this->line().p2() + p1;
 
899
                }
 
900
                else {
 
901
                        p2 = from->sceneAdjustedTerminalPoint(m_connector1);
 
902
                        p1 = p2 - this->line().p2();
 
903
                }
 
904
        }
 
905
        else {
 
906
                calcNewLine(from, to, p1, p2);
 
907
        }
 
908
        this->setPos(p1);
 
909
        this->setLine(0,0, p2.x() - p1.x(), p2.y() - p1.y() );
 
910
        //DebugDialog::debug(QString("set line %5: %1 %2, %3 %4, vis:%6 lyr:%7").arg(p1.x()).arg(p1.y()).arg(p2.x()).arg(p2.y()).arg(id()).arg(isVisible()).arg(m_viewIdentifier) );
 
911
        setConnector1Rect();
 
912
 
 
913
        if (chained) {
 
914
                foreach (ConnectorItem * otherEndTo, otherEnd->connectedToItems()) {
 
915
                        if (otherEndTo->attachedToItemType() == ModelPart::Wire) {
 
916
                                otherEndTo->attachedTo()->connectedMoved(otherEnd, otherEndTo);
 
917
                        }
 
918
                }
 
919
        }
 
920
}
 
921
 
 
922
 
 
923
FSvgRenderer * Wire::setUpConnectors(ModelPart * modelPart, ViewIdentifierClass::ViewIdentifier viewIdentifier) 
 
924
{
 
925
        clearConnectorItemCache();
 
926
 
 
927
        QString error;
 
928
        LayerAttributes layerAttributes;
 
929
        FSvgRenderer * renderer = ItemBase::setUpImage(modelPart, viewIdentifier, m_viewLayerID, m_viewLayerSpec, layerAttributes, error);
 
930
        if (renderer == NULL) {
 
931
                return NULL;
 
932
        }
 
933
 
 
934
        foreach (Connector * connector, m_modelPart->connectors().values()) {
 
935
                if (connector == NULL) continue;
 
936
 
 
937
                SvgIdLayer * svgIdLayer = connector->fullPinInfo(viewIdentifier, m_viewLayerID);
 
938
                if (svgIdLayer == NULL) continue;
 
939
 
 
940
                bool result = renderer->setUpConnector(svgIdLayer, false);
 
941
                if (!result) continue;
 
942
 
 
943
                ConnectorItem * connectorItem = newConnectorItem(connector);
 
944
                connectorItem->setRect(svgIdLayer->m_rect);
 
945
                connectorItem->setTerminalPoint(svgIdLayer->m_point);
 
946
                m_originalConnectorRect = svgIdLayer->m_rect;
 
947
 
 
948
                connectorItem->setCircular(true);
 
949
                //DebugDialog::debug(QString("terminal point %1 %2").arg(terminalPoint.x()).arg(terminalPoint.y()) );
 
950
        }
 
951
 
 
952
        return renderer;
 
953
}
 
954
 
 
955
/*
 
956
void Wire::setPos(const QPointF & pos) {
 
957
        ItemBase::setPos(pos);
 
958
}
 
959
*/
 
960
 
 
961
 
 
962
void Wire::setLineAnd(QLineF line, QPointF pos, bool useLine) {
 
963
        this->setPos(pos);
 
964
        if (useLine) this->setLine(line);
 
965
 
 
966
        setConnector1Rect();
 
967
}
 
968
 
 
969
ConnectorItem * Wire::otherConnector(ConnectorItem * oneConnector) {
 
970
        if (oneConnector == m_connector0) return m_connector1;
 
971
 
 
972
        return m_connector0;
 
973
}
 
974
 
 
975
ConnectorItem * Wire::connector0() {
 
976
        return m_connector0;
 
977
}
 
978
 
 
979
ConnectorItem * Wire::connector1() {
 
980
        return m_connector1;
 
981
}
 
982
 
 
983
void Wire::findConnectorsUnder() {
 
984
        foreach (ConnectorItem * connectorItem, cachedConnectorItems()) {
 
985
                connectorItem->findConnectorUnder(true, false, ConnectorItem::emptyConnectorItemList, false, NULL);
 
986
        }
 
987
}
 
988
 
 
989
void Wire::collectChained(QList<Wire *> & chained, QList<ConnectorItem *> & ends ) {
 
990
        chained.append(this);
 
991
        for (int i = 0; i < chained.count(); i++) {
 
992
                Wire * wire = chained[i];
 
993
                collectChained(wire->m_connector1, chained, ends);
 
994
                collectChained(wire->m_connector0, chained, ends);
 
995
        }
 
996
}
 
997
 
 
998
void Wire::collectChained(ConnectorItem * connectorItem, QList<Wire *> & chained, QList<ConnectorItem *> & ends) {
 
999
        if (connectorItem == NULL) return;
 
1000
 
 
1001
        foreach (ConnectorItem * connectedToItem, connectorItem->connectedToItems()) {
 
1002
                Wire * wire = qobject_cast<Wire *>(connectedToItem->attachedTo());
 
1003
                if (wire == NULL) {
 
1004
                        if (!ends.contains(connectedToItem)) {
 
1005
                                ends.append(connectedToItem);
 
1006
                        }
 
1007
                        continue;
 
1008
                }
 
1009
 
 
1010
                if (chained.contains(wire)) continue;
 
1011
                chained.append(wire);
 
1012
        }
 
1013
}
 
1014
 
 
1015
void Wire::collectWires(QList<Wire *> & wires) {
 
1016
        if (wires.contains(this)) return;
 
1017
 
 
1018
        wires.append(this);
 
1019
        //DebugDialog::debug(QString("collecting wire %1").arg(this->id()) );
 
1020
        collectWiresAux(wires, m_connector0);
 
1021
        collectWiresAux(wires, m_connector1);
 
1022
}
 
1023
 
 
1024
void Wire::collectWiresAux(QList<Wire *> & wires, ConnectorItem * start) {
 
1025
        foreach (ConnectorItem * toConnectorItem, start->connectedToItems()) {
 
1026
                if (toConnectorItem->attachedToItemType() == ModelPart::Wire) {
 
1027
                        qobject_cast<Wire *>(toConnectorItem->attachedTo())->collectWires(wires);
 
1028
                }
 
1029
        }
 
1030
 
 
1031
}
 
1032
 
 
1033
bool Wire::stickyEnabled()
 
1034
{
 
1035
        return (connector0()->connectionsCount() <= 0) && (connector1()->connectionsCount() <= 0);
 
1036
}
 
1037
 
 
1038
bool Wire::getTrace() {
 
1039
        return m_viewGeometry.getAnyTrace();
 
1040
}
 
1041
 
 
1042
bool Wire::getRouted() {
 
1043
        return m_viewGeometry.getRouted();
 
1044
}
 
1045
 
 
1046
void Wire::setRouted(bool routed) {
 
1047
        m_viewGeometry.setRouted(routed);
 
1048
}
 
1049
 
 
1050
void Wire::setRatsnest(bool ratsnest) {
 
1051
        m_viewGeometry.setRatsnest(ratsnest);
 
1052
}
 
1053
 
 
1054
bool Wire::getRatsnest() {
 
1055
        return m_viewGeometry.getRatsnest();
 
1056
}
 
1057
 
 
1058
void Wire::setAutoroutable(bool ar) {
 
1059
        m_viewGeometry.setAutoroutable(ar);
 
1060
}
 
1061
 
 
1062
bool Wire::getAutoroutable() {
 
1063
        return m_viewGeometry.getAutoroutable();
 
1064
}
 
1065
 
 
1066
void Wire::setNormal(bool normal) {
 
1067
        m_viewGeometry.setNormal(normal);
 
1068
}
 
1069
 
 
1070
bool Wire::getNormal() {
 
1071
        return m_viewGeometry.getNormal();
 
1072
}
 
1073
 
 
1074
void Wire::setColor(const QColor & color, double op) {
 
1075
        m_pen.setBrush(QBrush(color));
 
1076
        m_opacity = op;
 
1077
        m_colorName = color.name();
 
1078
        this->update();
 
1079
}
 
1080
 
 
1081
void Wire::setShadowColor(QColor & color) {
 
1082
        m_shadowBrush = QBrush(color);
 
1083
        m_shadowPen.setBrush(m_shadowBrush);
 
1084
        m_bendpointPen.setBrush(m_shadowBrush);
 
1085
        m_bendpoint2Pen.setBrush(m_shadowBrush);
 
1086
        if (m_connector0) m_connector0->restoreColor(false, 0, false);
 
1087
        if (m_connector0) m_connector1->restoreColor(false, 0, false);
 
1088
        this->update();
 
1089
}
 
1090
 
 
1091
const QColor & Wire::color() {
 
1092
        return m_pen.brush().color();
 
1093
}
 
1094
 
 
1095
void Wire::setWireWidth(double width, InfoGraphicsView * infoGraphicsView, double hoverStrokeWidth) {
 
1096
        if (m_pen.widthF() == width) return;
 
1097
 
 
1098
        prepareGeometryChange();
 
1099
        setPenWidth(width, infoGraphicsView, hoverStrokeWidth);
 
1100
        if (m_connector0) m_connector0->restoreColor(false, 0, false);
 
1101
        if (m_connector1) m_connector1->restoreColor(false, 0, false);
 
1102
        update();
 
1103
}
 
1104
 
 
1105
double Wire::width() {
 
1106
        return m_pen.widthF();
 
1107
}
 
1108
 
 
1109
double Wire::shadowWidth() {
 
1110
        return m_shadowPen.widthF();
 
1111
}
 
1112
 
 
1113
double Wire::mils() {
 
1114
        return 1000 * m_pen.widthF() / FSvgRenderer::printerScale();
 
1115
}
 
1116
 
 
1117
void Wire::setColorString(QString colorName, double op) {
 
1118
        // sets a color using the name (.e. "red")
 
1119
        // note: colorName is associated with a Fritzing color, not a Qt color
 
1120
 
 
1121
        QString colorString = colors.value(colorName, "");
 
1122
        if (colorString.isEmpty()) {
 
1123
                colorString = colorName;
 
1124
 
 
1125
                foreach (QString c, colors.keys()) {
 
1126
                        if (colors.value(c).compare(colorName, Qt::CaseInsensitive) == 0) {
 
1127
                                colorName = c;
 
1128
                                break;
 
1129
                        }
 
1130
                }
 
1131
        }
 
1132
 
 
1133
        QColor c;
 
1134
        c.setNamedColor(colorString);
 
1135
        setColor(c, op);
 
1136
        m_colorName = colorName;
 
1137
 
 
1138
        colorString = shadowColors.value(colorName, "");
 
1139
        if (colorString.isEmpty()) {
 
1140
                colorString = colorName;
 
1141
        }
 
1142
 
 
1143
        c.setNamedColor(colorString);
 
1144
        setShadowColor(c);
 
1145
}
 
1146
 
 
1147
QString Wire::hexString() {
 
1148
        return m_pen.brush().color().name();
 
1149
}
 
1150
 
 
1151
QString Wire::shadowHexString() {
 
1152
        return m_shadowPen.brush().color().name();
 
1153
}
 
1154
 
 
1155
QString Wire::colorString() {
 
1156
        return m_colorName;
 
1157
}
 
1158
 
 
1159
void Wire::initNames() {
 
1160
        if (colors.count() > 0) return;
 
1161
 
 
1162
        widths << 16 << 24 << 32 << 48;
 
1163
        widthTrans.insert(widths[0], tr("thin (16 mil)"));
 
1164
        widthTrans.insert(widths[1], tr("standard (24 mil)"));
 
1165
        widthTrans.insert(widths[2], tr("thick (32 mil)"));
 
1166
        widthTrans.insert(widths[3], tr("extra thick (48 mil)"));
 
1167
 
 
1168
        THIN_TRACE_WIDTH = GraphicsUtils::mils2pixels(widths[0], FSvgRenderer::printerScale());
 
1169
        STANDARD_TRACE_WIDTH = GraphicsUtils::mils2pixels(widths[1], FSvgRenderer::printerScale());
 
1170
        HALF_STANDARD_TRACE_WIDTH = STANDARD_TRACE_WIDTH / 2.0;
 
1171
 
 
1172
    // need a list because a hash table doesn't guarantee order 
 
1173
    colorNames.append(tr("blue"));
 
1174
        colorNames.append(tr("red"));
 
1175
    colorNames.append(tr("black"));
 
1176
        colorNames.append(tr("yellow"));
 
1177
        colorNames.append(tr("green"));
 
1178
        colorNames.append(tr("grey"));
 
1179
        colorNames.append(tr("white"));
 
1180
        colorNames.append(tr("orange"));
 
1181
    colorNames.append(tr("brown"));
 
1182
    colorNames.append(tr("purple"));
 
1183
    colorNames.append(tr("schematic black"));
 
1184
 
 
1185
        // need this hash table to translate from user's language to internal color name
 
1186
    colorTrans.insert(tr("blue"), "blue");
 
1187
        colorTrans.insert(tr("red"), "red");
 
1188
    colorTrans.insert(tr("black"), "black");
 
1189
        colorTrans.insert(tr("yellow"), "yellow");
 
1190
        colorTrans.insert(tr("green"), "green");
 
1191
        colorTrans.insert(tr("grey"), "grey");
 
1192
        colorTrans.insert(tr("white"), "white");
 
1193
        colorTrans.insert(tr("orange"), "orange");
 
1194
        colorTrans.insert(tr("brown"), "brown");
 
1195
    colorTrans.insert(tr("purple"), "purple");
 
1196
    colorTrans.insert(tr("schematic black"), "schematic black");
 
1197
 
 
1198
    colors.insert("blue",       "#418dd9");
 
1199
        colors.insert("red",    "#cc1414");
 
1200
    colors.insert("black",      "#404040");
 
1201
        colors.insert("yellow", "#ffe24d");
 
1202
        colors.insert("green",  "#47cc79");
 
1203
        colors.insert("grey",   "#999999");
 
1204
        colors.insert("white",  "#ffffff");
 
1205
        colors.insert("orange", "#ff7033");
 
1206
        colors.insert("jumper", ViewLayer::JumperColor);
 
1207
        colors.insert("trace",  ViewLayer::Copper0Color);    
 
1208
        colors.insert("trace1",  ViewLayer::Copper1Color);    
 
1209
        //colors.insert("unrouted", "#000000");
 
1210
        //colors.insert("blackblack", "#000000");
 
1211
        colors.insert("schematicGrey", "#9d9d9d");
 
1212
    colors.insert("purple", "#ab58a2");
 
1213
        colors.insert("brown", "#8c3b00");
 
1214
        colors.insert("schematic black", "#000000");
 
1215
 
 
1216
    shadowColors.insert("blue",         "#1b5bb3");
 
1217
        shadowColors.insert("red",              "#8c0000");
 
1218
    shadowColors.insert("black",        "#000000");
 
1219
        shadowColors.insert("yellow",   "#e6ab00");
 
1220
        shadowColors.insert("green",    "#00a63d");
 
1221
        shadowColors.insert("grey",             "#666666");
 
1222
        shadowColors.insert("white",    "#999999");
 
1223
        shadowColors.insert("orange",   "#d95821");
 
1224
    shadowColors.insert("jumper",       "#2d6563");
 
1225
        shadowColors.insert("trace",    "#d69b00");
 
1226
        shadowColors.insert("trace1",   "#d69b00");    
 
1227
        //shadowColors.insert("unrouted", "#000000");
 
1228
    shadowColors.insert("purple",       "#7a3a73");
 
1229
    shadowColors.insert("brown",        "#6c2710");
 
1230
        shadowColors.insert("schematicGrey", "#1d1d1d");
 
1231
        //shadowColors.insert("blackblack", "#000000");
 
1232
        shadowColors.insert("schematic black", "#000000");
 
1233
}
 
1234
 
 
1235
bool Wire::hasFlag(ViewGeometry::WireFlag flag)
 
1236
{
 
1237
        return m_viewGeometry.hasFlag(flag);
 
1238
}
 
1239
 
 
1240
bool Wire::hasAnyFlag(ViewGeometry::WireFlags flags)
 
1241
{
 
1242
        return m_viewGeometry.hasAnyFlag(flags);
 
1243
}
 
1244
 
 
1245
Wire * Wire::findTraced(ViewGeometry::WireFlags flags, QList<ConnectorItem *>  & ends) {
 
1246
        QList<Wire *> chainedWires;
 
1247
        this->collectChained(chainedWires, ends);
 
1248
        if (ends.count() != 2) {
 
1249
                DebugDialog::debug(QString("wire in jumper or trace must have two ends") );
 
1250
                return NULL;
 
1251
        }
 
1252
 
 
1253
        return ConnectorItem::directlyWiredTo(ends[0], ends[1], flags);
 
1254
}
 
1255
 
 
1256
QRgb Wire::getRgb(const QString & name) {
 
1257
        QString str = colors.value(name);
 
1258
        QColor c;
 
1259
        c.setNamedColor(str);
 
1260
        return c.rgb();
 
1261
}
 
1262
 
 
1263
void Wire::setWireFlags(ViewGeometry::WireFlags wireFlags) {
 
1264
        m_viewGeometry.setWireFlags(wireFlags);
 
1265
}
 
1266
 
 
1267
double Wire::opacity() {
 
1268
        return m_opacity;
 
1269
}
 
1270
 
 
1271
void Wire::setOpacity(double opacity) {
 
1272
        m_opacity = opacity;
 
1273
        this->update();
 
1274
}
 
1275
 
 
1276
bool Wire::draggingEnd() {
 
1277
        return m_dragEnd || m_dragCurve;
 
1278
}
 
1279
 
 
1280
void Wire::setCanChainMultiple(bool can) {
 
1281
        m_canChainMultiple = can;
 
1282
}
 
1283
 
 
1284
bool Wire::canChangeColor() {
 
1285
        if (getRatsnest()) return false;
 
1286
        if (!getTrace()) return true;
 
1287
 
 
1288
        return (this->m_viewIdentifier == ViewIdentifierClass::SchematicView);
 
1289
}
 
1290
 
 
1291
void Wire::collectDirectWires(QList<Wire *> & wires) {
 
1292
        if (!wires.contains(this)) {
 
1293
                wires.append(this);
 
1294
        }
 
1295
 
 
1296
        collectDirectWires(m_connector0, wires);
 
1297
        collectDirectWires(m_connector1, wires);
 
1298
}
 
1299
 
 
1300
void Wire::collectDirectWires(ConnectorItem * connectorItem, QList<Wire *> & wires) {
 
1301
        if (connectorItem->connectionsCount() != 1) return;
 
1302
 
 
1303
        ConnectorItem * toConnectorItem = connectorItem->connectedToItems()[0];
 
1304
        if (toConnectorItem->attachedToItemType() != ModelPart::Wire) return;
 
1305
 
 
1306
        Wire * nextWire = qobject_cast<Wire *>(toConnectorItem->attachedTo());
 
1307
        if (wires.contains(nextWire)) return;
 
1308
 
 
1309
        wires.append(nextWire);
 
1310
        nextWire->collectDirectWires(nextWire->otherConnector(toConnectorItem), wires);
 
1311
}
 
1312
 
 
1313
QVariant Wire::itemChange(GraphicsItemChange change, const QVariant &value)
 
1314
{
 
1315
    if (change == ItemSelectedChange) {
 
1316
                if (m_partLabel) {
 
1317
                        m_partLabel->update();
 
1318
                }
 
1319
 
 
1320
                if (!m_ignoreSelectionChange) {
 
1321
                        QList<Wire *> chained;
 
1322
                        QList<ConnectorItem *> ends;
 
1323
                        collectChained(chained, ends);
 
1324
                        InfoGraphicsView * infoGraphicsView = InfoGraphicsView::getInfoGraphicsView(this);
 
1325
                        if (infoGraphicsView) {
 
1326
                                infoGraphicsView->setIgnoreSelectionChangeEvents(true);
 
1327
                        }
 
1328
                        // DebugDialog::debug(QString("original wire selected %1 %2").arg(value.toBool()).arg(this->id()));
 
1329
                        foreach (Wire * wire, chained) {
 
1330
                                if (wire != this ) {
 
1331
                                        wire->setIgnoreSelectionChange(true);
 
1332
                                        wire->setSelected(value.toBool());
 
1333
                                        wire->setIgnoreSelectionChange(false);
 
1334
                                        // DebugDialog::debug(QString("wire selected %1 %2").arg(value.toBool()).arg(wire->id()));
 
1335
                                }
 
1336
                        }
 
1337
                        if (infoGraphicsView) {
 
1338
                                infoGraphicsView->setIgnoreSelectionChangeEvents(false);
 
1339
                        }
 
1340
                }
 
1341
    }
 
1342
    return ItemBase::itemChange(change, value);
 
1343
}
 
1344
 
 
1345
void Wire::cleanup() {
 
1346
}
 
1347
 
 
1348
void Wire::getConnectedColor(ConnectorItem * connectorItem, QBrush * &brush, QPen * &pen, double & opacity, double & negativePenWidth, bool & negativeOffsetRect) {
 
1349
 
 
1350
        connectorItem->setBigDot(false);
 
1351
        int count = 0;
 
1352
        bool bendpoint = true;
 
1353
        foreach (ConnectorItem * toConnectorItem, connectorItem->connectedToItems()) {
 
1354
                if (toConnectorItem->attachedToItemType() != ModelPart::Wire) {
 
1355
                        // for drawing a big dot on the end of a part connector in schematic view if the part is connected to more than one trace
 
1356
                        bendpoint = false;
 
1357
                        if (toConnectorItem->connectionsCount() > 1) {  
 
1358
                                InfoGraphicsView * infoGraphicsView = InfoGraphicsView::getInfoGraphicsView(this);
 
1359
                                if (infoGraphicsView != NULL && infoGraphicsView->hasBigDots()) {
 
1360
                                        int c = 0;
 
1361
                                        foreach (ConnectorItem * totoConnectorItem, toConnectorItem->connectedToItems()) {
 
1362
                                                if (totoConnectorItem->attachedToItemType() == ModelPart::Wire) {
 
1363
                                                        Wire * w = qobject_cast<Wire *>(totoConnectorItem->attachedTo());
 
1364
                                                        if (w && w->getTrace()) {
 
1365
                                                                c++;
 
1366
                                                        }
 
1367
                                                }
 
1368
                                        }
 
1369
                                        if (c > 1) {
 
1370
                                                count = 2;
 
1371
                                                break;
 
1372
                                        }
 
1373
                                }
 
1374
                        }
 
1375
 
 
1376
                        ItemBase::getConnectedColor(connectorItem, brush, pen, opacity, negativePenWidth, negativeOffsetRect);
 
1377
                        return;
 
1378
                }
 
1379
 
 
1380
                count++;
 
1381
        }
 
1382
 
 
1383
        if (count == 0) {
 
1384
                ItemBase::getConnectedColor(connectorItem, brush, pen, opacity, negativePenWidth, negativeOffsetRect);
 
1385
                return;
 
1386
        }
 
1387
        
 
1388
        // connectorItem is a bendpoint or connects to a multiply connected connector
 
1389
 
 
1390
        //if (!bendpoint) {
 
1391
                //DebugDialog::debug(QString("big dot %1 %2 %3").arg(this->id()).arg(connectorItem->connectorSharedID()).arg(count));
 
1392
        //}
 
1393
 
 
1394
        brush = &m_shadowBrush;
 
1395
        opacity = 1.0;
 
1396
        if (count > 1) {
 
1397
                // only ever reach here when drawing a connector that is connected to more than one trace
 
1398
                pen = &m_bendpoint2Pen;
 
1399
                negativePenWidth = m_bendpoint2Width;
 
1400
                negativeOffsetRect = m_negativeOffsetRect;
 
1401
                connectorItem->setBigDot(true);
 
1402
        }
 
1403
        else {
 
1404
                negativeOffsetRect = m_negativeOffsetRect;
 
1405
                negativePenWidth = m_bendpointWidth;
 
1406
                pen = &m_bendpointPen;
 
1407
        }
 
1408
}
 
1409
 
 
1410
void Wire::setPenWidth(double w, InfoGraphicsView * infoGraphicsView, double hoverStrokeWidth) {
 
1411
        m_hoverStrokeWidth = hoverStrokeWidth;
 
1412
        //DebugDialog::debug(QString("setting hoverstrokewidth %1 %2").arg(m_id).arg(m_hoverStrokeWidth));
 
1413
        m_pen.setWidthF(w);
 
1414
        infoGraphicsView->getBendpointWidths(this, w, m_bendpointWidth, m_bendpoint2Width, m_negativeOffsetRect);
 
1415
        m_bendpointPen.setWidthF(qAbs(m_bendpointWidth));
 
1416
        m_bendpoint2Pen.setWidthF(qAbs(m_bendpoint2Width));
 
1417
        m_shadowPen.setWidthF(w + 2);
 
1418
}
 
1419
 
 
1420
bool Wire::connectionIsAllowed(ConnectorItem * to) {
 
1421
        if (!ItemBase::connectionIsAllowed(to)) return false;
 
1422
 
 
1423
        Wire * w = qobject_cast<Wire *>(to->attachedTo());
 
1424
        if (w == NULL) return true;
 
1425
 
 
1426
        if (w->getRatsnest()) return false;
 
1427
 
 
1428
        return m_viewIdentifier != ViewIdentifierClass::BreadboardView;
 
1429
}
 
1430
 
 
1431
bool Wire::isGrounded() {
 
1432
        return ConnectorItem::isGrounded(connector0(), connector1());
 
1433
}
 
1434
 
 
1435
bool Wire::acceptsMouseDoubleClickConnectorEvent(ConnectorItem *, QGraphicsSceneMouseEvent *) {
 
1436
        return true;
 
1437
}
 
1438
 
 
1439
bool Wire::acceptsMouseMoveConnectorEvent(ConnectorItem *, QGraphicsSceneMouseEvent *) {
 
1440
        return true;
 
1441
}
 
1442
 
 
1443
bool Wire::acceptsMouseReleaseConnectorEvent(ConnectorItem *, QGraphicsSceneMouseEvent *) {
 
1444
        return true;
 
1445
}
 
1446
 
 
1447
void Wire::setIgnoreSelectionChange(bool ignore) {
 
1448
        m_ignoreSelectionChange = ignore;
 
1449
}
 
1450
 
 
1451
bool Wire::collectExtraInfo(QWidget * parent, const QString & family, const QString & prop, const QString & value, bool swappingEnabled, QString & returnProp, QString & returnValue, QWidget * & returnWidget)
 
1452
{
 
1453
        if (prop.compare("width", Qt::CaseInsensitive) == 0) {
 
1454
                // don't display width property
 
1455
                return false;
 
1456
        }
 
1457
 
 
1458
        if (prop.compare("color", Qt::CaseInsensitive) == 0) {
 
1459
                returnProp = tr("color");
 
1460
                if (canChangeColor()) {
 
1461
                        QComboBox * comboBox = new QComboBox(parent);
 
1462
                        comboBox->setEditable(false);
 
1463
                        comboBox->setEnabled(swappingEnabled);
 
1464
                        comboBox->setObjectName("infoViewComboBox");
 
1465
                        
 
1466
                        int ix = 0;
 
1467
                        QString englishCurrColor = colorString();
 
1468
                        foreach(QString transColorName, Wire::colorNames) {
 
1469
                                QString englishColorName = Wire::colorTrans.value(transColorName);
 
1470
                                comboBox->addItem(transColorName, QVariant(englishColorName));
 
1471
                                if (englishColorName.compare(englishCurrColor, Qt::CaseInsensitive) == 0) {
 
1472
                                        comboBox->setCurrentIndex(ix);
 
1473
                                }
 
1474
                                ix++;
 
1475
                        }
 
1476
 
 
1477
                        connect(comboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(colorEntry(const QString &)));
 
1478
                        returnWidget = comboBox;
 
1479
                        returnValue = comboBox->currentText();
 
1480
                        return true;
 
1481
                }
 
1482
                else {
 
1483
                        returnWidget = NULL;
 
1484
                        returnValue = colorString();
 
1485
                        return true;
 
1486
                }
 
1487
        }
 
1488
 
 
1489
        return ItemBase::collectExtraInfo(parent, family, prop, value, swappingEnabled, returnProp, returnValue, returnWidget);
 
1490
}
 
1491
 
 
1492
void Wire::colorEntry(const QString & text) {
 
1493
        Q_UNUSED(text);
 
1494
 
 
1495
        QComboBox * comboBox = qobject_cast<QComboBox *>(sender());
 
1496
        if (comboBox == NULL) return;
 
1497
 
 
1498
        QString color = comboBox->itemData(comboBox->currentIndex()).toString();
 
1499
 
 
1500
        InfoGraphicsView * infoGraphicsView = InfoGraphicsView::getInfoGraphicsView(this);
 
1501
        if (infoGraphicsView != NULL) {
 
1502
                infoGraphicsView->changeWireColor(color);
 
1503
        }
 
1504
}
 
1505
 
 
1506
bool Wire::hasPartLabel() {
 
1507
        
 
1508
        return false;
 
1509
}
 
1510
 
 
1511
ItemBase::PluralType Wire::isPlural() {
 
1512
        return Plural;
 
1513
}
 
1514
 
 
1515
void Wire::checkVisibility(ConnectorItem * onMe, ConnectorItem * onIt, bool connect) {
 
1516
        if (connect) {
 
1517
                if (!onIt->attachedTo()->isVisible()) {
 
1518
                        this->setVisible(false);
 
1519
                }
 
1520
                else {
 
1521
                        ConnectorItem * other = otherConnector(onMe);
 
1522
                        foreach (ConnectorItem * toConnectorItem, other->connectedToItems()) {
 
1523
                                if (toConnectorItem->attachedToItemType() == ModelPart::Wire) continue;
 
1524
 
 
1525
                                if (!toConnectorItem->attachedTo()->isVisible()) {
 
1526
                                        this->setVisible(false);
 
1527
                                        break;
 
1528
                                }
 
1529
                        }
 
1530
                }
 
1531
        }
 
1532
}
 
1533
 
 
1534
bool Wire::canSwitchLayers() {
 
1535
        return false;
 
1536
}
 
1537
 
 
1538
bool Wire::hasPartNumberProperty()
 
1539
{
 
1540
        return false;
 
1541
}
 
1542
 
 
1543
bool Wire::rotationAllowed() {
 
1544
        return false;
 
1545
}
 
1546
 
 
1547
bool Wire::rotation45Allowed() {
 
1548
        return false;
 
1549
}
 
1550
 
 
1551
void Wire::addedToScene(bool temporary) {
 
1552
        ItemBase::addedToScene(temporary);
 
1553
 
 
1554
        InfoGraphicsView * infoGraphicsView = InfoGraphicsView::getInfoGraphicsView(this);
 
1555
        if (infoGraphicsView == NULL) return;
 
1556
 
 
1557
        bool succeeded = connect(this, SIGNAL(wireChangedSignal(Wire*, const QLineF & , const QLineF & , QPointF, QPointF, ConnectorItem *, ConnectorItem *)    ),
 
1558
                        infoGraphicsView, SLOT(wireChangedSlot(Wire*, const QLineF & , const QLineF & , QPointF, QPointF, ConnectorItem *, ConnectorItem *)),
 
1559
                        Qt::DirectConnection);          // DirectConnection means call the slot directly like a subroutine, without waiting for a thread or queue
 
1560
        succeeded = connect(this, SIGNAL(wireChangedCurveSignal(Wire*, const Bezier *, const Bezier *, bool)),
 
1561
                        infoGraphicsView, SLOT(wireChangedCurveSlot(Wire*, const Bezier *, const Bezier *, bool)),
 
1562
                        Qt::DirectConnection);          // DirectConnection means call the slot directly like a subroutine, without waiting for a thread or queue
 
1563
        succeeded = succeeded && connect(this, SIGNAL(wireSplitSignal(Wire*, QPointF, QPointF, const QLineF & )),
 
1564
                        infoGraphicsView, SLOT(wireSplitSlot(Wire*, QPointF, QPointF, const QLineF & )));
 
1565
        succeeded = succeeded && connect(this, SIGNAL(wireJoinSignal(Wire*, ConnectorItem *)),
 
1566
                        infoGraphicsView, SLOT(wireJoinSlot(Wire*, ConnectorItem*)));
 
1567
        if (!succeeded) {
 
1568
                DebugDialog::debug("wire signal connect failed");
 
1569
        }
 
1570
}
 
1571
 
 
1572
void Wire::setConnectorDimensions(double width, double height) 
 
1573
{
 
1574
        setConnectorDimensionsAux(connector0(), width, height);
 
1575
        setConnectorDimensionsAux(connector1(), width, height);
 
1576
}
 
1577
 
 
1578
void Wire::setConnectorDimensionsAux(ConnectorItem * connectorItem, double width, double height) 
 
1579
{
 
1580
        QPointF p = connectorItem->rect().center();
 
1581
        QRectF r(p.x() - (width / 2), p.y() - (height / 2), width, height);
 
1582
        connectorItem->setRect(r);
 
1583
        connectorItem->setTerminalPoint(r.center() - r.topLeft());
 
1584
}
 
1585
 
 
1586
void Wire::originalConnectorDimensions(double & width, double & height) 
 
1587
{
 
1588
        width = m_originalConnectorRect.width();
 
1589
        height = m_originalConnectorRect.height();
 
1590
}
 
1591
 
 
1592
bool Wire::isBendpoint(ConnectorItem * connectorItem) {
 
1593
        return connectorItem->isBendpoint();
 
1594
}
 
1595
 
 
1596
double Wire::hoverStrokeWidth() {
 
1597
        return m_hoverStrokeWidth;
 
1598
}
 
1599
 
 
1600
const QLineF & Wire::getPaintLine() {
 
1601
        return m_line;
 
1602
}
 
1603
 
 
1604
/*!
 
1605
    Returns the item's line, or a null line if no line has been set.
 
1606
 
 
1607
    \sa setLine()
 
1608
*/
 
1609
QLineF Wire::line() const
 
1610
{
 
1611
    return m_line;
 
1612
}
 
1613
 
 
1614
/*!
 
1615
    Sets the item's line to be the given \a line.
 
1616
 
 
1617
    \sa line()
 
1618
*/
 
1619
void Wire::setLine(const QLineF &line)
 
1620
{
 
1621
    if (m_line == line)
 
1622
        return;
 
1623
    prepareGeometryChange();
 
1624
    m_line = line;
 
1625
    update();
 
1626
}
 
1627
 
 
1628
void Wire::setLine(double x1, double y1, double x2, double y2)
 
1629
 
1630
        setLine(QLineF(x1, y1, x2, y2)); 
 
1631
}
 
1632
 
 
1633
/*!
 
1634
    Returns the item's pen, or a black solid 0-width pen if no pen has
 
1635
    been set.
 
1636
 
 
1637
    \sa setPen()
 
1638
*/
 
1639
QPen Wire::pen() const
 
1640
{
 
1641
    return m_pen;
 
1642
}
 
1643
 
 
1644
/*!
 
1645
    Sets the item's pen to \a pen. If no pen is set, the line will be painted
 
1646
    using a black solid 0-width pen.
 
1647
 
 
1648
    \sa pen()
 
1649
*/
 
1650
void Wire::setPen(const QPen &pen)
 
1651
{
 
1652
        if (pen.widthF() != m_pen.widthF()) {
 
1653
                prepareGeometryChange();
 
1654
        }
 
1655
    m_pen = pen;
 
1656
    update();
 
1657
}
 
1658
 
 
1659
bool Wire::canHaveCurve() {
 
1660
        return m_canHaveCurve && (m_viewIdentifier == ViewIdentifierClass::BreadboardView);
 
1661
}
 
1662
 
 
1663
void Wire::dragCurve(QPointF eventPos, Qt::KeyboardModifiers)
 
1664
{
 
1665
        m_bezier->recalc(eventPos);
 
1666
}
 
1667
 
 
1668
void Wire::changeCurve(const Bezier * bezier)
 
1669
{
 
1670
        prepareGeometryChange();
 
1671
        if (m_bezier == NULL) m_bezier = new Bezier;
 
1672
        m_bezier->copy(bezier);
 
1673
        update();
 
1674
}
 
1675
 
 
1676
bool Wire::isCurved() {
 
1677
        return (m_bezier != NULL) && !m_bezier->isEmpty();
 
1678
}
 
1679
 
 
1680
const Bezier * Wire::curve() {
 
1681
        return m_bezier;
 
1682
}
 
1683
 
 
1684
const Bezier * Wire::undoCurve() {
 
1685
        return &UndoBezier;
 
1686
}
 
1687
 
 
1688
QPolygonF Wire::sceneCurve(QPointF offset) {
 
1689
        QPolygonF poly;
 
1690
        if (m_bezier == NULL) return poly;
 
1691
        if (m_bezier->isEmpty()) return poly;
 
1692
 
 
1693
        poly.append(m_line.p1() + pos() - offset);
 
1694
        poly.append(m_bezier->cp0() + pos() - offset);
 
1695
        poly.append(m_bezier->cp1() + pos() - offset);
 
1696
        poly.append(m_line.p2() + pos() - offset);
 
1697
        return poly;
 
1698
}
 
1699
 
 
1700
bool Wire::hasShadow() {
 
1701
        if (getRatsnest()) return false;
 
1702
        if (getTrace()) return false;
 
1703
        return m_pen.widthF() != m_shadowPen.widthF();
 
1704
}
 
1705
 
 
1706
bool Wire::eventFilter(QObject * object, QEvent * event)
 
1707
{
 
1708
        Q_UNUSED(object);
 
1709
        if (!(m_dragEnd || m_dragCurve)) {
 
1710
                if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
 
1711
                        InfoGraphicsView * infoGraphicsView = InfoGraphicsView::getInfoGraphicsView(this);;
 
1712
                        if (infoGraphicsView) {
 
1713
                                QPoint p = infoGraphicsView->mapFromGlobal(QCursor::pos());
 
1714
                                QPointF r = infoGraphicsView->mapToScene(p);
 
1715
                                QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
 
1716
                                // DebugDialog::debug(QString("got key event %1").arg(keyEvent->modifiers()));
 
1717
                                updateCursor(keyEvent->modifiers());
 
1718
                        }
 
1719
                }
 
1720
        }
 
1721
 
 
1722
        return false;
 
1723
}
 
1724
 
 
1725
void Wire::updateCursor(Qt::KeyboardModifiers modifiers)
 
1726
{
 
1727
        if (m_connectorHover) {
 
1728
                return;
 
1729
        }
 
1730
 
 
1731
        InfoGraphicsView * infoGraphicsView = InfoGraphicsView::getInfoGraphicsView(this);
 
1732
        bool segment = false;
 
1733
        int totalConnections = 0;
 
1734
        foreach (ConnectorItem * connectorItem, cachedConnectorItems()) {
 
1735
                totalConnections += connectorItem->connectionsCount();
 
1736
        }
 
1737
        if (totalConnections == 2 && modifiers & altOrMetaModifier()) {
 
1738
                segment = true;
 
1739
                foreach (ConnectorItem * connectorItem, cachedConnectorItems()) {
 
1740
                        if (connectorItem->connectionsCount() != 1) {
 
1741
                                segment = false;
 
1742
                                break;
 
1743
                        }
 
1744
 
 
1745
                        ConnectorItem * toConnectorItem = connectorItem->connectedToItems().at(0);
 
1746
                        if (toConnectorItem->attachedToItemType() != ModelPart::Wire) {
 
1747
                                segment = false;
 
1748
                                break;
 
1749
                        }
 
1750
                }
 
1751
        }
 
1752
                
 
1753
        if (segment) {
 
1754
                // dragging a segment of wire between bounded by two other wires
 
1755
                QApplication::changeOverrideCursor(*ConnectorItem::RubberbandCursor);
 
1756
        }
 
1757
        else if (totalConnections == 0) {
 
1758
                // only in breadboard view
 
1759
                QApplication::changeOverrideCursor(*ConnectorItem::MoveCursor);
 
1760
        }
 
1761
        else if (infoGraphicsView != NULL && infoGraphicsView->curvyWiresIndicated(modifiers)) {
 
1762
                QApplication::changeOverrideCursor(*ConnectorItem::MakeCurveCursor);
 
1763
        }
 
1764
        else {
 
1765
                QApplication::changeOverrideCursor(*ConnectorItem::NewBendpointCursor);
 
1766
        }
 
1767
}
 
1768
 
 
1769
bool Wire::canChainMultiple()
 
1770
{
 
1771
        return m_canChainMultiple;
 
1772
}