DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
SchemaGraphicArrow.cpp
Go to the documentation of this file.
1
2#include <QPainter>
3#include <QPen>
8#include <cmath>
9
11 SchemaGraphicObject * EndItem, bool IsInheritance,
12 bool IsComposite, QString ArrowName,
13 QString ArrowCardinality, QGraphicsItem * parent )
14 : QGraphicsLineItem ( parent ),
15 Start ( StartItem ),
16 End ( EndItem ),
17 Inheritance ( IsInheritance ),
18 Composite ( IsComposite ),
19 Name ( ArrowName ),
20 Cardinality ( ArrowCardinality ),
21 Label ( nullptr ),
22 LastDegree ( 0 ),
23 LastRotation ( 0 )
24{
25 //setPen(QPen(Qt::black,2,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin));
26 setFlag ( ItemIsSelectable, true );
27 //LabelString = Name + " - " + Cardinality;
28
29 if ( !Start->collidesWithItem ( End ) && !Inheritance )
30 {
31 Label = new QGraphicsSimpleTextItem ( Name + " - " + Cardinality );
32 Label->setParentItem ( this );
33 }
34
35}
36
40
42{
43 qreal extra = ( pen().width() + 20 ) / 2.0 + 10;
44 return QRectF ( line().p1(),
45 QSizeF ( line().p2().x() - line().p1().x(),
46 line().p2().y() - line().p1().y() ) ).normalized().adjusted (
47 -extra, -extra, extra, extra );
48}
49
51{
52 QPainterPath path = QGraphicsLineItem::shape();
53 path.addPolygon ( ArrowHead );
54 path.addText ( line().p2() + QPoint ( 10, 10 ), QFont ( "Helvetica [Cronyx]", 10 ),
55 Cardinality );
56 path.addText ( line().p1() + QPoint ( -10, -10 ), QFont ( "Helvetica [Cronyx]", 10 ),
57 Cardinality );
58 return path;
59}
60
62{
63 QLineF line ( mapFromItem ( Start, 0, 0 ), mapFromItem ( End, 0, 0 ) );
64 setLine ( line );
65}
66
71
76
78{
79 return Inheritance;
80}
81
83{
84 if ( Inheritance )
85 {
87 }
88 else
89 {
91 }
92}
93
95{
96 Scene->addItem ( Label );
97}
98
99void dbse::SchemaGraphicArrow::paint ( QPainter * painter,
100 const QStyleOptionGraphicsItem * option,
101 QWidget * widget )
102{
103 Q_UNUSED ( option )
104 Q_UNUSED ( widget )
105
106 if ( Start->collidesWithItem ( End ) )
107 {
108 return;
109 }
110
111 QPen myPen = pen();
112 myPen.setColor ( Qt::black );
113
114 QFont Font ( "Helvetica [Cronyx]", 10 );
115 qreal arrowSize = 10;
116 painter->setFont ( Font );
117 painter->setPen ( myPen );
118 painter->setBrush ( Qt::black );
119
120 QLineF centerLine ( Start->mapToScene ( Start->boundingRect().center() ),
121 End->mapToScene ( End->boundingRect().center() ) );
122 QPolygonF startPolygon = QPolygonF ( Start->boundingRect() );
123 QPolygonF endPolygon = QPolygonF ( End->boundingRect() );
124 QPointF p1 = endPolygon.first() + End->pos();
125 QPointF p2;
126 QPointF intersectPointStart;
127 QPointF intersectPointEnd;
128 QLineF polyLine;
129
130 for ( int i = 1; i < endPolygon.count(); ++i )
131 {
132 p2 = endPolygon.at ( i ) + End->pos();
133 polyLine = QLineF ( p1, p2 );
134 QLineF::IntersectType intersectType = polyLine.intersect ( centerLine, &intersectPointEnd );
135
136 if ( intersectType == QLineF::BoundedIntersection )
137 {
138 break;
139 }
140
141 p1 = p2;
142 }
143
144 p1 = startPolygon.first() + Start->pos();
145
146 for ( int i = 1; i < startPolygon.count(); ++i )
147 {
148 p2 = startPolygon.at ( i ) + Start->pos();
149 polyLine = QLineF ( p1, p2 );
150 QLineF::IntersectType intersectType = polyLine.intersect ( centerLine,
151 &intersectPointStart );
152
153 if ( intersectType == QLineF::BoundedIntersection )
154 {
155 break;
156 }
157
158 p1 = p2;
159 }
160
161 setLine ( QLineF ( intersectPointEnd, intersectPointStart ) );
162
163 double angle = ::acos ( line().dx() / line().length() );
164
165 if ( line().dy() >= 0 )
166 {
167 angle = ( M_PI * 2 ) - angle;
168 }
169
170 QPointF arrowP1 = line().p1()
171 + QPointF ( sin ( angle + M_PI / 3 ) * arrowSize, cos ( angle + M_PI / 3 ) * arrowSize );
172 QPointF arrowP2 = line().p1()
173 + QPointF ( sin ( angle + M_PI - M_PI / 3 ) * arrowSize,
174 cos ( angle + M_PI - M_PI / 3 ) * arrowSize );
175 QPointF middlePoint = QPointF ( ( arrowP1.x() + arrowP2.x() ) / 2,
176 ( arrowP1.y() + arrowP2.y() ) / 2 );
177 QPointF arrowP3 = QPointF ( line().p1().x() - 2 * ( line().p1().x() - middlePoint.x() ),
178 line().p1().y() - 2 * ( line().p1().y() - middlePoint.y() ) );
179 ArrowHead.clear();
180 ArrowHead << line().p1() << arrowP1 << arrowP2;
181
182 QFontMetrics Metrics ( Font );
183 Metrics.boundingRect ( Name + " - " + Cardinality );
184
185 painter->drawLine ( line() );
186 qreal degree = ( angle * 180 ) / M_PI;
187
188 if ( Label )
189 {
190
191 Label->setRotation ( -degree + LastDegree );
192
193 if ( degree >= 90 && degree < 270 )
194 {
195 Label->setTransformOriginPoint ( Label->boundingRect().center() );
196 Label->setRotation ( -180 );
197 Label->setTransformOriginPoint ( 0, 0 );
198 LastRotation = 180;
199 Label->setPos ( line().p2() + QPointF ( -5 * cos ( angle ), 5 * sin ( angle ) ) );
200 }
201 else
202 {
203 Label->setTransformOriginPoint ( Label->boundingRect().center() );
204 Label->setRotation ( 360 );
205 Label->setTransformOriginPoint ( 0, 0 );
206 LastRotation = -180;
207
208 if ( Composite )
209 {
210 Label->setPos ( line().p1() + QPointF ( 20 * cos ( angle ), -20 * sin ( angle ) ) );
211 }
212 else
213 {
214 Label->setPos ( line().p1() + QPointF ( 5 * cos ( angle ), -5 * sin ( angle ) ) );
215 }
216 }
217
218 LastDegree = degree;
219 }
220
221 if ( Inheritance )
222 {
223 painter->drawPolygon ( ArrowHead );
224 }
225 else if ( Composite )
226 {
228 ArrowHead.clear();
229 ArrowHead << line().p1() << arrowP1 << arrowP3 << arrowP2;
230 painter->drawPolygon ( ArrowHead );
231 }
232}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
SchemaGraphicObject * GetEndItem() const
SchemaGraphicArrow(SchemaGraphicObject *StartItem, SchemaGraphicObject *EndItem, bool IsInheritance, bool IsComposite, QString ArrowName, QString ArrowCardinality, QGraphicsItem *parent=nullptr)
Including QT Headers.
SchemaGraphicObject * Start
void SetLabelScene(SchemaGraphicsScene *Scene)
SchemaGraphicObject * End
SchemaGraphicObject * GetStartItem() const