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: 7000 $:
22 $Author: irascibl@gmail.com $:
23 $Date: 2013-04-29 07:24:08 +0200 (Mo, 29. Apr 2013) $
24 
25 ********************************************************************/
26 
27 #ifndef WIRE_H
28 #define WIRE_H
29 
30 
31 #include <QGraphicsLineItem>
32 #include <QLineF>
33 #include <QMouseEvent>
34 #include <QGraphicsSceneMouseEvent>
35 #include <QPainter>
36 #include <QStyleOptionGraphicsItem>
37 #include <QWidget>
38 #include <QHash>
39 #include <QMenu>
40 
41 #include "itembase.h"
42 #include "../utils/cursormaster.h"
43 
44 class WireAction : public QAction {
45 	Q_OBJECT
46 
47 public:
48 	WireAction(QAction *);
49 	WireAction(const QString & text, QObject * parent);
50 
51 	void setWire(Wire *);
52 	Wire * wire();
53 
54 protected:
55 	Wire * m_wire;
56 };
57 
58 class Wire : public ItemBase, public CursorKeyListener
59 {
60 Q_OBJECT
61 
62 public:
63 	Wire(class ModelPart *, ViewLayer::ViewID, const ViewGeometry &, long id, QMenu * itemMenu, bool initLabel);
64 	virtual ~Wire();
65 
66 
67 	// for debugging
68 	//void setPos(const QPointF & pos);
69 	// for debugging
70 
71     QLineF line() const;
72     virtual void setLine(const QLineF &line);
73     inline void setLine(double x1, double y1, double x2, double y2);
74     QPen pen() const;
75     void setPen(const QPen &pen);
76 
77 	void saveGeometry();
78 	bool itemMoved();
79 	void saveInstanceLocation(QXmlStreamWriter &);
80 	void writeGeometry(QXmlStreamWriter &);
81 	void moveItem(ViewGeometry & viewGeometry);
82 	void hoverEnterConnectorItem(QGraphicsSceneHoverEvent * event, class ConnectorItem * item);
83 	void hoverLeaveConnectorItem(QGraphicsSceneHoverEvent * event, class ConnectorItem * item);
84 
85 	void initDragEnd(ConnectorItem * dragEnd, QPointF scenePos);
86 	void initDragCurve(QPointF scenePos);
87 	bool initNewBendpoint(QPointF scenePos, class Bezier & left, class Bezier & right);
88 	void connectedMoved(ConnectorItem * from, ConnectorItem * to, QList<ConnectorItem *> & already);
89 	void setLineAnd(QLineF line, QPointF pos, bool useLine);
90 	ConnectorItem * otherConnector(ConnectorItem *);
91 	ConnectorItem * connector0();
92 	ConnectorItem * connector1();
93 	virtual class FSvgRenderer * setUp(ViewLayer::ViewLayerID viewLayerID, const LayerHash & viewLayers, class InfoGraphicsView *);
94 	void findConnectorsUnder();
95 	void collectChained(QList<Wire *> &, QList<ConnectorItem *> & ends);
96 	void collectWires(QList<Wire *> & wires);
97 	bool stickyEnabled();
98 	void setPcbPenBrush(QBrush & brush);
99 	bool getRouted();
100 	void setRouted(bool);
101 	void setRatsnest(bool);
102 	void setAutoroutable(bool);
103 	bool getAutoroutable();
104 	void setNormal(bool);
105 	bool getNormal();
106 	bool getTrace();
107 	bool isTraceType(ViewGeometry::WireFlag);
108 
109 	bool hasFlag(ViewGeometry::WireFlag);
110 	bool hasAnyFlag(ViewGeometry::WireFlags);
111 	void setWireFlags(ViewGeometry::WireFlags);
112 
113 	QString colorString();
114 	QString hexString();
115 	QString shadowHexString();
116 	void setColorString(QString, double opacity, bool restore);
117 	virtual void setColor(const QColor &, double opacity);
118 	double opacity();
119 	void setOpacity(double opacity);
120 	const QColor & color();
121 	void setWireWidth(double width, InfoGraphicsView *, double hoverStrokeWidth);
122 	void setPenWidth(double width, InfoGraphicsView *, double hoverStrokeWidth);
123 	double width();
124 	double shadowWidth();
125 	double mils();
126     void setExtras(QDomElement &, InfoGraphicsView *);
127 	Wire * findTraced(ViewGeometry::WireFlags flags, QList<ConnectorItem *>  & ends);
128 	bool draggingEnd();
129 	void simpleConnectedMoved(ConnectorItem * to);
130 	void simpleConnectedMoved(ConnectorItem * from, ConnectorItem * to);
131 	void setCanChainMultiple(bool);
132 	bool canChangeColor();
133 	void collectDirectWires(QList<Wire *> & wires);
134 	bool isGrounded();
135 	bool collectExtraInfo(QWidget * parent, const QString & family, const QString & prop, const QString & value, bool swappingEnabled, QString & returnProp, QString & returnValue, QWidget * & returnWidget, bool & hide);
136 	bool hasPartLabel();
137 	PluralType isPlural();
138 	virtual bool canSwitchLayers();
139 	void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 );
140 	void paintBody(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
141 
142 	bool hasPartNumberProperty();
143 	bool rotationAllowed();
144 	bool rotation45Allowed();
145 	void addedToScene(bool temporary);
146 	void setConnectorDimensions(double width, double height);
147 	void originalConnectorDimensions(double & width, double & height);
148 	double hoverStrokeWidth();
149     QPainterPath hoverShape() const;
150     QPainterPath shape() const;
151 	QRectF boundingRect() const;
152 	virtual const QLineF & getPaintLine();
153 	bool canHaveCurve();
154 	void changeCurve(const class Bezier *);
155 	bool isCurved();
156 	const class Bezier * curve();
157 	const class Bezier * undoCurve();
158 	QPolygonF sceneCurve(QPointF offset);
159 	bool hasShadow();
160 	bool canChainMultiple();
161 	void cursorKeyEvent(Qt::KeyboardModifiers modifiers);
162 	void setDisplayBendpointCursor(bool);
163     bool banded();
164     void setBanded(bool);
165 	void setProp(const QString & prop, const QString & value);
166 
167 protected slots:
168 	void colorEntry(const QString & text);
169     void setBandedProp(bool);
170 
171 public:
172 	static double STANDARD_TRACE_WIDTH;
173 	static double HALF_STANDARD_TRACE_WIDTH;
174 	static double THIN_TRACE_WIDTH;
175 
176 public:
177 	static void initNames();
178 	static void cleanup();
179     static QVector<qreal> TheDash;
180 
181 protected:
182 	void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
183 	void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
184 	void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
185 	void mouseMoveEventAux(QPointF eventPos, Qt::KeyboardModifiers);
186 	void dragCurve(QPointF eventPos, Qt::KeyboardModifiers);
187 	void mousePressEvent(QGraphicsSceneMouseEvent *event);
188 	void paintHover(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
189 	void initEnds(const ViewGeometry &, QRectF defaultRect, class InfoGraphicsView *);
190 	void connectionChange(ConnectorItem * onMe, ConnectorItem * onIt, bool connect);
191 	void mousePressConnectorEvent(ConnectorItem *, QGraphicsSceneMouseEvent *);
192 	void mouseDoubleClickConnectorEvent(ConnectorItem *);
193 	void mouseMoveConnectorEvent(ConnectorItem *, QGraphicsSceneMouseEvent *);
194 	void mouseReleaseConnectorEvent(ConnectorItem *, QGraphicsSceneMouseEvent *);
195 	bool acceptsMouseDoubleClickConnectorEvent(ConnectorItem *, QGraphicsSceneMouseEvent *);
196 	bool acceptsMouseMoveConnectorEvent(ConnectorItem *, QGraphicsSceneMouseEvent *);
197 	bool acceptsMouseReleaseConnectorEvent(ConnectorItem *, QGraphicsSceneMouseEvent *);
198  	virtual class FSvgRenderer * setUpConnectors(class ModelPart *, ViewLayer::ViewID);
199 	void collectChained(ConnectorItem * connectorItem, QList<Wire *> & chained, QList<ConnectorItem *> & ends);
200 	void setConnector0Rect();
201 	void setConnector1Rect();
202 	void collectWiresAux(QList<Wire *> & wires, ConnectorItem * start);
203 	void setShadowColor(QColor &, bool restore);
204 	void calcNewLine(ConnectorItem * from, ConnectorItem * to, QPointF & p1, QPointF & p2);
205 	void collectDirectWires(ConnectorItem * connectorItem, QList<Wire *> & wires, QList<ConnectorItem *> & junctions);
206 	QVariant itemChange(GraphicsItemChange change, const QVariant &value);
207 	void getConnectedColor(ConnectorItem *, QBrush &, QPen &, double & opacity, double & negativePenWidth, bool & negativeOffsetRect);
208 	bool connectionIsAllowed(ConnectorItem *);
209 	bool releaseDrag();
210 	void setIgnoreSelectionChange(bool);
211     virtual void setColorFromElement(QDomElement & element);
212 	void checkVisibility(ConnectorItem * onMe, ConnectorItem * onIt, bool connect);
213 	void setConnectorDimensionsAux(ConnectorItem *, double width, double height);
214 	bool isBendpoint(ConnectorItem * connectorItem);
215 	QPainterPath shapeAux(double width) const;
216 	void hoverLeaveEvent( QGraphicsSceneHoverEvent * event );
217 	void hoverEnterEvent( QGraphicsSceneHoverEvent * event );
218     void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
219 	void updateCursor(Qt::KeyboardModifiers);
220     ViewLayer::ViewID useViewIDForPixmap(ViewLayer::ViewID, bool swappingEnabled);
221 
222 protected:
223 	QLineF	m_line;
224 	QPen	m_pen;
225 	QPointF m_wireDragOrigin;
226 	bool m_dragEnd;
227 	bool m_dragCurve;
228 	bool m_drag0;
229 	QPointer<class ConnectorItem> m_connectorHover;
230 	QPointer<class ConnectorItem> m_connector0;
231 	QPointer<class ConnectorItem> m_connector1;
232 	QString m_colorName;
233 	QPen m_shadowPen;
234 	QBrush m_shadowBrush;
235 	QPen m_bendpointPen;
236 	QPen m_bendpoint2Pen;
237 	double m_bendpointWidth;
238 	double m_bendpoint2Width;
239 	bool m_negativeOffsetRect;
240 	double m_opacity;
241 	bool m_canChainMultiple;
242 	bool m_ignoreSelectionChange;
243 	bool m_markedDeleted;
244 	QRectF m_originalConnectorRect;
245 	double m_hoverStrokeWidth;
246 	bool m_canHaveCurve;
247 	class Bezier * m_bezier;
248 	bool m_displayBendpointCursor;
249     bool m_banded;
250 
251 public:
252 	static QStringList colorNames;
253 	static QHash<QString, QString> colorTrans;
254 	static QHash<int, QString> widthTrans;
255 	static QList<int> widths;
256 
257 signals:
258 	void wireChangedSignal(Wire* me, const QLineF & oldLine, const QLineF & newLine, QPointF oldPos, QPointF newPos, ConnectorItem * from, ConnectorItem * to);
259 	void wireChangedCurveSignal(Wire* me, const Bezier * oldB, const Bezier * newB, bool triggerFirstTime);
260 	void wireSplitSignal(Wire* me, QPointF newPos, QPointF oldPos, const QLineF & oldLine);
261 	void wireJoinSignal(Wire* me, ConnectorItem * clickedConnectorItem);
262 };
263 
264 #endif
265