QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
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 "qgsapplication.h"
22#include <QPainter>
23#include <QApplication>
24#include <QPalette>
25
27
28
29QgsModelArrowItem::QgsModelArrowItem( QgsModelComponentGraphicItem *startItem, Qt::Edge startEdge, int startIndex, bool startIsOutgoing, Marker startMarker,
30 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 }
101
102 switch ( mEndMarker )
103 {
104 case Marker::Circle:
105 painter->drawEllipse( mEndPoint, 3.0, 3.0 );
106 break;
107 case Marker::ArrowHead:
108 drawArrowHead( painter, mEndPoint, path().pointAtPercent( 1.0 ) - path().pointAtPercent( 0.95 ) );
109 break;
110 }
111
112 painter->setBrush( Qt::NoBrush );
113 painter->drawPath( path() );
114}
115
116void QgsModelArrowItem::drawArrowHead( QPainter *painter, const QPointF &position, const QPointF &vector )
117{
118 const float angle = atan2( vector.y(), vector.x() ) * 180.0 / M_PI;
119 painter->translate( position );
120 painter->rotate( angle );
121 QPolygonF arrowHead;
122 arrowHead << QPointF( 0, 0 ) << QPointF( -6, 4 ) << QPointF( -6, -4 ) << QPointF( 0, 0 );
123 painter->drawPolygon( arrowHead );
124 painter->rotate( -angle );
125 painter->translate( -position );
126}
127
128void QgsModelArrowItem::setPenStyle( Qt::PenStyle style )
129{
130 QPen p = pen();
131 p.setStyle( style );
132 setPen( p );
133 update();
134}
135
136void QgsModelArrowItem::updatePath()
137{
138 QList< QPointF > controlPoints;
139
140 // is there a fixed start or end point?
141 QPointF startPt;
142 bool hasStartPt = false;
143
144 // usually arrows attached to an algorithm have a concept of directional flow -- they are either
145 // "inputs" to the item or "outputs". In this case we need to reflect this in how we draw the linking
146 // arrows, because we always have "inputs" on the left/top side and "outputs" on the right/bottom
147 bool startHasSpecificDirectionalFlow = qobject_cast< QgsModelChildAlgorithmGraphicItem * >( mStartItem );
148 bool endHasSpecificDirectionalFlow = qobject_cast< QgsModelChildAlgorithmGraphicItem * >( mEndItem );
149
150 // some specific exceptions to the above
151 if ( qobject_cast< QgsModelCommentGraphicItem * >( mStartItem )
152 || qobject_cast< QgsModelCommentGraphicItem * >( mEndItem ) )
153 {
154 // comments can be freely attached to any side of an algorithm item without directional flow
155 startHasSpecificDirectionalFlow = false;
156 endHasSpecificDirectionalFlow = false;
157 }
158
159 if ( mStartIndex != -1 )
160 {
161 startPt = mStartItem->linkPoint( mStartEdge, mStartIndex, !mStartIsOutgoing );
162 hasStartPt = true;
163 }
164 QPointF endPt;
165 bool hasEndPt = false;
166 if ( mEndIndex != -1 )
167 {
168 endPt = mEndItem->linkPoint( mEndEdge, mEndIndex, mEndIsIncoming );
169 hasEndPt = true;
170 }
171
172 if ( !hasStartPt )
173 {
174 Qt::Edge startEdge;
175 QPointF pt;
176 if ( !hasEndPt )
177 pt = mStartItem->calculateAutomaticLinkPoint( mEndItem, startEdge );
178 else
179 pt = mStartItem->calculateAutomaticLinkPoint( endPt + mEndItem->pos(), startEdge );
180
181 controlPoints.append( pt );
182 mStartPoint = pt;
183 controlPoints.append( bezierPointForCurve( pt, startEdge, !mStartIsOutgoing, startHasSpecificDirectionalFlow ) );
184 }
185 else
186 {
187 mStartPoint = mStartItem->pos() + startPt;
188 controlPoints.append( mStartItem->pos() + startPt );
189 controlPoints.append( bezierPointForCurve( mStartItem->pos() + startPt, mStartEdge == Qt::BottomEdge ? Qt::RightEdge : Qt::LeftEdge, !mStartIsOutgoing, startHasSpecificDirectionalFlow ) );
190 }
191
192 if ( !hasEndPt )
193 {
194 Qt::Edge endEdge;
195 QPointF pt;
196 if ( !hasStartPt )
197 pt = mEndItem->calculateAutomaticLinkPoint( mStartItem, endEdge );
198 else
199 pt = mEndItem->calculateAutomaticLinkPoint( startPt + mStartItem->pos(), endEdge );
200
201 controlPoints.append( bezierPointForCurve( pt, endEdge, mEndIsIncoming, endHasSpecificDirectionalFlow ) );
202 controlPoints.append( pt );
203 mEndPoint = pt;
204 }
205 else
206 {
207 mEndPoint = mEndItem->pos() + endPt ;
208 controlPoints.append( bezierPointForCurve( mEndItem->pos() + endPt, mEndEdge == Qt::BottomEdge ? Qt::RightEdge : Qt::LeftEdge, mEndIsIncoming, endHasSpecificDirectionalFlow ) );
209 controlPoints.append( mEndItem->pos() + endPt );
210 }
211
212 QPainterPath path;
213 path.moveTo( controlPoints.at( 0 ) );
214 path.cubicTo( controlPoints.at( 1 ), controlPoints.at( 2 ), controlPoints.at( 3 ) );
215 setPath( path );
216}
217
218QPointF QgsModelArrowItem::bezierPointForCurve( const QPointF &point, Qt::Edge edge, bool incoming, bool hasSpecificDirectionalFlow ) const
219{
220 switch ( edge )
221 {
222 case Qt::LeftEdge:
223 return point + QPointF( hasSpecificDirectionalFlow ? ( incoming ? -50 : 50 ) : -50, 0 );
224
225 case Qt::RightEdge:
226 return point + QPointF( hasSpecificDirectionalFlow ? ( incoming ? -50 : 50 ) : 50, 0 );
227
228 case Qt::TopEdge:
229 return point + QPointF( 0, hasSpecificDirectionalFlow ? ( incoming ? -30 : 30 ) : -30 );
230
231 case Qt::BottomEdge:
232 return point + QPointF( 0, hasSpecificDirectionalFlow ? ( incoming ? -30 : 30 ) : 30 );
233 }
234 return QPointF();
235}
236
237
239
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)