From 0044a9203b9dfd5990fcf798ffaf63a2d966f88c Mon Sep 17 00:00:00 2001
From: "benjamin.jakimow" <benjamin.jakimow@geo.hu-berlin.de>
Date: Wed, 30 May 2018 13:07:02 +0200
Subject: [PATCH] Spectral Library can be shown as map canvas

---
 test/test_spectrallibraries.py               |  5 +-
 timeseriesviewer/main.py                     | 48 +++++++++++++-------
 timeseriesviewer/profilevisualization.py     |  2 +-
 timeseriesviewer/sandbox.py                  |  4 +-
 timeseriesviewer/spectrallibraries.py        | 24 ++++++----
 timeseriesviewer/ui/spectrallibrarywidget.ui |  3 ++
 6 files changed, 54 insertions(+), 32 deletions(-)

diff --git a/test/test_spectrallibraries.py b/test/test_spectrallibraries.py
index 4c3a3705..f15a9175 100644
--- a/test/test_spectrallibraries.py
+++ b/test/test_spectrallibraries.py
@@ -211,6 +211,8 @@ class TestInit(unittest.TestCase):
 
 
 
+
+
         self.assertIsInstance(sl1[0], SpectralProfile)
         self.assertEqual(sl1[0], sp1)
         self.assertEqual(sl1[1], sp2)
@@ -255,9 +257,6 @@ class TestInit(unittest.TestCase):
         self.assertTrue(len(sl3) == 2)
 
 
-
-
-
     def test_io(self):
         #sl1.plot()
 
diff --git a/timeseriesviewer/main.py b/timeseriesviewer/main.py
index 9cd17637..f10b7367 100644
--- a/timeseriesviewer/main.py
+++ b/timeseriesviewer/main.py
@@ -51,12 +51,19 @@ from timeseriesviewer.profilevisualization import SpectralTemporalVisualization
 import numpy as np
 
 
-DEBUG = False
-
-
 
+DEBUG = False
 
+from timeseriesviewer.spectrallibraries import createQgsField, createStandardFields
+import timeseriesviewer.spectrallibraries
 
+fields = [f for f in createStandardFields()]
+fields.insert(1, createQgsField('date', ''))
+fields.insert(2, createQgsField('sensorname', ''))
+standardFields = QgsFields()
+for field in fields:
+    standardFields.append(field)
+timeseriesviewer.spectrallibraries.createStandardFields = lambda: standardFields
 
 #ensure that required non-standard modules are available
 
@@ -469,20 +476,27 @@ class TimeSeriesViewer(QgisInterface, QObject):
         D.actionShowOnlineHelp.triggered.connect(lambda : webbrowser.open(URL_DOCUMENTATION))
 
         D.dockSpectralLibrary.SLW.sigLoadFromMapRequest.connect(D.actionIdentifySpectralProfile.trigger)
-
-        from timeseriesviewer.spectrallibraries import createQgsField
-        newFields = QgsFields()
-        newFields.append(createQgsField('date', ''))
-        newFields.append(createQgsField('sensorname', ''))
-
-        D.dockSpectralLibrary.speclib().addMissingFields(newFields)
-        names = D.dockSpectralLibrary.speclib().fieldNames()
-        for name in ['sensorname', 'date']:
-            iFrom  = names.index(name)
-        D.dockSpectralLibrary.SLW.tableViewSpeclib.horizontalHeader().moveSection(iFrom, 1)
-
-        s = ""
-
+        D.dockSpectralLibrary.SLW.setMapInteraction(True)
+
+
+        QgsProject.instance().addMapLayer(D.dockSpectralLibrary.speclib())
+
+        moveToFeatureCenter = QgsMapLayerAction('Move to', self, QgsMapLayer.VectorLayer)
+        moveToFeatureCenter.triggeredForFeature.connect(self.onMoveToFeature)
+        reg = QgsGui.instance().mapLayerActionRegistry()
+        assert isinstance(reg, QgsMapLayerActionRegistry)
+        reg.addMapLayerAction(moveToFeatureCenter)
+        reg.setDefaultActionForLayer(D.dockSpectralLibrary.speclib(), moveToFeatureCenter)
+
+    def onMoveToFeature(self, layer:QgsMapLayer, feature:QgsFeature):
+        g = feature.geometry()
+        if isinstance(g, QgsGeometry):
+            c = g.centroid()
+            x, y = c.asPoint()
+            crs = layer.crs()
+            center = SpatialPoint(crs, x, y)
+            self.spatialTemporalVis.setSpatialCenter(center)
+            s = ""
     def initQGISConnection(self):
 
         self.ui.actionImportExtent.triggered.connect(lambda: self.spatialTemporalVis.setSpatialExtent(SpatialExtent.fromMapCanvas(self.iface.mapCanvas())))
