From 214eb13836c7eed9a6a129b54a690d067a84ed29 Mon Sep 17 00:00:00 2001
From: Paul Beuchat <beuchatp@control.ee.ethz.ch>
Date: Fri, 27 Mar 2020 09:55:23 +0100
Subject: [PATCH] Added zmin and zmax fields to the System Config GUI. All
 zones get the same height when clicking the save in DB button. It would be
 nice to do a further update where each zone can have a different vertical
 extent

---
 .../systemConfigGUI/forms/mainguiwindow.ui    | 76 ++++++++++++++++++-
 .../systemConfigGUI/include/mainguiwindow.h   |  7 ++
 .../systemConfigGUI/src/mainguiwindow.cpp     | 55 +++++++++++++-
 3 files changed, 132 insertions(+), 6 deletions(-)

diff --git a/dfall_ws/src/dfall_pkg/GUI_Qt/systemConfigGUI/forms/mainguiwindow.ui b/dfall_ws/src/dfall_pkg/GUI_Qt/systemConfigGUI/forms/mainguiwindow.ui
index 2d50c2a9..5e470ee9 100755
--- a/dfall_ws/src/dfall_pkg/GUI_Qt/systemConfigGUI/forms/mainguiwindow.ui
+++ b/dfall_ws/src/dfall_pkg/GUI_Qt/systemConfigGUI/forms/mainguiwindow.ui
@@ -6,8 +6,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>1228</width>
-    <height>1559</height>
+    <width>1339</width>
+    <height>1619</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -116,6 +116,74 @@
             </property>
            </widget>
           </item>
+          <item>
+           <layout class="QHBoxLayout" name="horizontalLayout">
+            <item>
+             <widget class="QLabel" name="label_zmin">
+              <property name="text">
+               <string>zmin</string>
+              </property>
+             </widget>
+            </item>
+            <item>
+             <widget class="QLineEdit" name="lineEdit_zmin">
+              <property name="sizePolicy">
+               <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
+                <horstretch>0</horstretch>
+                <verstretch>0</verstretch>
+               </sizepolicy>
+              </property>
+              <property name="font">
+               <font>
+                <family>Courier</family>
+                <weight>75</weight>
+                <bold>true</bold>
+               </font>
+              </property>
+              <property name="text">
+               <string>-0.2</string>
+              </property>
+              <property name="alignment">
+               <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+              </property>
+             </widget>
+            </item>
+           </layout>
+          </item>
+          <item>
+           <layout class="QHBoxLayout" name="horizontalLayout_2">
+            <item>
+             <widget class="QLabel" name="label_zmax">
+              <property name="text">
+               <string>zmax</string>
+              </property>
+             </widget>
+            </item>
+            <item>
+             <widget class="QLineEdit" name="lineEdit_zmax">
+              <property name="sizePolicy">
+               <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
+                <horstretch>0</horstretch>
+                <verstretch>0</verstretch>
+               </sizepolicy>
+              </property>
+              <property name="font">
+               <font>
+                <family>Courier</family>
+                <weight>75</weight>
+                <bold>true</bold>
+               </font>
+              </property>
+              <property name="text">
+               <string>1.8</string>
+              </property>
+              <property name="alignment">
+               <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+              </property>
+             </widget>
+            </item>
+           </layout>
+          </item>
          </layout>
         </widget>
        </item>
@@ -972,8 +1040,8 @@
     <rect>
      <x>0</x>
      <y>0</y>
-     <width>1228</width>
-     <height>40</height>
+     <width>1339</width>
+     <height>47</height>
     </rect>
    </property>
   </widget>
diff --git a/dfall_ws/src/dfall_pkg/GUI_Qt/systemConfigGUI/include/mainguiwindow.h b/dfall_ws/src/dfall_pkg/GUI_Qt/systemConfigGUI/include/mainguiwindow.h
index bfa475fd..5d17177c 100755
--- a/dfall_ws/src/dfall_pkg/GUI_Qt/systemConfigGUI/include/mainguiwindow.h
+++ b/dfall_ws/src/dfall_pkg/GUI_Qt/systemConfigGUI/include/mainguiwindow.h
@@ -38,6 +38,7 @@
 #include <QTimer>
 #include <QGridLayout>
 #include <QGraphicsRectItem>
+#include <QLineEdit>
 
 
 #ifdef CATKIN_MAKE
