From 288c9c027d3d40fb5ed1d8b43db408c3d1ccc9ef Mon Sep 17 00:00:00 2001
From: Benjamin Jakimow <benjamin.jakimow@geo.hu-berlin.de>
Date: Tue, 21 Jul 2020 15:17:42 +0200
Subject: [PATCH] modified MapView writeXml / readXml to saves QgsTextFormat

Signed-off-by: Benjamin Jakimow <benjamin.jakimow@geo.hu-berlin.de>
---
 eotimeseriesviewer/main.py             |   4 +
 eotimeseriesviewer/mapvisualization.py | 126 +++++++++++++++++--------
 eotimeseriesviewer/timeseries.py       |   2 +-
 tests/test_main.py                     |  17 +---
 tests/test_timeseries.py               |   9 ++
 5 files changed, 102 insertions(+), 56 deletions(-)

diff --git a/eotimeseriesviewer/main.py b/eotimeseriesviewer/main.py
index e9b4c7f3..10e269a4 100644
--- a/eotimeseriesviewer/main.py
+++ b/eotimeseriesviewer/main.py
@@ -709,6 +709,10 @@ class EOTimeSeriesViewer(QgisInterface, QObject):
         root = doc.documentElement()
         node = root.firstChildElement('EOTSV')
         if node.nodeName() == 'EOTSV':
+            self.timeSeries().clear()
+            mapviews = self.mapViews()
+            for mv in mapviews:
+                self.mapWidget().removeMapView(mv)
             self.timeSeries().readXml(node)
             self.mapWidget().readXml(node)
 
diff --git a/eotimeseriesviewer/mapvisualization.py b/eotimeseriesviewer/mapvisualization.py
index 913cd5d4..ac9765b0 100644
--- a/eotimeseriesviewer/mapvisualization.py
+++ b/eotimeseriesviewer/mapvisualization.py
@@ -163,6 +163,10 @@ class MapView(QFrame):
         self.mIsVisible = True
         self.setTitle(name)
 
+        self.optionShowDate: QAction
+        self.optionShowSensorName: QAction
+        self.optionShowMapViewName: QAction
+
         m = QMenu()
         m.addAction(self.optionShowDate)
         m.addAction(self.optionShowSensorName)
@@ -174,6 +178,80 @@ class MapView(QFrame):
 
         fixMenuButtons(self)
 
