From 43e3b1bb150c9ccf1d430ca60c7eda10156e7c1e Mon Sep 17 00:00:00 2001
From: Jan Hausberg <jan.hausberg@kit.edu>
Date: Wed, 22 Jun 2022 10:20:47 +0200
Subject: [PATCH] Update HandUnitGuiPlugin UI and functionality for the right
 hand; remove unnecessary header files

---
 .../HandUnitPlugin/HandUnitGuiPlugin.cpp      | 152 ++++++++++++------
 .../HandUnitPlugin/HandUnitGuiPlugin.h        |   5 +-
 .../HandUnitPlugin/HandUnitGuiPlugin.ui       | 123 +++++++++-----
 3 files changed, 188 insertions(+), 92 deletions(-)

diff --git a/source/RobotAPI/gui-plugins/HandUnitPlugin/HandUnitGuiPlugin.cpp b/source/RobotAPI/gui-plugins/HandUnitPlugin/HandUnitGuiPlugin.cpp
index 7233e276e..ea3190228 100644
--- a/source/RobotAPI/gui-plugins/HandUnitPlugin/HandUnitGuiPlugin.cpp
+++ b/source/RobotAPI/gui-plugins/HandUnitPlugin/HandUnitGuiPlugin.cpp
@@ -36,8 +36,6 @@
 #include <QMessageBox>
 #include <QTimer>
 #include <QtWidgets/QSlider>
-#include <QtWidgets/QGridLayout>
-#include <QtWidgets/QLabel>
 #include <QtWidgets/QTableWidgetItem>
 #include <QString>
 
@@ -55,13 +53,14 @@ namespace armarx
         rightHandName("NOT SET YET"),
         leftHandUnitProxyName(""),
         rightHandUnitProxyName(""),
-        setLeftHandJointAnglesFlag(false)
+        setLeftHandJointAnglesFlag(false),
+        setRightHandJointAnglesFlag(false)
     {
         // init gui
         ui.setupUi(getWidget());
 
         setLeftHandJointAngleUpdateTask = new PeriodicTask<HandUnitWidget>(this, &HandUnitWidget::setLeftHandJointAngles, 50);
-        rightHandJointAngleUpdateTask = new PeriodicTask<HandUnitWidget>(this, &HandUnitWidget::setRightHandJointAngles, 50);
+        setRightHandJointAngleUpdateTask = new PeriodicTask<HandUnitWidget>(this, &HandUnitWidget::setRightHandJointAngles, 50);
         updateInfoTimer = new QTimer(this);
     }
 
@@ -78,6 +77,8 @@ namespace armarx
     {
         updateInfoTimer->start(50);
 
+
+
         setLeftHandJointAngleUpdateTask->start();
 
         leftHandUnitProxy = getProxy<HandUnitInterfacePrx>(leftHandUnitProxyName);
@@ -108,12 +109,16 @@ namespace armarx
 
 
 
-        rightHandJointAngleUpdateTask->start();
+        setRightHandJointAngleUpdateTask->start();
 
         rightHandUnitProxy = getProxy<HandUnitInterfacePrx>(rightHandUnitProxyName);
         rightHandName = rightHandUnitProxy->getHandName();
 
-        if (rightHandName != "Hand R" && rightHandName != "TCP R" && rightHandName != "Hand_R_EEF")
+        if (rightHandName == "Hand R" || rightHandName == "TCP R")
+        {
+            rightHandConversionFactor *= M_PI / 2;
+        }
+        else if (rightHandName != "Hand_R_EEF")
         {
             ARMARX_WARNING << "Right hand with name \"" << rightHandName << "\" is not supported.";
         }
@@ -125,6 +130,7 @@ namespace armarx
         for (int i = 0; i < rightHandPreshapeCount; ++i)
         {
             std::string shape = ((rightHandPreshapeStrings->getVariant(i))->get<std::string>());
+//            ARMARX_INFO << VAROUT(shape);
             rightHandList << QString::fromStdString(shape);
         }
 
@@ -133,7 +139,7 @@ namespace armarx
 
 
 
-        ARMARX_INFO << "initGUIJointFrames";
+//        ARMARX_INFO << "initGUIJointFrames";
         initGUIJointFrames();
         displayJointAngleUpdateTask = new PeriodicTask<HandUnitWidget>(this, &HandUnitWidget::updateJointValueTable, 50);
         displayJointAngleUpdateTask->start();
@@ -142,7 +148,7 @@ namespace armarx
     void HandUnitWidget::onDisconnectComponent()
     {
         setLeftHandJointAngleUpdateTask->stop();
-        rightHandJointAngleUpdateTask->stop();
+        setRightHandJointAngleUpdateTask->stop();
         displayJointAngleUpdateTask->stop();
         updateInfoTimer->stop();
     }
@@ -213,19 +219,20 @@ namespace armarx
             if (pair.first == currentLeftHandJoint)
             {
                 value = static_cast<float>(ui.horizontalSliderLeftHandJointPos->value() * leftHandConversionFactor);
-                ARMARX_INFO << VAROUT(value);
+//                ARMARX_INFO << VAROUT(value);
                 leftHandJa[pair.first] = value;
                 break;
             }
         }
 
