1 /*******************************************************************
2 
3 Part of the Fritzing project - http://fritzing.org
4 Copyright (c) 2007-2014 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: 6912 $:
22 $Author: irascibl@gmail.com $:
23 $Date: 2013-03-09 08:18:59 +0100 (Sa, 09. Mrz 2013) $
24 
25 ********************************************************************/
26 
27 #include "virtualwire.h"
28 #include "../connectors/connectoritem.h"
29 #include "../model/modelpart.h"
30 
31 const double VirtualWire::ShapeWidthExtra = 4;
32 
VirtualWire(ModelPart * modelPart,ViewLayer::ViewID viewID,const ViewGeometry & viewGeometry,long id,QMenu * itemMenu)33 VirtualWire::VirtualWire( ModelPart * modelPart, ViewLayer::ViewID viewID,  const ViewGeometry & viewGeometry, long id, QMenu * itemMenu  )
34 	: ClipableWire(modelPart, viewID,  viewGeometry,  id, itemMenu, false)
35 {
36 	// note: at this point in fritzing development, the VirtualWire class is only ever used for ratsnest wires
37 	modelPart->setLocalProp("ratsnest", "true");
38 	m_colorWasNamed = false;
39 	setFlag(QGraphicsItem::ItemIsSelectable, false);
40 }
41 
~VirtualWire()42 VirtualWire::~VirtualWire() {
43 }
44 
paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget * widget)45 void VirtualWire::paint (QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget ) {
46 	if (m_hidden) return;
47 
48 	m_hoverCount = m_connectorHoverCount = 0;			// kills any highlighting
49 	Wire::paint(painter, option, widget);
50 }
51 
connectionChange(ConnectorItem * onMe,ConnectorItem * onIt,bool connect)52 void VirtualWire::connectionChange(ConnectorItem * onMe, ConnectorItem * onIt, bool connect) {
53 	checkVisibility(onMe, onIt, connect);
54 }
55 
setUpConnectors(ModelPart * modelPart,ViewLayer::ViewID viewID)56 FSvgRenderer * VirtualWire::setUpConnectors(ModelPart * modelPart, ViewLayer::ViewID viewID) {
57 	FSvgRenderer * renderer = Wire::setUpConnectors(modelPart, viewID);
58 	hideConnectors();
59 	return renderer;
60 }
61 
hideConnectors()62 void VirtualWire::hideConnectors() {
63 	// m_connector0 and m_connector1 may not yet be initialized
64 	foreach (ConnectorItem * item, cachedConnectorItems()) {
65 		item->setHidden(true);
66 	}
67 }
68 
inactivateConnectors()69 void VirtualWire::inactivateConnectors() {
70 	// m_connector0 and m_connector1 may not yet be initialized
71 	foreach (ConnectorItem * item, cachedConnectorItems()) {
72 		item->setInactive(true);
73 	}
74 }
75 
setInactive(bool inactivate)76 void VirtualWire::setInactive(bool inactivate) {
77 	ItemBase::setInactive(inactivate);
78 
79 	if (!inactivate) {
80 		inactivateConnectors();
81 	}
82 }
83 
setHidden(bool hide)84 void VirtualWire::setHidden(bool hide) {
85 	ItemBase::setHidden(hide);
86 
87 	if (!hide) {
88 		hideConnectors();
89 	}
90 }
91 
tempRemoveAllConnections()92 void VirtualWire::tempRemoveAllConnections() {
93 	ConnectorItem * connectorItem = connector0();
94 	for (int j = connectorItem->connectedToItems().count() - 1; j >= 0; j--) {
95 		connectorItem->connectedToItems()[j]->tempRemove(connectorItem, false);
96 		connectorItem->tempRemove(connectorItem->connectedToItems()[j], false);
97 	}
98 	connectorItem = connector1();
99 	for (int j = connectorItem->connectedToItems().count() - 1; j >= 0; j--) {
100 		connectorItem->connectedToItems()[j]->tempRemove(connectorItem, false);
101 		connectorItem->tempRemove(connectorItem->connectedToItems()[j], false);
102 	}
103 }
104 
mousePressEvent(QGraphicsSceneMouseEvent * event)105 void VirtualWire::mousePressEvent(QGraphicsSceneMouseEvent *event)
106 {
107 	// ignore clicks where our connectors are supposed to be
108 	// so the click can percolate to some other graphicsitem that can use it
109 
110 	QList<QGraphicsItem *> items = scene()->items(event->scenePos());
111 	if (items.contains(m_connector0) || items.contains(m_connector1)) {
112 		event->ignore();
113 		return;
114 	}
115 
116 	// set selectable flag temporarily here so that dragging out a bendpoint is enabled
117 	setFlag(QGraphicsItem::ItemIsSelectable, true);
118 	ClipableWire::mousePressEvent(event);
119 	setFlag(QGraphicsItem::ItemIsSelectable, false);
120 }
121 
setColorWasNamed(bool colorWasNamed)122 void VirtualWire::setColorWasNamed(bool colorWasNamed) {
123 	m_colorWasNamed = colorWasNamed;
124 }
125 
colorWasNamed()126 bool VirtualWire::colorWasNamed() {
127 	return m_colorWasNamed;
128 }
129 
shape() const130 QPainterPath VirtualWire::shape() const
131 {
132 	return shapeAux(m_hoverStrokeWidth);
133 }
134