浏览代码

-added loadable settings file(ImageLabeler.ini by default, -c or -config /path/to/ini/file in the command line)

gapchich 13 年之前
父节点
当前提交
65d6969a66
共有 5 个文件被更改,包括 85 次插入10 次删除
  1. 46 1
      ImageLabeler.cpp
  2. 8 1
      ImageLabeler.h
  3. 14 6
      OptionsForm.cpp
  4. 3 1
      OptionsForm.h
  5. 14 1
      main.cpp

+ 46 - 1
ImageLabeler.cpp

@@ -17,6 +17,7 @@
 #include <QGridLayout>
 #include <QPixmap>
 #include <QLabel>
+#include <QCheckBox>
 #include <QScrollArea>
 #include <QPushButton>
 #include <QButtonGroup>
@@ -31,9 +32,10 @@
 #include <QDomDocument>
 #include <QFile>
 #include <QKeyEvent>
+#include <QSettings>
 #include <QDebug>
 
-ImageLabeler::ImageLabeler(QWidget *aParent) :
+ImageLabeler::ImageLabeler(QWidget *aParent, QString aSettingsPath) :
 	QMainWindow(aParent)
 {
 	/*
@@ -654,6 +656,13 @@ ImageLabeler::ImageLabeler(QWidget *aParent) :
 		SLOT(onOptionsSet())
 		);
 
+	QString settingsPath = aSettingsPath;
+	if (settingsPath.isEmpty())
+		settingsPath = QString("ImageLabeler.ini");
+
+	settings_ = new QSettings(settingsPath, QSettings::IniFormat, this);
+
+	readSettings(settings_);
 
 	image_holder_->setBoundingBoxList(&list_bounding_box_);
 	image_holder_->setPolygonList(&list_polygon_);
@@ -758,6 +767,35 @@ ImageLabeler::~ImageLabeler()
 	}
 
 	delete list_images_;
+	delete settings_;
+}
+
+bool
+ImageLabeler::readSettings(QSettings *aSettings)
+{
+	aSettings->beginGroup("global");
+	auto_color_generation_ =
+		aSettings->value("/auto_label_color_generation", 0).toBool();
+	options_form_.setAutoColorGeneration(auto_color_generation_);
+	PASCALpath_ = aSettings->value("/PASCAL_root_path", "").toString();
+
+	return true;
+}
+
+bool
+ImageLabeler::writeSettings(QSettings *aSettings)
+{
+	aSettings->beginGroup("global");
+	aSettings->setValue("/auto_label_color_generation", auto_color_generation_);
+	aSettings->setValue("/PASCAL_root_path", PASCALpath_);
+
+	return true;
+}
+
+void
+ImageLabeler::writeSettings()
+{
+	writeSettings(settings_);
 }
 
 void
@@ -3488,6 +3526,13 @@ ImageLabeler::wheelEvent(QWheelEvent *anEvent)
 	}
 }
 
+void
+ImageLabeler::closeEvent(QCloseEvent *anEvent)
+{
+	Q_UNUSED(anEvent)
+	writeSettings();
+}
+
 /*
  *
  */

+ 8 - 1
ImageLabeler.h

@@ -32,6 +32,7 @@ class QListWidgetItem;
 class QButtonGroup;
 class QDomDocument;
 class QDomElement;
+class QSettings;
 
 struct Image {
 	QString image_;
@@ -49,7 +50,10 @@ protected:
 	void wheelEvent(QWheelEvent *anEvent);
 	void keyPressEvent(QKeyEvent *anEvent);
 	void keyReleaseEvent(QKeyEvent *anEvent);
+	void closeEvent(QCloseEvent *anEvent);
 
+	bool readSettings(QSettings *aSettings);
+	bool writeSettings(QSettings *aSettings);
 	void getImagesFromDir(const QDir &dir);
 	void showWarning(const QString text);
 	bool askForUnsavedData();
@@ -89,7 +93,7 @@ protected:
 	bool selectImage(int anImageID);
 
 public:
-	ImageLabeler(QWidget *aParent = 0);
+	ImageLabeler(QWidget *aParent = 0, QString aSettingsPath = QString());
 	virtual ~ImageLabeler();
 
 public slots:
@@ -151,6 +155,7 @@ public slots:
 	void selectImage(QListWidgetItem *);
 	void removeImage();
 	void imageListPopupMenu(const QPoint &);
+	void writeSettings();
 
 private:
 	/* menu */
@@ -273,6 +278,8 @@ private:
 	/* flags */
 	bool interrupt_search_;
 	bool unsaved_data_;
+
+	QSettings *settings_;
 };
 
 #endif /* __IMAGELABELER_H__ */

+ 14 - 6
OptionsForm.cpp

@@ -10,6 +10,7 @@
 #include <QCheckBox>
 #include <QPushButton>
 #include <QLabel>
+#include <QLineEdit>
 #include <QBoxLayout>
 #include <QMessageBox>
 #include <QApplication>
@@ -32,7 +33,7 @@ OptionsForm::OptionsForm(QWidget *aParent)
 	auto_color_generation_->setText(tr("Automatic label color generation"));
 	button_set_PASCAL_root_ = new QPushButton(this);
 	button_set_PASCAL_root_->setText(tr("set PASCAL root path"));
-	label_PASCAL_root_ = new QLabel("", this);
+	edit_PASCAL_root_ = new QLineEdit("", this);
 	button_ok_ = new QPushButton(this);
 	button_ok_->setText(tr("OK"));
 	button_cancel_ = new QPushButton(this);
@@ -43,7 +44,7 @@ OptionsForm::OptionsForm(QWidget *aParent)
 	layout_v_->addLayout(layout_h_);
 
 	layout_PASCAL_root_->addWidget(button_set_PASCAL_root_);
-	layout_PASCAL_root_->addWidget(label_PASCAL_root_);
+	layout_PASCAL_root_->addWidget(edit_PASCAL_root_);
 
 	layout_h_->addWidget(button_ok_);
 	layout_h_->addWidget(button_cancel_);
@@ -75,7 +76,7 @@ OptionsForm::~OptionsForm()
 {
 	delete auto_color_generation_;
 	delete button_set_PASCAL_root_;
-	delete label_PASCAL_root_;
+	delete edit_PASCAL_root_;
 	delete button_ok_;
 	delete button_cancel_;
 
@@ -119,7 +120,7 @@ OptionsForm::newPascalPath()
 	}
 
 	*PASCALpath_ = newPath;
-	label_PASCAL_root_->setText(newPath);
+	edit_PASCAL_root_->setText(newPath);
 }
 
 bool
