From 639f0fb55bdd92f05fa0fc9c853e93c8d7cc972d Mon Sep 17 00:00:00 2001
From: Jan Grewe <jan.grewe@g-node.org>
Date: Tue, 2 Oct 2018 09:36:38 +0200
Subject: [PATCH] changes

---
 chart.cpp          |  6 ++++--
 databuffer.cpp     | 12 ++----------
 databuffer.h       |  2 --
 dataqueue.cpp      | 19 +++++++++++++++++++
 dataqueue.h        |  2 ++
 mainwindow.cpp     |  7 ++++---
 multichartview.cpp |  8 ++++++--
 7 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/chart.cpp b/chart.cpp
index 89c093d..91272b6 100644
--- a/chart.cpp
+++ b/chart.cpp
@@ -56,10 +56,12 @@ bool Chart::myWheelEvent(QGraphicsSceneWheelEvent *event) {
 }
 
 void Chart::XRangeChanged(qreal min, qreal max) {
-  if (series().at(0)->type() == QtCharts::QAbstractSeries::SeriesTypeLine) {
+    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()) {
-      emit newDataRequest(this->entityId, min, max);
     }
   }
+  */
 }
diff --git a/databuffer.cpp b/databuffer.cpp
index 1163ca2..3342e8e 100644
--- a/databuffer.cpp
+++ b/databuffer.cpp
@@ -14,17 +14,9 @@ QVector<QPointF> * DataBuffer::getBuffer() {
 }
 
 double DataBuffer::rangeMinimum() const {
-    return rangeMin;
-}
-
-void DataBuffer::rangeMinimum(double min) {
-    rangeMin = min;
+    return !buffer->isEmpty() ? buffer->constFirst().x() : 0.0;
 }
 
 double DataBuffer::rangeMaximum() const {
-    return rangeMax;
-}
-
-void DataBuffer::rangeMaximum(double max) {
-    rangeMax = max;
+    return !buffer->isEmpty() ? buffer->constLast().x() : 0.0;
 }
diff --git a/databuffer.h b/databuffer.h
index 897e732..d85be34 100644
--- a/databuffer.h
+++ b/databuffer.h
@@ -18,10 +18,8 @@ private:
 
 public:
     double rangeMaximum() const;
-    void rangeMaximum(double max);
 
     double rangeMinimum() const;
-    void rangeMinimum(double min);
 
     QVector<QPointF> * getBuffer();
 
diff --git a/dataqueue.cpp b/dataqueue.cpp
index 7441a09..5813dee 100644
--- a/dataqueue.cpp
+++ b/dataqueue.cpp
@@ -41,6 +41,25 @@ void DataQueue::requestData(const nix::DataArray &array, const nix::NDSize &coun
     start();
 }
 
+void DataQueue::requestData(const nix::DataArray &array, DataBuffer *buffer, qreal start, qreal end, std::string &requestId) {
+    if (end > buffer->rangeMaximum()) { // need to load new data
+        std::cerr << buffer->rangeMaximum() << " need to load new data! "<< std::endl;
+
+        /*DataRequest dr(requestId, array, count, offset);
+        mutex.lock();
+        std::deque<DataRequest>::iterator it;
+        for (it = requestQueue.begin(); it < requestQueue.end(); it++) {
+            std::cerr << it->array.id() << std::endl;
+            if (it->array.id() == array.id())
+                std::cerr << "updating request" <<std::endl;
+        }
+        requestQueue.push_back(dr);
+        mutex.unlock();
+        start();
+                */
+    }
+}
+
 void DataQueue::stop() {
     mutex.lock();
     stopped = true;
diff --git a/dataqueue.h b/dataqueue.h
index d33123a..df80b2b 100644
--- a/dataqueue.h
+++ b/dataqueue.h
@@ -11,6 +11,7 @@
 #include <deque>
 
 #include "dataloader.h"
+#include "databuffer.h"
 #include "dataresult1d.h"
 #include "datarequest.h"
 
@@ -30,6 +31,7 @@ signals:
 
 public slots:
     void requestData(const nix::DataArray &array, const nix::NDSize &count, const nix::NDSize &offset, std::string &requestId);
+    void requestData(const nix::DataArray &array, DataBuffer *buffer, qreal start, qreal end, std::string &requestId);
 
 
 private:
diff --git a/mainwindow.cpp b/mainwindow.cpp
index 6afba0a..ccc40e5 100644
--- a/mainwindow.cpp
+++ b/mainwindow.cpp
@@ -26,13 +26,14 @@ MainWindow::MainWindow(QWidget *parent) :
 
     dataQueue.start();
     conversionQueue.start();
-
+    /*
     file = nix::File::open("/home/grewe/zwischenlager/2017-08-17-af-invivo-1/2017-08-17-af-invivo-1.nix",
                            nix::FileMode::ReadOnly);
-    /*
+                           */
+
     file = nix::File::open("/Users/jan/zwischenlager/threading_test/dataservice/data/2017-08-17-af-invivo-1.nix",
                            nix::FileMode::ReadOnly);
-    */
+
     nix::Block b = file.getBlock(0);
     this->voltage = b.getDataArray("V-1");
     this->eod = b.getDataArray("EOD");
diff --git a/multichartview.cpp b/multichartview.cpp
index 935e7fc..b5db923 100644
--- a/multichartview.cpp
+++ b/multichartview.cpp
@@ -117,13 +117,17 @@ void MultiChartView::loadData(const ArrayInfo &info) {
 
 void MultiChartView::newDataRequest(const std::string &entity_id, qreal min, qreal max) {
     std::cerr << "request new data from: "<< entity_id << " with max: "<< max << std::endl;
-
+/*
     nix::NDSize offset(1, data_buffer[entity_id]->getBuffer()->size());
     nix::NDSize count(1, 100000);
     std::string request_id;
     std::cerr << offset << count;
+*/
+  //  dataQueue->requestData(arrayMap[entity_id], count, offset, request_id);
+    std::string request_id;
+
+    dataQueue->requestData(arrayMap[entity_id], data_buffer[entity_id], min, max, request_id);
 
-    dataQueue->requestData(arrayMap[entity_id], count, offset, request_id);
 }