Fixed issue 2 : save/load from directory, ask user to save objects on quit

Added new action "Remove all objects"

git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@78 620bd6b2-0a58-f614-fd9a-1bd335dccda9
This commit is contained in:
matlabbe 2012-02-04 00:11:11 +00:00
parent cd6b7e8680
commit aa1d2f67e9
3 changed files with 133 additions and 63 deletions

View File

@ -28,12 +28,14 @@
#include <QtGui/QSpinBox>
#include <QtGui/QStatusBar>
#include <QtGui/QProgressDialog>
#include <QtGui/QCloseEvent>
// 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)
{
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());
QMainWindow::closeEvent(event);
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; i<list.size(); ++i)
{
ObjWidget * obj = new ObjWidget();
obj->load(in);
bool alreadyLoaded = false;
for(int i=0; i<objects_.size(); ++i)
this->addObjectFromFile(list.at(i).filePath());
}
if(list.size())
{
if(objects_.at(i)->id() == obj->id())
{
alreadyLoaded = true;
break;
this->updateObjects();
}
}
if(!alreadyLoaded)
{
objects_.append(obj);
showObject(obj);
}
else
{
delete obj;
}
}
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);
QDir dir(dirPath);
if(dir.exists())
{
for(int i=0; i<objects_.size(); ++i)
{
objects_.at(i)->save(out);
objects_.at(i)->image().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<ObjWidget*> obj = objects_;
for(int i=0; i<obj.size(); ++i)
{
removeObject(obj[i]);
}
}
void MainWindow::addObject()
{
disconnect(camera_, SIGNAL(imageReceived(const cv::Mat &)), this, SLOT(update(const cv::Mat &)));
@ -207,6 +226,8 @@ void MainWindow::addObject()
if(dialog.exec() == QDialog::Accepted)
{
showObject(objects_.last());
updateData();
objectsModified_ = true;
}
this->startProcessing();
}
@ -218,16 +239,58 @@ void MainWindow::addObjectsFromFiles()
{
for(int i=0; i<fileNames.size(); ++i)
{
IplImage * img = cvLoadImage(fileNames[i].toStdString().c_str(), CV_LOAD_IMAGE_GRAYSCALE);
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)
{
objects_.append(new ObjWidget(0, std::vector<cv::KeyPoint>(), cv::Mat(), 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; i<objects_.size(); ++i)
{
if(objects_.at(i)->id() == 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::KeyPoint>(), cv::Mat(), img, "", ""));
this->showObject(objects_.last());
cvReleaseImage(&img);
}
}
updateObjects();
}
}
void MainWindow::loadSceneFromFile()
@ -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();
}
}

View File

@ -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<int, QPair<QRect, QTransform> > &);
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_ */

View File

@ -160,6 +160,8 @@
<addaction name="actionSetup_camera_from_video_file_2"/>
<addaction name="separator"/>
<addaction name="actionRestore_all_default_settings"/>
<addaction name="separator"/>
<addaction name="actionRemove_all_objects"/>
</widget>
<widget class="QMenu" name="menuView">
<property name="title">
@ -387,7 +389,7 @@
<property name="minimumSize">
<size>
<width>300</width>
<height>162</height>
<height>196</height>
</size>
</property>
<property name="windowTitle">
@ -514,6 +516,11 @@
<string>Setup camera from video file...</string>
</property>
</action>
<action name="actionRemove_all_objects">
<property name="text">
<string>Remove all objects</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>