Experiment.cpp 7.5 KB

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