Parcourir la source

WorkArea can be displayed with an image and SL.

Rémi Synave il y a 1 an
Parent
commit
ddf3132f2f
3 fichiers modifiés avec 91 ajouts et 45 suppressions
  1. 88 44
      WorkArea/WorkArea.cpp
  2. 3 0
      WorkArea/WorkArea.hpp
  3. 0 1
      dsl/MainWindow.cpp

+ 88 - 44
WorkArea/WorkArea.cpp

@@ -2,21 +2,56 @@
 #include <QColor>
 #include <QTextStream>
 
+#include <QDebug>
+#include <QStringList>
+
 #include "WorkArea.hpp"
 
 #include "StrengthLine.hpp"
 
 WorkArea::WorkArea (int screenSizeX, int screenSizeY, QWidget * parent):
-  QWidget
+  QWidget(parent)
 {
-parent}
+  this->screenSizeX = screenSizeX;
+  this->screenSizeY = screenSizeY;
+  readOnly = false;
+  img = NULL;
+  indexPointClicked = -1;
+
+}
 
+WorkArea::WorkArea (int screenSizeX, int screenSizeY, const std::string & filename, QWidget * parent):QWidget(parent)
 {
   this->screenSizeX = screenSizeX;
   this->screenSizeY = screenSizeY;
+  readOnly = false;
+  img = NULL;
   indexPointClicked = -1;
+  loadImage(filename);
+}
 
+WorkArea::WorkArea (int screenSizeX, int screenSizeY, const std::string & imageFilename, const std::string & SLFilename, bool readOnly, QWidget * parent):QWidget(parent)
+{
+  this->screenSizeX = screenSizeX;
+  this->screenSizeY = screenSizeY;
   img = NULL;
+  indexPointClicked = -1;
+  loadImage(imageFilename);
+  this->readOnly = readOnly;
+
+  QFile file(QString::fromStdString (SLFilename));
+  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
+    return;
+  
+  QTextStream in(&file);
+  while (!in.atEnd()) {
+    QString line = in.readLine();
+    QStringList parts = line.split(" ");
+    qDebug() << parts[0].toInt() << Qt::endl;
+    liste_points << new QPoint(parts[0].toInt(), parts[1].toInt());
+    liste_points << new QPoint(parts[2].toInt(), parts[3].toInt());
+  }
+  file.close();
 }
 
 WorkArea::~WorkArea ()
