QTfrontend/hwform.cpp
changeset 25 27aa8030322b
parent 21 dff476dcaaa3
child 26 e32fa14529f8
equal deleted inserted replaced
24:79c411363184 25:27aa8030322b
    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 "gamecmds.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);
    56 	{
    57 	{
    57 		HHNameEdit[i] = new QLineEdit(ui.GBoxHedgehogs);
    58 		HHNameEdit[i] = new QLineEdit(ui.GBoxHedgehogs);
    58 		HHNameEdit[i]->setGeometry(QRect(10, 20 + i * 30, 141, 20));
    59 		HHNameEdit[i]->setGeometry(QRect(10, 20 + i * 30, 141, 20));
    59 		HHNameEdit[i]->setMaxLength(15);
    60 		HHNameEdit[i]->setMaxLength(15);
    60 	}
    61 	}
    61 	
    62 
    62 	QStringList binds;
    63 	QStringList binds;
    63 	for(int i = 0; strlen(sdlkeys[i][1]) > 0; i++)
    64 	for(int i = 0; strlen(sdlkeys[i][1]) > 0; i++)
    64 	{
    65 	{
    65 		binds << sdlkeys[i][1];
    66 		binds << sdlkeys[i][1];
    66 	}
    67 	}
    67 	
    68 
    68 	for(int i = 0; i < BINDS_NUMBER; i++)
    69 	for(int i = 0; i < BINDS_NUMBER; i++)
    69 	{
    70 	{
    70 		LBind[i] = new QLabel(ui.GBoxBinds);
    71 		LBind[i] = new QLabel(ui.GBoxBinds);
    71 		LBind[i]->setGeometry(QRect(10, 23 + i * 30, 60, 20));
    72 		LBind[i]->setGeometry(QRect(10, 23 + i * 30, 60, 20));
    72 		LBind[i]->setText(cbinds[i].name);
    73 		LBind[i]->setText(cbinds[i].name);
    73 		LBind[i]->setAlignment(Qt::AlignRight);
    74 		LBind[i]->setAlignment(Qt::AlignRight);
    74 		CBBind[i] = new QComboBox(ui.GBoxBinds);
    75 		CBBind[i] = new QComboBox(ui.GBoxBinds);
    75 		CBBind[i]->setGeometry(QRect(80, 20 + i * 30, 80, 20));
    76 		CBBind[i]->setGeometry(QRect(80, 20 + i * 30, 80, 20));
    76 		CBBind[i]->addItems(binds);
    77 		CBBind[i]->addItems(binds);
    77 	}
    78 	}
    78 	
    79 
    79 	QDir tmpdir;
    80 	QDir tmpdir;
    80 	tmpdir.cd(DATA_PATH);
    81 	tmpdir.cd(DATA_PATH);
    81 	tmpdir.cd("Forts");
    82 	tmpdir.cd("Forts");
    82 	tmpdir.setFilter(QDir::Files);
    83 	tmpdir.setFilter(QDir::Files);
    83 	
    84 
    84 	ui.CBFort->addItems(tmpdir.entryList(QStringList("*L.png")).replaceInStrings(QRegExp("^(.*)L.png"), "\\1"));
    85 	ui.CBFort->addItems(tmpdir.entryList(QStringList("*L.png")).replaceInStrings(QRegExp("^(.*)L.png"), "\\1"));
    85 	
    86 
    86 	tmpdir.cd("../Graphics/Graves");
    87 	tmpdir.cd("../Graphics/Graves");
    87 	QStringList list = tmpdir.entryList(QStringList("*.png"));
    88 	QStringList list = tmpdir.entryList(QStringList("*.png"));
    88 	for (QStringList::Iterator it = list.begin(); it != list.end(); ++it )
    89 	for (QStringList::Iterator it = list.begin(); it != list.end(); ++it )
    89 	{
    90 	{
    90 		ui.CBGrave->addItem((*it).replace(QRegExp("^(.*).png"), "\\1"));
    91 		ui.CBGrave->addItem((*it).replace(QRegExp("^(.*).png"), "\\1"));
    93 	cfgdir.setPath(cfgdir.homePath());
    94 	cfgdir.setPath(cfgdir.homePath());
    94 	if (!cfgdir.exists(".hedgewars"))
    95 	if (!cfgdir.exists(".hedgewars"))
    95 	{
    96 	{
    96 		if (!cfgdir.mkdir(".hedgewars"))
    97 		if (!cfgdir.mkdir(".hedgewars"))
    97 		{
    98 		{
    98 			QMessageBox::critical(this, 
    99 			QMessageBox::critical(this,
    99 					tr("Error"),
   100 					tr("Error"),
   100 					tr("Cannot create directory %s").arg("/.hedgewars"),
   101 					tr("Cannot create directory %s").arg("/.hedgewars"),
   101 					tr("Quit"));
   102 					tr("Quit"));
   102 		}
   103 		}
   103 		return ;
   104 		return ;
   104 	}
   105 	}
   105 	cfgdir.cd(".hedgewars");
   106 	cfgdir.cd(".hedgewars");
   106 	
   107 
   107 	list = cfgdir.entryList(QStringList("*.cfg"));
   108 	list = cfgdir.entryList(QStringList("*.cfg"));
   108 	for (QStringList::Iterator it = list.begin(); it != list.end(); ++it )
   109 	for (QStringList::Iterator it = list.begin(); it != list.end(); ++it )
   109 	{
   110 	{
   110 		ui.CBTeamName->addItem((*it).replace(QRegExp("^(.*).cfg"), "\\1"));
   111 		ui.CBTeamName->addItem((*it).replace(QRegExp("^(.*).cfg"), "\\1"));
   111 	}
   112 	}
   112 	
   113 
   113 	
   114 
   114 	QFile settings(cfgdir.absolutePath() + "/options");
   115 	QFile settings(cfgdir.absolutePath() + "/options");
   115 	if (!settings.open(QIODevice::ReadOnly))
   116 	if (settings.open(QIODevice::ReadOnly))
   116 	{
   117 	{
   117 		return ;
   118 		QTextStream stream(&settings);
   118 	}
   119 		stream.setCodec("UTF-8");
   119 	QTextStream stream(&settings);
   120 		QString str;
   120 	stream.setCodec("UTF-8");	
   121 
   121 	QString str;
   122 		while (!stream.atEnd())
   122 	
       
   123 	while (!stream.atEnd())
       
   124 	{
       
   125 		str = stream.readLine();
       
   126 		if (str.startsWith(";")) continue;
       
   127 		if (str.startsWith("resolution "))
       
   128 		{
   123 		{
   129 			str.remove(0, 11);
   124 			str = stream.readLine();
   130 			ui.CBResolution->setCurrentIndex(str.toLong());
   125 			if (str.startsWith(";")) continue;
   131 		} else
   126 			if (str.startsWith("resolution "))
   132 		if (str.startsWith("fullscreen "))
   127 			{
   133 		{
   128 				str.remove(0, 11);
   134 			str.remove(0, 11);
   129 				ui.CBResolution->setCurrentIndex(str.toLong());
   135 			ui.CBFullscreen->setChecked(str.toLong());
   130 			} else
       
   131 			if (str.startsWith("fullscreen "))
       
   132 			{
       
   133 				str.remove(0, 11);
       
   134 				ui.CBFullscreen->setChecked(str.toLong());
       
   135 			}
   136 		}
   136 		}
   137 	}
   137 		settings.close();
   138 	settings.close();
   138 	}
   139 	connect(ui.BtnSPBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
   139 	connect(ui.BtnSPBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
   140 	connect(ui.BtnSetupBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
   140 	connect(ui.BtnSetupBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
       
   141 	connect(ui.BtnMPBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
   141 	connect(ui.BtnSinglePlayer, SIGNAL(clicked()), this, SLOT(GoToSinglePlayer()));
   142 	connect(ui.BtnSinglePlayer, SIGNAL(clicked()), this, SLOT(GoToSinglePlayer()));
   142 	connect(ui.BtnSetup, SIGNAL(clicked()), this, SLOT(GoToSetup()));
   143 	connect(ui.BtnSetup, SIGNAL(clicked()), this, SLOT(GoToSetup()));
       
   144 	connect(ui.BtnMultiplayer, SIGNAL(clicked()), this, SLOT(GoToMultiplayer()));
   143 	connect(ui.BtnNewTeam, SIGNAL(clicked()), this, SLOT(NewTeam()));
   145 	connect(ui.BtnNewTeam, SIGNAL(clicked()), this, SLOT(NewTeam()));
   144 	connect(ui.BtnEditTeam, SIGNAL(clicked()), this, SLOT(EditTeam()));
   146 	connect(ui.BtnEditTeam, SIGNAL(clicked()), this, SLOT(EditTeam()));
   145 	connect(ui.BtnTeamSave, SIGNAL(clicked()), this, SLOT(TeamSave()));
   147 	connect(ui.BtnTeamSave, SIGNAL(clicked()), this, SLOT(TeamSave()));
   146 	connect(ui.BtnTeamDiscard, SIGNAL(clicked()), this, SLOT(TeamDiscard()));
   148 	connect(ui.BtnTeamDiscard, SIGNAL(clicked()), this, SLOT(TeamDiscard()));
   147 	connect(ui.BtnSimpleGame, SIGNAL(clicked()), this, SLOT(SimpleGame()));
   149 	connect(ui.BtnSimpleGame, SIGNAL(clicked()), this, SLOT(SimpleGame()));
   164 void HWForm::GoToSetup()
   166 void HWForm::GoToSetup()
   165 {
   167 {
   166 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
   168 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
   167 }
   169 }
   168 
   170 
       
   171 void HWForm::GoToMultiplayer()
       
   172 {
       
   173 	ui.Pages->setCurrentIndex(ID_PAGE_MULTIPLAYER);
       
   174 }
   169 void HWForm::NewTeam()
   175 void HWForm::NewTeam()
   170 {
   176 {
   171 	tmpTeam = new HWTeam();
   177 	tmpTeam = new HWTeam("unnamed");
   172 	tmpTeam->SetCfgDir(cfgdir.absolutePath());
   178 	tmpTeam->SetCfgDir(cfgdir.absolutePath());
   173 	tmpTeam->SetToPage(this);
   179 	tmpTeam->SetToPage(this);
   174 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
   180 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
   175 }
   181 }
   176 
   182 
   177 void HWForm::EditTeam()
   183 void HWForm::EditTeam()
   178 {
   184 {
   179 	tmpTeam = new HWTeam();
   185 	tmpTeam = new HWTeam(ui.CBTeamName->currentText());
   180 	tmpTeam->SetCfgDir(cfgdir.absolutePath());
   186 	tmpTeam->SetCfgDir(cfgdir.absolutePath());
   181 	tmpTeam->LoadFromFile();
   187 	tmpTeam->LoadFromFile();
   182 	tmpTeam->SetToPage(this);
   188 	tmpTeam->SetToPage(this);
   183 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
   189 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
   184 }
   190 }
   185 
   191 
   186 void HWForm::TeamSave()
   192 void HWForm::TeamSave()
   187 {
   193 {
   188 	tmpTeam = new HWTeam();
       
   189 	tmpTeam->GetFromPage(this);
   194 	tmpTeam->GetFromPage(this);
   190 	tmpTeam->SaveToFile();
   195 	tmpTeam->SaveToFile();
   191 	delete tmpTeam;
   196 	delete tmpTeam;
   192 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
   197 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
   193 }
   198 }
   220 void HWForm::SaveOptions()
   225 void HWForm::SaveOptions()
   221 {
   226 {
   222 	QFile settings(cfgdir.absolutePath() + "/options");
   227 	QFile settings(cfgdir.absolutePath() + "/options");
   223 	if (!settings.open(QIODevice::WriteOnly))
   228 	if (!settings.open(QIODevice::WriteOnly))
   224 	{
   229 	{
   225 		QMessageBox::critical(this, 
   230 		QMessageBox::critical(this,
   226 				tr("Error"),
   231 				tr("Error"),
   227 				tr("Cannot save options to file %s").arg(settings.fileName()),
   232 				tr("Cannot save options to file %s").arg(settings.fileName()),
   228 				tr("Quit"));
   233 				tr("Quit"));
   229 		return ;
   234 		return ;
   230 	}
   235 	}