From e1232533d245dae3c5578e2997b5b6f9dad04c6d Mon Sep 17 00:00:00 2001
From: "Kilic, Deniz" <d.kilic@fz-juelich.de>
Date: Thu, 10 Sep 2020 09:35:31 +0200
Subject: [PATCH] - Using helper function to find mouse position - Using modern
 connect syntax for color picker

---
 include/control.h |  6 +++---
 include/view.h    |  4 ++--
 src/control.cpp   | 35 ++++++++++++++++-------------------
 src/view.cpp      |  4 ++--
 4 files changed, 23 insertions(+), 26 deletions(-)

diff --git a/include/control.h b/include/control.h
index 58e3e1149..02f846f40 100644
--- a/include/control.h
+++ b/include/control.h
@@ -172,7 +172,7 @@ public:
 
     void expandRange(QColor& fromColor, QColor& toColor, const std::array<int, 3>& clickedColor);
     void saveChange(const QColor& fromColor, const QColor& toColor, RectPlotItem* map);
-    bool getColors(QPoint& p, GraphicsView* graphicsView, std::array<int, 3>& clickedColor, QColor& toColor, QColor& fromColor, RectPlotItem*& map);
+    bool getColors(std::array<int, 3>& clickedColor, QColor& toColor, QColor& fromColor, RectPlotItem*& map);
 
     void setXml(QDomElement &elem);
     void getXml(QDomElement &elem);
@@ -243,8 +243,8 @@ private slots:
     void on_mapResetHeight_clicked();
     void on_mapResetPos_clicked();
     void on_mapDefaultHeight_valueChanged(double d);
-    void on_expandColor(QPoint p, GraphicsView* graphicsView);
-    void on_setColor(QPoint, GraphicsView*);
+    void on_expandColor();
+    void on_setColor();
 
 
     void on_trackShow_stateChanged(int i);
diff --git a/include/view.h b/include/view.h
index 43d9df8ec..83edda064 100644
--- a/include/view.h
+++ b/include/view.h
@@ -37,8 +37,8 @@ signals:
     void mouseRightDoubleClick(QPointF pos, int direction);
     void mouseMiddleDoubleClick(int direction);
     void mouseShiftWheel(int delta);
-    void colorSelected(QPoint p, GraphicsView* graphicsView);
-    void setColorEvent(QPoint p, GraphicsView* graphicsView);
+    void colorSelected();
+    void setColorEvent();
     //void mouseRightClick(QPointF pos);
 };
 
diff --git a/src/control.cpp b/src/control.cpp
index 92f338f3f..197435dce 100644
--- a/src/control.cpp
+++ b/src/control.cpp
@@ -3201,13 +3201,14 @@ void Control::getXml(QDomElement &elem)
 void Control::on_colorPickerButton_clicked(bool checked)
 {
     //true wenn neu gechecked, false wenn wieder abgewählt
+    GraphicsView *view = mMainWindow->getView();
     if(checked)
     {
-        connect(mMainWindow->getView(), SIGNAL(colorSelected(QPoint, GraphicsView*)), this, SLOT(on_expandColor(QPoint, GraphicsView*)));
-        connect(mMainWindow->getView(), SIGNAL(setColorEvent(QPoint, GraphicsView*)), this, SLOT(on_setColor(QPoint, GraphicsView*)));
+        connect(view, &GraphicsView::colorSelected, this, &Control::on_expandColor);
+        connect(view, &GraphicsView::setColorEvent, this, &Control::on_setColor);
     }else{
-        disconnect(mMainWindow->getView(), SIGNAL(colorSelected(QPoint, GraphicsView*)), this, SLOT(on_expandColor(QPoint, GraphicsView*)));
-        disconnect(mMainWindow->getView(), SIGNAL(setColorEvent(QPoint, GraphicsView*)), this, SLOT(on_setColor(QPoint, GraphicsView*)));
+        disconnect(view, &GraphicsView::colorSelected, this, &Control::on_expandColor);
+        disconnect(view, &GraphicsView::setColorEvent, this, &Control::on_setColor);
     }
 }
 
@@ -3223,30 +3224,25 @@ void Control::on_colorPickerButton_clicked(bool checked)
  *
  * @return Boolean describing, if a color was retrieved
  */
