Experiment.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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()%experimentation->size();
  208. int rand2 = std::rand()%experimentation->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)->at(1).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)->at(1).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)->at(1).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)->at(2).length()-6).toLatin1() << std::endl;
  268. if(experimentation->at(openedImage)->at(2).at(experimentation->at(openedImage)->at(2).length()-6).toLatin1() == '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)->at(2).length()-6).toLatin1() == '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)->at(2).length()-6).toLatin1() == '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. if(openedImage == 1)
  290. undoAct->setEnabled(true);
  291. progressBar->setValue((int)((openedImage+1.0)/(experimentation->size())*100.0));
  292. if(openedImage >= experimentation->size())
  293. {
  294. qDebug() << "That's all folks !" << Qt::endl;
  295. /* Saving result file*/
  296. QJsonObject root;
  297. QJsonArray choices;
  298. for (int i = 0; i < choice->size (); i++){
  299. QJsonObject choiceOnImage;
  300. choiceOnImage["index"] = i;
  301. choiceOnImage["image"] = QString(QFileInfo(experimentation->at(i)->at(0)).baseName());
  302. QString possibleChoices1(QFileInfo(experimentation->at(i)->at(1)).baseName().split("_").at(1));
  303. QString possibleChoices2(QFileInfo(experimentation->at(i)->at(2)).baseName().split("_").at(1));
  304. choiceOnImage["choices"] = possibleChoices1.append(", ").append(possibleChoices2);
  305. choiceOnImage["choice"] = QString(choice->at(i));
  306. choices.push_back(choiceOnImage);
  307. }
  308. root["choices"] = choices;
  309. QByteArray ba = QJsonDocument(root).toJson();
  310. QFile fout((*resultPath)+"/"+(*suffix)+QString::fromStdString (".json"));
  311. fout.open(QIODevice::WriteOnly);
  312. fout.write(ba);
  313. QMessageBox msgBox;
  314. msgBox.setText("That's all folks !\n\nThank you for your participation !");
  315. msgBox.exec();
  316. QApplication::quit();
  317. }
  318. else
  319. {
  320. // std::cout << experimentation->at(openedImage)->at(0).toStdString() << " - " << experimentation->at(openedImage)->at(1).toStdString() << " - " << experimentation->at(openedImage)->at(2).toStdString() << std::endl;
  321. // waLeft->loadImage (filenames->at(openedImage).toStdString ());
  322. // waRight->loadImage (filenames->at(openedImage).toStdString ());
  323. waLeft->loadImage (experimentation->at(openedImage)->at(0).toStdString ());
  324. waRight->loadImage (experimentation->at(openedImage)->at(0).toStdString ());
  325. if(QFile(experimentation->at(openedImage)->at(1)).exists()==false)
  326. {
  327. waRight->loadSL(experimentation->at(openedImage)->at(2).toStdString());
  328. for(int i = 0 ; i < waRight->getNumberOfLines() ; i++)
  329. waLeft->addRandomSL();
  330. waLeft->saveStrengthLine (experimentation->at(openedImage)->at(1).toStdString ());
  331. }
  332. else
  333. {
  334. if(QFile(experimentation->at(openedImage)->at(2)).exists()==false)
  335. {
  336. waLeft->loadSL(experimentation->at(openedImage)->at(1).toStdString());
  337. for(int i = 0 ; i < waLeft->getNumberOfLines() ; i++)
  338. waRight->addRandomSL();
  339. waRight->saveStrengthLine (experimentation->at(openedImage)->at(2).toStdString ());
  340. }
  341. else
  342. {
  343. waLeft->loadSL(experimentation->at(openedImage)->at(1).toStdString());
  344. waRight->loadSL(experimentation->at(openedImage)->at(2).toStdString());
  345. }
  346. }
  347. // right display
  348. // else
  349. // waRight->loadSL(experimentation->at(openedImage)->at(2).toStdString());
  350. /*if(side->size() == openedImage)
  351. {
  352. std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
  353. if((std::rand()%2) == 0)
  354. algo_side='L';
  355. else
  356. algo_side='R';
  357. side->append(QString(algo_side));
  358. }
  359. else
  360. algo_side = (side->at(openedImage)[0]).toLatin1();*/
  361. /*if(algo_side == 'L')
  362. {
  363. waLeft->loadSL (filenames_json->at(openedImage).toStdString ());
  364. QString jsonFile = (*resultPath)+"/"+(QFileInfo(filenames->at(openedImage)).baseName()+(QString("_random"))+".json");
  365. if(QFile(jsonFile).exists()==false)
  366. {
  367. for(int i = 0 ; i < waLeft->getNumberOfLines() ; i++)
  368. waRight->addRandomSL();
  369. waRight->saveStrengthLine (jsonFile.toStdString ());
  370. }
  371. else
  372. {
  373. waRight->loadSL(jsonFile.toStdString());
  374. }
  375. }
  376. else
  377. {
  378. waRight->loadSL (filenames_json->at(openedImage).toStdString ());
  379. QString jsonFile = (*resultPath)+"/"+(QFileInfo(filenames->at(openedImage)).baseName()+(QString("_random"))+".json");
  380. if(QFile(jsonFile).exists() == false)
  381. {
  382. for(int i = 0 ; i < waRight->getNumberOfLines() ; i++)
  383. waLeft->addRandomSL();
  384. waLeft->saveStrengthLine (jsonFile.toStdString ());
  385. }
  386. else
  387. {
  388. waLeft->loadSL(jsonFile.toStdString());
  389. }
  390. }*/
  391. waLeft->setReadOnly(true);
  392. waRight->setReadOnly(true);
  393. waLeft->repaint();
  394. waRight->repaint();
  395. setFixedSize (waLeft->geometry().width()*2+spaceInBetween, waLeft->geometry().height()+waLeft->geometry().y()+spaceInBetween+leftButton->geometry().height()+22); // +22 ??!!
  396. }
  397. }
  398. void
  399. Experiment::createActions ()
  400. {
  401. startAct = new QAction (tr ("&Start"), this);
  402. startAct->setShortcuts (QKeySequence::Save);
  403. connect (startAct, &QAction::triggered, this, &Experiment::start);
  404. exitAct = new QAction (tr ("E&xit"), this);
  405. exitAct->setShortcuts (QKeySequence::Quit);
  406. connect (exitAct, &QAction::triggered, this, &QWidget::close);
  407. undoAct = new QAction (tr ("&Undo"), this);
  408. undoAct->setShortcuts (QKeySequence::Undo);
  409. undoAct->setEnabled(false);
  410. connect (undoAct, &QAction::triggered, this, &Experiment::undo);
  411. }
  412. void
  413. Experiment::createMenus ()
  414. {
  415. fileMenu = menuBar ()->addMenu (tr ("&File"));
  416. fileMenu->addAction (startAct);
  417. fileMenu->addSeparator ();
  418. fileMenu->addAction (exitAct);
  419. editMenu = menuBar ()->addMenu (tr ("&Edit"));
  420. editMenu->addAction (undoAct);
  421. }