@@ -1,104 +1,99 | |||
|
1 | 1 | #include "callout.h" |
|
2 | 2 | #include <QPainter> |
|
3 | 3 | #include <QFontMetrics> |
|
4 | 4 | #include <QGraphicsSceneMouseEvent> |
|
5 | 5 | #include <QMouseEvent> |
|
6 | 6 | |
|
7 | 7 | Callout::Callout(QGraphicsItem * parent): |
|
8 | 8 | QGraphicsItem(parent) |
|
9 | 9 | { |
|
10 | 10 | } |
|
11 | 11 | |
|
12 | 12 | QRectF Callout::boundingRect() const |
|
13 | 13 | { |
|
14 | 14 | QPointF anchor = mapFromParent(m_anchor); |
|
15 | 15 | QRectF rect; |
|
16 | 16 | rect.setLeft(qMin(m_rect.left(), anchor.x())); |
|
17 | 17 | rect.setRight(qMax(m_rect.right(), anchor.x())); |
|
18 | 18 | rect.setTop(qMin(m_rect.top(), anchor.y())); |
|
19 | 19 | rect.setBottom(qMax(m_rect.bottom(), anchor.y())); |
|
20 | 20 | return rect; |
|
21 | 21 | } |
|
22 | 22 | |
|
23 | 23 | void Callout::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
24 | 24 | { |
|
25 | 25 | Q_UNUSED(option) |
|
26 | 26 | Q_UNUSED(widget) |
|
27 | 27 | QPainterPath path; |
|
28 | 28 | path.addRoundedRect(m_rect, 5, 5); |
|
29 | 29 | |
|
30 | 30 | QPointF anchor = mapFromParent(m_anchor); |
|
31 | 31 | if (!m_rect.contains(anchor)) { |
|
32 | 32 | QPointF point1, point2; |
|
33 | 33 | |
|
34 | 34 | // establish the position of the anchor point in relation to m_textRect |
|
35 | 35 | bool above = anchor.y() <= m_rect.top(); |
|
36 | 36 | bool aboveCenter = anchor.y() > m_rect.top() && anchor.y() <= m_rect.center().y(); |
|
37 | 37 | bool belowCenter = anchor.y() > m_rect.center().y() && anchor.y() <= m_rect.bottom(); |
|
38 | 38 | bool below = anchor.y() > m_rect.bottom(); |
|
39 | 39 | |
|
40 | 40 | bool onLeft = anchor.x() <= m_rect.left(); |
|
41 | 41 | bool leftOfCenter = anchor.x() > m_rect.left() && anchor.x() <= m_rect.center().x(); |
|
42 | 42 | bool rightOfCenter = anchor.x() > m_rect.center().x() && anchor.x() <= m_rect.right(); |
|
43 | 43 | bool onRight = anchor.x() > m_rect.right(); |
|
44 | 44 | |
|
45 | 45 | // get the nearest m_textRect corner. |
|
46 | 46 | qreal x = (onRight + rightOfCenter) * m_rect.width(); |
|
47 | 47 | qreal y = (below + belowCenter) * m_rect.height(); |
|
48 | 48 | bool cornerCase = (above && onLeft) || (above && onRight) || (below && onLeft) || (below && onRight); |
|
49 | 49 | bool vertical = qAbs(anchor.x() - x) > qAbs(anchor.y() - y); |
|
50 | 50 | |
|
51 | 51 | qreal x1 = x + leftOfCenter * 10 - rightOfCenter * 20 + cornerCase * !vertical * (onLeft * 10 - onRight * 20); |
|
52 | 52 | qreal y1 = y + aboveCenter * 10 - belowCenter * 20 + cornerCase * vertical * (above * 10 - below * 20);; |
|
53 | 53 | point1.setX(x1); |
|
54 | 54 | point1.setY(y1); |
|
55 | 55 | |
|
56 | 56 | qreal x2 = x + leftOfCenter * 20 - rightOfCenter * 10 + cornerCase * !vertical * (onLeft * 20 - onRight * 10);; |
|
57 | 57 | qreal y2 = y + aboveCenter * 20 - belowCenter * 10 + cornerCase * vertical * (above * 20 - below * 10);; |
|
58 | 58 | point2.setX(x2); |
|
59 | 59 | point2.setY(y2); |
|
60 | 60 | |
|
61 | 61 | path.moveTo(point1); |
|
62 | 62 | path.lineTo(mapFromParent(m_anchor)); |
|
63 | 63 | path.lineTo(point2); |
|
64 | 64 | path = path.simplified(); |
|
65 | 65 | } |
|
66 | 66 | painter->setBrush(QColor(255, 255, 255)); |
|
67 | 67 | painter->drawPath(path); |
|
68 | 68 | painter->drawText(m_textRect, m_text); |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | void Callout::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
72 | 72 | { |
|
73 | if (m_rect.contains(event->pos())) { | |
|
74 | m_clickOffset = event->pos(); | |
|
75 | event->setAccepted(true); | |
|
76 | } else { | |
|
77 | event->setAccepted(false); | |
|
78 | } | |
|
73 | event->setAccepted(true); | |
|
79 | 74 | } |
|
80 | 75 | |
|
81 | 76 | void Callout::mouseMoveEvent(QGraphicsSceneMouseEvent *event) |
|
82 | 77 | { |
|
83 | 78 | if (event->buttons() & Qt::LeftButton){ |
|
84 |
setPos(mapToParent(event->pos() - |
|
|
79 | setPos(mapToParent(event->pos() - event->buttonDownPos(Qt::LeftButton))); | |
|
85 | 80 | event->setAccepted(true); |
|
86 | 81 | } else { |
|
87 | 82 | event->setAccepted(false); |
|
88 | 83 | } |
|
89 | 84 | } |
|
90 | 85 | |
|
91 | 86 | void Callout::setText(const QString &text) |
|
92 | 87 | { |
|
93 | 88 | m_text = text; |
|
94 | 89 | QFontMetrics metrics(m_font); |
|
95 | 90 | m_textRect = metrics.boundingRect(QRect(0, 0, 150, 150), Qt::AlignLeft, m_text); |
|
96 | 91 | m_textRect.translate(5, 5); |
|
97 |
prepareGeometryChange(); |
|
|
92 | prepareGeometryChange(); | |
|
98 | 93 | m_rect = m_textRect.adjusted(-5, -5, 5, 5); |
|
99 | 94 | } |
|
100 | 95 | |
|
101 | 96 | void Callout::setAnchor(QPointF point) |
|
102 | 97 | { |
|
103 | 98 | m_anchor = point; |
|
104 | 99 | } |
@@ -1,33 +1,32 | |||
|
1 | 1 | #ifndef CALLOUT_H |
|
2 | 2 | #define CALLOUT_H |
|
3 | 3 | |
|
4 | 4 | #include <QGraphicsItem> |
|
5 | 5 | #include <QFont> |
|
6 | 6 | |
|
7 | 7 | class QGraphicsSceneMouseEvent; |
|
8 | 8 | |
|
9 | 9 | class Callout : public QGraphicsItem |
|
10 | 10 | { |
|
11 | 11 | public: |
|
12 | 12 | Callout(QGraphicsItem * parent = 0); |
|
13 | 13 | |
|
14 | 14 | void setText(const QString &text); |
|
15 | 15 | void setAnchor(QPointF point); |
|
16 | 16 | |
|
17 | 17 | QRectF boundingRect() const; |
|
18 | 18 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget); |
|
19 | 19 | |
|
20 | 20 | protected: |
|
21 | 21 | void mousePressEvent(QGraphicsSceneMouseEvent *event); |
|
22 | 22 | void mouseMoveEvent(QGraphicsSceneMouseEvent *event); |
|
23 | 23 | |
|
24 | 24 | private: |
|
25 | 25 | QString m_text; |
|
26 | 26 | QRectF m_textRect; |
|
27 | 27 | QRectF m_rect; |
|
28 | 28 | QPointF m_anchor; |
|
29 | 29 | QFont m_font; |
|
30 | QPointF m_clickOffset; | |
|
31 | 30 | }; |
|
32 | 31 | |
|
33 | 32 | #endif // CALLOUT_H |
General Comments 0
You need to be logged in to leave comments.
Login now