-bool Control::getColors(QPoint& p, GraphicsView* graphicsView, std::array<int, 3>& clickedColor, QColor& toColor, QColor& fromColor, RectPlotItem*& map)
+bool Control::getColors(std::array<int, 3>& clickedColor, QColor& toColor, QColor& fromColor, RectPlotItem*& map)
 {
     if(mMainWindow->getImg().empty())
     {
         return false;
     }
 
-    cv::Mat hsvImg;
-    cv::cvtColor(mMainWindow->getImg(), hsvImg, cv::COLOR_BGR2HSV);
+    QImage *hsvImg = mMainWindow->getImage();
+    QPointF imgPoint = mMainWindow->getMousePosOnImage();
 
-    QPointF pointOnScene = graphicsView->mapToScene(p);
-    QPointF imgPoint = mMainWindow->getImageItem()->mapToScene(pointOnScene);
-    imgPoint.setX(imgPoint.x() + mMainWindow->getImageBorderSize());
-    imgPoint.setY(imgPoint.y() + mMainWindow->getImageBorderSize());
-
-    if(imgPoint.x() < 0 || imgPoint.x() > hsvImg.cols || imgPoint.y() < 0 || imgPoint.y() > hsvImg.rows)
+    if(imgPoint.x() < 0 || imgPoint.x() > hsvImg->width() || imgPoint.y() < 0 || imgPoint.y() > hsvImg->height())
     {
         debout << "Clicked outside the image with color picker." << std::endl;
         return false;
     }
     //debout << "Koordinaten: " << imgPoint.x() << ", " << imgPoint.y() << std::endl;
 
-    cv::Vec3b color = hsvImg.at<cv::Vec3b>((int)imgPoint.y(), (int)imgPoint.x());
-    clickedColor =  {color[0] * 2, color[1], color[2]};
+    QColor color {hsvImg->pixel(imgPoint.toPoint())};//hsvImg.at<cv::Vec3b>((int)imgPoint.y(), (int)imgPoint.x());
+    clickedColor =  {color.hue(), color.saturation(), color.value()};
     //debout << "Farbe: " << (int)clickedColor[0] << ", " << (int)clickedColor[1] << ", " << (int)clickedColor[2] << std::endl;
 
     map = this->getColorPlot()->getMapItem();
@@ -3265,13 +3261,13 @@ bool Control::getColors(QPoint& p, GraphicsView* graphicsView, std::array<int, 3
  * @param p
  * @param graphicsView
  */
-void Control::on_expandColor(QPoint p, GraphicsView* graphicsView)
+void Control::on_expandColor()
 {
 
     QColor fromColor, toColor;
     RectPlotItem* map;
     std::array<int, 3> clickedColor;
-    if(!getColors(p, graphicsView, clickedColor, toColor, fromColor, map))
+    if(!getColors(clickedColor, toColor, fromColor, map))
     {
         return;
     }
@@ -3287,13 +3283,14 @@ void Control::on_expandColor(QPoint p, GraphicsView* graphicsView)
  * @param p
  * @param graphicsView
  */
-void Control::on_setColor(QPoint p , GraphicsView* graphicsView){
+void Control::on_setColor()
+{
     constexpr int BUFFER = 5;
 
     QColor fromColor, toColor;
     RectPlotItem* map;
     std::array<int, 3> clickedColor;
-    if(!getColors(p, graphicsView, clickedColor, toColor, fromColor, map))
+    if(!getColors(clickedColor, toColor, fromColor, map))
     {
         return;
     }
diff --git a/src/view.cpp b/src/view.cpp
index 7c30d24c7..bf3318c74 100644
--- a/src/view.cpp
+++ b/src/view.cpp
@@ -177,9 +177,9 @@ void GraphicsView::mousePressEvent(QMouseEvent *event)
 {
 
     if(event->modifiers() & Qt::ShiftModifier){
-        emit setColorEvent(event->pos(), this);
+        emit setColorEvent();
     }else{
-        emit colorSelected(event->pos(), this);
+        emit colorSelected();
     }
     QGraphicsView::mousePressEvent(event);
 }
-- 
GitLab