From fafe110c1c364792b10073ce489646959ed8a62b Mon Sep 17 00:00:00 2001
From: "simon.fanetti" <simon.fanetti@etu.hesge.ch>
Date: Fri, 22 May 2020 19:43:44 +0200
Subject: [PATCH] show frame to edit cropping mask

---
 app/SandboxSetup/SandboxSetup.pro | 10 ++--
 app/SandboxSetup/croppingmask.cpp | 71 +++++----------------------
 app/SandboxSetup/croppingmask.h   | 13 ++---
 app/SandboxSetup/croppingmask.ui  | 76 ++++++++++++-----------------
 app/SandboxSetup/maskedit.cpp     | 79 +++++++++++++++++++++++++++++++
 app/SandboxSetup/maskedit.h       | 34 +++++++++++++
 app/SandboxSetup/maskedit.ui      | 50 +++++++++++++++++++
 7 files changed, 215 insertions(+), 118 deletions(-)
 create mode 100644 app/SandboxSetup/maskedit.cpp
 create mode 100644 app/SandboxSetup/maskedit.h
 create mode 100644 app/SandboxSetup/maskedit.ui

diff --git a/app/SandboxSetup/SandboxSetup.pro b/app/SandboxSetup/SandboxSetup.pro
index b9aa084..adb2321 100644
--- a/app/SandboxSetup/SandboxSetup.pro
+++ b/app/SandboxSetup/SandboxSetup.pro
@@ -5,6 +5,7 @@
 #-------------------------------------------------
 
 QT       += core gui
+QT       += uitools
 
 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 
@@ -27,17 +28,20 @@ SOURCES += \
         main.cpp \
     monitorgui.cpp \
     camerafocus.cpp \
-    croppingmask.cpp
+    croppingmask.cpp \
+    maskedit.cpp
 
 HEADERS += \
     monitorgui.h \
     camerafocus.h \
-    croppingmask.h
+    croppingmask.h \
+    maskedit.h
 
 FORMS += \
     monitorgui.ui \
     camerafocus.ui \
-    croppingmask.ui
+    croppingmask.ui \
+    maskedit.ui
 
 
 
diff --git a/app/SandboxSetup/croppingmask.cpp b/app/SandboxSetup/croppingmask.cpp
index b76acca..da305cd 100644
--- a/app/SandboxSetup/croppingmask.cpp
+++ b/app/SandboxSetup/croppingmask.cpp
@@ -5,8 +5,11 @@ CroppingMask::CroppingMask(SandboxSetup *sandbox, QWidget *parent) :
     QDialog(parent),
     ui(new Ui::CroppingMask)
 {
-    ui->setupUi(this);
+
     setup = sandbox;
+    ui->setupUi(this);
+    maskEdit = new MaskEdit(ui->frame->geometry());
+    ui->vLayout->addWidget(maskEdit, 1);
     // TODO : try to load cropping mask from file
 
 }
@@ -16,40 +19,30 @@ CroppingMask::~CroppingMask()
     delete ui;
 }
 
