Experiment.cpp 8.4 KB

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