QGIS API Documentation 3.43.0-Master (e737cc10456)
qgsmodelarrowitem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmodelarrowitem.cpp
3 ----------------------------------
4 Date : March 2020
5 Copyright : (C) 2020 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include <math.h>
17
18#include "qgsmodelarrowitem.h"
19#include "moc_qgsmodelarrowitem.cpp"
20#include "qgsapplication.h"
23#include <QPainter>
24#include <QApplication>
25#include <QPalette>
26
28
29
30QgsModelArrowItem::QgsModelArrowItem( QgsModelComponentGraphicItem *startItem, Qt::Edge startEdge, int startIndex, bool startIsOutgoing, Marker startMarker, QgsModelComponentGraphicItem *endItem, Qt::Edge endEdge, int endIndex, bool endIsIncoming, Marker endMarker )
31 : QObject( nullptr )
32 , mStartItem( startItem )
33 , mStartEdge( startEdge )
34 , mStartIndex( startIndex )
35 , mStartIsOutgoing( startIsOutgoing )
36 , mStartMarker( startMarker )
37 , mEndItem( endItem )
38 , mEndEdge( endEdge )
39 , mEndIndex( endIndex )
40 , mEndIsIncoming( endIsIncoming )
41 , mEndMarker( endMarker )
42{
43 setCacheMode( QGraphicsItem::DeviceCoordinateCache );
44 setFlag( QGraphicsItem::ItemIsSelectable, false );
45 mColor = QApplication::palette().color( QPalette::Text );
46 mColor.setAlpha( 150 );
47 setPen( QPen( mColor, 8, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin ) );
48 setZValue( QgsModelGraphicsScene::ArrowLink );
49 updatePath();
50
51 connect( mStartItem, &QgsModelComponentGraphicItem::updateArrowPaths, this, &QgsModelArrowItem::updatePath );
52 connect( mStartItem, &QgsModelComponentGraphicItem::repaintArrows, this, [=] { update(); } );
53 connect( mEndItem, &QgsModelComponentGraphicItem::updateArrowPaths, this, &QgsModelArrowItem::updatePath );
54 connect( mEndItem, &QgsModelComponentGraphicItem::repaintArrows, this, [=] { update(); } );
55}
56
57QgsModelArrowItem::QgsModelArrowItem( QgsModelComponentGraphicItem *startItem, Qt::Edge startEdge, int startIndex, Marker startMarker, QgsModelComponentGraphicItem *endItem, Marker endMarker )
58 : QgsModelArrowItem( startItem, startEdge, startIndex, true, startMarker, endItem, Qt::LeftEdge, -1, true, endMarker )
59{
60}
61
62QgsModelArrowItem::QgsModelArrowItem( QgsModelComponentGraphicItem *startItem, Marker startMarker, QgsModelComponentGraphicItem *endItem, Qt::Edge endEdge, int endIndex, Marker endMarker )
63 : QgsModelArrowItem( startItem, Qt::LeftEdge, -1, true, startMarker, endItem, endEdge, endIndex, true, endMarker )
64{
65}
66
67QgsModelArrowItem::QgsModelArrowItem( QgsModelComponentGraphicItem *startItem, Marker startMarker, QgsModelComponentGraphicItem *endItem, Marker endMarker )
68 : QgsModelArrowItem( startItem, Qt::LeftEdge, -1, true, startMarker, endItem, Qt::LeftEdge, -1, true, endMarker )
69{
70}
71
72
73void QgsModelArrowItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * )
74{
75 QColor color = mColor;
76
77 if ( mStartItem->state() == QgsModelComponentGraphicItem::Selected || mEndItem->state() == QgsModelComponentGraphicItem::Selected )
78 color.setAlpha( 220 );
79 else if ( mStartItem->state() == QgsModelComponentGraphicItem::Hover || mEndItem->state() == QgsModelComponentGraphicItem::Hover )
80 color.setAlpha( 150 );
81 else
82 color.setAlpha( 80 );
83
84 QPen p = pen();
85 p.setColor( color );
86 p.setWidth( 1 );
87 painter->setPen( p );
88 painter->setBrush( color );
89 painter->setRenderHint( QPainter::Antialiasing );
90
91
92 switch ( mStartMarker )
93 {
94 case Marker::Circle:
95 painter->drawEllipse( mStartPoint, 3.0, 3.0 );
96 break;
97 case Marker::ArrowHead:
98 drawArrowHead( painter, mStartPoint, path().pointAtPercent( 0.0 ) - path().pointAtPercent( 0.05 ) );
99 break;
100 case Marker::NoMarker:
101 break;
102 }
103
104 switch ( mEndMarker )
105 {
106 case Marker::Circle:
107 painter->drawEllipse( mEndPoint, 3.0, 3.0 );
108 break;
109 case Marker::ArrowHead:
110 drawArrowHead( painter, mEndPoint, path().pointAtPercent( 1.0 ) - path().pointAtPercent( 0.95 ) );
111 break;
112 case Marker::NoMarker:
113 break;
114 }
115
116 painter->setBrush( Qt::NoBrush );
117 painter->drawPath( path() );
118}
119
120void QgsModelArrowItem::drawArrowHead( QPainter *painter, const QPointF &position, const QPointF &vector )
121{
122 const float angle = atan2( vector.y(), vector.x() ) * 180.0 / M_PI;
123 painter->translate( position );
124 painter->rotate( angle );
125 QPolygonF arrowHead;
126 arrowHead << QPointF( 0, 0 ) << QPointF( -6, 4 ) << QPointF( -6, -4 ) << QPointF( 0, 0 );
127 painter->drawPolygon( arrowHead );
128 painter->rotate( -angle );
129 painter->translate( -position );
130}
131
132void QgsModelArrowItem::setPenStyle( Qt::PenStyle style )
133{
134 QPen p = pen();
135 p.setStyle( style );
136 setPen( p );
137 update();
138}
139
140void QgsModelArrowItem::updatePath()
141{
142 QList<QPointF> controlPoints;
143
144 // is there a fixed start or end point?
145 QPointF startPt;
146 bool hasStartPt = false;
147
148 // usually arrows attached to an algorithm have a concept of directional flow -- they are either
149 // "inputs" to the item or "outputs". In this case we need to reflect this in how we draw the linking
150 // arrows, because we always have "inputs" on the left/top side and "outputs" on the right/bottom
151 bool startHasSpecificDirectionalFlow = qobject_cast<QgsModelChildAlgorithmGraphicItem *>( mStartItem );
152 bool endHasSpecificDirectionalFlow = qobject_cast<QgsModelChildAlgorithmGraphicItem *>( mEndItem );
153
154 // some specific exceptions to the above
155 if ( qobject_cast<QgsModelCommentGraphicItem *>( mStartItem )
156 || qobject_cast<QgsModelCommentGraphicItem *>( mEndItem ) )
157 {
158 // comments can be freely attached to any side of an algorithm item without directional flow
159 startHasSpecificDirectionalFlow = false;
160 endHasSpecificDirectionalFlow = false;
161 }
162
163 if ( mStartIndex != -1 )
164 {
165 startPt = mStartItem->linkPoint( mStartEdge, mStartIndex, !mStartIsOutgoing );
166 hasStartPt = true;
167 }
168 QPointF endPt;
169 bool hasEndPt = false;
170 if ( mEndIndex != -1 )
171 {
172 endPt = mEndItem->linkPoint( mEndEdge, mEndIndex, mEndIsIncoming );
173 hasEndPt = true;
174 }
175
176 if ( !hasStartPt )
177 {
178 Qt::Edge startEdge;
179 QPointF pt;
180 if ( !hasEndPt )
181 pt = mStartItem->calculateAutomaticLinkPoint( mEndItem, startEdge );
182 else
183 pt = mStartItem->calculateAutomaticLinkPoint( endPt + mEndItem->pos(), startEdge );
184
185 controlPoints.append( pt );
186 mStartPoint = pt;
187 controlPoints.append( bezierPointForCurve( pt, startEdge, !mStartIsOutgoing, startHasSpecificDirectionalFlow ) );
188 }
189 else
190 {
191 mStartPoint = mStartItem->pos() + startPt;
192 controlPoints.append( mStartItem->pos() + startPt );
193 controlPoints.append( bezierPointForCurve( mStartItem->pos() + startPt, mStartEdge == Qt::BottomEdge ? Qt::RightEdge : Qt::LeftEdge, !mStartIsOutgoing, startHasSpecificDirectionalFlow ) );
194 }
195
196 if ( !hasEndPt )
197 {
198 Qt::Edge endEdge;
199 QPointF pt;
200 if ( !hasStartPt )
201 pt = mEndItem->calculateAutomaticLinkPoint( mStartItem, endEdge );
202 else
203 pt = mEndItem->calculateAutomaticLinkPoint( startPt + mStartItem->pos(), endEdge );
204
205 controlPoints.append( bezierPointForCurve( pt, endEdge, mEndIsIncoming, endHasSpecificDirectionalFlow ) );
206 controlPoints.append( pt );
207 mEndPoint = pt;
208 }
209 else
210 {
211 mEndPoint = mEndItem->pos() + endPt;
212 controlPoints.append( bezierPointForCurve( mEndItem->pos() + endPt, mEndEdge == Qt::BottomEdge ? Qt::RightEdge : Qt::LeftEdge, mEndIsIncoming, endHasSpecificDirectionalFlow ) );
213 controlPoints.append( mEndItem->pos() + endPt );
214 }
215
216 QPainterPath path;
217 path.moveTo( controlPoints.at( 0 ) );
218 path.cubicTo( controlPoints.at( 1 ), controlPoints.at( 2 ), controlPoints.at( 3 ) );
219 setPath( path );
220}
221
222QPointF QgsModelArrowItem::bezierPointForCurve( const QPointF &point, Qt::Edge edge, bool incoming, bool hasSpecificDirectionalFlow ) const
223{
224 switch ( edge )
225 {
226 case Qt::LeftEdge:
227 return point + QPointF( hasSpecificDirectionalFlow ? ( incoming ? -50 : 50 ) : -50, 0 );
228
229 case Qt::RightEdge:
230 return point + QPointF( hasSpecificDirectionalFlow ? ( incoming ? -50 : 50 ) : 50, 0 );
231
232 case Qt::TopEdge:
233 return point + QPointF( 0, hasSpecificDirectionalFlow ? ( incoming ? -30 : 30 ) : -30 );
234
235 case Qt::BottomEdge:
236 return point + QPointF( 0, hasSpecificDirectionalFlow ? ( incoming ? -30 : 30 ) : 30 );
237 }
238 return QPointF();
239}
240
241
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)