19#include "moc_qgsmodelarrowitem.cpp"
24#include <QApplication>
30QgsModelArrowItem::QgsModelArrowItem( QgsModelComponentGraphicItem *startItem, Qt::Edge startEdge,
int startIndex,
bool startIsOutgoing, Marker startMarker,
31 QgsModelComponentGraphicItem *endItem, Qt::Edge endEdge,
int endIndex,
bool endIsIncoming, Marker endMarker )
33 , mStartItem( startItem )
34 , mStartEdge( startEdge )
35 , mStartIndex( startIndex )
36 , mStartIsOutgoing( startIsOutgoing )
37 , mStartMarker( startMarker )
40 , mEndIndex( endIndex )
41 , mEndIsIncoming( endIsIncoming )
42 , mEndMarker( endMarker )
44 setCacheMode( QGraphicsItem::DeviceCoordinateCache );
45 setFlag( QGraphicsItem::ItemIsSelectable,
false );
46 mColor = QApplication::palette().color( QPalette::Text );
47 mColor.setAlpha( 150 );
48 setPen( QPen( mColor, 8, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin ) );
49 setZValue( QgsModelGraphicsScene::ArrowLink );
52 connect( mStartItem, &QgsModelComponentGraphicItem::updateArrowPaths,
this, &QgsModelArrowItem::updatePath );
53 connect( mStartItem, &QgsModelComponentGraphicItem::repaintArrows,
this, [ = ] { update(); } );
54 connect( mEndItem, &QgsModelComponentGraphicItem::updateArrowPaths,
this, &QgsModelArrowItem::updatePath );
55 connect( mEndItem, &QgsModelComponentGraphicItem::repaintArrows,
this, [ = ] { update(); } );
58QgsModelArrowItem::QgsModelArrowItem( QgsModelComponentGraphicItem *startItem, Qt::Edge startEdge,
int startIndex, Marker startMarker, QgsModelComponentGraphicItem *endItem, Marker endMarker )
59 : QgsModelArrowItem( startItem, startEdge, startIndex, true, startMarker, endItem, Qt::LeftEdge, -1, true, endMarker )
63QgsModelArrowItem::QgsModelArrowItem( QgsModelComponentGraphicItem *startItem, Marker startMarker, QgsModelComponentGraphicItem *endItem, Qt::Edge endEdge,
int endIndex, Marker endMarker )
64 : QgsModelArrowItem( startItem, Qt::LeftEdge, -1, true, startMarker, endItem, endEdge, endIndex, true, endMarker )
68QgsModelArrowItem::QgsModelArrowItem( QgsModelComponentGraphicItem *startItem, Marker startMarker, QgsModelComponentGraphicItem *endItem, Marker endMarker )
69 : QgsModelArrowItem( startItem, Qt::LeftEdge, -1, true, startMarker, endItem, Qt::LeftEdge, -1, true, endMarker )
74void QgsModelArrowItem::paint( QPainter *painter,
const QStyleOptionGraphicsItem *, QWidget * )
76 QColor color = mColor;
78 if ( mStartItem->state() == QgsModelComponentGraphicItem::Selected || mEndItem->state() == QgsModelComponentGraphicItem::Selected )
79 color.setAlpha( 220 );
80 else if ( mStartItem->state() == QgsModelComponentGraphicItem::Hover || mEndItem->state() == QgsModelComponentGraphicItem::Hover )
81 color.setAlpha( 150 );
89 painter->setBrush( color );
90 painter->setRenderHint( QPainter::Antialiasing );
93 switch ( mStartMarker )
96 painter->drawEllipse( mStartPoint, 3.0, 3.0 );
98 case Marker::ArrowHead:
99 drawArrowHead( painter, mStartPoint, path().pointAtPercent( 0.0 ) - path().pointAtPercent( 0.05 ) );
103 switch ( mEndMarker )
106 painter->drawEllipse( mEndPoint, 3.0, 3.0 );
108 case Marker::ArrowHead:
109 drawArrowHead( painter, mEndPoint, path().pointAtPercent( 1.0 ) - path().pointAtPercent( 0.95 ) );
113 painter->setBrush( Qt::NoBrush );
114 painter->drawPath( path() );
117void QgsModelArrowItem::drawArrowHead( QPainter *painter,
const QPointF &position,
const QPointF &vector )
119 const float angle = atan2( vector.y(), vector.x() ) * 180.0 / M_PI;
120 painter->translate( position );
121 painter->rotate( angle );
123 arrowHead << QPointF( 0, 0 ) << QPointF( -6, 4 ) << QPointF( -6, -4 ) << QPointF( 0, 0 );
124 painter->drawPolygon( arrowHead );
125 painter->rotate( -angle );
126 painter->translate( -position );
129void QgsModelArrowItem::setPenStyle( Qt::PenStyle style )
137void QgsModelArrowItem::updatePath()
139 QList< QPointF > controlPoints;
143 bool hasStartPt =
false;
148 bool startHasSpecificDirectionalFlow = qobject_cast< QgsModelChildAlgorithmGraphicItem * >( mStartItem );
149 bool endHasSpecificDirectionalFlow = qobject_cast< QgsModelChildAlgorithmGraphicItem * >( mEndItem );
152 if ( qobject_cast< QgsModelCommentGraphicItem * >( mStartItem )
153 || qobject_cast< QgsModelCommentGraphicItem * >( mEndItem ) )
156 startHasSpecificDirectionalFlow =
false;
157 endHasSpecificDirectionalFlow =
false;
160 if ( mStartIndex != -1 )
162 startPt = mStartItem->linkPoint( mStartEdge, mStartIndex, !mStartIsOutgoing );
166 bool hasEndPt =
false;
167 if ( mEndIndex != -1 )
169 endPt = mEndItem->linkPoint( mEndEdge, mEndIndex, mEndIsIncoming );
178 pt = mStartItem->calculateAutomaticLinkPoint( mEndItem, startEdge );
180 pt = mStartItem->calculateAutomaticLinkPoint( endPt + mEndItem->pos(), startEdge );
182 controlPoints.append( pt );
184 controlPoints.append( bezierPointForCurve( pt, startEdge, !mStartIsOutgoing, startHasSpecificDirectionalFlow ) );
188 mStartPoint = mStartItem->pos() + startPt;
189 controlPoints.append( mStartItem->pos() + startPt );
190 controlPoints.append( bezierPointForCurve( mStartItem->pos() + startPt, mStartEdge == Qt::BottomEdge ? Qt::RightEdge : Qt::LeftEdge, !mStartIsOutgoing, startHasSpecificDirectionalFlow ) );
198 pt = mEndItem->calculateAutomaticLinkPoint( mStartItem, endEdge );
200 pt = mEndItem->calculateAutomaticLinkPoint( startPt + mStartItem->pos(), endEdge );
202 controlPoints.append( bezierPointForCurve( pt, endEdge, mEndIsIncoming, endHasSpecificDirectionalFlow ) );
203 controlPoints.append( pt );
208 mEndPoint = mEndItem->pos() + endPt ;
209 controlPoints.append( bezierPointForCurve( mEndItem->pos() + endPt, mEndEdge == Qt::BottomEdge ? Qt::RightEdge : Qt::LeftEdge, mEndIsIncoming, endHasSpecificDirectionalFlow ) );
210 controlPoints.append( mEndItem->pos() + endPt );
214 path.moveTo( controlPoints.at( 0 ) );
215 path.cubicTo( controlPoints.at( 1 ), controlPoints.at( 2 ), controlPoints.at( 3 ) );
219QPointF QgsModelArrowItem::bezierPointForCurve(
const QPointF &point, Qt::Edge edge,
bool incoming,
bool hasSpecificDirectionalFlow )
const
224 return point + QPointF( hasSpecificDirectionalFlow ? ( incoming ? -50 : 50 ) : -50, 0 );
227 return point + QPointF( hasSpecificDirectionalFlow ? ( incoming ? -50 : 50 ) : 50, 0 );
230 return point + QPointF( 0, hasSpecificDirectionalFlow ? ( incoming ? -30 : 30 ) : -30 );
233 return point + QPointF( 0, hasSpecificDirectionalFlow ? ( incoming ? -30 : 30 ) : 30 );
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)