Use QSettings to storing config
authorunc0rr
Fri, 15 Dec 2006 12:48:40 +0000
changeset 301 29bf9c1a3ad3
parent 300 be94799f33d2
child 302 7aca131ecd7f
Use QSettings to storing config
QTfrontend/gameuiconfig.cpp
QTfrontend/gameuiconfig.h
QTfrontend/hwform.cpp
--- a/QTfrontend/gameuiconfig.cpp	Thu Dec 14 20:06:46 2006 +0000
+++ b/QTfrontend/gameuiconfig.cpp	Fri Dec 15 12:48:40 2006 +0000
@@ -24,53 +24,20 @@
 #include "hwconsts.h"
 #include "fpsedit.h"
 
-GameUIConfig::GameUIConfig(HWForm * FormWidgets)
-	: QObject()
+GameUIConfig::GameUIConfig(HWForm * FormWidgets, const QString & fileName)
+	: QSettings(fileName, QSettings::IniFormat)
 {
 	Form = FormWidgets;
 
-	QFile settings(cfgdir->absolutePath() + "/options");
-	if (settings.open(QIODevice::ReadOnly))
-	{
-		QTextStream stream(&settings);
-		stream.setCodec("UTF-8");
-		QString str;
+	Form->ui.pageOptions->CBResolution->setCurrentIndex(value("video/resolution").toUInt());
+	Form->ui.pageOptions->CBFullscreen->setChecked(value("video/fullscreen", false).toBool());
+
+	Form->ui.pageOptions->CBEnableSound->setChecked(value("audio/sound", true).toBool());
 
-		while (!stream.atEnd())
-		{
-			str = stream.readLine();
-			if (str.startsWith(";")) continue;
-			if (str.startsWith("resolution "))
-			{
-				Form->ui.pageOptions->CBResolution->setCurrentIndex(str.mid(11).toLong());
-			} else
-			if (str.startsWith("fullscreen "))
-			{
-				Form->ui.pageOptions->CBFullscreen->setChecked(str.mid(11).toLong());
-			} else
-			if (str.startsWith("sound "))
-			{
-				Form->ui.pageOptions->CBEnableSound->setChecked(str.mid(6).toLong());
-			} else
-			if (str.startsWith("nick "))
-			{
-				Form->ui.pageNet->editNetNick->setText(str.mid(5));
-			} else
-			if (str.startsWith("ip "))
-			{
-				Form->ui.pageNet->editIP->setText(str.mid(3));
-			} else
-			if (str.startsWith("showfps "))
-			{
-				Form->ui.pageOptions->CBShowFPS->setChecked(str.mid(8).toLong());
-			} else
-			if (str.startsWith("interval "))
-			{
-				Form->ui.pageOptions->fpsedit->setValue(str.mid(9).toUInt());
-			}
-		}
-		settings.close();
-	}
+	Form->ui.pageNet->editNetNick->setText(value("net/nick", QLineEdit::tr("unnamed")).toString());
+	Form->ui.pageNet->editIP->setText(value("net/ip", "").toString());
+	Form->ui.pageOptions->CBShowFPS->setChecked(value("fps/show", false).toBool());
+	Form->ui.pageOptions->fpsedit->setValue(value("fps/interval", 27).toUInt());
 
 	QFile themesfile(datadir->absolutePath() + "/Themes/themes.cfg");
 	if (themesfile.open(QIODevice::ReadOnly)) {
@@ -82,7 +49,7 @@
 		}
 		themesfile.close();
 	} else {
-		QMessageBox::critical(0, "Error", "Cannot access themes.cfg", "OK");
+		QMessageBox::critical(FormWidgets, "Error", "Cannot access themes.cfg", "OK");
 	}
 }
 
@@ -99,26 +66,16 @@
 
 void GameUIConfig::SaveOptions()
 {
-	QFile settings(cfgdir->absolutePath() + "/options");
-	if (!settings.open(QIODevice::WriteOnly))
-	{
-		QMessageBox::critical(0,
-				tr("Error"),
-				tr("Cannot save options to file %1").arg(settings.fileName()),
-				tr("Quit"));
-		return ;
-	}
-	QTextStream stream(&settings);
-	stream.setCodec("UTF-8");
-	stream << "; Generated by Hedgewars, do not modify" << endl;
-	stream << "resolution " << vid_Resolution() << endl;
-	stream << "fullscreen " << vid_Fullscreen() << endl;
-	stream << "sound " << isSoundEnabled() << endl;
-	stream << "nick " << Form->ui.pageNet->editNetNick->text() << endl;
-	stream << "ip " << Form->ui.pageNet->editIP->text() << endl;
-	stream << "showfps " << isShowFPSEnabled() << endl;
-	stream << "interval " << Form->ui.pageOptions->fpsedit->value() << endl;
-	settings.close();
+	setValue("video/resolution", vid_Resolution());
+	setValue("video/fullscreen", vid_Fullscreen());
+
+	setValue("audio/sound", isSoundEnabled());
+
+	setValue("net/nick", Form->ui.pageNet->editNetNick->text());
+	setValue("net/ip", Form->ui.pageNet->editIP->text());
+
+	setValue("fps/show", isShowFPSEnabled());
+	setValue("fps/interval", Form->ui.pageOptions->fpsedit->value());
 }
 
 int GameUIConfig::vid_Resolution()
--- a/QTfrontend/gameuiconfig.h	Thu Dec 14 20:06:46 2006 +0000
+++ b/QTfrontend/gameuiconfig.h	Fri Dec 15 12:48:40 2006 +0000
@@ -19,18 +19,19 @@
 #ifndef GAMECONFIG_H
 #define GAMECONFIG_H
 
-#include <QObject>
+#include <QSettings>
 #include <QDir>
 #include <QStringList>
 
 class HWForm;
+class QSettings;
 
-class GameUIConfig : public QObject
+class GameUIConfig : public QSettings
 {
 	Q_OBJECT
 
 public:
-	GameUIConfig(HWForm * FormWidgets);
+	GameUIConfig(HWForm * FormWidgets, const QString & fileName);
 	QStringList GetTeamsList();
 	int vid_Resolution();
 	bool vid_Fullscreen();
--- a/QTfrontend/hwform.cpp	Thu Dec 14 20:06:46 2006 +0000
+++ b/QTfrontend/hwform.cpp	Fri Dec 15 12:48:40 2006 +0000
@@ -40,7 +40,7 @@
 {
 	ui.setupUi(this);
 
-	config = new GameUIConfig(this);
+	config = new GameUIConfig(this, cfgdir->absolutePath() + "/hedgewars.ini");
 
 	UpdateTeamsLists();