From 06ea75ec8c41dc37537b173cb4df424bda92712e Mon Sep 17 00:00:00 2001
From: roangel <roangel@student.ethz.ch>
Date: Tue, 23 May 2017 11:18:09 +0200
Subject: [PATCH] Added linker object that will link CFZones to CFs

---
 pps_ws/src/d_fall_pps/CMakeLists.txt          |  1 +
 .../GUI_Qt/CrazyFlyGUI/include/CFLinker.h     | 30 +++++++++++
 .../GUI_Qt/CrazyFlyGUI/include/crazyFly.h     |  9 ++++
 .../GUI_Qt/CrazyFlyGUI/include/crazyFlyZone.h | 11 ++++
 .../CrazyFlyGUI/include/mainguiwindow.h       |  4 ++
 .../GUI_Qt/CrazyFlyGUI/src/CFLinker.cpp       | 52 +++++++++++++++++++
 .../GUI_Qt/CrazyFlyGUI/src/crazyFly.cpp       | 21 ++++++++
 .../GUI_Qt/CrazyFlyGUI/src/crazyFlyZone.cpp   | 25 +++++++++
 .../GUI_Qt/CrazyFlyGUI/src/mainguiwindow.cpp  | 23 ++++----
 9 files changed, 162 insertions(+), 14 deletions(-)
 create mode 100644 pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/CFLinker.h
 create mode 100644 pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/CFLinker.cpp

diff --git a/pps_ws/src/d_fall_pps/CMakeLists.txt b/pps_ws/src/d_fall_pps/CMakeLists.txt
index 3dbceb99..0ed2fc87 100755
--- a/pps_ws/src/d_fall_pps/CMakeLists.txt
+++ b/pps_ws/src/d_fall_pps/CMakeLists.txt
@@ -233,6 +233,7 @@ set(MY_CPP_SOURCES              # compilation of sources
     ${MY_LIB_PATH_SRC}/marker.cpp
     ${MY_LIB_PATH_SRC}/rosNodeThread.cpp
     ${MY_LIB_PATH_SRC}/crazyFly.cpp
+    ${MY_LIB_PATH_SRC}/CFLinker.cpp
     )
 
 
diff --git a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/CFLinker.h b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/CFLinker.h
new file mode 100644
index 00000000..9e8b533e
--- /dev/null
+++ b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/CFLinker.h
@@ -0,0 +1,30 @@
+#ifndef CFLINKER_H
+#define CFLINKER_H
+
+#include "globalDefinitions.h"
+#include "crazyFly.h"
+#include "crazyFlyZone.h"
+
+class CFLinker
+{
+public:
+    explicit CFLinker();
+    ~CFLinker();
+
+    void link(crazyFly* crazyfly, crazyFlyZone* crazyfly_zone);
+    void link(crazyFlyZone* crazyfly_zone, crazyFly* crazyfly);
+
+    void unlink(crazyFly* crazyfly,  crazyFlyZone* crazyfly_zone);
+
+private:
+
+    struct link {
+        int cf_zone_index;
+        std::string cf_name;
+    };
+
+    std::vector<struct link> links;
+};
+
+
+#endif
diff --git a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/crazyFly.h b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/crazyFly.h
index ea580131..23804e19 100644
--- a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/crazyFly.h
+++ b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/crazyFly.h
@@ -34,6 +34,11 @@ public:
 
     void setScaleCFs(double scale);
 
+    // linking stuff
+    void assignCFZone(int cf_zone_index);
+    void removeAssigned();
+    bool isAssigned();
+
 private:
 
     // info to fill by message
@@ -49,6 +54,10 @@ private:
     // info for plotting CF
     qreal m_width;
     qreal m_height;
+
+    // linking stuff
+    bool m_assigned;
+    int m_assigned_cf_zone_index;
 };
 
 
