##// END OF EJS Templates
Updated callout example
Marek Rosa -
r2346:1937d28dc870
parent child
Show More
@@ -0,0 +1,59
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef VIEW_H
22 #define VIEW_H
23 #include <QGraphicsView>
24 #include <QChartGlobal>
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 class QChart;
28 QTCOMMERCIALCHART_END_NAMESPACE
29
30 class QGraphicsScene;
31 class QResizeEvent;
32 class Callout;
33 class QMouseEvent;
34
35 QTCOMMERCIALCHART_USE_NAMESPACE
36
37 class View: public QGraphicsView
38 {
39 Q_OBJECT
40
41 public:
42 View(QWidget *parent = 0);
43
44 protected:
45 void resizeEvent(QResizeEvent *event);
46 void mouseMoveEvent(QMouseEvent *event);
47
48 public slots:
49 void keepCallout();
50 void tooltip(QPointF point, bool state);
51
52 private:
53 QGraphicsSimpleTextItem *m_coordX;
54 QGraphicsSimpleTextItem *m_coordY;
55 QChart *m_chart;
56 Callout *m_tooltip;
57 };
58
59 #endif
@@ -1,102 +1,102
1 1 #include "callout.h"
2 2 #include <QPainter>
3 3 #include <QFontMetrics>
4 4 #include <QGraphicsSceneMouseEvent>
5 #include <QCursor>
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_textRect.left(), anchor.x()));
17 17 rect.setRight(qMax(m_textRect.right() + 8, anchor.x()));
18 18 rect.setTop(qMin(m_textRect.top(), anchor.y()));
19 19 rect.setBottom(qMax(m_textRect.bottom() + 8, 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_textRect, 5, 5);
29 29
30 30 QPointF anchor = mapFromParent(m_anchor);
31 31 if (!m_textRect.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_textRect.top();
36 36 bool aboveCenter = anchor.y() > m_textRect.top() && anchor.y() <= m_textRect.center().y();
37 37 bool belowCenter = anchor.y() > m_textRect.center().y() && anchor.y() <= m_textRect.bottom();
38 38 bool below = anchor.y() > m_textRect.bottom();
39 39
40 40 bool onLeft = anchor.x() <= m_textRect.left();
41 41 bool leftOfCenter = anchor.x() > m_textRect.left() && anchor.x() <= m_textRect.center().x();
42 42 bool rightOfCenter = anchor.x() > m_textRect.center().x() && anchor.x() <= m_textRect.right();
43 43 bool onRight = anchor.x() > m_textRect.right();
44 44
45 45 // get the nearest m_textRect corner.
46 46 qreal x = (onRight + rightOfCenter) * m_textRect.width();
47 47 qreal y = (below + belowCenter) * m_textRect.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.adjusted(4, 4, 0, 0), m_text);
69 69 }
70 70
71 71 void Callout::mousePressEvent(QGraphicsSceneMouseEvent *event)
72 72 {
73 73 if (m_textRect.contains(event->pos())) {
74 74 m_clickOffset = event->pos();
75 75 event->setAccepted(true);
76 76 } else {
77 77 event->setAccepted(false);
78 78 }
79 79 }
80 80
81 81 void Callout::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
82 82 {
83 83 if (m_textRect.contains(event->pos())){
84 84 setPos(mapToParent(event->pos() - m_clickOffset));
85 85 event->setAccepted(true);
86 86 } else {
87 87 event->setAccepted(false);
88 88 }
89 89 }
90 90
91 91 void Callout::setText(const QString &text)
92 92 {
93 93 m_text = text;
94 94 QFontMetrics metrics(m_font);
95 95 prepareGeometryChange();
96 96 m_textRect = metrics.boundingRect(QRect(0, 0, 150, 150), Qt::AlignLeft, m_text).adjusted(0, 0, 4, 4);
97 97 }
98 98
99 99 void Callout::setAnchor(QPointF point)
100 100 {
101 101 m_anchor = point;
102 102 }
@@ -1,13 +1,15
1 1 !include( ../examples.pri ) {
2 2 error( "Couldn't find the examples.pri file!" )
3 3 }
4 4
5 5 TARGET = callout
6 6 TEMPLATE = app
7 7
8 SOURCES += main.cpp\
9 widget.cpp \
10 callout.cpp
8 SOURCES += \
9 main.cpp\
10 callout.cpp \
11 view.cpp
11 12
12 HEADERS += widget.h \
13 callout.h
13 HEADERS += \
14 callout.h \
15 view.h
@@ -1,11 +1,11
1 1 #include <QApplication>
2 #include "widget.h"
2 #include "view.h"
3 3
4 4 int main(int argc, char *argv[])
5 5 {
6 6 QApplication a(argc, argv);
7 Widget w;
7 View w;
8 8 w.show();
9 9
10 10 return a.exec();
11 11 }
@@ -1,85 +1,119
1 #include "widget.h"
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "view.h"
22 #include <QResizeEvent>
2 23 #include <QGraphicsScene>
3 #include <QGraphicsView>
4 #include <QVBoxLayout>
5 24 #include <QChart>
6 25 #include <QLineSeries>
7 26 #include <QSplineSeries>
8 27 #include <QGraphicsTextItem>
9 #include <QGraphicsLineItem>
10 28 #include "callout.h"
29 #include <QMouseEvent>
11 30
12 QTCOMMERCIALCHART_USE_NAMESPACE
13
14 Widget::Widget(QWidget *parent)
15 : QWidget(parent),
16 m_scene(0),
31 View::View(QWidget *parent)
32 : QGraphicsView(new QGraphicsScene, parent),
33 m_coordX(0),
34 m_coordY(0),
17 35 m_chart(0),
18 m_view(0),
19 36 m_tooltip(0)
20 37 {
38 setDragMode(QGraphicsView::NoDrag);
39 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
40 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
41
21 42 // chart
22 43 m_chart = new QChart;
23 m_chart->setMinimumSize(640, 480);
44 m_chart->setMinimumSize(800, 600);
24 45 m_chart->setTitle("Hover the line to show callout. Click the line to make it stay");
25 46 m_chart->legend()->hide();
26 47 QLineSeries *series = new QLineSeries;
27 48 series->append(1, 3);
28 49 series->append(4, 5);
29 50 series->append(5, 4.5);
30 51 series->append(7, 1);
31 52 series->append(11, 2);
32 53 m_chart->addSeries(series);
33 54
34 55 QSplineSeries *series2 = new QSplineSeries;
35 56 series2->append(1.6, 1.4);
36 57 series2->append(2.4, 3.5);
37 58 series2->append(3.7, 2.5);
38 59 series2->append(7, 4);
39 60 series2->append(10, 2);
40 61 m_chart->addSeries(series2);
41 62
42 63 m_chart->createDefaultAxes();
64 m_chart->setAcceptHoverEvents(true);
43 65
44 m_scene = new QGraphicsScene;
45 m_view = new QGraphicsView(m_scene);
46 m_view->setRenderHint(QPainter::Antialiasing);
47 m_scene->addItem(m_chart);
66 setRenderHint(QPainter::Antialiasing);
67 scene()->addItem(m_chart);
48 68
49 QVBoxLayout *mainLayout = new QVBoxLayout;
50 mainLayout->addWidget(m_view);
51 setLayout(mainLayout);
69 m_coordX = new QGraphicsSimpleTextItem(m_chart);
70 m_coordX->setPos(m_chart->size().width()/2 - 50, m_chart->size().height());
71 m_coordX->setText("X: ");
72 m_coordY = new QGraphicsSimpleTextItem(m_chart);
73 m_coordY->setPos(m_chart->size().width()/2 + 50, m_chart->size().height());
74 m_coordY->setText("Y: ");
52 75
53 76 connect(series, SIGNAL(clicked(QPointF)), this, SLOT(keepCallout()));
54 77 connect(series, SIGNAL(hovered(QPointF, bool)), this, SLOT(tooltip(QPointF,bool)));
55 78
56 79 connect(series2, SIGNAL(clicked(QPointF)), this, SLOT(keepCallout()));
57 80 connect(series2, SIGNAL(hovered(QPointF, bool)), this, SLOT(tooltip(QPointF,bool)));
81
82 this->setMouseTracking(true);
83 }
84
85 void View::resizeEvent(QResizeEvent *event)
86 {
87 if (scene())
88 scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
89 QGraphicsView::resizeEvent(event);
58 90 }
59 91
60 Widget::~Widget()
92 void View::mouseMoveEvent(QMouseEvent *event)
61 93 {
62
94 m_coordX->setText(QString("X: %1").arg(m_chart->mapToValue(event->pos()).x()));
95 m_coordY->setText(QString("Y: %1").arg(m_chart->mapToValue(event->pos()).y()));
96 QGraphicsView::mouseMoveEvent(event);
63 97 }
64 98
65 void Widget::keepCallout()
99 void View::keepCallout()
66 100 {
67 101 m_tooltip = new Callout(m_chart);
68 102 }
69 103
70 void Widget::tooltip(QPointF point, bool state)
104 void View::tooltip(QPointF point, bool state)
71 105 {
72 106 if (m_tooltip == 0)
73 107 m_tooltip = new Callout(m_chart);
74 108
75 109 if (state) {
76 110 m_tooltip->setText(QString("X: %1 \nY: %2 ").arg(point.x()).arg(point.y()));
77 111 QXYSeries *series = qobject_cast<QXYSeries *>(sender());
78 112 m_tooltip->setAnchor(m_chart->mapToPosition(point, series));
79 113 m_tooltip->setPos(m_chart->mapToPosition(point, series) + QPoint(10, -50));
80 114 m_tooltip->setZValue(11);
81 115 m_tooltip->show();
82 116 } else {
83 117 m_tooltip->hide();
84 118 }
85 119 }
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now