#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "Experiment.hpp" Experiment::Experiment ():QMainWindow() { createActions (); createMenus (); filenames = NULL; filenames_json = NULL; suffix = NULL; choice = NULL; algo_side='X'; dNFixed = false; expeInit = false; setWindowTitle (tr ("DSL - Draw Strength Line")); QScreen *screen = QGuiApplication::primaryScreen (); QRect screenGeometry = screen->geometry (); screenWidth = screenGeometry.width () - 200; screenHeight = screenGeometry.height () - 200; QWidget *central= new QWidget(this); setCentralWidget (central); setContentsMargins(0, 0, 0, 0); QVBoxLayout *layoutV = new QVBoxLayout(central); layoutV->setSpacing(0); layoutV->setContentsMargins(0, 0, 0, 0); central->setLayout(layoutV); // First line of the interface QHBoxLayout *layoutH1 = new QHBoxLayout(central); layoutH1->setSpacing(0); layoutH1->setContentsMargins(0, 0, 0, 0); layoutV->addLayout(layoutH1); waLeft = new WorkArea (((screenWidth-spaceInBetween)/2), screenHeight, central); waLeft->setContentsMargins(0, 0, 0, 0); layoutH1->addWidget(waLeft); QWidget *w1SpaceInBetween = new QWidget(central); w1SpaceInBetween->setContentsMargins(0, 0, 0, 0); w1SpaceInBetween->setFixedSize(spaceInBetween,0); layoutH1->addWidget(w1SpaceInBetween); waRight = new WorkArea (((screenWidth-spaceInBetween)/2), screenHeight, central); waRight->setContentsMargins(0, 0, 0, 0); layoutH1->addWidget(waRight); // second line of interface QWidget *hSpaceInBetween = new QWidget(central); hSpaceInBetween->setContentsMargins(0, 0, 0, 0); hSpaceInBetween->setFixedSize(0, spaceInBetween); layoutV->addWidget(hSpaceInBetween); // third line of interface QHBoxLayout *layoutH2 = new QHBoxLayout(central); layoutH2->setSpacing(0); layoutH2->setContentsMargins(0, 0, 0, 0); layoutV->addLayout(layoutH2); leftButton = new QPushButton("Image de gauche", central); leftButton->setFixedHeight(30); connect(leftButton, SIGNAL(clicked(bool)), this, SLOT(leftImage())); layoutH2->addWidget(leftButton); QWidget *w2SpaceInBetween = new QWidget(); w2SpaceInBetween->setContentsMargins(0, 0, 0, 0); w2SpaceInBetween->setFixedSize(spaceInBetween,0); layoutH2->addWidget(w2SpaceInBetween); rightButton = new QPushButton("Image de droite", central); rightButton->setFixedHeight(30); connect(rightButton, SIGNAL(clicked(bool)), this, SLOT(rightImage())); layoutH2->addWidget(rightButton); // Fourth line of the interface progressBar = new QProgressBar(central); progressBar->setFixedHeight(20); progressBar->setRange(0, maxProgressBar); progressBar->setValue(0); layoutV->addWidget(progressBar); timer = new QTimer(central); timer->setSingleShot(false); timer->setInterval(time); setFixedSize (screenWidth, screenHeight); } void Experiment::start () { QString dir; QString* directoryname; if(!dNFixed) { dir = QString(QDir::currentPath()+"/data"); directoryname = new QString(dir); QDir::setCurrent(*directoryname); dNFixed = true; }else { dir = QString(QDir::currentPath()); directoryname = new QString(dir); } if(!(QDir(*directoryname).exists())) { QMessageBox msgBox; msgBox.setText("Data files can't be found. data folder is nedded."); msgBox.exec(); std::exit(EXIT_FAILURE); } if(!dir.isEmpty()) { bool ok=false; suffix = new QString(QInputDialog::getText(this, tr("Your ID"), tr("ID to use :"), QLineEdit::Normal, QDir::home().dirName(), &ok)); if((*suffix) == "algo" || (*suffix) == "random" || (*suffix) == "_algo" || (*suffix) == "_random") { QMessageBox msgBox; msgBox.setText("Your name can't be [_]algo or [_]random."); msgBox.exec(); ok = false; } if(ok) { delete filenames; delete filenames_json; delete choice; filenames = new QStringList(); filenames_json = new QStringList(); choice = new QStringList(); QDirIterator it(*directoryname, {"*.jpg"}, QDir::Files); while (it.hasNext()) { filenames->append( it.next() ); } for(int i = 0 ; isize() ; i++) { QString jsonFile(QDir::currentPath ().append("/").append(QFileInfo(filenames->at(i)).baseName().append(*suffix_IA).append(".json"))); if(! (QFile(jsonFile).exists()) ) { qDebug() << "ATTENTION !!!! " << jsonFile << " file missing"; ok=false; }else{ filenames_json->append(jsonFile); } } if(!ok) std::exit(EXIT_FAILURE); std::srand(static_cast(std::time(nullptr)+std::rand())); for(int i = 0 ; isize()*2 ; i++) { int rand1 = std::rand()%filenames->size(); int rand2 = std::rand()%filenames->size(); filenames->swapItemsAt(rand1, rand2); filenames_json->swapItemsAt(rand1, rand2); } openedImage = -1; expeInit = true; openNext(); } } } void Experiment::leftImage(){ if(expeInit) { if((algo_side != 'L') && (algo_side != 'R')) { qDebug() << "Something went wrong"; QApplication::quit(); } if(algo_side == 'L') choice->append(QString("algo")); else choice->append(QString("random")); openNext(); } } void Experiment::rightImage(){ if(expeInit) { if((algo_side != 'L') && (algo_side != 'R')) { qDebug() << "Something went wrong"; QApplication::quit(); } if(algo_side == 'R') choice->append(QString("algo")); else choice->append(QString("random")); openNext(); } } void Experiment::openNext (){ openedImage = openedImage+1; progressBar->setValue((int)((openedImage+1.0)/(filenames->size())*100.0)); if(openedImage >= filenames->size()) { qDebug() << "That's all folks !" << Qt::endl; /* Saving result file*/ QJsonObject root; QJsonArray choices; for (int i = 0; i < choice->size (); i++){ QJsonObject choiceOnImage; choiceOnImage["index"] = i; choiceOnImage["image"] = QString(filenames->at(i)); choiceOnImage["choice"] = QString(choice->at(i)); choices.push_back(choiceOnImage); } root["choices"] = choices; QByteArray ba = QJsonDocument(root).toJson(); QFile fout((*suffix)+QString::fromStdString (".json")); fout.open(QIODevice::WriteOnly); fout.write(ba); QApplication::quit(); } else { waLeft->loadImage (filenames->at(openedImage).toStdString ()); waRight->loadImage (filenames->at(openedImage).toStdString ()); std::srand(static_cast(std::time(nullptr)+std::rand())); if((std::rand()%2) == 0) { waLeft->loadSL (filenames_json->at(openedImage).toStdString ()); algo_side='L'; for(int i = 0 ; i < waLeft->getNumberOfLines() ; i++) { waRight->addRandomSL(); QString jsonFile = QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(QString("_random")).append(".json")); waRight->saveStrengthLine (jsonFile.toStdString ()); } } else { waRight->loadSL (filenames_json->at(openedImage).toStdString ()); algo_side='R'; for(int i = 0 ; i < waRight->getNumberOfLines() ; i++) { waLeft->addRandomSL(); QString jsonFile = QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(QString("_random")).append(".json")); waLeft->saveStrengthLine (jsonFile.toStdString ()); } } waLeft->setReadOnly(true); waRight->setReadOnly(true); waLeft->repaint(); waRight->repaint(); setFixedSize (waLeft->geometry().width()*2+spaceInBetween, waLeft->geometry().height()+waLeft->geometry().y()+spaceInBetween+leftButton->geometry().height()+22); // +22 ??!! } } void Experiment::createActions () { startAct = new QAction (tr ("&Start"), this); startAct->setShortcuts (QKeySequence::Save); connect (startAct, &QAction::triggered, this, &Experiment::start); exitAct = new QAction (tr ("E&xit"), this); exitAct->setShortcuts (QKeySequence::Quit); connect (exitAct, &QAction::triggered, this, &QWidget::close); } void Experiment::createMenus () { fileMenu = menuBar ()->addMenu (tr ("&File")); fileMenu->addAction (startAct); fileMenu->addSeparator (); fileMenu->addAction (exitAct); }