瀏覽代碼

Json file to save and load strength lines

Rémi Synave 1 年之前
父節點
當前提交
eef974aed5
共有 4 個文件被更改,包括 48 次插入29 次删除
  1. 37 22
      WorkArea/WorkArea.cpp
  2. 1 1
      csl/Experiment.cpp
  3. 5 5
      csl/main.cpp
  4. 5 1
      dsl/MainWindow.cpp

+ 37 - 22
WorkArea/WorkArea.cpp

@@ -1,6 +1,9 @@
 #include <QFile>
 #include <QColor>
 #include <QTextStream>
+#include <QJsonObject>
+#include <QJsonArray>
+#include <QJsonDocument>
 
 #include <QDebug>
 #include <QStringList>
@@ -62,42 +65,54 @@ WorkArea::loadImage (const std::string & filename)
   liste_points.clear ();
   
   setFixedSize (img->width (), img->height ());
-  maxLines = 3;
 }
 
 void
 WorkArea::loadSL (const std::string & filename)
 {
-  //qDebug() << resizeFactor << Qt::endl;
   liste_points.clear();
-  QFile file(QString::fromStdString (filename));
-  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
-    return;
-  
-  QTextStream in(&file);
-  while (!in.atEnd()) {
-    QString line = in.readLine();
-    QStringList parts = line.split(" ");
-    liste_points << new QPoint((int)(parts[0].toDouble()*resizeFactor), (int)(parts[1].toDouble()*resizeFactor));
-    liste_points << new QPoint((int)(parts[2].toDouble()*resizeFactor), (int)(parts[3].toDouble()*resizeFactor));
-    //qDebug() << liste_points.back()->x() << " " << liste_points.back()->y() << Qt::endl;
+
+  QFile fin(QString::fromStdString (filename));
+  fin.open(QIODevice::ReadOnly);
+  QByteArray ba2 = fin.readAll();
+  QJsonParseError parseError;
+  QJsonDocument doc2 = QJsonDocument::fromJson(ba2, &parseError);
+  QJsonArray lines = doc2["lines"].toArray();
+  for(unsigned int i=0; i<lines.size(); i++){
+    QJsonArray line = lines[i].toArray();
+    QJsonArray point1 = line[0].toArray();
+    QJsonArray point2 = line[1].toArray();
+    liste_points << new QPoint((int)(point1[0].toDouble()*resizeFactor), (int)(point1[1].toDouble()*resizeFactor));
+    liste_points << new QPoint((int)(point2[0].toDouble()*resizeFactor), (int)(point2[1].toDouble()*resizeFactor));
   }
-  file.close();
 }
 
 void
 WorkArea::saveStrengthLine (const std::string & filename)
 {
-  QFile file (QString::fromStdString (filename));
-
-  if (!file.open (QIODevice::WriteOnly | QIODevice::Text))
-    return;
 
-  QTextStream out (&file);
-  for (int i = 0; i < liste_points.size () / 2; i++)
-    out << (liste_points[i*2]->x ()/resizeFactor) << " " << (liste_points[i*2]->y ()/resizeFactor) << " " << (liste_points[i*2+1]->x ()/resizeFactor) << " " << (liste_points[i*2+1]->y ()/resizeFactor) << "\n";
+  QJsonArray lines;
+  
+  for (int i = 0; i < liste_points.size () / 2; i++){
+    QJsonArray line;
+    QJsonArray point1;
+    point1.push_back(liste_points[i*2]->x ()/resizeFactor);
+    point1.push_back(liste_points[i*2]->y ()/resizeFactor);
+    line.push_back(point1);
+    QJsonArray point2;
+    point2.push_back(liste_points[i*2+1]->x ()/resizeFactor);
+    point2.push_back(liste_points[i*2+1]->y ()/resizeFactor);
+    line.push_back(point2);
+
+    lines.push_back(line);
+  }
+  
+  root["lines"] = lines;
 
-  file.close ();
+  QByteArray ba = QJsonDocument(root).toJson();
+  QFile fout(QString::fromStdString (filename));
+  fout.open(QIODevice::WriteOnly);
+  fout.write(ba);
 }
 
 void

+ 1 - 1
csl/Experiment.cpp

@@ -97,7 +97,7 @@ Experiment::Experiment ():QMainWindow()
   /*POUR TEST*/
   
   QString img_a_ouvrir("/home/remi/images/screenshot.png");
-  QString sl1_a_ouvrir("/home/remi/images/screenshot.txt");
+  QString sl1_a_ouvrir("/home/remi/images/fzerfrezf.json");
   QString sl2_a_ouvrir("/home/remi/images/screenshotb.txt");
 
   openC( img_a_ouvrir, sl1_a_ouvrir, sl2_a_ouvrir);

+ 5 - 5
csl/main.cpp

@@ -7,12 +7,12 @@
 int
 main (int argc, char *argv[])
 {
-  QString fileToLoad = "";
-  QString fileToSave = "";
+  // QString fileToLoad = "";
+  // QString fileToSave = "";
   
   QApplication app (argc, argv);
   
-  Informations infos(fileToLoad, fileToSave);
+  /*Informations infos(fileToLoad, fileToSave);
   infos.show();
   app.exec();
 
@@ -22,10 +22,10 @@ main (int argc, char *argv[])
     {
       qDebug() << "Chaine1 recue : " << fileToLoad << Qt::endl;
       qDebug() << "Chaine2 recue : " << fileToSave << Qt::endl;
-      
+  */
       Experiment window;
       window.show ();
       app.exec ();
-    }
+      //}
   return 0;
 }

+ 5 - 1
dsl/MainWindow.cpp

@@ -4,6 +4,8 @@
 #include <QMenuBar>
 #include <QFileDialog>
 
+#include <QDebug>
+
 #include "MainWindow.hpp"
 
 
@@ -48,7 +50,9 @@ MainWindow::save ()
 						   QDir::currentPath (),
 						   QObject::
 						   tr
-						   ("Text file (*.txt);;All files (*.*)"));
+						   ("JSON file (*.json);;All files (*.*)"));
+  if(!filename.endsWith(".json"))
+    filename.append(".json");
   wa->saveStrengthLine (filename.toStdString ());
 }