123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- #include <cstdlib>
- #include <ctime>
- #include <QGuiApplication>
- #include <QScreen>
- #include <QWidget>
- #include <QMenuBar>
- #include <QFileDialog>
- #include <QInputDialog>
- #include <QDirIterator>
- #include <QVBoxLayout>
- #include <QHBoxLayout>
- #include <QPushButton>
- #include <QTextStream>
- #include <QJsonObject>
- #include <QJsonArray>
- #include <QJsonDocument>
- #include <QMessageBox>
- #include <QDebug>
- #include "Experiment.hpp"
- Experiment::Experiment ():QMainWindow()
- {
- createActions ();
- createMenus ();
-
- filenames = NULL;
- //filenames_json = NULL;
- experimentation = NULL;
- suffix = NULL;
- choice = NULL;
- side = NULL;
- dataPath = new QString(QDir::currentPath()+"/data");
- resultPath = NULL;
- //algo_side='X';
- expeStarted = false;
- openedImage = -1;
- 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);
- connect(waLeft, SIGNAL(click()), this, SLOT(leftImage()));
- 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);
- connect(waRight, SIGNAL(click()), this, SLOT(rightImage()));
- 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);
- QMessageBox msgBox;
- msgBox.setText("Welcome !\nYou are about to take part in an experiment.\nFor instructions, please refer to the pdf available in the same folder as this software.\n\nWhen you're ready, start the experiment with:\nFile -> Start\nor\nCTRL+S.\n\nThank you.");
- msgBox.exec();
- }
- void
- Experiment::start ()
- {
- bool ok = false;
-
- if(!(QDir(*dataPath).exists()))
- {
- QMessageBox msgBox;
- msgBox.setText("Data files can't be found. data folder is nedded.");
- msgBox.exec();
- std::exit(EXIT_FAILURE);
- }
-
- suffix = new QString(QInputDialog::getText(this, tr("Your ID"),
- tr("ID to use :"), QLineEdit::Normal,
- QDir::home().dirName(), &ok));
- if((*suffix) == "algo" || (*suffix) == "_algo" || (*suffix) == "random" || (*suffix) == "_random" || (*suffix) == "expert" || (*suffix) == "_expert")
- {
- QMessageBox msgBox;
- msgBox.setText("Your name can't be [_]algo or [_]random. or [_]expert");
- msgBox.exec();
- ok = false;
- }
- resultPath = new QString(QDir::currentPath()+"/"+(*suffix));
- if(QDir(*resultPath).exists())
- {
- QMessageBox msgBox;
- msgBox.setText("This id is already used. Choose another one or take the necessary action to solve the problem.");
- msgBox.exec();
- ok = false;
- }
- else
- {
- QDir(QDir::currentPath()).mkdir(*suffix);
- }
-
- if(ok)
- {
- delete filenames;
- //delete filenames_json;
- delete experimentation;
- delete choice;
- delete side;
- filenames = new QStringList();
- //filenames_json = new QStringList();
- experimentation = new QList<QStringList*>();
- choice = new QStringList();
- side = new QStringList();
-
- QDirIterator it(*dataPath, {"*.jpg"}, QDir::Files);
- while (it.hasNext()) {
- filenames->append( it.next() );
- }
- std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
- for(int i = 0 ; i<filenames->size() ; i++)
- {
- QString jsonFile((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_algo)+".json");
- QString jsonFile2((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_expert)+".json");
- if((! (QFile(jsonFile).exists()) ) || (! (QFile(jsonFile2).exists()) ))
- {
- qDebug() << "WARNING !!!! " << jsonFile << " file missing";
- qDebug() << "OR\nWARNING !!!! " << jsonFile2 << " file missing";
- ok=false;
- }else
- {
- //filenames_json->append(jsonFile);
- QStringList *toAppend = new QStringList();
- toAppend->append(filenames->at(i));
- if((std::rand()%2) == 0)
- {
- toAppend->append(QString((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_algo)+".json"));
- toAppend->append(QString((*resultPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_random)+".json"));
- }
- else
- {
- toAppend->append(QString((*resultPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_random)+".json"));
- toAppend->append(QString((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_algo)+".json"));
- }
- experimentation->append(toAppend);
- toAppend = new QStringList();
- toAppend->append(filenames->at(i));
- if((std::rand()%2) == 0)
- {
- toAppend->append(QString((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_algo)+".json"));
- toAppend->append(QString((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_expert)+".json"));
- }
- else
- {
- toAppend->append(QString((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_expert)+".json"));
- toAppend->append(QString((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_algo)+".json"));
- }
- experimentation->append(toAppend);
- }
- }
- for(int i=0 ; i<experimentation->size() ; i++)
- {
- // std::cout << experimentation->at(i)->at(0).toStdString() << " - " << experimentation->at(i)->at(1).toStdString() << " - " << experimentation->at(i)->at(2).toStdString() << std::endl;
- // std::cout << experimentation->at(i)->at(0).toStdString() << std::endl;
- }
- std::cout << std::endl << std::endl;
- if(!ok)
- std::exit(EXIT_FAILURE);
-
- /*for(int i = 0 ; i<filenames->size()*2 ; i++)
- {
- int rand1 = std::rand()%filenames->size();
- int rand2 = std::rand()%filenames->size();
- filenames->swapItemsAt(rand1, rand2);
- filenames_json->swapItemsAt(rand1, rand2);
- }*/
- for(int i = 0 ; i<experimentation->size()*4 ; i++)
- {
- int rand1 = std::rand()%experimentation->size();
- int rand2 = std::rand()%experimentation->size();
- experimentation->swapItemsAt(rand1, rand2);
- }
-
- openedImage = -1;
- expeStarted = true;
- openNext();
- }
-
- }
- void
- Experiment::undo ()
- {
- openedImage = openedImage-2;
- choice->removeLast();
-
- if(openedImage < 1)
- undoAct->setEnabled(false);
- openNext();
- }
- void Experiment::leftImage(){
- if(expeStarted)
- {
- /*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"));*/
- if(experimentation->at(openedImage)->at(1).at(experimentation->at(openedImage)->at(1).length()-6) == 'o')
- {
- choice->append(QString("algo"));
- // std::cout << "Choix algo" << std::endl;
- }
- if(experimentation->at(openedImage)->at(1).at(experimentation->at(openedImage)->at(1).length()-6) == 'm')
- {
- choice->append(QString("random"));
- // std::cout << "Choix random" << std::endl;
- }
- if(experimentation->at(openedImage)->at(1).at(experimentation->at(openedImage)->at(1).length()-6) == 't')
- {
- choice->append(QString("expert"));
- // std::cout << "Choix expert" << std::endl;
- }
-
- openNext();
- }
- }
- void Experiment::rightImage(){
- if(expeStarted)
- {
- /*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"));*/
- // std::cout << experimentation->at(openedImage)->at(2).at(experimentation->at(openedImage)->at(2).length()-6).toLatin1() << std::endl;
- if(experimentation->at(openedImage)->at(2).at(experimentation->at(openedImage)->at(2).length()-6).toLatin1() == 'o')
- {
- choice->append(QString("algo"));
- // std::cout << "Choix algo" << std::endl;
- }
- if(experimentation->at(openedImage)->at(2).at(experimentation->at(openedImage)->at(2).length()-6).toLatin1() == 'm')
- {
- choice->append(QString("random"));
- // std::cout << "Choix random" << std::endl;
- }
- if(experimentation->at(openedImage)->at(2).at(experimentation->at(openedImage)->at(2).length()-6).toLatin1() == 't')
- {
- choice->append(QString("expert"));
- // std::cout << "Choix expert" << std::endl;
- }
-
-
- openNext();
- }
- }
- void
- Experiment::openNext (){
- openedImage = openedImage+1;
-
-
- if(openedImage == 1)
- undoAct->setEnabled(true);
-
- progressBar->setValue((int)((openedImage+1.0)/(experimentation->size())*100.0));
-
- if(openedImage >= experimentation->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(QFileInfo(experimentation->at(i)->at(0)).baseName());
- QString possibleChoices1(QFileInfo(experimentation->at(i)->at(1)).baseName().split("_").at(1));
- QString possibleChoices2(QFileInfo(experimentation->at(i)->at(2)).baseName().split("_").at(1));
- choiceOnImage["choices"] = possibleChoices1.append(", ").append(possibleChoices2);
- choiceOnImage["choice"] = QString(choice->at(i));
- choices.push_back(choiceOnImage);
- }
-
- root["choices"] = choices;
-
- QByteArray ba = QJsonDocument(root).toJson();
- QFile fout((*resultPath)+"/"+(*suffix)+QString::fromStdString (".json"));
- fout.open(QIODevice::WriteOnly);
- fout.write(ba);
-
- QMessageBox msgBox;
- msgBox.setText("That's all folks !\n\nThank you for your participation !");
- msgBox.exec();
- QApplication::quit();
- }
- else
- {
- // std::cout << experimentation->at(openedImage)->at(0).toStdString() << " - " << experimentation->at(openedImage)->at(1).toStdString() << " - " << experimentation->at(openedImage)->at(2).toStdString() << std::endl;
-
- // waLeft->loadImage (filenames->at(openedImage).toStdString ());
- // waRight->loadImage (filenames->at(openedImage).toStdString ());
- waLeft->loadImage (experimentation->at(openedImage)->at(0).toStdString ());
- waRight->loadImage (experimentation->at(openedImage)->at(0).toStdString ());
- if(QFile(experimentation->at(openedImage)->at(1)).exists()==false)
- {
- waRight->loadSL(experimentation->at(openedImage)->at(2).toStdString());
- for(int i = 0 ; i < waRight->getNumberOfLines() ; i++)
- waLeft->addRandomSL();
- waLeft->saveStrengthLine (experimentation->at(openedImage)->at(1).toStdString ());
- }
- else
- {
- if(QFile(experimentation->at(openedImage)->at(2)).exists()==false)
- {
- waLeft->loadSL(experimentation->at(openedImage)->at(1).toStdString());
- for(int i = 0 ; i < waLeft->getNumberOfLines() ; i++)
- waRight->addRandomSL();
- waRight->saveStrengthLine (experimentation->at(openedImage)->at(2).toStdString ());
- }
- else
- {
- waLeft->loadSL(experimentation->at(openedImage)->at(1).toStdString());
- waRight->loadSL(experimentation->at(openedImage)->at(2).toStdString());
- }
- }
- // right display
-
- // else
- // waRight->loadSL(experimentation->at(openedImage)->at(2).toStdString());
- /*if(side->size() == openedImage)
- {
- std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand()));
- if((std::rand()%2) == 0)
- algo_side='L';
- else
- algo_side='R';
- side->append(QString(algo_side));
- }
- else
- algo_side = (side->at(openedImage)[0]).toLatin1();*/
- /*if(algo_side == 'L')
- {
- waLeft->loadSL (filenames_json->at(openedImage).toStdString ());
- QString jsonFile = (*resultPath)+"/"+(QFileInfo(filenames->at(openedImage)).baseName()+(QString("_random"))+".json");
- if(QFile(jsonFile).exists()==false)
- {
- for(int i = 0 ; i < waLeft->getNumberOfLines() ; i++)
- waRight->addRandomSL();
- waRight->saveStrengthLine (jsonFile.toStdString ());
- }
- else
- {
- waRight->loadSL(jsonFile.toStdString());
- }
- }
- else
- {
- waRight->loadSL (filenames_json->at(openedImage).toStdString ());
- QString jsonFile = (*resultPath)+"/"+(QFileInfo(filenames->at(openedImage)).baseName()+(QString("_random"))+".json");
- if(QFile(jsonFile).exists() == false)
- {
- for(int i = 0 ; i < waRight->getNumberOfLines() ; i++)
- waLeft->addRandomSL();
- waLeft->saveStrengthLine (jsonFile.toStdString ());
- }
- else
- {
- waLeft->loadSL(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);
- undoAct = new QAction (tr ("&Undo"), this);
- undoAct->setShortcuts (QKeySequence::Undo);
- undoAct->setEnabled(false);
- connect (undoAct, &QAction::triggered, this, &Experiment::undo);
- }
- void
- Experiment::createMenus ()
- {
- fileMenu = menuBar ()->addMenu (tr ("&File"));
- fileMenu->addAction (startAct);
- fileMenu->addSeparator ();
- fileMenu->addAction (exitAct);
- editMenu = menuBar ()->addMenu (tr ("&Edit"));
- editMenu->addAction (undoAct);
- }
|