QtBase  v6.3.1
arrow.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 
52 #include "arrow.h"
53 #include "diagramitem.h"
54 
55 #include <QPainter>
56 #include <QPen>
57 #include <QtMath>
58 
61  : QGraphicsLineItem(parent), myStartItem(startItem), myEndItem(endItem)
62 {
65 }
67 
70 {
71  qreal extra = (pen().width() + 20) / 2.0;
72 
73  return QRectF(line().p1(), QSizeF(line().p2().x() - line().p1().x(),
74  line().p2().y() - line().p1().y()))
75  .normalized()
76  .adjusted(-extra, -extra, extra, extra);
77 }
79 
82 {
84  path.addPolygon(arrowHead);
85  return path;
86 }
88 
91 {
92  QLineF line(mapFromItem(myStartItem, 0, 0), mapFromItem(myEndItem, 0, 0));
93  setLine(line);
94 }
96 
99  QWidget *)
100 {
101  if (myStartItem->collidesWithItem(myEndItem))
102  return;
103 
104  QPen myPen = pen();
105  myPen.setColor(myColor);
106  qreal arrowSize = 20;
107  painter->setPen(myPen);
108  painter->setBrush(myColor);
110 
111  QLineF centerLine(myStartItem->pos(), myEndItem->pos());
112  QPolygonF endPolygon = myEndItem->polygon();
113  QPointF p1 = endPolygon.first() + myEndItem->pos();
114  QPointF intersectPoint;
115  for (int i = 1; i < endPolygon.count(); ++i) {
116  QPointF p2 = endPolygon.at(i) + myEndItem->pos();
117  QLineF polyLine = QLineF(p1, p2);
118  QLineF::IntersectionType intersectionType =
119  polyLine.intersects(centerLine, &intersectPoint);
120  if (intersectionType == QLineF::BoundedIntersection)
121  break;
122  p1 = p2;
123  }
124 
125  setLine(QLineF(intersectPoint, myStartItem->pos()));
127 
128  double angle = std::atan2(-line().dy(), line().dx());
129 
130  QPointF arrowP1 = line().p1() + QPointF(sin(angle + M_PI / 3) * arrowSize,
131  cos(angle + M_PI / 3) * arrowSize);
132  QPointF arrowP2 = line().p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
133  cos(angle + M_PI - M_PI / 3) * arrowSize);
134 
135  arrowHead.clear();
136  arrowHead << line().p1() << arrowP1 << arrowP2;
138  painter->drawLine(line());
139  painter->drawPolygon(arrowHead);
140  if (isSelected()) {
141  painter->setPen(QPen(myColor, 1, Qt::DashLine));
142  QLineF myLine = line();
143  myLine.translate(0, 4.0);
144  painter->drawLine(myLine);
145  myLine.translate(0,-8.0);
146  painter->drawLine(myLine);
147  }
148 }
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
void updatePosition()
[2]
Definition: arrow.cpp:90
Arrow(DiagramItem *startItem, DiagramItem *endItem, QGraphicsItem *parent=nullptr)
[0]
Definition: arrow.cpp:60
QPainterPath shape() const override
[1]
Definition: arrow.cpp:81
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=nullptr) override
[3]
Definition: arrow.cpp:98
QRectF boundingRect() const override
[0]
Definition: arrow.cpp:69
QPolygonF polygon() const
Definition: diagramitem.h:78
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:83
bool isSelected() const
QPointF pos() const
qreal y() const
virtual bool collidesWithItem(const QGraphicsItem *other, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape) const
QPointF mapFromItem(const QGraphicsItem *item, const QPointF &point) const
qreal x() const
void setFlag(GraphicsItemFlag flag, bool enabled=true)
The QGraphicsLineItem class provides a line item that you can add to a QGraphicsScene.
QPainterPath shape() const override
void setPen(const QPen &pen)
void setLine(const QLineF &line)
QLineF line() const
The QLineF class provides a two-dimensional vector using floating point precision.
Definition: qline.h:215
constexpr QPointF p1() const
Definition: qline.h:322
void translate(const QPointF &p)
Definition: qline.h:347
IntersectionType
Definition: qline.h:218
@ BoundedIntersection
Definition: qline.h:218
IntersectionType intersects(const QLineF &l, QPointF *intersectionPoint=nullptr) const
Definition: qline.cpp:674
const_reference at(qsizetype i) const noexcept
Definition: qlist.h:457
qsizetype count() const noexcept
Definition: qlist.h:415
T & first()
Definition: qlist.h:643
void clear()
Definition: qlist.h:445
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:82
void setPen(const QColor &color)
Definition: qpainter.cpp:3640
void drawLine(const QLineF &line)
Definition: qpainter.h:477
void setBrush(const QBrush &brush)
Definition: qpainter.cpp:3755
void drawPolygon(const QPointF *points, int pointCount, Qt::FillRule fillRule=Qt::OddEvenFill)
Definition: qpainter.cpp:4575
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:65
The QPen class defines how a QPainter should draw lines and outlines of shapes.
Definition: qpen.h:61
int width() const
Definition: qpen.cpp:622
void setColor(const QColor &color)
Definition: qpen.cpp:767
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:242
The QPolygonF class provides a list of points using floating point precision. \inmodule QtGui.
Definition: qpolygon.h:128
The QRectF class defines a finite rectangle in the plane using floating point precision.
Definition: qrect.h:511
constexpr QRectF adjusted(qreal x1, qreal y1, qreal x2, qreal y2) const noexcept
Definition: qrect.h:822
QRectF normalized() const noexcept
Definition: qrect.cpp:1535
The QSizeF class defines the size of a two-dimensional object using floating point precision.
Definition: qsize.h:235
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem.
Definition: qstyleoption.h:684
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
QPixmap p2
QPixmap p1
[0]
#define M_PI
Definition: easing.cpp:23
@ SolidLine
Definition: qnamespace.h:1113
@ DashLine
Definition: qnamespace.h:1114
@ RoundJoin
Definition: qnamespace.h:1134
@ RoundCap
Definition: qnamespace.h:1127
QT_END_INCLUDE_NAMESPACE typedef double qreal
Definition: qglobal.h:341
GLfloat angle
GLsizei const GLchar *const * path
Definition: qopenglext.h:4283
QPainter painter(this)
[7]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent