Experiment.cpp 7.1 KB

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