+    @staticmethod
+    def readXml(node: QDomNode):
+        if node.nodeName() == 'MapView':
+            nodeMapView = node
+        else:
+            nodeMapView = node.firstChildElement('MapView')
+
+        if nodeMapView.nodeName() != 'MapView':
+            return None
+
+        context = QgsReadWriteContext()
+        mapView = MapView()
+
+        def to_bool(value) -> bool:
+            return str(value).lower() in ['1', 'true']
+
+        mapView.setName(nodeMapView.attribute('name'))
+        mapView.setMapBackgroundColor(QColor(nodeMapView.attribute('bg')))
+        mapView.setVisibility(to_bool(nodeMapView.attribute('visible')))
+        mapView.optionShowDate.setChecked(to_bool(nodeMapView.attribute('showDate')))
+        mapView.optionShowSensorName.setChecked(to_bool(nodeMapView.attribute('showSensorName')))
+        mapView.optionShowMapViewName.setChecked(to_bool(nodeMapView.attribute('showMapViewName')))
+
+        #nodeMapView.setAttribute('showDate', str(self.optionShowDate.checked()))
+        #nodeMapView.setAttribute('showSensorName', str(self.optionShowSensorName.checked()))
+        #nodeMapView.setAttribute('showMapViewName', str(self.optionShowMapViewName.checked()))
+
+        textFormat = mapView.mapTextFormat()
+        textFormat.readXml(nodeMapView, context)
+        lyrNode = node.firstChildElement('MapViewProxyLayer').toElement()
+        while lyrNode.nodeName() == 'MapViewProxyLayer':
+            sid = lyrNode.attribute('sensor_id')
+            styleNode = lyrNode.firstChildElement('LayerStyle')
+            style = QgsMapLayerStyle()
+            style.readXml(styleNode)
+            sensor = SensorInstrument(sid)
+            mapView.addSensor(sensor)
+            lyr = mapView.sensorProxyLayer(sensor)
+            lyr.setMapLayerStyle(style)
+
+            lyrNode = lyrNode.nextSibling().toElement()
+        return mapView
+
+    def writeXml(self, node: QDomNode, doc: QDomDocument):
+
+        nodeMapView = doc.createElement('MapView')
+        nodeMapView.setAttribute('name', self.name())
+        nodeMapView.setAttribute('bg', self.mapBackgroundColor().name())
+        nodeMapView.setAttribute('visible', str(self.isVisible()))
+        nodeMapView.setAttribute('showDate', str(self.optionShowDate.isChecked()))
+        nodeMapView.setAttribute('showSensorName', str(self.optionShowSensorName.isChecked()))
+        nodeMapView.setAttribute('showMapViewName', str(self.optionShowMapViewName.isChecked()))
+
+        """
+        m.addAction(self.optionShowDate)
+        m.addAction(self.optionShowSensorName)
+        m.addAction(self.optionShowMapViewName)
+        """
+        context = QgsReadWriteContext()
+        nodeTextStyle = self.mapTextFormat().writeXml(doc, context)
+        nodeMapView.appendChild(nodeTextStyle)
+
+        for sensor in self.sensors():
+            lyr = self.sensorProxyLayer(sensor)
+            if isinstance(lyr, SensorProxyLayer):
+                sensorNode = doc.createElement('MapViewProxyLayer')
+                sensorNode.setAttribute('sensor_id', sensor.id())
+                style: QgsMapLayerStyle = lyr.mapLayerStyle()
+                styleNode = doc.createElement('LayerStyle')
+                style.writeXml(styleNode)
+                sensorNode.appendChild(styleNode)
+                nodeMapView.appendChild(sensorNode)
+        node.appendChild(nodeMapView)
+
     def setName(self, name: str):
         self.setTitle(name)
 
@@ -1207,19 +1285,7 @@ class MapWidget(QFrame):
         mwNode.appendChild(crsNode)
 
         for mapView in self.mapViews():
-            mvNode = doc.createElement('MAP_VIEW')
-            mvNode.setAttribute('name', mapView.name())
-            for sensor in mapView.sensors():
-                lyr = mapView.sensorProxyLayer(sensor)
-                if isinstance(lyr, SensorProxyLayer):
-                    sensorNode = doc.createElement('MAP_VIEW_PROXY_LAYER')
-                    sensorNode.setAttribute('sensor_id', sensor.id())
-                    style: QgsMapLayerStyle = lyr.mapLayerStyle()
-                    styleNode = doc.createElement('STYLE')
-                    style.writeXml(styleNode)
-                    sensorNode.appendChild(styleNode)
-                    mvNode.appendChild(sensorNode)
-            mwNode.appendChild(mvNode)
+            mapView.writeXml(mwNode, doc)
         node.appendChild(mwNode)
         return True
 
@@ -1246,34 +1312,14 @@ class MapWidget(QFrame):
                 self.setCrs(extent.crs())
                 self.setSpatialExtent(extent)
 
-        mvNode = node.firstChildElement('MAP_VIEW').toElement()
-        while mvNode.nodeName() == 'MAP_VIEW':
-            mvName = mvNode.attribute('name')
-            mapView: MapView = None
-            # find existing map view with same name
-            for mv in self.mapViews():
-                if mv.name() == mvName:
-                    mapView = mv
-            # no map view with same name, create a new
-            if not isinstance(mapView, MapView):
-                mapView = MapView()
-                mapView.setName(mvName)
-                self.addMapView(mapView)
+        mvNode = node.firstChildElement('MapView').toElement()
+        while mvNode.nodeName() == 'MapView':
+            mapView = MapView.readXml(mvNode)
+            if isinstance(mapView, MapView):
+                for s in mapView.sensors():
+                    self.timeSeries().addSensor(s)
 
