| 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 Qt Charts module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL$ |
| 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 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 or (at your option) any later version |
| 20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
| 21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
| 22 | ** included in the packaging of this file. Please review the following |
| 23 | ** information to ensure the GNU General Public License requirements will |
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 25 | ** |
| 26 | ** $QT_END_LICENSE$ |
| 27 | ** |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | #include "view.h" |
| 31 | #include <QtGui/QResizeEvent> |
| 32 | #include <QtWidgets/QGraphicsScene> |
| 33 | #include <QtCharts/QChart> |
| 34 | #include <QtCharts/QLineSeries> |
| 35 | #include <QtCharts/QSplineSeries> |
| 36 | #include <QtWidgets/QGraphicsTextItem> |
| 37 | #include "callout.h" |
| 38 | #include <QtGui/QMouseEvent> |
| 39 | |
| 40 | View::View(QWidget *parent) |
| 41 | : QGraphicsView(new QGraphicsScene, parent), |
| 42 | m_coordX(0), |
| 43 | m_coordY(0), |
| 44 | m_chart(0), |
| 45 | m_tooltip(0) |
| 46 | { |
| 47 | setDragMode(QGraphicsView::NoDrag); |
| 48 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 49 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 50 | |
| 51 | // chart |
| 52 | m_chart = new QChart; |
| 53 | m_chart->setMinimumSize(aw: 640, ah: 480); |
| 54 | m_chart->setTitle("Hover the line to show callout. Click the line to make it stay" ); |
| 55 | m_chart->legend()->hide(); |
| 56 | QLineSeries *series = new QLineSeries; |
| 57 | series->append(x: 1, y: 3); |
| 58 | series->append(x: 4, y: 5); |
| 59 | series->append(x: 5, y: 4.5); |
| 60 | series->append(x: 7, y: 1); |
| 61 | series->append(x: 11, y: 2); |
| 62 | m_chart->addSeries(series); |
| 63 | |
| 64 | QSplineSeries *series2 = new QSplineSeries; |
| 65 | series2->append(x: 1.6, y: 1.4); |
| 66 | series2->append(x: 2.4, y: 3.5); |
| 67 | series2->append(x: 3.7, y: 2.5); |
| 68 | series2->append(x: 7, y: 4); |
| 69 | series2->append(x: 10, y: 2); |
| 70 | m_chart->addSeries(series: series2); |
| 71 | |
| 72 | m_chart->createDefaultAxes(); |
| 73 | m_chart->setAcceptHoverEvents(true); |
| 74 | |
| 75 | setRenderHint(hint: QPainter::Antialiasing); |
| 76 | scene()->addItem(item: m_chart); |
| 77 | |
| 78 | m_coordX = new QGraphicsSimpleTextItem(m_chart); |
| 79 | m_coordX->setPos(ax: m_chart->size().width()/2 - 50, ay: m_chart->size().height()); |
| 80 | m_coordX->setText("X: " ); |
| 81 | m_coordY = new QGraphicsSimpleTextItem(m_chart); |
| 82 | m_coordY->setPos(ax: m_chart->size().width()/2 + 50, ay: m_chart->size().height()); |
| 83 | m_coordY->setText("Y: " ); |
| 84 | |
| 85 | connect(sender: series, signal: &QLineSeries::clicked, receiver: this, slot: &View::keepCallout); |
| 86 | connect(sender: series, signal: &QLineSeries::hovered, receiver: this, slot: &View::tooltip); |
| 87 | |
| 88 | connect(sender: series2, signal: &QSplineSeries::clicked, receiver: this, slot: &View::keepCallout); |
| 89 | connect(sender: series2, signal: &QSplineSeries::hovered, receiver: this, slot: &View::tooltip); |
| 90 | |
| 91 | this->setMouseTracking(true); |
| 92 | } |
| 93 | |
| 94 | void View::resizeEvent(QResizeEvent *event) |
| 95 | { |
| 96 | if (scene()) { |
| 97 | scene()->setSceneRect(QRect(QPoint(0, 0), event->size())); |
| 98 | m_chart->resize(size: event->size()); |
| 99 | m_coordX->setPos(ax: m_chart->size().width()/2 - 50, ay: m_chart->size().height() - 20); |
| 100 | m_coordY->setPos(ax: m_chart->size().width()/2 + 50, ay: m_chart->size().height() - 20); |
| 101 | const auto callouts = m_callouts; |
| 102 | for (Callout *callout : callouts) |
| 103 | callout->updateGeometry(); |
| 104 | } |
| 105 | QGraphicsView::resizeEvent(event); |
| 106 | } |
| 107 | |
| 108 | void View::mouseMoveEvent(QMouseEvent *event) |
| 109 | { |
| 110 | m_coordX->setText(QString("X: %1" ).arg(a: m_chart->mapToValue(position: event->pos()).x())); |
| 111 | m_coordY->setText(QString("Y: %1" ).arg(a: m_chart->mapToValue(position: event->pos()).y())); |
| 112 | QGraphicsView::mouseMoveEvent(event); |
| 113 | } |
| 114 | |
| 115 | void View::keepCallout() |
| 116 | { |
| 117 | m_callouts.append(t: m_tooltip); |
| 118 | m_tooltip = new Callout(m_chart); |
| 119 | } |
| 120 | |
| 121 | void View::tooltip(QPointF point, bool state) |
| 122 | { |
| 123 | if (m_tooltip == 0) |
| 124 | m_tooltip = new Callout(m_chart); |
| 125 | |
| 126 | if (state) { |
| 127 | m_tooltip->setText(QString("X: %1 \nY: %2 " ).arg(a: point.x()).arg(a: point.y())); |
| 128 | m_tooltip->setAnchor(point); |
| 129 | m_tooltip->setZValue(11); |
| 130 | m_tooltip->updateGeometry(); |
| 131 | m_tooltip->show(); |
| 132 | } else { |
| 133 | m_tooltip->hide(); |
| 134 | } |
| 135 | } |
| 136 | |