diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 503f8ff0..1397e066 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -28,12 +28,14 @@ #include #include #include +#include // Camera ownership transferred MainWindow::MainWindow(Camera * camera, QWidget * parent) : QMainWindow(parent), camera_(camera), - lowestRefreshRate_(99) + lowestRefreshRate_(99), + objectsModified_(false) { ui_ = new Ui_mainWindow(); ui_->setupUi(this); @@ -88,6 +90,7 @@ MainWindow::MainWindow(Camera * camera, QWidget * parent) : connect(ui_->actionSetup_camera_from_video_file_2, SIGNAL(triggered()), this, SLOT(setupCameraFromVideoFile())); connect(ui_->actionAbout, SIGNAL(triggered()), aboutDialog_ , SLOT(exec())); connect(ui_->actionRestore_all_default_settings, SIGNAL(triggered()), ui_->toolBox, SLOT(resetAllPages())); + connect(ui_->actionRemove_all_objects, SIGNAL(triggered()), this, SLOT(removeAllObjects())); ui_->actionSetup_camera_from_video_file->setCheckable(true); ui_->actionSetup_camera_from_video_file_2->setCheckable(true); @@ -113,8 +116,33 @@ MainWindow::~MainWindow() void MainWindow::closeEvent(QCloseEvent * event) { - Settings::saveSettings(Settings::iniDefaultPath(), this->saveGeometry()); - QMainWindow::closeEvent(event); + bool quit = true; + this->stopProcessing(); + if(objectsModified_ && this->isVisible() && objects_.size()) + { + int ret = QMessageBox::question(this, tr("Save new objects"), tr("Do you want to save added objects?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); + switch(ret) + { + case QMessageBox::Yes: + quit = this->saveObjects(); + break; + case QMessageBox::Cancel: + quit = false; + break; + case QMessageBox::No: + default: + break; + } + } + if(quit) + { + Settings::saveSettings(Settings::iniDefaultPath(), this->saveGeometry()); + event->accept(); + } + else + { + event->ignore(); + } } ParametersToolBox * MainWindow::parametersToolBox() const @@ -122,72 +150,54 @@ ParametersToolBox * MainWindow::parametersToolBox() const return ui_->toolBox; } -bool MainWindow::loadObjects(const QString & fileName) +void MainWindow::loadObjects(const QString & dirPath) { - QFile file(fileName); - if(file.open(QIODevice::ReadOnly)) + QDir dir(dirPath); + if(dir.exists()) { - QDataStream in(&file); - while(!in.atEnd()) + QStringList filters; + filters << "*.png" << "*.jpg" << "*.bmp" << "*.tiff"; + QFileInfoList list = dir.entryInfoList(filters, QDir::Files, QDir::Name); + for(int i=0; iload(in); - bool alreadyLoaded = false; - for(int i=0; iid() == obj->id()) - { - alreadyLoaded = true; - break; - } - } - if(!alreadyLoaded) - { - objects_.append(obj); - showObject(obj); - } - else - { - delete obj; - } + this->addObjectFromFile(list.at(i).filePath()); + } + if(list.size()) + { + this->updateObjects(); } - file.close(); - return true; } - return false; } -void MainWindow::saveObjects(const QString & fileName) +void MainWindow::saveObjects(const QString & dirPath) { - QFile file(fileName); - file.open(QIODevice::WriteOnly); - QDataStream out(&file); - for(int i=0; isave(out); + for(int i=0; iimage().save(QString("%1/%2.bmp").arg(dirPath).arg(objects_.at(i)->id())); + } } - file.close(); } void MainWindow::loadObjects() { - QString fileName = QFileDialog::getOpenFileName(this, tr("Load objects..."), Settings::workingDirectory(), "*.obj"); - if(!fileName.isEmpty()) + QString dirPath = QFileDialog::getExistingDirectory(this, tr("Load objects..."), Settings::workingDirectory()); + if(!dirPath.isEmpty()) { - loadObjects(fileName); + loadObjects(dirPath); } } -void MainWindow::saveObjects() +bool MainWindow::saveObjects() { - QString fileName = QFileDialog::getSaveFileName(this, tr("Save objects..."), (Settings::workingDirectory() + "/") +Settings::currentDetectorType()+Settings::currentDescriptorType()+QString("%1.obj").arg(objects_.size()), "*.obj"); - if(!fileName.isEmpty()) + QString dirPath = QFileDialog::getExistingDirectory(this, tr("Save objects..."), Settings::workingDirectory()); + if(!dirPath.isEmpty()) { - if(!fileName.endsWith(".obj")) - { - fileName.append(".obj");//default - } - saveObjects(fileName); + saveObjects(dirPath); + return true; } + return false; } void MainWindow::removeObject(ObjWidget * object) @@ -200,6 +210,15 @@ void MainWindow::removeObject(ObjWidget * object) } } +void MainWindow::removeAllObjects() +{ + QList obj = objects_; + for(int i=0; istartProcessing(); } @@ -218,18 +239,60 @@ void MainWindow::addObjectsFromFiles() { for(int i=0; i(), cv::Mat(), img, "", "")); - this->showObject(objects_.last()); - cvReleaseImage(&img); - } + this->addObjectFromFile(fileNames.at(i)); } + objectsModified_ = true; updateObjects(); } } +void MainWindow::addObjectFromFile(const QString & filePath) +{ + if(!filePath.isNull()) + { + IplImage * img = cvLoadImage(filePath.toStdString().c_str(), CV_LOAD_IMAGE_GRAYSCALE); + if(img) + { + int id = 0; + QFileInfo file(filePath); + QStringList list = file.fileName().split('.'); + if(list.size()) + { + printf("asdfddsa %s\n", list.front().toStdString().c_str()); + bool ok = false; + id = list.front().toInt(&ok); + if(ok) + { + printf("id=%d\n", id); + for(int i=0; iid() == id) + { + if(this->isVisible()) + { + QMessageBox::warning(this, tr("Warning"), tr("Object %1 already added, a new ID will be generated.").arg(id)); + } + else + { + printf("WARNING: Object %d already added, a new ID will be generated.", id); + } + id = 0; + break; + } + } + } + else + { + id = 0; + } + } + objects_.append(new ObjWidget(id, std::vector(), cv::Mat(), img, "", "")); + this->showObject(objects_.last()); + cvReleaseImage(&img); + } + } +} + void MainWindow::loadSceneFromFile() { QString fileName = QFileDialog::getOpenFileName(this, tr("Load scene..."), Settings::workingDirectory(), tr("Image Files (*.png *.jpg *.bmp *.tiff)")); @@ -303,8 +366,6 @@ void MainWindow::showObject(ObjWidget * obj) connect(obj, SIGNAL(destroyed(QObject *)), detectorDescriptorType, SLOT(deleteLater())); connect(obj, SIGNAL(destroyed(QObject *)), vLayout, SLOT(deleteLater())); ui_->verticalLayout_objects->insertLayout(ui_->verticalLayout_objects->count()-1, vLayout); - - this->updateData(); } } diff --git a/src/MainWindow.h b/src/MainWindow.h index c30b0d28..180a3667 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -29,8 +29,8 @@ public: MainWindow(Camera * camera = 0, QWidget * parent = 0); virtual ~MainWindow(); - bool loadObjects(const QString & fileName); - void saveObjects(const QString & fileName); + void loadObjects(const QString & dirPath); + void saveObjects(const QString & dirPath); ParametersToolBox * parametersToolBox() const; @@ -44,12 +44,13 @@ public slots: private slots: void loadObjects(); - void saveObjects(); + bool saveObjects(); void addObject(); void addObjectsFromFiles(); void loadSceneFromFile(); void setupCameraFromVideoFile(); void removeObject(ObjWidget * object); + void removeAllObjects(); void update(const cv::Mat & image); void updateObjects(); void notifyParametersChanged(); @@ -58,6 +59,7 @@ signals: void objectsFound(const QMap > &); private: + void addObjectFromFile(const QString & filePath); void showObject(ObjWidget * obj); void updateData(); @@ -71,7 +73,7 @@ private: QTime updateRate_; QTime refreshStartTime_; int lowestRefreshRate_; - + bool objectsModified_; }; #endif /* MainWindow_H_ */ diff --git a/src/ui/mainWindow.ui b/src/ui/mainWindow.ui index cfeeee8c..b5542f71 100644 --- a/src/ui/mainWindow.ui +++ b/src/ui/mainWindow.ui @@ -160,6 +160,8 @@ + + @@ -387,7 +389,7 @@ 300 - 162 + 196 @@ -514,6 +516,11 @@ Setup camera from video file... + + + Remove all objects + +