@@ -65,11 +100,14 @@ WorkArea::paint (QPainter & painter)
 
   for (int i = 0; i < liste_points.length (); i++)
     {
-      painter.setBrush (QBrush (Qt::green));
-      painter.setPen (QPen (Qt::black));
-      painter.drawEllipse (liste_points[i]->x () - taillePoint / 2,
-			   liste_points[i]->y () - taillePoint / 2,
-			   taillePoint, taillePoint);
+      if(readOnly==false)
+	{
+	  painter.setBrush (QBrush (Qt::green));
+	  painter.setPen (QPen (Qt::black));
+	  painter.drawEllipse (liste_points[i]->x () - taillePoint / 2,
+			       liste_points[i]->y () - taillePoint / 2,
+			       taillePoint, taillePoint);
+	}
 
 
       if ((i % 2) == 1)
@@ -88,53 +126,56 @@ WorkArea::paint (QPainter & painter)
 void
 WorkArea::mousePressEvent (QMouseEvent * event)
 {
-  if (img != NULL)
+  if(readOnly==false)
     {
-      // Est ce qu'on a cliqué sur un point existant ?
-      indexPointClicked = -1;
-      for (int i = 0; i < liste_points.length (); i++)
+      if (img != NULL)
 	{
-	  QPoint point = event->pos () - *(liste_points[i]);
-	  if (point.manhattanLength () < taillePoint)
+	  // Est ce qu'on a cliqué sur un point existant ?
+	  indexPointClicked = -1;
+	  for (int i = 0; i < liste_points.length (); i++)
 	    {
-	      indexPointClicked = i;
+	      QPoint point = event->pos () - *(liste_points[i]);
+	      if (point.manhattanLength () < taillePoint)
+		{
+		  indexPointClicked = i;
+		}
 	    }
-	}
 
-      // Clic gauche
-      if (event->button () == Qt::LeftButton)
-	{
-	  if (indexPointClicked == -1)
+	  // Clic gauche
+	  if (event->button () == Qt::LeftButton)
 	    {
-	      indexPointClicked = liste_points.size ();
-	      liste_points << new QPoint (event->x (), event->y ());
+	      if (indexPointClicked == -1)
+		{
+		  indexPointClicked = liste_points.size ();
+		  liste_points << new QPoint (event->x (), event->y ());
+		}
 	    }
-	}
-      // Clic droit
-      if (event->button () == Qt::RightButton)
-	{
-	  if (indexPointClicked != -1)
+	  // Clic droit
+	  if (event->button () == Qt::RightButton)
 	    {
-	      if (indexPointClicked == (liste_points.size () - 1))
-		liste_points.removeAt (indexPointClicked);
-	      else
+	      if (indexPointClicked != -1)
 		{
-		  if ((indexPointClicked % 2) == 0)
-		    {
-		      QPoint *single =
-			liste_points.takeAt (indexPointClicked + 1);
-		      liste_points.removeAt (indexPointClicked);
-		      liste_points << single;
-		    }
+		  if (indexPointClicked == (liste_points.size () - 1))
+		    liste_points.removeAt (indexPointClicked);
 		  else
 		    {
-		      QPoint *single =
-			liste_points.takeAt (indexPointClicked - 1);
-		      liste_points.removeAt (indexPointClicked - 1);
-		      liste_points << single;
+		      if ((indexPointClicked % 2) == 0)
+			{
+			  QPoint *single =
+			    liste_points.takeAt (indexPointClicked + 1);
+			  liste_points.removeAt (indexPointClicked);
+			  liste_points << single;
+			}
+		      else
+			{
+			  QPoint *single =
+			    liste_points.takeAt (indexPointClicked - 1);
+			  liste_points.removeAt (indexPointClicked - 1);
+			  liste_points << single;
+			}
 		    }
+		  indexPointClicked = -1;
 		}
-	      indexPointClicked = -1;
 	    }
 	}
     }
@@ -144,10 +185,13 @@ WorkArea::mousePressEvent (QMouseEvent * event)
 void
 WorkArea::mouseMoveEvent (QMouseEvent * event)
 {
-  if (indexPointClicked != -1)
+  if(readOnly==false)
     {
-      liste_points[indexPointClicked]->setX (event->x ());
-      liste_points[indexPointClicked]->setY (event->y ());
+      if (indexPointClicked != -1)
+	{
+	  liste_points[indexPointClicked]->setX (event->x ());
+	  liste_points[indexPointClicked]->setY (event->y ());
+	}
     }
   repaint ();
 }

+ 3 - 0
WorkArea/WorkArea.hpp

@@ -22,6 +22,7 @@ private:
   const int epaisseurLigne = 3;
   
   unsigned int screenSizeX, screenSizeY;
+  bool readOnly;
   
   QImage *img;
   QList < QPoint * >liste_points;
@@ -37,6 +38,8 @@ protected:
   
 public:
   WorkArea (int screenSizeX, int screenSizeY, QWidget * parent = nullptr);
+  WorkArea (int screenSizeX, int screenSizeY, const std::string & filename, QWidget * parent = nullptr);
+  WorkArea (int screenSizeX, int screenSizeY, const std::string & imageFilename, const std::string & SLFilename, bool readOnly, QWidget * parent = nullptr);
    ~WorkArea ();
   
   void paint (QPainter & painter);

+ 0 - 1
dsl/MainWindow.cpp

@@ -19,7 +19,6 @@ MainWindow::MainWindow ()
   int screenWidth = screenGeometry.width () - 200;
   int screenHeight = screenGeometry.height () - 200;
   
-
   wa = new WorkArea (screenWidth, screenHeight, this);
   
   setCentralWidget (wa);