68 lines
2.1 KiB
C++
68 lines
2.1 KiB
C++
#include "chart.h"
|
|
#include <QtWidgets/QGesture>
|
|
#include <QtWidgets/QGraphicsScene>
|
|
#include <QtWidgets/QGraphicsView>
|
|
#include <QLineSeries>
|
|
#include <QValueAxis>
|
|
#include <iostream>
|
|
#include <QGraphicsSceneEvent>
|
|
|
|
Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
|
|
: QChart(QChart::ChartTypeCartesian, parent, wFlags)
|
|
{
|
|
setContentsMargins(0, 0, 0, 0);
|
|
setMargins(QMargins(0,0,0,0));
|
|
setBackgroundRoundness(0);
|
|
}
|
|
|
|
Chart::~Chart() {
|
|
|
|
}
|
|
|
|
bool Chart::sceneEvent(QEvent *event) {
|
|
if (event->type() == QEvent::Gesture)
|
|
return gestureEvent(static_cast<QGestureEvent *>(event));
|
|
if (event->type() == QEvent::GraphicsSceneWheel) {
|
|
QGraphicsSceneWheelEvent *swe = static_cast<QGraphicsSceneWheelEvent*>(event);
|
|
return myWheelEvent(swe);
|
|
}
|
|
return QChart::event(event);
|
|
}
|
|
|
|
bool Chart::gestureEvent(QGestureEvent *event) {
|
|
if (QGesture *gesture = event->gesture(Qt::PanGesture)) {
|
|
QPanGesture *pan = static_cast<QPanGesture *>(gesture);
|
|
QChart::scroll(-(pan->delta().x()), pan->delta().y());
|
|
}
|
|
if (QGesture *gesture = event->gesture(Qt::PinchGesture)) {
|
|
QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture);
|
|
if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged)
|
|
QChart::zoom(pinch->scaleFactor());
|
|
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool Chart::myWheelEvent(QGraphicsSceneWheelEvent *event) {
|
|
bool in = event->delta() > 0;
|
|
bool scale_x = event->scenePos().x() > this->geometry().right()/4;
|
|
QRectF view = plotArea();
|
|
qreal zoom = 0.1;
|
|
qreal dwidth = view.width() * (scale_x ? (in ? -1.0 : 1.0) * zoom/2 : 0.0);
|
|
qreal dheight = view.height() * (scale_x ? 0.0 : (in ? -1.0 : 1.0) * zoom/2);
|
|
view.adjust(dwidth/2, dheight/2, -1.0 * dwidth/2, -1.0 * dheight/2);
|
|
this->zoomIn(view);
|
|
return true;
|
|
}
|
|
|
|
void Chart::XRangeChanged(qreal min, qreal max) {
|
|
emit newDataRequest(this->entityId, min, max);
|
|
/*
|
|
if (series().at(0)->type() == QtCharts::QAbstractSeries::SeriesTypeLine) {
|
|
QLineSeries *s = static_cast<QLineSeries *>(series().at(0));
|
|
if (max > s->at(s->count()-1).x()) {
|
|
}
|
|
}
|
|
*/
|
|
}
|