Experiment.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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(!(QDir(*directoryname).exists()))
  108. {
  109. QMessageBox msgBox;
  110. msgBox.setText("Data files can't be found. data folder is nedded.");
  111. msgBox.exec();
  112. std::exit(EXIT_FAILURE);
  113. }
  114. if(!dir.isEmpty())
  115. {
  116. bool ok=false;
  117. suffix = new QString(QInputDialog::getText(this, tr("Your ID"),
  118. tr("ID to use :"), QLineEdit::Normal,
  119. QDir::home().dirName(), &ok));
  120. if((*suffix) == "algo" || (*suffix) == "random" || (*suffix) == "_algo" || (*suffix) == "_random")
  121. {
  122. QMessageBox msgBox;
  123. msgBox.setText("Your name can't be [_]algo or [_]random.");
  124. msgBox.exec();
  125. ok = false;
  126. }
  127. if(ok)
  128. {
  129. delete filenames;
  130. delete filenames_json;
  131. delete choice;
  132. filenames = new QStringList();
  133. filenames_json = new QStringList();
  134. choice = new QStringList();
  135. QDirIterator it(*directoryname, {"*.jpg"}, QDir::Files);
  136. while (it.hasNext()) {
  137. filenames->append( it.next() );
  138. }
  139. for(int i = 0 ; i<filenames->size() ; i++)
  140. {
  141. QString jsonFile(QDir::currentPath ().append("/").append(QFileInfo(filenames->at(i)).baseName().append(*suffix_IA).append(".json")));
  142. if(! (QFile(jsonFile).exists()) )
  143. {
  144. qDebug() << "ATTENTION !!!! " << jsonFile << " file missing";
  145. ok=false;
  146. }else{
  147. filenames_json->append(jsonFile);
  148. }
  149. }
  150. if(!ok)
  151. std::exit(EXIT_FAILURE);
  152. std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
  153. for(int i = 0 ; i<filenames->size()*2 ; i++)
  154. {
  155. int rand1 = std::rand()%filenames->size();
  156. int rand2 = std::rand()%filenames->size();
  157. filenames->swapItemsAt(rand1, rand2);
  158. filenames_json->swapItemsAt(rand1, rand2);
  159. }
  160. openedImage = -1;
  161. expeInit = true;
  162. openNext();
  163. }
  164. }
  165. }
  166. void Experiment::leftImage(){
  167. if(expeInit)
  168. {
  169. if((algo_side != 'L') && (algo_side != 'R'))
  170. {
  171. qDebug() << "Something went wrong";
  172. QApplication::quit();
  173. }
  174. if(algo_side == 'L')
  175. choice->append(QString("algo"));
  176. else
  177. choice->append(QString("random"));
  178. openNext();
  179. }
  180. }
  181. void Experiment::rightImage(){
  182. if(expeInit)
  183. {
  184. if((algo_side != 'L') && (algo_side != 'R'))
  185. {
  186. qDebug() << "Something went wrong";
  187. QApplication::quit();
  188. }
  189. if(algo_side == 'R')
  190. choice->append(QString("algo"));
  191. else
  192. choice->append(QString("random"));
  193. openNext();
  194. }
  195. }
  196. void
  197. Experiment::openNext (){
  198. openedImage = openedImage+1;
  199. progressBar->setValue((int)((openedImage+1.0)/(filenames->size())*100.0));
  200. if(openedImage >= filenames->size())
  201. {
  202. qDebug() << "That's all folks !" << Qt::endl;
  203. /* Saving result file*/
  204. QJsonObject root;
  205. QJsonArray choices;
  206. for (int i = 0; i < choice->size (); i++){
  207. QJsonObject choiceOnImage;
  208. choiceOnImage["index"] = i;
  209. choiceOnImage["image"] = QString(filenames->at(i));
  210. choiceOnImage["choice"] = QString(choice->at(i));
  211. choices.push_back(choiceOnImage);
  212. }
  213. root["choices"] = choices;
  214. QByteArray ba = QJsonDocument(root).toJson();
  215. QFile fout((*suffix)+QString::fromStdString (".json"));
  216. fout.open(QIODevice::WriteOnly);
  217. fout.write(ba);
  218. QApplication::quit();
  219. }
  220. else
  221. {
  222. waLeft->loadImage (filenames->at(openedImage).toStdString ());
  223. waRight->loadImage (filenames->at(openedImage).toStdString ());
  224. std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
  225. if((std::rand()%2) == 0)
  226. {
  227. waLeft->loadSL (filenames_json->at(openedImage).toStdString ());
  228. algo_side='L';
  229. for(int i = 0 ; i < waLeft->getNumberOfLines() ; i++)
  230. {
  231. waRight->addRandomSL();
  232. QString jsonFile = QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(QString("_random")).append(".json"));
  233. waRight->saveStrengthLine (jsonFile.toStdString ());
  234. }
  235. }
  236. else
  237. {
  238. waRight->loadSL (filenames_json->at(openedImage).toStdString ());
  239. algo_side='R';
  240. for(int i = 0 ; i < waRight->getNumberOfLines() ; i++)
  241. {
  242. waLeft->addRandomSL();
  243. QString jsonFile = QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(QString("_random")).append(".json"));
  244. waLeft->saveStrengthLine (jsonFile.toStdString ());
  245. }
  246. }
  247. waLeft->setReadOnly(true);
  248. waRight->setReadOnly(true);
  249. waLeft->repaint();
  250. waRight->repaint();
  251. setFixedSize (waLeft->geometry().width()*2+spaceInBetween, waLeft->geometry().height()+waLeft->geometry().y()+spaceInBetween+leftButton->geometry().height()+22); // +22 ??!!
  252. }
  253. }
  254. void
  255. Experiment::createActions ()
  256. {
  257. startAct = new QAction (tr ("&Start"), this);
  258. startAct->setShortcuts (QKeySequence::Save);
  259. connect (startAct, &QAction::triggered, this, &Experiment::start);
  260. exitAct = new QAction (tr ("E&xit"), this);
  261. exitAct->setShortcuts (QKeySequence::Quit);
  262. connect (exitAct, &QAction::triggered, this, &QWidget::close);
  263. }
  264. void
  265. Experiment::createMenus ()
  266. {
  267. fileMenu = menuBar ()->addMenu (tr ("&File"));
  268. fileMenu->addAction (startAct);
  269. fileMenu->addSeparator ();
  270. fileMenu->addAction (exitAct);
  271. }