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