From 42ceed25fea90a42eb3b89cd12cc9a70f5de8e66 Mon Sep 17 00:00:00 2001
From: Olivier Jossoud <olivier.jossoud@lsce.ipsl.fr>
Date: Thu, 7 Nov 2019 11:00:58 +0100
Subject: [PATCH] Explo/Stab. Show 4 boundaries as datetime strings.

---
 pyqt/stabwindow.ui      | 26 ++++++++++++++++++++++++++
 src/gui/uistabwindow.py | 14 ++++++++++++++
 src/uim/explouim.py     | 23 +++++++++++++++++++++++
 3 files changed, 63 insertions(+)

diff --git a/pyqt/stabwindow.ui b/pyqt/stabwindow.ui
index 14ca7b4..afe0d9d 100644
--- a/pyqt/stabwindow.ui
+++ b/pyqt/stabwindow.ui
@@ -67,6 +67,32 @@
     </rect>
    </property>
   </widget>
+  <widget class="QWidget" name="horizontalLayoutWidget_2">
+   <property name="geometry">
+    <rect>
+     <x>610</x>
+     <y>10</y>
+     <width>721</width>
+     <height>31</height>
+    </rect>
+   </property>
+   <layout class="QHBoxLayout" name="horizontalLayout_2">
+    <item>
+     <widget class="QLabel" name="stab_label_timeranges">
+      <property name="text">
+       <string>Time ranges:</string>
+      </property>
+     </widget>
+    </item>
+    <item>
+     <widget class="QLineEdit" name="stab_lineedit_timeranges">
+      <property name="toolTip">
+       <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Tab-separated boundaries of the two regions (blue and green area).&lt;/p&gt;&lt;p&gt;Can be directly copy-pasted in Excel/Calc.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
+      </property>
+     </widget>
+    </item>
+   </layout>
+  </widget>
  </widget>
  <customwidgets>
   <customwidget>
diff --git a/src/gui/uistabwindow.py b/src/gui/uistabwindow.py
index 7592df0..1908ca2 100644
--- a/src/gui/uistabwindow.py
+++ b/src/gui/uistabwindow.py
@@ -35,6 +35,18 @@ class Ui_StabWindow(object):
         self.stab_graphicsview = PlotWidget(StabWindow)
         self.stab_graphicsview.setGeometry(QtCore.QRect(10, 50, 1321, 651))
         self.stab_graphicsview.setObjectName("stab_graphicsview")
+        self.horizontalLayoutWidget_2 = QtWidgets.QWidget(StabWindow)
+        self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(610, 10, 721, 31))
+        self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
+        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
+        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
+        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
+        self.stab_label_timeranges = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
+        self.stab_label_timeranges.setObjectName("stab_label_timeranges")
+        self.horizontalLayout_2.addWidget(self.stab_label_timeranges)
+        self.stab_lineedit_timeranges = QtWidgets.QLineEdit(self.horizontalLayoutWidget_2)
+        self.stab_lineedit_timeranges.setObjectName("stab_lineedit_timeranges")
+        self.horizontalLayout_2.addWidget(self.stab_lineedit_timeranges)
 
         self.retranslateUi(StabWindow)
         QtCore.QMetaObject.connectSlotsByName(StabWindow)
@@ -44,5 +56,7 @@ class Ui_StabWindow(object):
         StabWindow.setWindowTitle(_translate("StabWindow", "Stabilization time analysis"))
         self.stab_label_instrument.setText(_translate("StabWindow", "Instrument: "))
         self.stab_label_variable.setText(_translate("StabWindow", "  Variable:"))
+        self.stab_label_timeranges.setText(_translate("StabWindow", "Time ranges:"))
+        self.stab_lineedit_timeranges.setToolTip(_translate("StabWindow", "<html><head/><body><p>Tab-separated boundaries of the two regions (blue and green area).</p><p>Can be directly copy-pasted in Excel/Calc.</p></body></html>"))
 
 from pyqtgraph import PlotWidget
diff --git a/src/uim/explouim.py b/src/uim/explouim.py
index f323bee..95450cd 100644
--- a/src/uim/explouim.py
+++ b/src/uim/explouim.py
@@ -557,6 +557,7 @@ class ExploUim:
         self.stab_region_left.setZValue(10)
         self.stab_plot_item.addItem(self.stab_region_left, ignoreBounds=True)
         self.stab_region_left.sigRegionChanged.connect(lambda region: self.__update_stab_mean__(region, "left"))
+        self.stab_region_left.sigRegionChanged.connect(self.__update_time_ranges__)
         self.stab_left_mean = 0.0
         self.stab_left_mean_line = pg.InfiniteLine(pen=pg.mkPen(color=[0, 0, 255]), angle=0)
         self.stab_plot_item.addItem(self.stab_left_mean_line)
@@ -566,6 +567,7 @@ class ExploUim:
         self.stab_region_right.setZValue(10)
         self.stab_plot_item.addItem(self.stab_region_right, ignoreBounds=True)
         self.stab_region_right.sigRegionChanged.connect(lambda region: self.__update_stab_mean__(region, "right"))
+        self.stab_region_right.sigRegionChanged.connect(self.__update_time_ranges__)
         self.stab_right_mean = 0.0
         self.stab_right_mean_line = pg.InfiniteLine(pen=pg.mkPen(color=[0, 255, 0]), angle=0)
         self.stab_plot_item.addItem(self.stab_right_mean_line)
@@ -689,3 +691,24 @@ class ExploUim:
         self.stab_time_text.setText("<-- " + "{:.2f}".format(time_delta) + "s -->")
         self.stab_time_text.setPos(low_datetime + (high_datetime-low_datetime)/2,
                                    high_y)
+
+    def __update_time_ranges__(self) -> None:
+        """Update the UI lineEdit with the current datetime boundaries of the two regions.
+        These 4 boundaries can then be copy-pasted by the user to Excel or Calc or any TSV file, for later reference.
+        """
+        # Get the 2*2 boundaries, as epoch timestamp
+        left_min = self.stab_region_left.getRegion()[0]
+        left_max = self.stab_region_left.getRegion()[1]
+        right_min = self.stab_region_right.getRegion()[0]
+        right_max = self.stab_region_right.getRegion()[1]
+        boundaries_list = [left_min, left_max, right_min, right_max]
+
+        # Exit if right region is not yet placed.
+        if (right_min == 0) & (right_max == 1.0):
+            return
+
+        # Convert epoch timestamps to ISO datetime strings
+        boundaries_list = [datetime.datetime.fromtimestamp(boundary).strftime(utils.datetime_format)
+                           for boundary in boundaries_list]
+
+        self.stab_window.ui.stab_lineedit_timeranges.setText("\t".join(boundaries_list))
-- 
GitLab