@@ -128,6 +129,12 @@ OptionsForm::autoColorGeneration()
 	return auto_color_generation_->isChecked();
 }
 
+void
+OptionsForm::setAutoColorGeneration(bool flag)
+{
+	auto_color_generation_->setChecked(flag);
+}
+
 void
 OptionsForm::showOptions()
 {
@@ -137,10 +144,11 @@ OptionsForm::showOptions()
 	}
 
 	if (PASCALpath_->isEmpty())
-		label_PASCAL_root_->setText(tr("root path is not set yet"));
+		edit_PASCAL_root_->setText(tr("root path is not set yet"));
 	else
-		label_PASCAL_root_->setText(*PASCALpath_);
+		edit_PASCAL_root_->setText(*PASCALpath_);
 
+	adjustSize();
 	show();
 }
 

+ 3 - 1
OptionsForm.h

@@ -12,6 +12,7 @@
 
 class QCheckBox;
 class QPushButton;
+class QLineEdit;
 class QLabel;
 class QVBoxLayout;
 class QHBoxLayout;
@@ -32,6 +33,7 @@ public slots:
 	void setOptions();
 	void showOptions();
 	void newPascalPath();
+	void setAutoColorGeneration(bool flag);
 
 signals:
 	void optionsSet();
@@ -39,7 +41,7 @@ signals:
 private:
 	QCheckBox *auto_color_generation_;
 	QPushButton *button_set_PASCAL_root_;
-	QLabel *label_PASCAL_root_;
+	QLineEdit *edit_PASCAL_root_;
 	QPushButton *button_ok_;
 	QPushButton *button_cancel_;
 

+ 14 - 1
main.cpp

@@ -1,5 +1,7 @@
 #include <QApplication>
 #include <QTextCodec>
+#include <QString>
+#include <QDebug>
 
 #include "ImageLabeler.h"
 
@@ -8,9 +10,20 @@ int main(int argc, char *argv[])
 	QApplication app(argc, argv);
     app.setApplicationName(QObject::tr("Image Labeler"));
 
+    int i = 1;
+    QString settingsPath;
+    while (i < argc) {
+    	if ((QString(argv[i]) == "-config" || QString(argv[i]) == "-c") &&
+    		i + 1 < argc)
+    	{
+    		settingsPath = QString(argv[i + 1]);
+    	}
+    	i++;
+    }
+
     QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
 
-    ImageLabeler imageLabeler;
+    ImageLabeler imageLabeler(0, settingsPath);
     imageLabeler.show();
 
     return app.exec();