diff --git a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/crazyFlyZone.h b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/crazyFlyZone.h
index f1f230d4..7c86ef1c 100755
--- a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/crazyFlyZone.h
+++ b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/crazyFlyZone.h
@@ -10,6 +10,7 @@ class crazyFlyZone : public myGraphicsRectItem
 {
 public:
     explicit crazyFlyZone(const QRectF & rect, int index, QGraphicsItem * parent = 0);
+    ~crazyFlyZone();
 
     int getIndex();
     void setIndex(int index);
@@ -17,11 +18,21 @@ public:
     void setLabelPosition();
     void updateLabel(QString string);
     void rectSizeChanged();
+
+    // stuff for linking
+    void linkCF(std::string cf_name);
+    bool isLinked();
+    void removeLink();
+
 protected:
 
 private:
     int _index;
     QGraphicsSimpleTextItem* label;
+
+    // stuff for linking
+    bool m_linked;
+    std::string m_crazyfly_linked_name; //in the future this will be a vector of crazyFlies maybe
 };
 
 
diff --git a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/mainguiwindow.h b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/mainguiwindow.h
index 9948bc66..a2f61b2d 100755
--- a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/mainguiwindow.h
+++ b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/mainguiwindow.h
@@ -12,8 +12,10 @@
 #include "rosNodeThread.h"
 #include "marker.h"
 #include "crazyFly.h"
+#include "CFLinker.h"
 #endif
 
+
 #include "ui_mainguiwindow.h"
 #include "myGraphicsScene.h"
 #include "globalDefinitions.h"
@@ -110,10 +112,12 @@ private:
     myGraphicsScene* scene;
     void _init();
 
+
     #ifdef CATKIN_MAKE
     rosNodeThread* _rosNodeThread;
     std::vector<Marker*> markers_vector;
     std::vector<crazyFly*> crazyflies_vector;
+    CFLinker cf_linker;
     #endif
 };
 
diff --git a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/CFLinker.cpp b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/CFLinker.cpp
new file mode 100644
index 00000000..3e25a9f9
--- /dev/null
+++ b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/CFLinker.cpp
@@ -0,0 +1,52 @@
+#include "CFLinker.h"
+
+
+CFLinker::CFLinker()
+{
+}
+
+CFLinker::~CFLinker()
+{
+}
+
+void CFLinker::link(crazyFly* crazyfly, crazyFlyZone* crazyfly_zone)
+{
+    struct link tmp_link;
+
+    tmp_link.cf_zone_index = crazyfly_zone->getIndex();
+    tmp_link.cf_name = crazyfly->getName();
+
+    crazyfly_zone->linkCF(tmp_link.cf_name);
+    crazyfly->assignCFZone(tmp_link.cf_zone_index);
+
+    links.push_back(tmp_link);
+}
+
+void CFLinker::link(crazyFlyZone* crazyfly_zone, crazyFly* crazyfly)
+{
+    link(crazyfly, crazyfly_zone);
+}
+
+void CFLinker::unlink(crazyFly* crazyfly, crazyFlyZone* crazyfly_zone)
+{
+    bool found = false;
+    int index_found;
+    for(int i = 0; i < links.size(); i++)
+    {
+        if(links[i].cf_zone_index == crazyfly_zone->getIndex())
+        {
+            if(crazyfly->getName() == links[i].cf_name)
+            {
+                found = true;
+                index_found = i;
+            }
+        }
+    }
+
+    if(found)
+    {
+        crazyfly_zone->removeLink();
+        crazyfly->removeAssigned();
+        links.erase(links.begin() + index_found);
+    }
+}
diff --git a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/crazyFly.cpp b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/crazyFly.cpp
index 5c14c8af..9b5f961b 100644
--- a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/crazyFly.cpp
+++ b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/crazyFly.cpp
@@ -10,6 +10,7 @@ crazyFly::crazyFly(const CrazyflieData* p_crazyfly_msg, QGraphicsItem * parent)
     updateCF(p_crazyfly_msg);
     m_width = DRONE_WIDTH;
     m_height = DRONE_HEIGHT;
+    m_assigned = false;
 }
 
 crazyFly::~crazyFly()
@@ -54,3 +55,23 @@ void crazyFly::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
 {
     this->renderer()->render(painter,this->boundingRect());
 }