-        ARMARX_INFO << VAROUT(value / leftHandConversionFactor);
+//        ARMARX_INFO << VAROUT(value / leftHandConversionFactor);
         ui.lcdNumberLeftHandJointValue->display(value / leftHandConversionFactor);
         leftHandUnitProxy->setJointAngles(leftHandJa);
     }
 
     void HandUnitWidget::setRightHandJointAngles()
     {
+//        ARMARX_INFO << "setRightHandJointAngles";
         if (!rightHandUnitProxy)
         {
             ARMARX_WARNING << "invalid proxy";
@@ -241,26 +248,30 @@ namespace armarx
 
         NameValueMap rightHandJa;
         NameValueMap currentRightHandJointValues = rightHandUnitProxy->getCurrentJointValues();
-
-        if (rightHandName == "Hand_R_EEF")
+        float value = 0;
+        if (rightHandName == "Hand R" || rightHandName == "TCP R")
         {
-            for (const auto& pair : currentRightHandJointValues)
-            {
-                rightHandJa[pair.first] = ui.frameRightHand->findChild<QSlider *>("horizontalSlider" + QString::fromUtf8(pair.first.c_str()))->value() / 100.0f;
-            }
+            rightHandConversionFactor *= M_PI / 2;
         }
-        else if (rightHandName == "Hand R" || rightHandName == "TCP R")
+        else if (rightHandName != "Hand_R_EEF")
         {
-            for (const auto& pair : currentRightHandJointValues)
-            {
-                rightHandJa[pair.first] = ui.frameRightHand->findChild<QSlider *>("horizontalSlider" + QString::fromUtf8(pair.first.c_str()))->value() / 100.0f * M_PI / 2;
-            }
+            ARMARX_WARNING << "Right hand with name \"" << rightHandName << "\" is not supported.";
+            return;
         }
-        else
+
+        for (const auto& pair : currentRightHandJointValues)
         {
-            ARMARX_WARNING << "Right hand with name \"" << rightHandName << "\" is not supported.";
+            if (pair.first == currentRightHandJoint)
+            {
+                value = static_cast<float>(ui.horizontalSliderRightHandJointPos->value() * rightHandConversionFactor);
+//                ARMARX_INFO << VAROUT(value);
+                rightHandJa[pair.first] = value;
+                break;
+            }
         }
 
+//        ARMARX_INFO << VAROUT(value / rightHandConversionFactor);
+        ui.lcdNumberRightHandJointValue->display(value / rightHandConversionFactor);
         rightHandUnitProxy->setJointAngles(rightHandJa);
     }
 
@@ -324,11 +335,26 @@ namespace armarx
             ui.tableWidgetLeftHand->setItem(frameLeftHandRowIdx, 1, newItem);
             frameLeftHandRowIdx++;
         }