@@ -131,6 +132,8 @@ private slots:
     #ifdef CATKIN_MAKE
     #endif
 
+    float validate_and_get_value_from_lineEdit(QLineEdit * lineEdit, float min, float max, int decimals, float default_value);
+
     void doNumCrazyFlyZonesChanged(int n);
     void transitionToMode(int mode);
     void on_removeTable_clicked();
@@ -188,6 +191,10 @@ private slots:
     
     // For the emergency stop button
     void on_emergency_stop_button_clicked();
+
+    // For automatically validating the zmin and zmax line edits
+    void on_lineEdit_zmin_editingFinished();
+    void on_lineEdit_zmax_editingFinished();
     
 
 private:
diff --git a/dfall_ws/src/dfall_pkg/GUI_Qt/systemConfigGUI/src/mainguiwindow.cpp b/dfall_ws/src/dfall_pkg/GUI_Qt/systemConfigGUI/src/mainguiwindow.cpp
index 54a48fd3..a93637d1 100755
--- a/dfall_ws/src/dfall_pkg/GUI_Qt/systemConfigGUI/src/mainguiwindow.cpp
+++ b/dfall_ws/src/dfall_pkg/GUI_Qt/systemConfigGUI/src/mainguiwindow.cpp
@@ -85,6 +85,33 @@ MainGUIWindow::~MainGUIWindow()
     delete ui;
 }
 
+float MainGUIWindow::validate_and_get_value_from_lineEdit(QLineEdit * lineEdit, float min, float max, int decimals, float default_value)
+{
+    // Initialise the value to the default
+    float return_value = default_value;
+
+    // Check that the field is NOT empty
+    if(!lineEdit->text().isEmpty())
+    {
+        // Extraxt the value from the field as a float
+        return_value = (lineEdit->text()).toFloat();
+        // Ensure that it is in the range specified
+        if (return_value < min)
+            return_value = min;
+        else if (return_value > max)
+            return_value = max;
+    }
+
+    // Clip the value to the specified decimal places
+
+
+    // Put the value back into the line edit
+    lineEdit->setText(QString::number( return_value, 'f', decimals));
+
+    // Return the value
+    return return_value;
+}
+
 int MainGUIWindow::getTabIndexFromName(QString name)
 {
     int found_name = -1;
@@ -841,6 +868,10 @@ void MainGUIWindow::on_unlink_button_clicked()
 
 void MainGUIWindow::on_save_in_DB_button_clicked()
 {
+    // Get the zmin and zmax values from the line edits
+    float current_zmin = validate_and_get_value_from_lineEdit(ui->lineEdit_zmin,-10.0,10.0,2,-0.2);
+    float current_zmax = validate_and_get_value_from_lineEdit(ui->lineEdit_zmax,-10.0,10.0,2,1.8);
+
 #ifdef CATKIN_MAKE
     // we need to update and then save?
     CrazyflieDB tmp_db;
@@ -867,8 +898,8 @@ void MainGUIWindow::on_save_in_DB_button_clicked()
                 tmp_entry.crazyflieContext.localArea.ymin = y_min * FROM_UNITS_TO_METERS;
                 tmp_entry.crazyflieContext.localArea.ymax = y_max * FROM_UNITS_TO_METERS;
 
-                tmp_entry.crazyflieContext.localArea.zmin = -0.2;
-                tmp_entry.crazyflieContext.localArea.zmax = 2.2;
+                tmp_entry.crazyflieContext.localArea.zmin = current_zmin;
+                tmp_entry.crazyflieContext.localArea.zmax = current_zmax;
             }
         }
         tmp_db.crazyflieEntries.push_back(tmp_entry);
@@ -1104,3 +1135,23 @@ void MainGUIWindow::on_emergency_stop_button_clicked()
     emergencyStopPublisher.publish(msg);
 #endif
 }
+
+
+//    ----------------------------------------------------------------------------------
+//
+//
+//
+//
+//
+//    ----------------------------------------------------------------------------------
+
+void MainGUIWindow::on_lineEdit_zmin_editingFinished()
+{
+    validate_and_get_value_from_lineEdit(ui->lineEdit_zmin,-10.0,10.0,2,-0.2);
+}
+
+void MainGUIWindow::on_lineEdit_zmax_editingFinished()
+{
+    validate_and_get_value_from_lineEdit(ui->lineEdit_zmax,-10.0,10.0,2,1.8);
+}
+
-- 
GitLab