Experiment.cpp 9.2 KB

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