-void CroppingMask::paintEvent(QPaintEvent *){
-    QPainter p;
-    p.begin(this);
-    p.end();
-}
-
 void CroppingMask::on_btnTakePicture_clicked()
 {
     setup->captureBlueScreen();
     cv::Mat rgb = setup->camera.getRGBFrame();
     cameraRGBFrame = rgb;
 
-    QImage img = QImage((uchar *)rgb.data, (int)rgb.cols, (int)rgb.rows, static_cast<int>(rgb.step.buf[0]), QImage::Format_RGB888);
-    QPixmap image = QPixmap::fromImage(img);
-
-    //ui->lblFrame->setPixmap(image);
-
     // no config found
-    if(!maskValideInFrame(rgb)){
-        float y = ui->lblMask->height();
-        float x = ui->lblMask->width();
+    if(!maskValideInFrame(&rgb)){
+        float y = rgb.size().height;
+        float x = rgb.size().width;
         rectPoints = std::vector<cv::Point>{ cv::Point(1.0/4*x, 1.0/4*y), cv::Point(1.0/4*x, 3.0/4*y), cv::Point(3.0/4*x, 3.0/4*y), cv::Point(3.0/4*x, 1.0/4*y) };
     }
-    drawMask(rectPoints);
+
+    maskEdit->updateFrame(&cameraRGBFrame, &rectPoints);
 }
 
-bool CroppingMask::maskValideInFrame(cv::Mat rgb){
+bool CroppingMask::maskValideInFrame(cv::Mat *rgb){
 
     if(rectPoints.empty())
         return false;
 
     for(uint i=0; i<rectPoints.size(); i++){
-        if( !(( 0<=rectPoints[i].x && rectPoints[i].x<rgb.size().width ) &&
-            ( 0<=rectPoints[i].y && rectPoints[i].y<rgb.size().height ) ))
+        if( !(( 0<=rectPoints[i].x && rectPoints[i].x<rgb->size().width ) &&
+            ( 0<=rectPoints[i].y && rectPoints[i].y<rgb->size().height ) ))
             {
             return false;
         }
@@ -58,47 +51,7 @@ bool CroppingMask::maskValideInFrame(cv::Mat rgb){
     return true;
 }
 
-void CroppingMask::drawMask(std::vector<cv::Point> rectPoints){
-
-    QPixmap img;
-    QPainter maskPainter(&img);
-    QPolygon poly;
-
-    for(uint i=0; i<rectPoints.size(); i++){
-        poly << QPoint(rectPoints[i].x, rectPoints[i].y);
-    }
-
-    // QPen: style(), width(), brush(), capStyle() and joinStyle().
-    QPen pen(Qt::green, 2, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin);
-    maskPainter.setPen(pen);
-    maskPainter.drawPolygon(poly);
-
-    maskPainter.drawRect(15,15,100,100);
-
-
-    QPixmap px;
-    QPainter p(&px);
-    p.setPen(Qt::blue);
-    p.drawLine(5,5, 40, 40);
-    p.end();
-
-    ui->lblMask->setPixmap(px);
-}
-
-void CroppingMask::scaleMaskWindowToFrame(){
-
-    float xCoeff = cameraRGBFrame.size().width / ui->lblMask->width();
-    float yCoeff = cameraRGBFrame.size().height / ui->lblMask->height();
-
-    for(uint i=0; i<rectPoints.size(); i++){
-        rectPoints[i].x = rectPoints[i].x * xCoeff;
-        rectPoints[i].y = rectPoints[i].y * yCoeff;
-    }
-}
-
 void CroppingMask::on_btnbxEnd_accepted()
 {
     state = true;
-    // Needed because the image shown is stretched
-    scaleMaskWindowToFrame();
 }
diff --git a/app/SandboxSetup/croppingmask.h b/app/SandboxSetup/croppingmask.h
index b582cbc..9c3bb00 100644
--- a/app/SandboxSetup/croppingmask.h
+++ b/app/SandboxSetup/croppingmask.h
@@ -3,10 +3,7 @@
 
 #include <QDialog>
 #include <sandboxSetup.h>
-
-#include <QPainter>
-#include <QPolygon>
-//#include <QPainterPath>
+#include "maskedit.h"
 
 namespace Ui {
 class CroppingMask;
@@ -32,13 +29,9 @@ private:
     std::vector<cv::Point> rectPoints;
     bool state = false;
     cv::Mat cameraRGBFrame;
+    MaskEdit *maskEdit;
 
-    bool maskValideInFrame(cv::Mat rgb);
-    void drawMask(std::vector<cv::Point> rectPoints);
-    void scaleMaskWindowToFrame();
-
-protected:
-    void paintEvent(QPaintEvent* event);
+    bool maskValideInFrame(cv::Mat *rgb);
 
 };
 
diff --git a/app/SandboxSetup/croppingmask.ui b/app/SandboxSetup/croppingmask.ui
index c206a8c..c4b6dbc 100644
--- a/app/SandboxSetup/croppingmask.ui
+++ b/app/SandboxSetup/croppingmask.ui
@@ -7,7 +7,7 @@
     <x>0</x>
     <y>0</y>
     <width>635</width>
-    <height>422</height>
+    <height>442</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -20,7 +20,7 @@
    <property name="geometry">
     <rect>
      <x>270</x>
-     <y>370</y>
+     <y>380</y>
      <width>341</width>
      <height>32</height>
     </rect>
@@ -32,35 +32,10 @@
     <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
    </property>
   </widget>
-  <widget class="QLabel" name="lblFrame">
-   <property name="geometry">
-    <rect>
-     <x>40</x>
-     <y>30</y>
-     <width>441</width>
-     <height>281</height>
-    </rect>
-   </property>
-   <property name="lineWidth">
-    <number>3</number>
-   </property>
-   <property name="text">
-    <string>Press &quot;Take Picture&quot; when your beamer is ready</string>
-   </property>
-   <property name="scaledContents">
-    <bool>true</bool>
-   </property>
-   <property name="alignment">
-    <set>Qt::AlignCenter</set>
-   </property>
-   <property name="wordWrap">
-    <bool>false</bool>
-   </property>
-  </widget>
   <widget class="QPushButton" name="btnTakePicture">
    <property name="geometry">
     <rect>
-     <x>520</x>
+     <x>510</x>
      <y>60</y>
      <width>101</width>
      <height>25</height>
@@ -70,31 +45,40 @@
     <string>Take Picture</string>
    </property>
   </widget>
-  <widget class="QLabel" name="lblMask">
+  <widget class="QFrame" name="frame">
    <property name="geometry">
     <rect>
      <x>40</x>
-     <y>30</y>
+     <y>40</y>
      <width>441</width>
-     <height>281</height>
+     <height>301</height>
     </rect>
    </property>
-   <property name="lineWidth">
-    <number>3</number>
-   </property>
-   <property name="text">
-    <string/>
-   </property>
-   <property name="scaledContents">
-    <bool>true</bool>
-   </property>
-   <property name="alignment">
-    <set>Qt::AlignCenter</set>
-   </property>
-   <property name="wordWrap">
-    <bool>false</bool>
-   </property>
+   <property name="frameShape">
+    <enum>QFrame::StyledPanel</enum>
+   </property>
+   <property name="frameShadow">
+    <enum>QFrame::Raised</enum>
+   </property>
+   <widget class="QWidget" name="verticalLayoutWidget">
+    <property name="geometry">
+     <rect>
+      <x>0</x>
+      <y>0</y>
+      <width>441</width>
+      <height>301</height>
+     </rect>
+    </property>
+    <layout class="QVBoxLayout" name="vLayout">
+     <property name="sizeConstraint">
+      <enum>QLayout::SetDefaultConstraint</enum>
+     </property>
+    </layout>
+   </widget>
   </widget>
+  <zorder>frame</zorder>
+  <zorder>btnbxEnd</zorder>
+  <zorder>btnTakePicture</zorder>
  </widget>
  <resources/>
  <connections>
diff --git a/app/SandboxSetup/maskedit.cpp b/app/SandboxSetup/maskedit.cpp
new file mode 100644
index 0000000..d5392ad
--- /dev/null
+++ b/app/SandboxSetup/maskedit.cpp
@@ -0,0 +1,79 @@
+#include "maskedit.h"
+#include "ui_maskedit.h"
+
+MaskEdit::MaskEdit(const QRect geo, QWidget *parent) :
+    QFrame(parent),
+    ui(new Ui::MaskEdit)
+{
+    ui->setupUi(this);
+    setGeometry(0,0, geo.width(), geo.height());
+    ui->lblFrame->setGeometry(0,0, width(), height());
+}
+
+MaskEdit::~MaskEdit()
+{
+    delete ui;
+}
+
+
+void MaskEdit::updateFrame(cv::Mat *frame, std::vector<cv::Point> *points){
+    capture = frame;
+    rectPoints = points;
+    update();
+}
+
+void MaskEdit::updateFrame(cv::Mat *frame){
+    capture = frame;
+    update();
+}
+
+void MaskEdit::updateFrame(std::vector<cv::Point> *points){
+    rectPoints = points;
+    update();
+}
+
+
+
+void MaskEdit::paintEvent(QPaintEvent *){
+
+    if(capture != nullptr){
+
+        QImage img = QImage((uchar *)capture->data, (int)capture->cols, (int)capture->rows, static_cast<int>(capture->step.buf[0]), QImage::Format_RGB888);
+        QPixmap px = QPixmap::fromImage(img);
+        QPainter maskPainter(&px);
+
+        // QPen: style(), width(), brush(), capStyle() and joinStyle().
+        QPen pen(Qt::green, 2, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin);
+        maskPainter.setPen(pen);
+
+        QPolygon poly;
+        int size = 10;
+
+        for(uint i=0; i<rectPoints->size(); i++){
+
+            int x = rectPoints->at(i).x;
+            int y = rectPoints->at(i).y;
+            poly << QPoint(x, y);
+            maskPainter.drawEllipse(x-(size/2), y-(size/2), size, size);
+        }
+
+        maskPainter.drawPolygon(poly);
+        maskPainter.end();
+
+        ui->lblFrame->setPixmap(px);
+    }
+}
+
+/*
+void CroppingMask::scaleMaskWindowToFrame(){
+
+    float xCoeff = cameraRGBFrame.size().width / ui->vLayout->width();
+    float yCoeff = cameraRGBFrame.size().height / ui->vLayout->height();
+
+    for(uint i=0; i<rectPoints.size(); i++){
+        rectPoints[i].x = rectPoints[i].x * xCoeff;
+        rectPoints[i].y = rectPoints[i].y * yCoeff;
+    }
+
+}
+*/
diff --git a/app/SandboxSetup/maskedit.h b/app/SandboxSetup/maskedit.h
new file mode 100644
index 0000000..ee6ce86
--- /dev/null
+++ b/app/SandboxSetup/maskedit.h
@@ -0,0 +1,34 @@
+#ifndef MASKEDIT_H
+#define MASKEDIT_H
+
+#include <QPen>
+#include <QPainter>
+#include <QFrame>
+#include <opencv2/opencv.hpp>
+
+namespace Ui {
+class MaskEdit;
+}
+
+class MaskEdit : public QFrame
+{
+    Q_OBJECT
+
+public:
+    explicit MaskEdit(const QRect geo, QWidget *parent = 0);
+    ~MaskEdit();
+    void updateFrame(cv::Mat *frame, std::vector<cv::Point> *points);
+    void updateFrame(cv::Mat *frame);
+    void updateFrame(std::vector<cv::Point> *points);
+
+private:
+    Ui::MaskEdit *ui;
+    cv::Mat *capture = nullptr;
+    std::vector<cv::Point> *rectPoints = nullptr;
+
+protected:
+    void paintEvent(QPaintEvent* event);
+
+};
+
+#endif // MASKEDIT_H
diff --git a/app/SandboxSetup/maskedit.ui b/app/SandboxSetup/maskedit.ui
new file mode 100644
index 0000000..f070dbf
--- /dev/null
+++ b/app/SandboxSetup/maskedit.ui
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MaskEdit</class>
+ <widget class="QFrame" name="MaskEdit">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>488</width>
+    <height>349</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Frame</string>
+  </property>
+  <property name="frameShape">
+   <enum>QFrame::StyledPanel</enum>
+  </property>
+  <property name="frameShadow">
+   <enum>QFrame::Raised</enum>
+  </property>
+  <widget class="QLabel" name="lblFrame">
+   <property name="geometry">
+    <rect>
+     <x>20</x>
+     <y>30</y>
+     <width>441</width>
+     <height>281</height>
+    </rect>
+   </property>
+   <property name="lineWidth">
+    <number>3</number>
+   </property>
+   <property name="text">
+    <string>Press &quot;Take Picture&quot; when your beamer is ready</string>
+   </property>
+   <property name="scaledContents">
+    <bool>true</bool>
+   </property>
+   <property name="alignment">
+    <set>Qt::AlignCenter</set>
+   </property>
+   <property name="wordWrap">
+    <bool>false</bool>
+   </property>
+  </widget>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>
-- 
GitLab