From e65decc86a4ce1793a7a35e1c952e45dd73cd8d7 Mon Sep 17 00:00:00 2001 From: Jan Grewe Date: Wed, 5 Sep 2018 18:10:34 +0200 Subject: [PATCH] try to catch wheel event --- chart.cpp | 12 +++++++++++ chart.h | 2 +- chartview.cpp | 10 ++++++++- chartview.h | 1 + multichartview.cpp | 54 +++++++++++++++++++++++++++++----------------- multichartview.h | 15 ++++++++++++- multichartview.ui | 27 ++++++++++++++++++++++- 7 files changed, 97 insertions(+), 24 deletions(-) diff --git a/chart.cpp b/chart.cpp index f746bbd..ca1904e 100644 --- a/chart.cpp +++ b/chart.cpp @@ -12,6 +12,9 @@ Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags) // They can only be grabbed here in the QGraphicsWidget (QChart). grabGesture(Qt::PinchGesture); grabGesture(Qt::PanGesture); + setContentsMargins(0, 0, 0, 0); + setMargins(QMargins(0,0,0,0)); + setBackgroundRoundness(0); } Chart::~Chart() { @@ -21,6 +24,9 @@ Chart::~Chart() { bool Chart::sceneEvent(QEvent *event) { if (event->type() == QEvent::Gesture) return gestureEvent(static_cast(event)); + std::cerr << "chart: Wheel!"; + std::cerr << (event->type()) << std::endl; + return QChart::event(event); } @@ -38,6 +44,12 @@ bool Chart::gestureEvent(QGestureEvent *event) { return true; } +void Chart::wheelEvent(QGraphicsSceneWheelEvent *event) { + std::cerr << "chart: Wheel!"; + + QChart::wheelEvent(event); +} + void Chart::XRangeChanged(qreal min, qreal max) { std::cerr << "XRangeChanged" << min << "\t"<< max << std::endl; if (series().at(0)->type() == QtCharts::QAbstractSeries::SeriesTypeLine) { diff --git a/chart.h b/chart.h index 2ca407d..b831a52 100644 --- a/chart.h +++ b/chart.h @@ -16,7 +16,7 @@ public: explicit Chart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); ~Chart(); -protected: + void wheelEvent(QGraphicsSceneWheelEvent *event); bool sceneEvent(QEvent *event); private: diff --git a/chartview.cpp b/chartview.cpp index 3c5d125..7c0fe56 100644 --- a/chartview.cpp +++ b/chartview.cpp @@ -1,12 +1,13 @@ #include "chartview.h" #include #include - +#include ChartView::ChartView(QChart *chart, QWidget *parent) : QChartView(chart, parent), m_isTouching(false) { setRubberBand(QChartView::RectangleRubberBand); + setContentsMargins(0,0,0,0); } @@ -23,12 +24,19 @@ bool ChartView::viewportEvent(QEvent *event) // will only slow us down. chart()->setAnimationOptions(QChart::NoAnimation); } + return QChartView::viewportEvent(event); } +void ChartView::wheelEvent(QMouseEvent *event) { + std::cerr << "Mouse event" << std::endl; + std::cerr << event->type(); + //QChartView::whwheelEvent(event); +} void ChartView::mousePressEvent(QMouseEvent *event) { + std::cerr << "MousePress" << std::endl; if (m_isTouching) return; QChartView::mousePressEvent(event); diff --git a/chartview.h b/chartview.h index fba87b5..60c46dd 100644 --- a/chartview.h +++ b/chartview.h @@ -17,6 +17,7 @@ protected: void mouseMoveEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); void keyPressEvent(QKeyEvent *event); + void wheelEvent(QMouseEvent *event); private: bool m_isTouching; diff --git a/multichartview.cpp b/multichartview.cpp index 9d92c3c..2948c65 100644 --- a/multichartview.cpp +++ b/multichartview.cpp @@ -13,8 +13,9 @@ MultiChartView::MultiChartView(DataQueue *queue, QWidget *parent) : xRangeMin = 0.0; connect(dataQueue, SIGNAL(resultReady(QString)), this, SLOT(dataReady(QString))); //connect(&dataQueue, SIGNAL(finished()), &dataQueue, SLOT(deleteLater())); - - + labelFont = QFont("Sans", 10); + tickLabelFont = QFont("Sans", 7); + setContentsMargins(QMargins(0,0,0,0)); } MultiChartView::~MultiChartView() @@ -35,8 +36,6 @@ void MultiChartView::addArray(const nix::DataArray &array) { while (dataQueue->isRunning()){ // uncool!!! } - - this->arrayMap.insert(std::pair(array.id(), array)); ArrayInfo ai(array); this->infoMap.insert(std::pair(ai.arrayid, &ai)); @@ -61,10 +60,30 @@ void MultiChartView::addArray(const nix::DataArray &array) { Chart* MultiChartView::createChart(const ArrayInfo &info) { Chart *chart = new Chart(); - chart->setTitle(QString::fromStdString(arrayMap.at(info.arrayid).name())); + //chart->setTitle(QString::fromStdString(info.name)); + chart->setToolTip(QString::fromStdString(info.name)); chart->legend()->hide(); chart->createDefaultAxes(); + QValueAxis *axisY = new QValueAxis; + axisY->setTickCount(10); + axisY->applyNiceNumbers(); + axisY->setTitleText(QString::fromStdString(info.ylabel)); + axisY->setTitleFont(labelFont); + axisY->setLabelsFont(tickLabelFont); + chart->setAxisY(axisY); + + QValueAxis *axisX = new QValueAxis; + axisX->setRange(info.xoffset, info.xmax); + axisX->setTickCount(10); + axisX->setLabelFormat("%.4f"); + axisX->setTitleFont(labelFont); + axisX->setTitleText(QString::fromStdString(info.xlabel)); + axisX->setLabelsFont(tickLabelFont); + axisX->applyNiceNumbers(); + chart->setAxisX(axisX); + connect(axisX, SIGNAL(rangeChanged(qreal,qreal)), chart, SLOT(XRangeChanged(qreal,qreal))); + this->chartMap.insert(std::pair(info.arrayid, chart)); return chart; } @@ -73,6 +92,7 @@ ChartView* MultiChartView::createChartView(Chart *chart, const ArrayInfo &info) ChartView *view = new ChartView(chart); view->setLineWidth(1); view->setRenderHint(QPainter::Antialiasing); + /* QMargins margins = voltageChart->margins(); int marg1 = margins.left(); @@ -110,21 +130,15 @@ void MultiChartView::dataConverted(QLineSeries *series, QString entityId, double series->setUseOpenGL(true); chart->addSeries(series); - QValueAxis *axisY = new QValueAxis; - double range = ymax - ymin; - axisY->setRange(ymin - 0.2 * range, ymax + 0.2 * range); - axisY->setTickCount(10); - axisY->applyNiceNumbers(); - axisY->setTitleText("Test"); - chart->setAxisY(axisY, series); - - QValueAxis *axisX = new QValueAxis; - axisX->setRange(series->at(0).x(), series->at(series->count()-1).x()); - axisX->setTickCount(10); - axisX->setLabelFormat("%.4f"); - axisX->applyNiceNumbers(); - chart->setAxisX(axisX, series); - connect(axisX, SIGNAL(rangeChanged(qreal,qreal)), chart, SLOT(XRangeChanged(qreal,qreal))); + if (chart->axisX()->type() == QAbstractAxis::AxisTypeValue) { + QValueAxis *axisX = static_cast(chart->axisX()); + axisX->setRange(series->at(0).x(), series->at(series->count()-1).x()); + } + if (chart->axisY()->type() == QAbstractAxis::AxisTypeValue) { + QValueAxis *axisY = static_cast(chart->axisY()); + double range = ymax - ymin; + axisY->setRange(ymin - 0.2 * range, ymax + 0.2 * range); + } QPen pen = series->pen(); pen.setWidth(1); diff --git a/multichartview.h b/multichartview.h index deea1f6..daac7d3 100644 --- a/multichartview.h +++ b/multichartview.h @@ -20,26 +20,38 @@ struct ArrayInfo { xDim = guessBestXdim(array); nix::Dimension d = array.getDimension(xDim); dimType = d.dimensionType(); - + xlabel = ""; if (dimType == nix::DimensionType::Sample) { nix::SampledDimension sd = d.asSampledDimension(); xoffset = sd.offset() ? *sd.offset() : 0.0; sampleinterval = sd.samplingInterval(); xmax = dataExtent[xDim - 1] * sampleinterval + xoffset; + xlabel = sd.label() ? *sd.label() : ""; + xlabel += sd.unit() ? " [" + *sd.unit() + "]" : ""; + } else if (dimType == nix::DimensionType::Range) { nix::RangeDimension rd = d.asRangeDimension(); tics = rd.ticks(); xoffset = tics.front(); xmax = tics.back(); sampleinterval = 0.0; + xlabel = rd.label() ? *rd.label() : ""; + xlabel += rd.unit() ? " [" + *rd.unit() + "]" : ""; } else { // SetDimension xoffset = 0.0; xmax = dataExtent[xDim - 1]; sampleinterval = 1.0; } + name = array.name(); + ylabel = array.label() ? *array.label() : ""; + ylabel += array.unit() ? " [" + *array.unit() + "]" : ""; + } std::string arrayid; + std::string name; + std::string ylabel; + std::string xlabel; int xDim; nix::DimensionType dimType; double xoffset, xmax, sampleinterval; @@ -101,6 +113,7 @@ private: std::map chartMap; std::map viewMap; std::map infoMap; + QFont labelFont, tickLabelFont; Chart* createChart(const ArrayInfo &info); ChartView* createChartView(Chart *chart, const ArrayInfo &info); diff --git a/multichartview.ui b/multichartview.ui index 0121cc8..50087f8 100644 --- a/multichartview.ui +++ b/multichartview.ui @@ -13,9 +13,34 @@ Form + + false + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + - + + + 0 + +