+
+void crazyFly::assignCFZone(int cf_zone_index)
+{
+    m_assigned = true;
+    m_assigned_cf_zone_index = cf_zone_index;
+}
+
+void crazyFly::removeAssigned()
+{
+    if(m_assigned)
+    {
+        m_assigned = false;
+        m_assigned_cf_zone_index = -1;
+    }
+}
+
+bool crazyFly::isAssigned()
+{
+    return m_assigned;
+}
diff --git a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/crazyFlyZone.cpp b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/crazyFlyZone.cpp
index 81a01df3..4d8c7491 100755
--- a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/crazyFlyZone.cpp
+++ b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/crazyFlyZone.cpp
@@ -6,6 +6,11 @@ crazyFlyZone::crazyFlyZone(const QRectF & rect, int index,  QGraphicsItem * pare
 {
     this->setPen(QPen(Qt::black, 0));
     setIndex(index);
+    m_linked = false;
+}
+
+crazyFlyZone::~crazyFlyZone()
+{
 }
 
 void crazyFlyZone::updateLabel(QString string)
@@ -45,3 +50,23 @@ void crazyFlyZone::rectSizeChanged() // pure virtual coming from parent
 {
     setLabelPosition();
 }
+
+void crazyFlyZone::linkCF(std::string cf_name)
+{
+    m_crazyfly_linked_name = cf_name;
+    m_linked = true;
+}
+
+bool crazyFlyZone::isLinked()
+{
+    return m_linked;
+}
+
+void crazyFlyZone::removeLink()
+{
+    if(m_linked)
+    {
+        m_crazyfly_linked_name = "";
+        m_linked = false;
+    }
+}
diff --git a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/mainguiwindow.cpp b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/mainguiwindow.cpp
index 2fe743f5..8a951077 100755
--- a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/mainguiwindow.cpp
+++ b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/mainguiwindow.cpp
@@ -23,27 +23,19 @@
 using namespace d_fall_pps;
 #endif
 
-#ifdef CATKIN_MAKE
-MainGUIWindow::MainGUIWindow(int argc, char **argv, QWidget *parent) :
-    QMainWindow(parent),
-    ui(new Ui::MainGUIWindow)//,
-    // _rosNodeThread(argc, argv, "/ViconDataPublisher/ViconData")
-{
-    _rosNodeThread = new rosNodeThread(argc, argv, "/ViconDataPublisher/ViconData");
-
-    ui->setupUi(this);
-    _init();
-}
-#else
 MainGUIWindow::MainGUIWindow(int argc, char **argv, QWidget *parent) :
     QMainWindow(parent),
     ui(new Ui::MainGUIWindow)
+    #ifdef CATKIN_MAKE
+    ,cf_linker()
+    #endif
 {
-
+    #ifdef CATKIN_MAKE
+    _rosNodeThread = new rosNodeThread(argc, argv, "/ViconDataPublisher/ViconData");
+    #endif
     ui->setupUi(this);
     _init();
 }
-#endif
 
 
 MainGUIWindow::~MainGUIWindow()
@@ -100,11 +92,14 @@ void MainGUIWindow::_init()
     ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
     ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
 
+
+    // scene
     scene = new myGraphicsScene(ui->frame_drawing);
     scene->setSceneRect(-100 * FROM_METERS_TO_UNITS, -100 * FROM_METERS_TO_UNITS, 200 * FROM_METERS_TO_UNITS, 200 * FROM_METERS_TO_UNITS);
 
     ui->graphicsView->setScene(scene);
 
+    // connections
     QObject::connect(ui->tabWidget, SIGNAL(tabCloseRequested(int)), scene, SLOT(removeCrazyFlyZone(int)));
     QObject::connect(scene, SIGNAL(numCrazyFlyZonesChanged(int)), this, SLOT(doNumCrazyFlyZonesChanged(int)));
     QObject::connect(ui->tabWidget, SIGNAL(currentChanged(int)), scene, SLOT(setSelectedCrazyFlyZone(int)));
-- 
GitLab