diff --git a/timeseriesviewer/profilevisualization.py b/timeseriesviewer/profilevisualization.py
index 5b720344..e28441d9 100644
--- a/timeseriesviewer/profilevisualization.py
+++ b/timeseriesviewer/profilevisualization.py
@@ -1973,7 +1973,7 @@ class SpectralTemporalVisualization(QObject):
                         x1 = max(pdi.xData.max(), x1)
 
             if x0 is not None:
-                self.plot2D.plotItem().setXRange(x0, x1)
+                self.plot2D.plotItem.setXRange(x0, x1)
                 #self.plot2D.xAxisInitialized = True
 
     @QtCore.pyqtSlot()
diff --git a/timeseriesviewer/sandbox.py b/timeseriesviewer/sandbox.py
index c489f984..d43e26a5 100644
--- a/timeseriesviewer/sandbox.py
+++ b/timeseriesviewer/sandbox.py
@@ -352,9 +352,9 @@ if __name__ == '__main__':
     if False: sandboxQgisBridge()
     if False: sandboxGui()
 
-    if False: sandboxTestdata()
+    if True: sandboxTestdata()
     if False: sandboxDemo()
-    if True:
+    if False:
         import timeseriesviewer.spectrallibraries
         timeseriesviewer.spectrallibraries.__sandbox()
     #close QGIS
diff --git a/timeseriesviewer/spectrallibraries.py b/timeseriesviewer/spectrallibraries.py
index 00e5049e..c43807b2 100644
--- a/timeseriesviewer/spectrallibraries.py
+++ b/timeseriesviewer/spectrallibraries.py
@@ -2534,7 +2534,7 @@ class SpectralLibraryWidget(QFrame, loadUI('spectrallibrarywidget.ui')):
             field = d.field()
             b = self.mSpeclib.isEditable()
             self.mSpeclib.startEditing()
-            self.mSpeclib.addAttribute(field)
+            self.mSpecli.addAttribute(field)
             saveEdits(self.mSpeclib, leaveEditable=b)
 
     def onRemoveAttribute(self):
@@ -2553,20 +2553,26 @@ class SpectralLibraryWidget(QFrame, loadUI('spectrallibrarywidget.ui')):
         s  =""
 
 
-    def updateTableConfig(self):
+    def updateTableConfig(self, config = None):
         """
         Updates the QgsAttributeTableConfig, basically only to hide columns to be hidden.
         """
 
-        self.mTableConfig = QgsAttributeTableConfig()
-        self.mTableConfig.update(self.mSpeclib.fields())
+        if not isinstance(config, QgsAttributeTableConfig):
 
-        for i, columnConfig in enumerate(self.mTableConfig.columns()):
-            if columnConfig.name.startswith(HIDDEN_ATTRIBUTE_PREFIX):
-                self.mTableConfig.setColumnHidden(i, True)
-            if columnConfig.name == 'source':
-                self.mTableConfig.setColumnWidth(i, 25)
+            config = QgsAttributeTableConfig()
+            config.update(self.mSpeclib.fields())
+
+            for i, columnConfig in enumerate(config.columns()):
+                assert isinstance(columnConfig, QgsAttributeTableConfig.ColumnConfig)
+                hidden = columnConfig.name.startswith(HIDDEN_ATTRIBUTE_PREFIX)
+                config.setColumnHidden(i, hidden)
+            #config.setActionWidgetVisible(False)
+            #self.mTableConfig.setColumnHidden(i, True)
+                #if columnConfig.name == 'source':
+                #    self.mTableConfig.setColumnWidth(i, 25)
 
+        self.mTableConfig = config
         self.mSpeclib.setAttributeTableConfig(self.mTableConfig)
         self.mFilterModel.setAttributeTableConfig(self.mTableConfig)
         #self.tableViewSpeclib.setAttributeTableConfig(self.mTableConfig)
diff --git a/timeseriesviewer/ui/spectrallibrarywidget.ui b/timeseriesviewer/ui/spectrallibrarywidget.ui
index 3709a20a..39b37c25 100644
--- a/timeseriesviewer/ui/spectrallibrarywidget.ui
+++ b/timeseriesviewer/ui/spectrallibrarywidget.ui
@@ -524,6 +524,9 @@
    <property name="checkable">
     <bool>true</bool>
    </property>
+   <property name="checked">
+    <bool>true</bool>
+   </property>
    <property name="icon">
     <iconset resource="resources.qrc">
      <normaloff>:/timeseriesviewer/icons/profile2speclib_auto.svg</normaloff>:/timeseriesviewer/icons/profile2speclib_auto.svg</iconset>
-- 
GitLab