QTfrontend/hwform.cpp
changeset 84 0f6669da2fcb
parent 60 7fbfa2f13f6f
child 85 44d9045b26ff
--- a/QTfrontend/hwform.cpp	Mon Jul 24 14:03:51 2006 +0000
+++ b/QTfrontend/hwform.cpp	Mon Jul 24 22:37:34 2006 +0000
@@ -44,6 +44,7 @@
 #include "hwform.h"
 #include "sdlkeys.h"
 #include "hwconsts.h"
+#include "gameconfig.h"
 
 HWForm::HWForm(QWidget *parent)
 	: QMainWindow(parent)
@@ -99,65 +100,26 @@
 		ui.CBGrave->addItem((*it).replace(QRegExp("^(.*).png"), "\\1"));
 	}
 
-	cfgdir.setPath(cfgdir.homePath());
-	if (!cfgdir.exists(".hedgewars"))
-	{
-		if (!cfgdir.mkdir(".hedgewars"))
-		{
-			QMessageBox::critical(this,
-					tr("Error"),
-					tr("Cannot create directory %1").arg("/.hedgewars"),
-					tr("Quit"));
-			return ;
-		}
-	}
-	cfgdir.cd(".hedgewars");
+	config = new GameConfig(this);
+
+	QStringList teamslist = config->GetTeamsList();
 
-	list = cfgdir.entryList(QStringList("*.cfg"));
-
-	if(list.empty()) {
+	if(teamslist.empty()) {
 	  HWTeam defaultTeam("DefaultTeam");
-	  defaultTeam.SetCfgDir(cfgdir.absolutePath());
+	  defaultTeam.SetCfgDir(config->cfgdir.absolutePath());
 	  defaultTeam.SaveToFile();
-	  list.push_back("DefaultTeam");
+	  teamslist.push_back("DefaultTeam");
 	}
 
 	pts=new TeamSelWidget(ui.Pages->widget(ID_PAGE_SINGLEPLAYER));
 
-	for (QStringList::Iterator it = list.begin(); it != list.end(); ++it )
+	for (QStringList::Iterator it = teamslist.begin(); it != teamslist.end(); ++it )
 	{
 	  QString tmpTeamStr=(*it).replace(QRegExp("^(.*).cfg$"), "\\1");
 	  pts->addTeam(tmpTeamStr);
 	  ui.CBTeamName->addItem(tmpTeamStr);
 	}
 
-	QFile settings(cfgdir.absolutePath() + "/options");
-	if (settings.open(QIODevice::ReadOnly))
-	{
-		QTextStream stream(&settings);
-		stream.setCodec("UTF-8");
-		QString str;
-
-		while (!stream.atEnd())
-		{
-			str = stream.readLine();
-			if (str.startsWith(";")) continue;
-			if (str.startsWith("resolution "))
-			{
-				ui.CBResolution->setCurrentIndex(str.mid(11).toLong());
-			} else
-			if (str.startsWith("fullscreen "))
-			{
-				ui.CBFullscreen->setChecked(str.mid(11).toLong());
-			} else
-			if (str.startsWith("nick "))
-			{
-				ui.editNetNick->setText(str.mid(5));
-			}
-		}
-		settings.close();
-	}
-
 	connect(ui.BtnSPBack,	SIGNAL(clicked()),	this, SLOT(GoToMain()));
 	connect(ui.BtnDemosBack,	SIGNAL(clicked()),	this, SLOT(GoToMain()));
 	connect(ui.BtnSetupBack,	SIGNAL(clicked()),	this, SLOT(GoToMain()));
@@ -174,7 +136,7 @@
 	connect(ui.BtnTeamSave,	SIGNAL(clicked()),	this, SLOT(TeamSave()));
 	connect(ui.BtnTeamDiscard,	SIGNAL(clicked()),	this, SLOT(TeamDiscard()));
 	connect(ui.BtnSimpleGame,	SIGNAL(clicked()),	this, SLOT(SimpleGame()));
-	connect(ui.BtnSaveOptions,	SIGNAL(clicked()),	this, SLOT(SaveOptions()));
+	connect(ui.BtnSaveOptions,	SIGNAL(clicked()),	config, SLOT(SaveOptions()));
 	connect(ui.BtnPlayDemo,	SIGNAL(clicked()),	this, SLOT(PlayDemo()));
 	connect(ui.BtnNetConnect,	SIGNAL(clicked()),	this, SLOT(NetConnect()));
 	connect(ui.BtnNetChatDisconnect, SIGNAL(clicked()), this, SLOT(NetDisconnect()));
@@ -234,7 +196,7 @@
 void HWForm::NewTeam()
 {
 	tmpTeam = new HWTeam("unnamed");
-	tmpTeam->SetCfgDir(cfgdir.absolutePath());
+	tmpTeam->SetCfgDir(config->cfgdir.absolutePath());
 	tmpTeam->SetToPage(this);
 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
 }
@@ -242,7 +204,7 @@
 void HWForm::EditTeam()
 {
 	tmpTeam = new HWTeam(ui.CBTeamName->currentText());
-	tmpTeam->SetCfgDir(cfgdir.absolutePath());
+	tmpTeam->SetCfgDir(config->cfgdir.absolutePath());
 	tmpTeam->LoadFromFile();
 	tmpTeam->SetToPage(this);
 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
@@ -263,7 +225,7 @@
 
 void HWForm::SimpleGame()
 {
-	game = new HWGame(ui.CBResolution->currentIndex(), ui.CBFullscreen->isChecked());
+	game = new HWGame(config);
 	game->AddTeam("DefaultTeam");
 	game->AddTeam("DefaultTeam");
 	game->StartLocal();
@@ -281,26 +243,6 @@
 	ui.FortPreview->setPixmap(pix);
 }
 
-void HWForm::SaveOptions()
-{
-	QFile settings(cfgdir.absolutePath() + "/options");
-	if (!settings.open(QIODevice::WriteOnly))
-	{
-		QMessageBox::critical(this,
-				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 " << ui.CBResolution->currentIndex() << endl;
-	stream << "fullscreen " << ui.CBFullscreen->isChecked() << endl;
-	stream << "nick " << ui.editNetNick->text() << endl;
-	settings.close();
-}
-
 void HWForm::PlayDemo()
 {
 	QListWidgetItem * curritem = ui.DemosList->currentItem();
@@ -312,13 +254,13 @@
 				tr("OK"));
 		return ;
 	}
-	game = new HWGame(ui.CBResolution->currentIndex(), ui.CBFullscreen->isChecked());
+	game = new HWGame(config);
 	game->PlayDemo(QString(DATA_PATH) + "/Demos/" + curritem->text() + ".hwd_1");
 }
 
 void HWForm::NetConnect()
 {
-	hwnet = new HWNet(ui.CBResolution->currentIndex(), ui.CBFullscreen->isChecked());
+	hwnet = new HWNet(config);
 	connect(hwnet, SIGNAL(Connected()), this, SLOT(GoToNetChat()));
 	connect(hwnet, SIGNAL(AddGame(const QString &)), this, SLOT(AddGame(const QString &)));
 	connect(hwnet, SIGNAL(EnteredGame()), this, SLOT(NetGameEnter()));
@@ -355,7 +297,7 @@
 void HWForm::NetAddTeam()
 {
 	HWTeam team("DefaultTeam");
-	team.SetCfgDir(cfgdir.absolutePath());
+	team.SetCfgDir(config->cfgdir.absolutePath());
 	team.LoadFromFile();
 	hwnet->AddTeam(team);
 }