qsswraper/widgets/qchartwrap.cpp

108 lines
3.7 KiB
C++
Raw Normal View History

2021-08-04 09:15:42 +00:00
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qchartwrap.h"
#include <QtGui/QResizeEvent>
#include <QtWidgets/QGraphicsScene>
#include <QtCharts/QChart>
#include <QtCharts/QLineSeries>
#include <QtCharts/QSplineSeries>
#include <QtWidgets/QGraphicsTextItem>
#include "chart_callout.h"
#include <QtGui/QMouseEvent>
QChartWrap::QChartWrap(QWidget *parent)
: QChartView(parent),
m_coordX(0),
m_coordY(0),
m_tooltip(0)
{
setDragMode(QGraphicsView::NoDrag);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// chart
this->chart()->setAcceptHoverEvents(true);
setRenderHint(QPainter::Antialiasing);
scene()->addItem(this->chart());
m_coordX = new QGraphicsSimpleTextItem(this->chart());
m_coordX->setPos(0, this->chart()->size().height());
m_coordX->setText("X: ");
m_coordY = new QGraphicsSimpleTextItem(this->chart());
m_coordY->setPos(100, this->chart()->size().height());
m_coordY->setText("Y: ");
this->setMouseTracking(true);
this->chart()->setMargins(QMargins(0,0,0,0));
}
void QChartWrap::resizeEvent(QResizeEvent *event)
{
if (scene()) {
scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
this->chart()->resize(event->size());
m_coordX->setPos(0, this->chart()->size().height() - 20);
m_coordY->setPos(100, this->chart()->size().height() - 20);
const auto callouts = m_callouts;
for (ChartCallout *callout : callouts)
callout->updateGeometry();
}
QChartView::resizeEvent(event);
}
void QChartWrap::mouseMoveEvent(QMouseEvent *event)
{
m_coordX->setText(QString("X: %1").arg(this->chart()->mapToValue(event->pos()).x()));
m_coordY->setText(QString("Y: %1").arg(this->chart()->mapToValue(event->pos()).y()));
QChartView::mouseMoveEvent(event);
}
void QChartWrap::keepCallout()
{
m_callouts.append(m_tooltip);
m_tooltip = new ChartCallout(this->chart());
}
void QChartWrap::tooltip(QPointF point, bool state)
{
if (m_tooltip == 0)
m_tooltip = new ChartCallout(this->chart());
if (state) {
m_tooltip->setText(QString("X: %1 \nY: %2 ").arg(point.x()).arg(point.y()));
m_tooltip->setAnchor(point);
m_tooltip->setZValue(11);
m_tooltip->updateGeometry();
m_tooltip->show();
} else {
m_tooltip->hide();
}
}