From e14bfb7ea1e805ca24e5bfe2e89b520964203a20 Mon Sep 17 00:00:00 2001
From: roangel <roangel@student.ethz.ch>
Date: Tue, 30 May 2017 16:11:16 +0200
Subject: [PATCH] now we are creating cf zones whenever they are in database
 and not in GUI

---
 .../CrazyFlyGUI/include/myGraphicsScene.h     |  2 ++
 .../GUI_Qt/CrazyFlyGUI/src/mainguiwindow.cpp  | 26 +++++++++++++++++++
 .../CrazyFlyGUI/src/myGraphicsScene.cpp       | 12 ++++++++-
 3 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/myGraphicsScene.h b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/myGraphicsScene.h
index c9691a58..b1e5f4da 100755
--- a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/myGraphicsScene.h
+++ b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/include/myGraphicsScene.h
@@ -44,6 +44,8 @@ public:
 
     void removeTable();
 
+    void addCFZone(QRectF rect, int index);
+
 public slots:
     void removeCrazyFlyZone(int cf_zone_index);
     void setSelectedCrazyFlyZone(int index);
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 1073d852..66fa3044 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
@@ -712,6 +712,32 @@ void MainGUIWindow::on_load_from_DB_button_clicked()
             std::string cf_name = m_data_base.crazyflieEntries[i].crazyflieContext.crazyflieName;
             int cf_zone_index = m_data_base.crazyflieEntries[i].crazyflieContext.localArea.crazyfly_zone_index;
             // we should first create the cf zones that are in the database?
+            bool cf_zone_exists;
+            qreal width = m_data_base.crazyflieEntries[i].crazyflieContext.localArea.xmax - m_data_base.crazyflieEntries[i].crazyflieContext.localArea.xmin;
+            qreal height = m_data_base.crazyflieEntries[i].crazyflieContext.localArea.ymax - m_data_base.crazyflieEntries[i].crazyflieContext.localArea.ymin;
+            QRectF tmp_rect(m_data_base.crazyflieEntries[i].crazyflieContext.localArea.xmin,
+                            m_data_base.crazyflieEntries[i].crazyflieContext.localArea.ymax,
+                            width,
+                            height);
+            int found_j;
+            for(int j = 0; j < scene->crazyfly_zones.size(); j++)
+            {
+                if(cf_zone_index == scene->crazyfly_zones[j]->getIndex())
+                {
+                    cf_zone_exists = true;
+                    found_j = j;
+                    break;
+                }
+            }
+            if(!cf_zone_exists)
+            {
+                scene->addCFZone(tmp_rect, cf_zone_index);
+            }
+            else
+            {
+                scene->crazyfly_zones[found_j]->setRect(tmp_rect);
+                scene->crazyfly_zones[found_j]->rectSizeChanged();
+            }
             int student_id = m_data_base.crazyflieEntries[i].studentID;
             cf_linker->link(student_id, cf_zone_index, cf_name);
         }
diff --git a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/myGraphicsScene.cpp b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/myGraphicsScene.cpp
index 81c0ad31..f92e0180 100755
--- a/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/myGraphicsScene.cpp
+++ b/pps_ws/src/d_fall_pps/GUI_Qt/CrazyFlyGUI/src/myGraphicsScene.cpp
@@ -95,6 +95,17 @@ int myGraphicsScene::checkSelectedCrazyFlyZone()
     return -1;
 }
 
+void myGraphicsScene::addCFZone(QRectF rect, int index)
+{
+    tmp_crazyfly_zone_item = new crazyFlyZone(rect, index);
+    addItem(tmp_crazyfly_zone_item);
+    tmp_crazyfly_zone_item->setRect(tmp_crazyfly_zone_item->rect().normalized());
+    addCrazyFlyZoneToVector(tmp_crazyfly_zone_item);
+    std::string str = std::to_string(tmp_crazyfly_zone_item->getIndex() + 1);
+    tmp_crazyfly_zone_item->setLabel(str.c_str());
+    setSelectedCrazyFlyZone(crazyfly_zones.size() - 1); //select just created rectangle
+}
+
 void myGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
 {
     if (mouseEvent->button() != Qt::LeftButton)
@@ -413,7 +424,6 @@ void myGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
                 {
                     tmp_crazyfly_zone_item->setRect(tmp_crazyfly_zone_item->rect().normalized());
                     addCrazyFlyZoneToVector(tmp_crazyfly_zone_item);
-                    // TODO: set first available number, not size of vector. Index + 1
                     std::string str = std::to_string(tmp_crazyfly_zone_item->getIndex() + 1);
                     tmp_crazyfly_zone_item->setLabel(str.c_str());
                     setSelectedCrazyFlyZone(crazyfly_zones.size() - 1); //select just created rectangle
-- 
GitLab