+
+
+
+        NameValueMap currentRightHandJointValues = rightHandUnitProxy->getCurrentJointValues();
+//        ARMARX_INFO << VAROUT(rightHandUnitProxy->getCurrentJointValues());
+        int frameRightHandRowIdx = 0;
+
+        for (const auto& pair : currentRightHandJointValues)
+        {
+//            ARMARX_INFO << VAROUT(pair.first);
+            QString name(pair.first.c_str());
+            QTableWidgetItem* newItem = new QTableWidgetItem(QString::number(pair.second));
+            ui.tableWidgetRightHand->setItem(frameRightHandRowIdx, 1, newItem);
+            frameRightHandRowIdx++;
+        }
     }
 
     void HandUnitWidget::selectLeftHandJoint(int i)
     {
-        ARMARX_INFO << "selectLeftHandJoint " << i;
+//        ARMARX_INFO << "selectLeftHandJoint " << i;
         NameValueMap currentLeftHandJointValues = leftHandUnitProxy->getCurrentJointValues();
         int idx = 0;
 
@@ -340,7 +366,28 @@ namespace armarx
                 int convertedValue = static_cast<int>(pair.second / leftHandConversionFactor);
                 ui.horizontalSliderLeftHandJointPos->setSliderPosition(convertedValue);
                 ui.lcdNumberLeftHandJointValue->display(convertedValue);
-                ARMARX_INFO << "Found joint";
+//                ARMARX_INFO << "Found joint";
+                break;
+            }
+            idx++;
+        }
+    }
+
+    void HandUnitWidget::selectRightHandJoint(int i)
+    {
+//        ARMARX_INFO << "selectRightHandJoint " << i;
+        NameValueMap currentRightHandJointValues = rightHandUnitProxy->getCurrentJointValues();
+        int idx = 0;
+
+        for (const auto& pair: currentRightHandJointValues)
+        {
+            if (idx == i)
+            {
+                currentRightHandJoint = pair.first;
+                int convertedValue = static_cast<int>(pair.second / rightHandConversionFactor);
+                ui.horizontalSliderRightHandJointPos->setSliderPosition(convertedValue);
+                ui.lcdNumberRightHandJointValue->display(convertedValue);
+//                ARMARX_INFO << "Found joint";
                 break;
             }
             idx++;
@@ -349,13 +396,13 @@ namespace armarx
 
     void HandUnitWidget::setLeftHandPreshape(std::string preshape)
     {
-        ARMARX_INFO << "Setting new left hand shape: " << preshape;
+//        ARMARX_INFO << "Setting new left hand shape: " << preshape;
         leftHandUnitProxy->setShape(preshape);
     }
 
     void HandUnitWidget::setRightHandPreshape(std::string preshape)
     {
-        ARMARX_INFO << "Setting new right hand shape: " << preshape;
+//        ARMARX_INFO << "Setting new right hand shape: " << preshape;
         rightHandUnitProxy->setShape(preshape);
     }
 
@@ -411,37 +458,36 @@ namespace armarx
 
 
 
-//        NameValueMap currentRightHandJointValues = rightHandUnitProxy->getCurrentJointValues();
-//        QGridLayout* gridLayoutRightHand = new QGridLayout(ui.frameRightHand);
-//        int frameRightHandRowIdx = 0;
-
-//        connect(ui.buttonPreshapeRightHand, SIGNAL(clicked()), this, SLOT(preshapeRightHand()), Qt::UniqueConnection);
-//        connect(ui.buttonOpenRightHand, SIGNAL(clicked()), this, SLOT(openRightHand()), Qt::UniqueConnection);
-//        connect(ui.buttonCloseRightHand, SIGNAL(clicked()), this, SLOT(closeRightHand()), Qt::UniqueConnection);
-//        connect(ui.buttonRelaxRightHand, SIGNAL(clicked()), this, SLOT(relaxRightHand()), Qt::UniqueConnection);
-//        connect(ui.buttonSetRightHandJointAngles, SIGNAL(clicked()), this, SLOT(requestSetRightHandJointAngles()), Qt::UniqueConnection);
-
-//        for (const auto& pair : currentRightHandJointValues)
-//        {
-//            QLabel* label = new QLabel(ui.frameRightHand);
-//            label->setText(QString::fromUtf8(pair.first.c_str()));
-//            gridLayoutRightHand->addWidget(label, frameRightHandRowIdx, 0, 1, 1);
-
-//            QSlider* horizontalSlider = new QSlider(ui.frameRightHand);
-//            horizontalSlider->setObjectName("horizontalSlider" + QString::fromUtf8(pair.first.c_str()));
-//            horizontalSlider->setMaximum(1);
-//            horizontalSlider->setOrientation(Qt::Horizontal);
-//            connect(horizontalSlider, SIGNAL(sliderMoved(int)), this, SLOT(requestSetRightHandJointAngles()), Qt::UniqueConnection);
-//            gridLayoutRightHand->addWidget(horizontalSlider, frameRightHandRowIdx, 1, 1, 2);
+        NameValueMap currentRightHandJointValues = rightHandUnitProxy->getCurrentJointValues();
+//        ARMARX_INFO << VAROUT(rightHandUnitProxy->getCurrentJointValues());
+        int frameRightHandRowIdx = 0;
 
-//            frameRightHandRowIdx++;
-//        }
+        connect(ui.buttonPreshapeRightHand, SIGNAL(clicked()), this, SLOT(preshapeRightHand()), Qt::UniqueConnection);
+        connect(ui.buttonOpenRightHand, SIGNAL(clicked()), this, SLOT(openRightHand()), Qt::UniqueConnection);
+        connect(ui.buttonCloseRightHand, SIGNAL(clicked()), this, SLOT(closeRightHand()), Qt::UniqueConnection);
+        connect(ui.buttonRelaxRightHand, SIGNAL(clicked()), this, SLOT(relaxRightHand()), Qt::UniqueConnection);
+        ui.horizontalSliderRightHandJointPos->setMaximum(100);
+        ui.horizontalSliderRightHandJointPos->setMinimum(0);
+        connect(ui.horizontalSliderRightHandJointPos, SIGNAL(sliderMoved(int)), this, SLOT(requestSetRightHandJointAngles()), Qt::UniqueConnection);
+        connect(ui.comboRightHandJoints, SIGNAL(currentIndexChanged(int)), this, SLOT(selectRightHandJoint(int)), Qt::UniqueConnection);
 
-//        ui.frameRightHand->setLayout(gridLayoutRightHand);
+        ui.tableWidgetRightHand->setRowCount(currentRightHandJointValues.size());
+        ui.tableWidgetRightHand->setColumnCount(2);
 
+        ui.comboRightHandJoints->clear();
 
+        qRegisterMetaType<QVector<int> >("QVector<int>");
+        for (const auto& pair : currentRightHandJointValues)
+        {
+//            ARMARX_INFO << VAROUT(pair.first);
+            QString name(pair.first.c_str());
+            QTableWidgetItem* newItem = new QTableWidgetItem(name);
+            ui.tableWidgetRightHand->setItem(frameRightHandRowIdx, 0, newItem);
+            ui.comboRightHandJoints->addItem(name);
+            frameRightHandRowIdx++;
+        }
 
-//        connect(updateInfoTimer, SIGNAL(timeout()), this, SLOT(updateInfoLabel()));
+        ui.comboRightHandJoints->setCurrentIndex(-1);
     }
 
 
diff --git a/source/RobotAPI/gui-plugins/HandUnitPlugin/HandUnitGuiPlugin.h b/source/RobotAPI/gui-plugins/HandUnitPlugin/HandUnitGuiPlugin.h
index 60415021b..24d7900ce 100644
--- a/source/RobotAPI/gui-plugins/HandUnitPlugin/HandUnitGuiPlugin.h
+++ b/source/RobotAPI/gui-plugins/HandUnitPlugin/HandUnitGuiPlugin.h
@@ -127,6 +127,7 @@ namespace armarx
         void updateInfoLabel();
         void updateJointValueTable();
         void selectLeftHandJoint(int i);
+        void selectRightHandJoint(int i);
 
     private:
         void setLeftHandPreshape(std::string preshape);
@@ -151,13 +152,15 @@ namespace armarx
         QPointer<HandUnitConfigDialog> dialog;
 
         PeriodicTask<HandUnitWidget>::pointer_type setLeftHandJointAngleUpdateTask;
-        PeriodicTask<HandUnitWidget>::pointer_type rightHandJointAngleUpdateTask;
+        PeriodicTask<HandUnitWidget>::pointer_type setRightHandJointAngleUpdateTask;
         PeriodicTask<HandUnitWidget>::pointer_type displayJointAngleUpdateTask;
         QTimer* updateInfoTimer;
         bool setLeftHandJointAnglesFlag;
         bool setRightHandJointAnglesFlag;
         std::basic_string<char> currentLeftHandJoint;
+        std::basic_string<char> currentRightHandJoint;
         double leftHandConversionFactor= 1.0 / 100.0;
+        double rightHandConversionFactor = 1.0 / 100.0;
 
 
         // HandUnitListener interface
diff --git a/source/RobotAPI/gui-plugins/HandUnitPlugin/HandUnitGuiPlugin.ui b/source/RobotAPI/gui-plugins/HandUnitPlugin/HandUnitGuiPlugin.ui
index cda76d797..cec1e1ea7 100644
--- a/source/RobotAPI/gui-plugins/HandUnitPlugin/HandUnitGuiPlugin.ui
+++ b/source/RobotAPI/gui-plugins/HandUnitPlugin/HandUnitGuiPlugin.ui
@@ -120,25 +120,6 @@
     <string>&lt;Info Here&gt;</string>
    </property>
   </widget>
-  <widget class="QFrame" name="frameRightHand">
-   <property name="enabled">
-    <bool>true</bool>
-   </property>
-   <property name="geometry">
-    <rect>
-     <x>330</x>
-     <y>66</y>
-     <width>230</width>
-     <height>321</height>
-    </rect>
-   </property>
-   <property name="frameShape">
-    <enum>QFrame::StyledPanel</enum>
-   </property>
-   <property name="frameShadow">
-    <enum>QFrame::Raised</enum>
-   </property>
-  </widget>
   <widget class="Line" name="line">
    <property name="geometry">
     <rect>
@@ -152,25 +133,6 @@
     <enum>Qt::Vertical</enum>
    </property>
   </widget>
-  <widget class="QPushButton" name="buttonSetRightHandJointAngles">
-   <property name="geometry">
-    <rect>
-     <x>570</x>
-     <y>215</y>
-     <width>50</width>
-     <height>25</height>
-    </rect>
-   </property>
-   <property name="maximumSize">
-    <size>
-     <width>50</width>
-     <height>16777215</height>
-    </size>
-   </property>
-   <property name="text">
-    <string>Set</string>
-   </property>
-  </widget>
   <widget class="QPushButton" name="buttonCloseLeftHand">
    <property name="geometry">
     <rect>
@@ -367,6 +329,91 @@
     <enum>Qt::Horizontal</enum>
    </property>
   </widget>
+  <widget class="QLCDNumber" name="lcdNumberRightHandJointValue">
+   <property name="enabled">
+    <bool>true</bool>
+   </property>
+   <property name="geometry">
+    <rect>
+     <x>330</x>
+     <y>117</y>
+     <width>64</width>
+     <height>26</height>
+    </rect>
+   </property>
+   <property name="maximumSize">
+    <size>
+     <width>80</width>
+     <height>100</height>
+    </size>
+   </property>
+   <property name="toolTip">
+    <string/>
+   </property>
+   <property name="smallDecimalPoint">
+    <bool>false</bool>
+   </property>
+  </widget>
+  <widget class="QLabel" name="labelSelectRightHandJoint">
+   <property name="geometry">
+    <rect>
+     <x>330</x>
+     <y>80</y>
+     <width>81</width>
+     <height>17</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>Select Joint</string>
+   </property>
+  </widget>
+  <widget class="QFrame" name="frameRightHand">
+   <property name="geometry">
+    <rect>
+     <x>329</x>
+     <y>156</y>
+     <width>301</width>
+     <height>231</height>
+    </rect>
+   </property>
+   <property name="frameShape">
+    <enum>QFrame::StyledPanel</enum>
+   </property>
+   <property name="frameShadow">
+    <enum>QFrame::Raised</enum>
+   </property>
+   <layout class="QGridLayout" name="gridLayout_4">
+    <item row="0" column="0">
+     <widget class="QTableWidget" name="tableWidgetRightHand"/>
+    </item>
+   </layout>
+  </widget>
+  <widget class="QComboBox" name="comboRightHandJoints">
+   <property name="geometry">
+    <rect>
+     <x>420</x>
+     <y>75</y>
+     <width>150</width>
+     <height>25</height>
+    </rect>
+   </property>
+  </widget>
+  <widget class="QSlider" name="horizontalSliderRightHandJointPos">
+   <property name="geometry">
+    <rect>
+     <x>410</x>
+     <y>120</y>
+     <width>221</width>
+     <height>16</height>
+    </rect>
+   </property>
+   <property name="maximum">
+    <number>100</number>
+   </property>
+   <property name="orientation">
+    <enum>Qt::Horizontal</enum>
+   </property>
+  </widget>
  </widget>
  <resources/>
  <connections/>
-- 
GitLab