Experiment.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. suffix = NULL;
  27. choice = NULL;
  28. side = NULL;
  29. dataPath = new QString(QDir::currentPath()+"/data");
  30. resultPath = NULL;
  31. algo_side='X';
  32. expeStarted = false;
  33. openedImage = -1;
  34. setWindowTitle (tr ("DSL - Draw Strength Line"));
  35. QScreen *screen = QGuiApplication::primaryScreen ();
  36. QRect screenGeometry = screen->geometry ();
  37. screenWidth = screenGeometry.width () - 200;
  38. screenHeight = screenGeometry.height () - 200;
  39. QWidget *central= new QWidget(this);
  40. setCentralWidget (central);
  41. setContentsMargins(0, 0, 0, 0);
  42. QVBoxLayout *layoutV = new QVBoxLayout(central);
  43. layoutV->setSpacing(0);
  44. layoutV->setContentsMargins(0, 0, 0, 0);
  45. central->setLayout(layoutV);
  46. // First line of the interface
  47. QHBoxLayout *layoutH1 = new QHBoxLayout(central);
  48. layoutH1->setSpacing(0);
  49. layoutH1->setContentsMargins(0, 0, 0, 0);
  50. layoutV->addLayout(layoutH1);
  51. waLeft = new WorkArea (((screenWidth-spaceInBetween)/2), screenHeight, central);
  52. waLeft->setContentsMargins(0, 0, 0, 0);
  53. connect(waLeft, SIGNAL(click()), this, SLOT(leftImage()));
  54. layoutH1->addWidget(waLeft);
  55. QWidget *w1SpaceInBetween = new QWidget(central);
  56. w1SpaceInBetween->setContentsMargins(0, 0, 0, 0);
  57. w1SpaceInBetween->setFixedSize(spaceInBetween,0);
  58. layoutH1->addWidget(w1SpaceInBetween);
  59. waRight = new WorkArea (((screenWidth-spaceInBetween)/2), screenHeight, central);
  60. waRight->setContentsMargins(0, 0, 0, 0);
  61. connect(waRight, SIGNAL(click()), this, SLOT(rightImage()));
  62. layoutH1->addWidget(waRight);
  63. // second line of interface
  64. QWidget *hSpaceInBetween = new QWidget(central);
  65. hSpaceInBetween->setContentsMargins(0, 0, 0, 0);
  66. hSpaceInBetween->setFixedSize(0, spaceInBetween);
  67. layoutV->addWidget(hSpaceInBetween);
  68. // third line of interface
  69. QHBoxLayout *layoutH2 = new QHBoxLayout(central);
  70. layoutH2->setSpacing(0);
  71. layoutH2->setContentsMargins(0, 0, 0, 0);
  72. layoutV->addLayout(layoutH2);
  73. leftButton = new QPushButton("Image de gauche", central);
  74. leftButton->setFixedHeight(30);
  75. connect(leftButton, SIGNAL(clicked(bool)), this, SLOT(leftImage()));
  76. layoutH2->addWidget(leftButton);
  77. QWidget *w2SpaceInBetween = new QWidget();
  78. w2SpaceInBetween->setContentsMargins(0, 0, 0, 0);
  79. w2SpaceInBetween->setFixedSize(spaceInBetween,0);
  80. layoutH2->addWidget(w2SpaceInBetween);
  81. rightButton = new QPushButton("Image de droite", central);
  82. rightButton->setFixedHeight(30);
  83. connect(rightButton, SIGNAL(clicked(bool)), this, SLOT(rightImage()));
  84. layoutH2->addWidget(rightButton);
  85. // Fourth line of the interface
  86. progressBar = new QProgressBar(central);
  87. progressBar->setFixedHeight(20);
  88. progressBar->setRange(0, maxProgressBar);
  89. progressBar->setValue(0);
  90. layoutV->addWidget(progressBar);
  91. timer = new QTimer(central);
  92. timer->setSingleShot(false);
  93. timer->setInterval(time);
  94. setFixedSize (screenWidth, screenHeight);
  95. QMessageBox msgBox;
  96. 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.");
  97. msgBox.exec();
  98. }
  99. void
  100. Experiment::start ()
  101. {
  102. bool ok = false;
  103. if(!(QDir(*dataPath).exists()))
  104. {
  105. QMessageBox msgBox;
  106. msgBox.setText("Data files can't be found. data folder is nedded.");
  107. msgBox.exec();
  108. std::exit(EXIT_FAILURE);
  109. }
  110. suffix = new QString(QInputDialog::getText(this, tr("Your ID"),
  111. tr("ID to use :"), QLineEdit::Normal,
  112. QDir::home().dirName(), &ok));
  113. if((*suffix) == "algo" || (*suffix) == "_algo" || (*suffix) == "random" || (*suffix) == "_random" || (*suffix) == "expert" || (*suffix) == "_expert")
  114. {
  115. QMessageBox msgBox;
  116. msgBox.setText("Your name can't be [_]algo or [_]random. or [_]expert");
  117. msgBox.exec();
  118. ok = false;
  119. }
  120. resultPath = new QString(QDir::currentPath()+"/"+(*suffix));
  121. if(QDir(*resultPath).exists())
  122. {
  123. QMessageBox msgBox;
  124. msgBox.setText("This id is already used. Choose another one or take the necessary action to solve the problem.");
  125. msgBox.exec();
  126. ok = false;
  127. }
  128. else
  129. {
  130. QDir(QDir::currentPath()).mkdir(*suffix);
  131. }
  132. if(ok)
  133. {
  134. delete filenames;
  135. delete filenames_json;
  136. delete choice;
  137. delete side;
  138. filenames = new QStringList();
  139. filenames_json = new QStringList();
  140. choice = new QStringList();
  141. side = new QStringList();
  142. QDirIterator it(*dataPath, {"*.jpg"}, QDir::Files);
  143. while (it.hasNext()) {
  144. filenames->append( it.next() );
  145. }
  146. for(int i = 0 ; i<filenames->size() ; i++)
  147. {
  148. QString jsonFile((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_algo)+".json");
  149. if(! (QFile(jsonFile).exists()) )
  150. {
  151. qDebug() << "WARNING !!!! " << jsonFile << " file missing";
  152. ok=false;
  153. }else{
  154. filenames_json->append(jsonFile);
  155. }
  156. }
  157. if(!ok)
  158. std::exit(EXIT_FAILURE);
  159. std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
  160. for(int i = 0 ; i<filenames->size()*2 ; i++)
  161. {
  162. int rand1 = std::rand()%filenames->size();
  163. int rand2 = std::rand()%filenames->size();
  164. filenames->swapItemsAt(rand1, rand2);
  165. filenames_json->swapItemsAt(rand1, rand2);
  166. }
  167. openedImage = -1;
  168. expeStarted = true;
  169. openNext();
  170. }
  171. }
  172. void
  173. Experiment::undo ()
  174. {
  175. openedImage = openedImage-2;
  176. choice->removeLast();
  177. if(openedImage < 1)
  178. undoAct->setEnabled(false);
  179. openNext();
  180. }
  181. void Experiment::leftImage(){
  182. if(expeStarted)
  183. {
  184. if((algo_side != 'L') && (algo_side != 'R'))
  185. {
  186. qDebug() << "Something went wrong";
  187. QApplication::quit();
  188. }
  189. if(algo_side == 'L')
  190. choice->append(QString("algo"));
  191. else
  192. choice->append(QString("random"));
  193. openNext();
  194. }
  195. }
  196. void Experiment::rightImage(){
  197. if(expeStarted)
  198. {
  199. if((algo_side != 'L') && (algo_side != 'R'))
  200. {
  201. qDebug() << "Something went wrong";
  202. QApplication::quit();
  203. }
  204. if(algo_side == 'R')
  205. choice->append(QString("algo"));
  206. else
  207. choice->append(QString("random"));
  208. openNext();
  209. }
  210. }
  211. void
  212. Experiment::openNext (){
  213. openedImage = openedImage+1;
  214. if(openedImage == 1)
  215. undoAct->setEnabled(true);
  216. progressBar->setValue((int)((openedImage+1.0)/(filenames->size())*100.0));
  217. if(openedImage >= filenames->size())
  218. {
  219. qDebug() << "That's all folks !" << Qt::endl;
  220. /* Saving result file*/
  221. QJsonObject root;
  222. QJsonArray choices;
  223. for (int i = 0; i < choice->size (); i++){
  224. QJsonObject choiceOnImage;
  225. choiceOnImage["index"] = i;
  226. choiceOnImage["image"] = QString(QFileInfo(filenames->at(i)).baseName());
  227. choiceOnImage["choice"] = QString(choice->at(i));
  228. choices.push_back(choiceOnImage);
  229. }
  230. root["choices"] = choices;
  231. QByteArray ba = QJsonDocument(root).toJson();
  232. QFile fout((*resultPath)+"/"+(*suffix)+QString::fromStdString (".json"));
  233. fout.open(QIODevice::WriteOnly);
  234. fout.write(ba);
  235. QMessageBox msgBox;
  236. msgBox.setText("That's all folks !\n\nThank you for your participation !");
  237. msgBox.exec();
  238. QApplication::quit();
  239. }
  240. else
  241. {
  242. waLeft->loadImage (filenames->at(openedImage).toStdString ());
  243. waRight->loadImage (filenames->at(openedImage).toStdString ());
  244. if(side->size() == openedImage)
  245. {
  246. std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
  247. if((std::rand()%2) == 0)
  248. algo_side='L';
  249. else
  250. algo_side='R';
  251. side->append(QString(algo_side));
  252. }
  253. else
  254. algo_side = (side->at(openedImage)[0]).toLatin1();
  255. if(algo_side == 'L')
  256. {
  257. waLeft->loadSL (filenames_json->at(openedImage).toStdString ());
  258. QString jsonFile = (*resultPath)+"/"+(QFileInfo(filenames->at(openedImage)).baseName()+(QString("_random"))+".json");
  259. if(QFile(jsonFile).exists()==false)
  260. {
  261. for(int i = 0 ; i < waLeft->getNumberOfLines() ; i++)
  262. waRight->addRandomSL();
  263. waRight->saveStrengthLine (jsonFile.toStdString ());
  264. }
  265. else
  266. {
  267. waRight->loadSL(jsonFile.toStdString());
  268. }
  269. }
  270. else
  271. {
  272. waRight->loadSL (filenames_json->at(openedImage).toStdString ());
  273. QString jsonFile = (*resultPath)+"/"+(QFileInfo(filenames->at(openedImage)).baseName()+(QString("_random"))+".json");
  274. if(QFile(jsonFile).exists() == false)
  275. {
  276. for(int i = 0 ; i < waRight->getNumberOfLines() ; i++)
  277. waLeft->addRandomSL();
  278. waLeft->saveStrengthLine (jsonFile.toStdString ());
  279. }
  280. else
  281. {
  282. waLeft->loadSL(jsonFile.toStdString());
  283. }
  284. }
  285. waLeft->setReadOnly(true);
  286. waRight->setReadOnly(true);
  287. waLeft->repaint();
  288. waRight->repaint();
  289. setFixedSize (waLeft->geometry().width()*2+spaceInBetween, waLeft->geometry().height()+waLeft->geometry().y()+spaceInBetween+leftButton->geometry().height()+22); // +22 ??!!
  290. }
  291. }
  292. void
  293. Experiment::createActions ()
  294. {
  295. startAct = new QAction (tr ("&Start"), this);
  296. startAct->setShortcuts (QKeySequence::Save);
  297. connect (startAct, &QAction::triggered, this, &Experiment::start);
  298. exitAct = new QAction (tr ("E&xit"), this);
  299. exitAct->setShortcuts (QKeySequence::Quit);
  300. connect (exitAct, &QAction::triggered, this, &QWidget::close);
  301. undoAct = new QAction (tr ("&Undo"), this);
  302. undoAct->setShortcuts (QKeySequence::Undo);
  303. undoAct->setEnabled(false);
  304. connect (undoAct, &QAction::triggered, this, &Experiment::undo);
  305. }
  306. void
  307. Experiment::createMenus ()
  308. {
  309. fileMenu = menuBar ()->addMenu (tr ("&File"));
  310. fileMenu->addAction (startAct);
  311. fileMenu->addSeparator ();
  312. fileMenu->addAction (exitAct);
  313. editMenu = menuBar ()->addMenu (tr ("&Edit"));
  314. editMenu->addAction (undoAct);
  315. }