-            sensorNode = mvNode.firstChildElement('MAP_VIEW_PROXY_LAYER').toElement()
-            while not sensorNode.nodeName() == 'MAP_VIEW_PROXY_LAYER':
-                sid = sensorNode.attribute('sensor_id')
-                sensor = self.timeSeries().findMatchingSensor(sid)
-                if sensor:
-                    lyr = mapView.sensorProxyLayer(sensor)
-                    if isinstance(lyr, SensorProxyLayer):
-                        styleNode = sensorNode.firstChildElement('STYLE')
-                        if styleNode.nodeName() == 'STYLE':
-                            style = QgsMapLayerStyle()
-                            style.readXml(styleNode)
-                            lyr.setMapLayerStyle(style)
-
-                sensorNode = sensorNode.nextSibling().toElement()
+                self.addMapView(mapView)
             mvNode = mvNode.nextSibling().toElement()
 
     def usedLayers(self) -> typing.List[QgsMapLayer]:
diff --git a/eotimeseriesviewer/timeseries.py b/eotimeseriesviewer/timeseries.py
index 427d43c4..295edbab 100644
--- a/eotimeseriesviewer/timeseries.py
+++ b/eotimeseriesviewer/timeseries.py
@@ -2190,7 +2190,7 @@ class TimeSeries(QAbstractItemModel):
             tssNode = tssNode.nextSibling()
 
         if len(to_add) > 0:
-            self.addSources(to_add, runAsync=False)
+            self.addSources(to_add, runAsync=True)
 
     def data(self, index, role):
         """
diff --git a/tests/test_main.py b/tests/test_main.py
index 08f24316..26bb1458 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -83,6 +83,7 @@ class TestMain(EOTSVTestCase):
         from eotimeseriesviewer.main import EOTimeSeriesViewer
         TSV = EOTimeSeriesViewer()
         TSV.createMapView('True Color')
+        TSV.createMapView('False Color')
 
         TSV.loadExampleTimeSeries(loadAsync=True)
         while QgsApplication.taskManager().countActiveTasks() > 0 or len(TSV.timeSeries().mTasks) > 0:
@@ -96,6 +97,7 @@ class TestMain(EOTSVTestCase):
         path = self.testOutputDirectory() / 'test.qgz'
         QgsProject.instance().write(path.as_posix())
         self.assertTrue(QgsProject.instance().read(path.as_posix()))
+        TSV.onReloadProject()
 
         self.showGui([TSV.ui])
 
@@ -109,21 +111,6 @@ class TestMain(EOTSVTestCase):
         self.assertIsInstance(TSV, EOTimeSeriesViewer)
         self.showGui(TSV.ui)
 
-    def test_TaskManagerStatusButton(self):
-
-        bar = QgsStatusBar()
-        w = TaskManagerStatusButton()
-        bar.addPermanentWidget(w, 10, QgsStatusBar.AnchorLeft)
-        bar.showMessage('my status')
-        w.mInfoLabel.setText('emoty')
-        self.showGui(bar)
-
-    def test_mapcanvasextent(self):
-        edit = QgsDateTimeEdit()
-
-        self.showGui(edit)
-
-
 
     def test_TimeSeriesViewerInvalidSource(self):
 
diff --git a/tests/test_timeseries.py b/tests/test_timeseries.py
index 59cf11e2..0ec87e72 100644
--- a/tests/test_timeseries.py
+++ b/tests/test_timeseries.py
@@ -512,6 +512,15 @@ class TestTimeSeries(EOTSVTestCase):
         lyr = sensor.proxyRasterLayer()
         self.assertIsInstance(lyr, QgsRasterLayer)
 
+
+        doc = QDomDocument('eotsv')
+        node = doc.createElement('MySensor')
+        sensor.writeXml(node, doc)
+
+        sensor3 = SensorInstrument.readXml(node)
+
+        self.assertEqual(sensor, sensor3)
+
     def test_datematching(self):
         pass
 
-- 
GitLab