Experiment.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <QGuiApplication>
  4. #include <QScreen>
  5. #include <QWidget>
  6. #include <QMenuBar>
  7. #include <QFileDialog>
  8. #include <QInputDialog>
  9. #include <QDirIterator>
  10. #include <QVBoxLayout>
  11. #include <QHBoxLayout>
  12. #include <QPushButton>
  13. #include <QTextStream>
  14. #include <QJsonObject>
  15. #include <QJsonArray>
  16. #include <QJsonDocument>
  17. #include <QMessageBox>
  18. #include <QDebug>
  19. #include "Experiment.hpp"
  20. Experiment::Experiment ():QMainWindow()
  21. {
  22. createActions ();
  23. createMenus ();
  24. filenames = NULL;
  25. //filenames_json = NULL;
  26. experimentation = NULL;
  27. suffix = NULL;
  28. choice = NULL;
  29. side = NULL;
  30. dataPath = new QString(QDir::currentPath()+"/data");
  31. resultPath = NULL;
  32. //algo_side='X';
  33. expeStarted = false;
  34. openedImage = -1;
  35. setWindowTitle (tr ("DSL - Draw Strength Line"));
  36. QScreen *screen = QGuiApplication::primaryScreen ();
  37. QRect screenGeometry = screen->geometry ();
  38. screenWidth = screenGeometry.width () - 200;
  39. screenHeight = screenGeometry.height () - 200;
  40. QWidget *central= new QWidget(this);
  41. setCentralWidget (central);
  42. setContentsMargins(0, 0, 0, 0);
  43. QVBoxLayout *layoutV = new QVBoxLayout(central);
  44. layoutV->setSpacing(0);
  45. layoutV->setContentsMargins(0, 0, 0, 0);
  46. central->setLayout(layoutV);
  47. // First line of the interface
  48. QHBoxLayout *layoutH1 = new QHBoxLayout(central);
  49. layoutH1->setSpacing(0);
  50. layoutH1->setContentsMargins(0, 0, 0, 0);
  51. layoutV->addLayout(layoutH1);
  52. waLeft = new WorkArea (((screenWidth-spaceInBetween)/2), screenHeight, central);
  53. waLeft->setContentsMargins(0, 0, 0, 0);
  54. connect(waLeft, SIGNAL(click()), this, SLOT(leftImage()));
  55. layoutH1->addWidget(waLeft);
  56. QWidget *w1SpaceInBetween = new QWidget(central);
  57. w1SpaceInBetween->setContentsMargins(0, 0, 0, 0);
  58. w1SpaceInBetween->setFixedSize(spaceInBetween,0);
  59. layoutH1->addWidget(w1SpaceInBetween);
  60. waRight = new WorkArea (((screenWidth-spaceInBetween)/2), screenHeight, central);
  61. waRight->setContentsMargins(0, 0, 0, 0);
  62. connect(waRight, SIGNAL(click()), this, SLOT(rightImage()));
  63. layoutH1->addWidget(waRight);
  64. // second line of interface
  65. QWidget *hSpaceInBetween = new QWidget(central);
  66. hSpaceInBetween->setContentsMargins(0, 0, 0, 0);
  67. hSpaceInBetween->setFixedSize(0, spaceInBetween);
  68. layoutV->addWidget(hSpaceInBetween);
  69. // third line of interface
  70. QHBoxLayout *layoutH2 = new QHBoxLayout(central);
  71. layoutH2->setSpacing(0);
  72. layoutH2->setContentsMargins(0, 0, 0, 0);
  73. layoutV->addLayout(layoutH2);
  74. leftButton = new QPushButton("Image de gauche", central);
  75. leftButton->setFixedHeight(30);
  76. connect(leftButton, SIGNAL(clicked(bool)), this, SLOT(leftImage()));
  77. layoutH2->addWidget(leftButton);
  78. QWidget *w2SpaceInBetween = new QWidget();
  79. w2SpaceInBetween->setContentsMargins(0, 0, 0, 0);
  80. w2SpaceInBetween->setFixedSize(spaceInBetween,0);
  81. layoutH2->addWidget(w2SpaceInBetween);
  82. rightButton = new QPushButton("Image de droite", central);
  83. rightButton->setFixedHeight(30);
  84. connect(rightButton, SIGNAL(clicked(bool)), this, SLOT(rightImage()));
  85. layoutH2->addWidget(rightButton);
  86. // Fourth line of the interface
  87. progressBar = new QProgressBar(central);
  88. progressBar->setFixedHeight(20);
  89. progressBar->setRange(0, maxProgressBar);
  90. progressBar->setValue(0);
  91. layoutV->addWidget(progressBar);
  92. timer = new QTimer(central);
  93. timer->setSingleShot(false);
  94. timer->setInterval(time);
  95. setFixedSize (screenWidth, screenHeight);
  96. QMessageBox msgBox;
  97. msgBox.setText("Welcome !\nYou are about to take part in an experiment.\nFor instructions, please refer to the pdf available in the same folder as this software.\n\nWhen you're ready, start the experiment with:\nFile -> Start\nor\nCTRL+S.\n\nThank you.");
  98. msgBox.exec();
  99. }
  100. void
  101. Experiment::start ()
  102. {
  103. bool ok = false;
  104. if(!(QDir(*dataPath).exists()))
  105. {
  106. QMessageBox msgBox;
  107. msgBox.setText("Data files can't be found. data folder is nedded.");
  108. msgBox.exec();
  109. std::exit(EXIT_FAILURE);
  110. }
  111. suffix = new QString(QInputDialog::getText(this, tr("Your ID"),
  112. tr("ID to use :"), QLineEdit::Normal,
  113. QDir::home().dirName(), &ok));
  114. if((*suffix) == "algo" || (*suffix) == "_algo" || (*suffix) == "random" || (*suffix) == "_random" || (*suffix) == "expert" || (*suffix) == "_expert")
  115. {
  116. QMessageBox msgBox;
  117. msgBox.setText("Your name can't be [_]algo or [_]random. or [_]expert");
  118. msgBox.exec();
  119. ok = false;
  120. }
  121. resultPath = new QString(QDir::currentPath()+"/"+(*suffix));
  122. if(QDir(*resultPath).exists())
  123. {
  124. QMessageBox msgBox;
  125. msgBox.setText("This id is already used. Choose another one or take the necessary action to solve the problem.");
  126. msgBox.exec();
  127. ok = false;
  128. }
  129. else
  130. {
  131. QDir(QDir::currentPath()).mkdir(*suffix);
  132. }
  133. if(ok)
  134. {
  135. delete filenames;
  136. //delete filenames_json;
  137. delete experimentation;
  138. delete choice;
  139. delete side;
  140. filenames = new QStringList();
  141. //filenames_json = new QStringList();
  142. experimentation = new QList<QStringList*>();
  143. choice = new QStringList();
  144. side = new QStringList();
  145. QDirIterator it(*dataPath, {"*.jpg"}, QDir::Files);
  146. while (it.hasNext()) {
  147. filenames->append( it.next() );
  148. }
  149. std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
  150. for(int i = 0 ; i<filenames->size() ; i++)
  151. {
  152. QString jsonFile((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_algo)+".json");
  153. QString jsonFile2((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_expert)+".json");
  154. if((! (QFile(jsonFile).exists()) ) || (! (QFile(jsonFile2).exists()) ))
  155. {
  156. qDebug() << "WARNING !!!! " << jsonFile << " file missing";
  157. qDebug() << "OR\nWARNING !!!! " << jsonFile2 << " file missing";
  158. ok=false;
  159. }else
  160. {
  161. //filenames_json->append(jsonFile);
  162. QStringList *toAppend = new QStringList();
  163. toAppend->append(filenames->at(i));
  164. if((std::rand()%2) == 0)
  165. {
  166. toAppend->append(QString((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_algo)+".json"));
  167. toAppend->append(QString((*resultPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_random)+".json"));
  168. }
  169. else
  170. {
  171. toAppend->append(QString((*resultPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_random)+".json"));
  172. toAppend->append(QString((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_algo)+".json"));
  173. }
  174. experimentation->append(toAppend);
  175. toAppend = new QStringList();
  176. toAppend->append(filenames->at(i));
  177. if((std::rand()%2) == 0)
  178. {
  179. toAppend->append(QString((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_algo)+".json"));
  180. toAppend->append(QString((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_expert)+".json"));
  181. }
  182. else
  183. {
  184. toAppend->append(QString((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_expert)+".json"));
  185. toAppend->append(QString((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_algo)+".json"));
  186. }
  187. experimentation->append(toAppend);
  188. }
  189. }
  190. for(int i=0 ; i<experimentation->size() ; i++)
  191. {
  192. // std::cout << experimentation->at(i)->at(0).toStdString() << " - " << experimentation->at(i)->at(1).toStdString() << " - " << experimentation->at(i)->at(2).toStdString() << std::endl;
  193. // std::cout << experimentation->at(i)->at(0).toStdString() << std::endl;
  194. }
  195. std::cout << std::endl << std::endl;
  196. if(!ok)
  197. std::exit(EXIT_FAILURE);
  198. /*for(int i = 0 ; i<filenames->size()*2 ; i++)
  199. {
  200. int rand1 = std::rand()%filenames->size();
  201. int rand2 = std::rand()%filenames->size();
  202. filenames->swapItemsAt(rand1, rand2);
  203. filenames_json->swapItemsAt(rand1, rand2);
  204. }*/
  205. for(int i = 0 ; i<experimentation->size()*4 ; i++)
  206. {
  207. int rand1 = std::rand()%filenames->size();
  208. int rand2 = std::rand()%filenames->size();
  209. experimentation->swapItemsAt(rand1, rand2);
  210. }
  211. openedImage = -1;
  212. expeStarted = true;
  213. openNext();
  214. }
  215. }
  216. void
  217. Experiment::undo ()
  218. {
  219. openedImage = openedImage-2;
  220. choice->removeLast();
  221. if(openedImage < 1)
  222. undoAct->setEnabled(false);
  223. openNext();
  224. }
  225. void Experiment::leftImage(){
  226. if(expeStarted)
  227. {
  228. /*if((algo_side != 'L') && (algo_side != 'R'))
  229. {
  230. qDebug() << "Something went wrong";
  231. QApplication::quit();
  232. }
  233. if(algo_side == 'L')
  234. choice->append(QString("algo"));
  235. else
  236. choice->append(QString("random"));*/
  237. if(experimentation->at(openedImage)->at(1).at(experimentation->at(openedImage)->length()-6) == 'o')
  238. {
  239. choice->append(QString("algo"));
  240. std::cout << "Choix algo" << std::endl;
  241. }
  242. if(experimentation->at(openedImage)->at(1).at(experimentation->at(openedImage)->length()-6) == 'm')
  243. {
  244. choice->append(QString("random"));
  245. std::cout << "Choix random" << std::endl;
  246. }
  247. if(experimentation->at(openedImage)->at(1).at(experimentation->at(openedImage)->length()-6) == 't')
  248. {
  249. choice->append(QString("expert"));
  250. std::cout << "Choix expert" << std::endl;
  251. }
  252. openNext();
  253. }
  254. }
  255. void Experiment::rightImage(){
  256. if(expeStarted)
  257. {
  258. /*if((algo_side != 'L') && (algo_side != 'R'))
  259. {
  260. qDebug() << "Something went wrong";
  261. QApplication::quit();
  262. }
  263. if(algo_side == 'R')
  264. choice->append(QString("algo"));
  265. else
  266. choice->append(QString("random"));*/
  267. std::cout << experimentation->at(openedImage)->at(2).at(experimentation->at(openedImage)->length()-6).toLatin1() << std::endl;
  268. if(experimentation->at(openedImage)->at(2).at(experimentation->at(openedImage)->length()-6) == 'o')
  269. {
  270. choice->append(QString("algo"));
  271. std::cout << "Choix algo" << std::endl;
  272. }
  273. if(experimentation->at(openedImage)->at(2).at(experimentation->at(openedImage)->length()-6) == 'm')
  274. {
  275. choice->append(QString("random"));
  276. std::cout << "Choix random" << std::endl;
  277. }
  278. if(experimentation->at(openedImage)->at(2).at(experimentation->at(openedImage)->length()-6) == 't')
  279. {
  280. choice->append(QString("expert"));
  281. std::cout << "Choix expert" << std::endl;
  282. }
  283. openNext();
  284. }
  285. }
  286. void
  287. Experiment::openNext (){
  288. openedImage = openedImage+1;
  289. std::cout << experimentation->at(openedImage)->at(0).toStdString() << " - " << experimentation->at(openedImage)->at(1).toStdString() << " - " << experimentation->at(openedImage)->at(2).toStdString() << std::endl;
  290. if(openedImage == 1)
  291. undoAct->setEnabled(true);
  292. progressBar->setValue((int)((openedImage+1.0)/(filenames->size())*100.0));
  293. if(openedImage >= filenames->size())
  294. {
  295. qDebug() << "That's all folks !" << Qt::endl;
  296. /* Saving result file*/
  297. QJsonObject root;
  298. QJsonArray choices;
  299. for (int i = 0; i < choice->size (); i++){
  300. QJsonObject choiceOnImage;
  301. choiceOnImage["index"] = i;
  302. choiceOnImage["image"] = QString(QFileInfo(filenames->at(i)).baseName());
  303. choiceOnImage["choice"] = QString(choice->at(i));
  304. choices.push_back(choiceOnImage);
  305. }
  306. root["choices"] = choices;
  307. QByteArray ba = QJsonDocument(root).toJson();
  308. QFile fout((*resultPath)+"/"+(*suffix)+QString::fromStdString (".json"));
  309. fout.open(QIODevice::WriteOnly);
  310. fout.write(ba);
  311. QMessageBox msgBox;
  312. msgBox.setText("That's all folks !\n\nThank you for your participation !");
  313. msgBox.exec();
  314. QApplication::quit();
  315. }
  316. else
  317. {
  318. // waLeft->loadImage (filenames->at(openedImage).toStdString ());
  319. // waRight->loadImage (filenames->at(openedImage).toStdString ());
  320. waLeft->loadImage (experimentation->at(openedImage)->at(0).toStdString ());
  321. waRight->loadImage (experimentation->at(openedImage)->at(0).toStdString ());
  322. // left display
  323. if(QFile(experimentation->at(openedImage)->at(1)).exists()==false)
  324. {
  325. for(int i = 0 ; i < waLeft->getNumberOfLines() ; i++)
  326. waLeft->addRandomSL();
  327. waLeft->saveStrengthLine (experimentation->at(openedImage)->at(1).toStdString ());
  328. }
  329. else
  330. waLeft->loadSL(experimentation->at(openedImage)->at(1).toStdString());
  331. // right display
  332. if(QFile(experimentation->at(openedImage)->at(2)).exists()==false)
  333. {
  334. for(int i = 0 ; i < waRight->getNumberOfLines() ; i++)
  335. waRight->addRandomSL();
  336. waRight->saveStrengthLine (experimentation->at(openedImage)->at(2).toStdString ());
  337. }
  338. else
  339. waRight->loadSL(experimentation->at(openedImage)->at(2).toStdString());
  340. /*if(side->size() == openedImage)
  341. {
  342. std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
  343. if((std::rand()%2) == 0)
  344. algo_side='L';
  345. else
  346. algo_side='R';
  347. side->append(QString(algo_side));
  348. }
  349. else
  350. algo_side = (side->at(openedImage)[0]).toLatin1();*/
  351. /*if(algo_side == 'L')
  352. {
  353. waLeft->loadSL (filenames_json->at(openedImage).toStdString ());
  354. QString jsonFile = (*resultPath)+"/"+(QFileInfo(filenames->at(openedImage)).baseName()+(QString("_random"))+".json");
  355. if(QFile(jsonFile).exists()==false)
  356. {
  357. for(int i = 0 ; i < waLeft->getNumberOfLines() ; i++)
  358. waRight->addRandomSL();
  359. waRight->saveStrengthLine (jsonFile.toStdString ());
  360. }
  361. else
  362. {
  363. waRight->loadSL(jsonFile.toStdString());
  364. }
  365. }
  366. else
  367. {
  368. waRight->loadSL (filenames_json->at(openedImage).toStdString ());
  369. QString jsonFile = (*resultPath)+"/"+(QFileInfo(filenames->at(openedImage)).baseName()+(QString("_random"))+".json");
  370. if(QFile(jsonFile).exists() == false)
  371. {
  372. for(int i = 0 ; i < waRight->getNumberOfLines() ; i++)
  373. waLeft->addRandomSL();
  374. waLeft->saveStrengthLine (jsonFile.toStdString ());
  375. }
  376. else
  377. {
  378. waLeft->loadSL(jsonFile.toStdString());
  379. }
  380. }*/
  381. waLeft->setReadOnly(true);
  382. waRight->setReadOnly(true);
  383. waLeft->repaint();
  384. waRight->repaint();
  385. setFixedSize (waLeft->geometry().width()*2+spaceInBetween, waLeft->geometry().height()+waLeft->geometry().y()+spaceInBetween+leftButton->geometry().height()+22); // +22 ??!!
  386. }
  387. }
  388. void
  389. Experiment::createActions ()
  390. {
  391. startAct = new QAction (tr ("&Start"), this);
  392. startAct->setShortcuts (QKeySequence::Save);
  393. connect (startAct, &QAction::triggered, this, &Experiment::start);
  394. exitAct = new QAction (tr ("E&xit"), this);
  395. exitAct->setShortcuts (QKeySequence::Quit);
  396. connect (exitAct, &QAction::triggered, this, &QWidget::close);
  397. undoAct = new QAction (tr ("&Undo"), this);
  398. undoAct->setShortcuts (QKeySequence::Undo);
  399. undoAct->setEnabled(false);
  400. connect (undoAct, &QAction::triggered, this, &Experiment::undo);
  401. }
  402. void
  403. Experiment::createMenus ()
  404. {
  405. fileMenu = menuBar ()->addMenu (tr ("&File"));
  406. fileMenu->addAction (startAct);
  407. fileMenu->addSeparator ();
  408. fileMenu->addAction (exitAct);
  409. editMenu = menuBar ()->addMenu (tr ("&Edit"));
  410. editMenu->addAction (undoAct);
  411. }