##// END OF EJS Templates
Callout example: added extra space around the text
Marek Rosa -
r2378:c144d02c5537
parent child
Show More
@@ -13,10 +13,10 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_textRect.left(), anchor.x()));
16 rect.setLeft(qMin(m_rect.left(), anchor.x()));
17 rect.setRight(qMax(m_textRect.right() + 8, anchor.x()));
17 rect.setRight(qMax(m_rect.right(), anchor.x()));
18 rect.setTop(qMin(m_textRect.top(), anchor.y()));
18 rect.setTop(qMin(m_rect.top(), anchor.y()));
19 rect.setBottom(qMax(m_textRect.bottom() + 8, anchor.y()));
19 rect.setBottom(qMax(m_rect.bottom(), anchor.y()));
20 return rect;
20 return rect;
21 }
21 }
22
22
@@ -25,26 +25,26 void Callout::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q
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_textRect, 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_textRect.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_textRect.top();
35 bool above = anchor.y() <= m_rect.top();
36 bool aboveCenter = anchor.y() > m_textRect.top() && anchor.y() <= m_textRect.center().y();
36 bool aboveCenter = anchor.y() > m_rect.top() && anchor.y() <= m_rect.center().y();
37 bool belowCenter = anchor.y() > m_textRect.center().y() && anchor.y() <= m_textRect.bottom();
37 bool belowCenter = anchor.y() > m_rect.center().y() && anchor.y() <= m_rect.bottom();
38 bool below = anchor.y() > m_textRect.bottom();
38 bool below = anchor.y() > m_rect.bottom();
39
39
40 bool onLeft = anchor.x() <= m_textRect.left();
40 bool onLeft = anchor.x() <= m_rect.left();
41 bool leftOfCenter = anchor.x() > m_textRect.left() && anchor.x() <= m_textRect.center().x();
41 bool leftOfCenter = anchor.x() > m_rect.left() && anchor.x() <= m_rect.center().x();
42 bool rightOfCenter = anchor.x() > m_textRect.center().x() && anchor.x() <= m_textRect.right();
42 bool rightOfCenter = anchor.x() > m_rect.center().x() && anchor.x() <= m_rect.right();
43 bool onRight = anchor.x() > m_textRect.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_textRect.width();
46 qreal x = (onRight + rightOfCenter) * m_rect.width();
47 qreal y = (below + belowCenter) * m_textRect.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
@@ -65,12 +65,12 void Callout::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q
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.adjusted(4, 4, 0, 0), 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_textRect.contains(event->pos())) {
73 if (m_rect.contains(event->pos())) {
74 m_clickOffset = event->pos();
74 m_clickOffset = event->pos();
75 event->setAccepted(true);
75 event->setAccepted(true);
76 } else {
76 } else {
@@ -80,7 +80,7 void Callout::mousePressEvent(QGraphicsSceneMouseEvent *event)
80
80
81 void Callout::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
81 void Callout::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
82 {
82 {
83 if (m_textRect.contains(event->pos())){
83 if (event->buttons() & Qt::LeftButton){
84 setPos(mapToParent(event->pos() - m_clickOffset));
84 setPos(mapToParent(event->pos() - m_clickOffset));
85 event->setAccepted(true);
85 event->setAccepted(true);
86 } else {
86 } else {
@@ -92,8 +92,10 void Callout::setText(const QString &text)
92 {
92 {
93 m_text = text;
93 m_text = text;
94 QFontMetrics metrics(m_font);
94 QFontMetrics metrics(m_font);
95 prepareGeometryChange();
95 m_textRect = metrics.boundingRect(QRect(0, 0, 150, 150), Qt::AlignLeft, m_text);
96 m_textRect = metrics.boundingRect(QRect(0, 0, 150, 150), Qt::AlignLeft, m_text).adjusted(0, 0, 4, 4);
96 m_textRect.translate(5, 5);
97 prepareGeometryChange();
98 m_rect = m_textRect.adjusted(-5, -5, 5, 5);
97 }
99 }
98
100
99 void Callout::setAnchor(QPointF point)
101 void Callout::setAnchor(QPointF point)
@@ -24,6 +24,7 protected:
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 QPointF m_anchor;
28 QPointF m_anchor;
28 QFont m_font;
29 QFont m_font;
29 QPointF m_clickOffset;
30 QPointF m_clickOffset;
@@ -89,8 +89,6 void View::resizeEvent(QResizeEvent *event)
89 m_chart->resize(event->size());
89 m_chart->resize(event->size());
90 m_coordX->setPos(m_chart->size().width()/2 - 50, m_chart->size().height() - 20);
90 m_coordX->setPos(m_chart->size().width()/2 - 50, m_chart->size().height() - 20);
91 m_coordY->setPos(m_chart->size().width()/2 + 50, m_chart->size().height() - 20);
91 m_coordY->setPos(m_chart->size().width()/2 + 50, m_chart->size().height() - 20);
92 // for (int i = 0; i < children().count(); i++)
93 // if ()
94 }
92 }
95 QGraphicsView::resizeEvent(event);
93 QGraphicsView::resizeEvent(event);
96 }
94 }
General Comments 0
You need to be logged in to leave comments. Login now