QTfrontend/hwform.cpp
changeset 84 0f6669da2fcb
parent 60 7fbfa2f13f6f
child 85 44d9045b26ff
equal deleted inserted replaced
83:207c85fbef51 84:0f6669da2fcb
    42 #include <QTextStream>
    42 #include <QTextStream>
    43 
    43 
    44 #include "hwform.h"
    44 #include "hwform.h"
    45 #include "sdlkeys.h"
    45 #include "sdlkeys.h"
    46 #include "hwconsts.h"
    46 #include "hwconsts.h"
       
    47 #include "gameconfig.h"
    47 
    48 
    48 HWForm::HWForm(QWidget *parent)
    49 HWForm::HWForm(QWidget *parent)
    49 	: QMainWindow(parent)
    50 	: QMainWindow(parent)
    50 {
    51 {
    51 	ui.setupUi(this);
    52 	ui.setupUi(this);
    97 	for (QStringList::Iterator it = list.begin(); it != list.end(); ++it )
    98 	for (QStringList::Iterator it = list.begin(); it != list.end(); ++it )
    98 	{
    99 	{
    99 		ui.CBGrave->addItem((*it).replace(QRegExp("^(.*).png"), "\\1"));
   100 		ui.CBGrave->addItem((*it).replace(QRegExp("^(.*).png"), "\\1"));
   100 	}
   101 	}
   101 
   102 
   102 	cfgdir.setPath(cfgdir.homePath());
   103 	config = new GameConfig(this);
   103 	if (!cfgdir.exists(".hedgewars"))
   104 
   104 	{
   105 	QStringList teamslist = config->GetTeamsList();
   105 		if (!cfgdir.mkdir(".hedgewars"))
   106 
   106 		{
   107 	if(teamslist.empty()) {
   107 			QMessageBox::critical(this,
       
   108 					tr("Error"),
       
   109 					tr("Cannot create directory %1").arg("/.hedgewars"),
       
   110 					tr("Quit"));
       
   111 			return ;
       
   112 		}
       
   113 	}
       
   114 	cfgdir.cd(".hedgewars");
       
   115 
       
   116 	list = cfgdir.entryList(QStringList("*.cfg"));
       
   117 
       
   118 	if(list.empty()) {
       
   119 	  HWTeam defaultTeam("DefaultTeam");
   108 	  HWTeam defaultTeam("DefaultTeam");
   120 	  defaultTeam.SetCfgDir(cfgdir.absolutePath());
   109 	  defaultTeam.SetCfgDir(config->cfgdir.absolutePath());
   121 	  defaultTeam.SaveToFile();
   110 	  defaultTeam.SaveToFile();
   122 	  list.push_back("DefaultTeam");
   111 	  teamslist.push_back("DefaultTeam");
   123 	}
   112 	}
   124 
   113 
   125 	pts=new TeamSelWidget(ui.Pages->widget(ID_PAGE_SINGLEPLAYER));
   114 	pts=new TeamSelWidget(ui.Pages->widget(ID_PAGE_SINGLEPLAYER));
   126 
   115 
   127 	for (QStringList::Iterator it = list.begin(); it != list.end(); ++it )
   116 	for (QStringList::Iterator it = teamslist.begin(); it != teamslist.end(); ++it )
   128 	{
   117 	{
   129 	  QString tmpTeamStr=(*it).replace(QRegExp("^(.*).cfg$"), "\\1");
   118 	  QString tmpTeamStr=(*it).replace(QRegExp("^(.*).cfg$"), "\\1");
   130 	  pts->addTeam(tmpTeamStr);
   119 	  pts->addTeam(tmpTeamStr);
   131 	  ui.CBTeamName->addItem(tmpTeamStr);
   120 	  ui.CBTeamName->addItem(tmpTeamStr);
   132 	}
       
   133 
       
   134 	QFile settings(cfgdir.absolutePath() + "/options");
       
   135 	if (settings.open(QIODevice::ReadOnly))
       
   136 	{
       
   137 		QTextStream stream(&settings);
       
   138 		stream.setCodec("UTF-8");
       
   139 		QString str;
       
   140 
       
   141 		while (!stream.atEnd())
       
   142 		{
       
   143 			str = stream.readLine();
       
   144 			if (str.startsWith(";")) continue;
       
   145 			if (str.startsWith("resolution "))
       
   146 			{
       
   147 				ui.CBResolution->setCurrentIndex(str.mid(11).toLong());
       
   148 			} else
       
   149 			if (str.startsWith("fullscreen "))
       
   150 			{
       
   151 				ui.CBFullscreen->setChecked(str.mid(11).toLong());
       
   152 			} else
       
   153 			if (str.startsWith("nick "))
       
   154 			{
       
   155 				ui.editNetNick->setText(str.mid(5));
       
   156 			}
       
   157 		}
       
   158 		settings.close();
       
   159 	}
   121 	}
   160 
   122 
   161 	connect(ui.BtnSPBack,	SIGNAL(clicked()),	this, SLOT(GoToMain()));
   123 	connect(ui.BtnSPBack,	SIGNAL(clicked()),	this, SLOT(GoToMain()));
   162 	connect(ui.BtnDemosBack,	SIGNAL(clicked()),	this, SLOT(GoToMain()));
   124 	connect(ui.BtnDemosBack,	SIGNAL(clicked()),	this, SLOT(GoToMain()));
   163 	connect(ui.BtnSetupBack,	SIGNAL(clicked()),	this, SLOT(GoToMain()));
   125 	connect(ui.BtnSetupBack,	SIGNAL(clicked()),	this, SLOT(GoToMain()));
   172 	connect(ui.BtnNewTeam,	SIGNAL(clicked()),	this, SLOT(NewTeam()));
   134 	connect(ui.BtnNewTeam,	SIGNAL(clicked()),	this, SLOT(NewTeam()));
   173 	connect(ui.BtnEditTeam,	SIGNAL(clicked()),	this, SLOT(EditTeam()));
   135 	connect(ui.BtnEditTeam,	SIGNAL(clicked()),	this, SLOT(EditTeam()));
   174 	connect(ui.BtnTeamSave,	SIGNAL(clicked()),	this, SLOT(TeamSave()));
   136 	connect(ui.BtnTeamSave,	SIGNAL(clicked()),	this, SLOT(TeamSave()));
   175 	connect(ui.BtnTeamDiscard,	SIGNAL(clicked()),	this, SLOT(TeamDiscard()));
   137 	connect(ui.BtnTeamDiscard,	SIGNAL(clicked()),	this, SLOT(TeamDiscard()));
   176 	connect(ui.BtnSimpleGame,	SIGNAL(clicked()),	this, SLOT(SimpleGame()));
   138 	connect(ui.BtnSimpleGame,	SIGNAL(clicked()),	this, SLOT(SimpleGame()));
   177 	connect(ui.BtnSaveOptions,	SIGNAL(clicked()),	this, SLOT(SaveOptions()));
   139 	connect(ui.BtnSaveOptions,	SIGNAL(clicked()),	config, SLOT(SaveOptions()));
   178 	connect(ui.BtnPlayDemo,	SIGNAL(clicked()),	this, SLOT(PlayDemo()));
   140 	connect(ui.BtnPlayDemo,	SIGNAL(clicked()),	this, SLOT(PlayDemo()));
   179 	connect(ui.BtnNetConnect,	SIGNAL(clicked()),	this, SLOT(NetConnect()));
   141 	connect(ui.BtnNetConnect,	SIGNAL(clicked()),	this, SLOT(NetConnect()));
   180 	connect(ui.BtnNetChatDisconnect, SIGNAL(clicked()), this, SLOT(NetDisconnect()));
   142 	connect(ui.BtnNetChatDisconnect, SIGNAL(clicked()), this, SLOT(NetDisconnect()));
   181 	connect(ui.BtnNetChatJoin,	SIGNAL(clicked()),	this, SLOT(NetJoin()));
   143 	connect(ui.BtnNetChatJoin,	SIGNAL(clicked()),	this, SLOT(NetJoin()));
   182 	connect(ui.BtnNetChatCreate,	SIGNAL(clicked()),	this, SLOT(NetCreate()));
   144 	connect(ui.BtnNetChatCreate,	SIGNAL(clicked()),	this, SLOT(NetCreate()));
   232 }
   194 }
   233 
   195 
   234 void HWForm::NewTeam()
   196 void HWForm::NewTeam()
   235 {
   197 {
   236 	tmpTeam = new HWTeam("unnamed");
   198 	tmpTeam = new HWTeam("unnamed");
   237 	tmpTeam->SetCfgDir(cfgdir.absolutePath());
   199 	tmpTeam->SetCfgDir(config->cfgdir.absolutePath());
   238 	tmpTeam->SetToPage(this);
   200 	tmpTeam->SetToPage(this);
   239 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
   201 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
   240 }
   202 }
   241 
   203 
   242 void HWForm::EditTeam()
   204 void HWForm::EditTeam()
   243 {
   205 {
   244 	tmpTeam = new HWTeam(ui.CBTeamName->currentText());
   206 	tmpTeam = new HWTeam(ui.CBTeamName->currentText());
   245 	tmpTeam->SetCfgDir(cfgdir.absolutePath());
   207 	tmpTeam->SetCfgDir(config->cfgdir.absolutePath());
   246 	tmpTeam->LoadFromFile();
   208 	tmpTeam->LoadFromFile();
   247 	tmpTeam->SetToPage(this);
   209 	tmpTeam->SetToPage(this);
   248 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
   210 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
   249 }
   211 }
   250 
   212 
   261 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
   223 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
   262 }
   224 }
   263 
   225 
   264 void HWForm::SimpleGame()
   226 void HWForm::SimpleGame()
   265 {
   227 {
   266 	game = new HWGame(ui.CBResolution->currentIndex(), ui.CBFullscreen->isChecked());
   228 	game = new HWGame(config);
   267 	game->AddTeam("DefaultTeam");
   229 	game->AddTeam("DefaultTeam");
   268 	game->AddTeam("DefaultTeam");
   230 	game->AddTeam("DefaultTeam");
   269 	game->StartLocal();
   231 	game->StartLocal();
   270 }
   232 }
   271 
   233 
   277 
   239 
   278 void HWForm::CBFort_activated(const QString & fortname)
   240 void HWForm::CBFort_activated(const QString & fortname)
   279 {
   241 {
   280 	QPixmap pix(QString(DATA_PATH) + "/Forts/" + fortname + "L.png");
   242 	QPixmap pix(QString(DATA_PATH) + "/Forts/" + fortname + "L.png");
   281 	ui.FortPreview->setPixmap(pix);
   243 	ui.FortPreview->setPixmap(pix);
   282 }
       
   283 
       
   284 void HWForm::SaveOptions()
       
   285 {
       
   286 	QFile settings(cfgdir.absolutePath() + "/options");
       
   287 	if (!settings.open(QIODevice::WriteOnly))
       
   288 	{
       
   289 		QMessageBox::critical(this,
       
   290 				tr("Error"),
       
   291 				tr("Cannot save options to file %1").arg(settings.fileName()),
       
   292 				tr("Quit"));
       
   293 		return ;
       
   294 	}
       
   295 	QTextStream stream(&settings);
       
   296 	stream.setCodec("UTF-8");
       
   297 	stream << "; Generated by Hedgewars, do not modify" << endl;
       
   298 	stream << "resolution " << ui.CBResolution->currentIndex() << endl;
       
   299 	stream << "fullscreen " << ui.CBFullscreen->isChecked() << endl;
       
   300 	stream << "nick " << ui.editNetNick->text() << endl;
       
   301 	settings.close();
       
   302 }
   244 }
   303 
   245 
   304 void HWForm::PlayDemo()
   246 void HWForm::PlayDemo()
   305 {
   247 {
   306 	QListWidgetItem * curritem = ui.DemosList->currentItem();
   248 	QListWidgetItem * curritem = ui.DemosList->currentItem();
   310 				tr("Error"),
   252 				tr("Error"),
   311 				tr("Please, select demo from the list above"),
   253 				tr("Please, select demo from the list above"),
   312 				tr("OK"));
   254 				tr("OK"));
   313 		return ;
   255 		return ;
   314 	}
   256 	}
   315 	game = new HWGame(ui.CBResolution->currentIndex(), ui.CBFullscreen->isChecked());
   257 	game = new HWGame(config);
   316 	game->PlayDemo(QString(DATA_PATH) + "/Demos/" + curritem->text() + ".hwd_1");
   258 	game->PlayDemo(QString(DATA_PATH) + "/Demos/" + curritem->text() + ".hwd_1");
   317 }
   259 }
   318 
   260 
   319 void HWForm::NetConnect()
   261 void HWForm::NetConnect()
   320 {
   262 {
   321 	hwnet = new HWNet(ui.CBResolution->currentIndex(), ui.CBFullscreen->isChecked());
   263 	hwnet = new HWNet(config);
   322 	connect(hwnet, SIGNAL(Connected()), this, SLOT(GoToNetChat()));
   264 	connect(hwnet, SIGNAL(Connected()), this, SLOT(GoToNetChat()));
   323 	connect(hwnet, SIGNAL(AddGame(const QString &)), this, SLOT(AddGame(const QString &)));
   265 	connect(hwnet, SIGNAL(AddGame(const QString &)), this, SLOT(AddGame(const QString &)));
   324 	connect(hwnet, SIGNAL(EnteredGame()), this, SLOT(NetGameEnter()));
   266 	connect(hwnet, SIGNAL(EnteredGame()), this, SLOT(NetGameEnter()));
   325 	connect(hwnet, SIGNAL(ChangeInTeams(const QStringList &)), this, SLOT(ChangeInNetTeams(const QStringList &)));
   267 	connect(hwnet, SIGNAL(ChangeInTeams(const QStringList &)), this, SLOT(ChangeInNetTeams(const QStringList &)));
   326 	hwnet->Connect("172.19.5.168", 6667, ui.editNetNick->text());
   268 	hwnet->Connect("172.19.5.168", 6667, ui.editNetNick->text());
   353 }
   295 }
   354 
   296 
   355 void HWForm::NetAddTeam()
   297 void HWForm::NetAddTeam()
   356 {
   298 {
   357 	HWTeam team("DefaultTeam");
   299 	HWTeam team("DefaultTeam");
   358 	team.SetCfgDir(cfgdir.absolutePath());
   300 	team.SetCfgDir(config->cfgdir.absolutePath());
   359 	team.LoadFromFile();
   301 	team.LoadFromFile();
   360 	hwnet->AddTeam(team);
   302 	hwnet->AddTeam(team);
   361 }
   303 }
   362 
   304 
   363 void HWForm::NetStartGame()
   305 void HWForm::NetStartGame()