QTfrontend/hwform.cpp
changeset 20 ccd2c45f043d
child 21 dff476dcaaa3
equal deleted inserted replaced
19:09de46a3328c 20:ccd2c45f043d
       
     1 /*
       
     2  * Hedgewars, a worms-like game
       
     3  * Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
       
     4  *
       
     5  * Distributed under the terms of the BSD-modified licence:
       
     6  *
       
     7  * Permission is hereby granted, free of charge, to any person obtaining a copy
       
     8  * of this software and associated documentation files (the "Software"), to deal
       
     9  * with the Software without restriction, including without limitation the
       
    10  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
       
    11  * sell copies of the Software, and to permit persons to whom the Software is
       
    12  * furnished to do so, subject to the following conditions:
       
    13  *
       
    14  * 1. Redistributions of source code must retain the above copyright notice,
       
    15  *    this list of conditions and the following disclaimer.
       
    16  * 2. Redistributions in binary form must reproduce the above copyright notice,
       
    17  *    this list of conditions and the following disclaimer in the documentation
       
    18  *    and/or other materials provided with the distribution.
       
    19  * 3. The name of the author may not be used to endorse or promote products
       
    20  *    derived from this software without specific prior written permission.
       
    21  *
       
    22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
       
    23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
       
    24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
       
    25  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
       
    28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
       
    29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
       
    30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
       
    31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    32  */
       
    33 
       
    34 #include <QtGui>
       
    35 #include <QStringList>
       
    36 #include <QProcess>
       
    37 #include <QDir>
       
    38 #include <QPixmap>
       
    39 #include <QRegExp>
       
    40 #include <QIcon>
       
    41 #include <QFile>
       
    42 #include <QTextStream>
       
    43 
       
    44 #include "hwform.h"
       
    45 #include "sdlkeys.h"
       
    46 #include "hwconsts.h"
       
    47 
       
    48 HWForm::HWForm(QWidget *parent)
       
    49 	: QMainWindow(parent)
       
    50 {
       
    51 	ui.setupUi(this);
       
    52 	TeamNameEdit = new QLineEdit(ui.GBoxTeam);
       
    53 	TeamNameEdit->setGeometry(QRect(10, 20, 141, 20));
       
    54 	TeamNameEdit->setMaxLength(15);
       
    55 	for(int i = 0; i < 8; i++)
       
    56 	{
       
    57 		HHNameEdit[i] = new QLineEdit(ui.GBoxHedgehogs);
       
    58 		HHNameEdit[i]->setGeometry(QRect(10, 20 + i * 30, 141, 20));
       
    59 		HHNameEdit[i]->setMaxLength(15);
       
    60 	}
       
    61 	
       
    62 	QStringList binds;
       
    63 	for(int i = 0; strlen(sdlkeys[i][1]) > 0; i++)
       
    64 	{
       
    65 		binds << sdlkeys[i][1];
       
    66 	}
       
    67 	
       
    68 	for(int i = 0; i < BINDS_NUMBER; i++)
       
    69 	{
       
    70 		LBind[i] = new QLabel(ui.GBoxBinds);
       
    71 		LBind[i]->setGeometry(QRect(10, 23 + i * 30, 60, 20));
       
    72 		LBind[i]->setText(cbinds[i].name);
       
    73 		LBind[i]->setAlignment(Qt::AlignRight);
       
    74 		CBBind[i] = new QComboBox(ui.GBoxBinds);
       
    75 		CBBind[i]->setGeometry(QRect(80, 20 + i * 30, 80, 20));
       
    76 		CBBind[i]->addItems(binds);
       
    77 	}
       
    78 	
       
    79 	QDir tmpdir;
       
    80 	tmpdir.cd(DATA_PATH);
       
    81 	tmpdir.cd("Forts");
       
    82 	tmpdir.setFilter(QDir::Files);
       
    83 	
       
    84 	ui.CBFort->addItems(tmpdir.entryList(QStringList("*L.png")).replaceInStrings(QRegExp("^(.*)L.png"), "\\1"));
       
    85 	
       
    86 	tmpdir.cd("../Graphics/Graves");
       
    87 	QStringList list = tmpdir.entryList(QStringList("*.png"));
       
    88 	for (QStringList::Iterator it = list.begin(); it != list.end(); ++it )
       
    89 	{
       
    90 		ui.CBGrave->addItem((*it).replace(QRegExp("^(.*).png"), "\\1"));
       
    91 	}
       
    92 
       
    93 	cfgdir.setPath(cfgdir.homePath());
       
    94 	if (!cfgdir.exists(".hedgewars"))
       
    95 	{
       
    96 		if (!cfgdir.mkdir(".hedgewars"))
       
    97 		{
       
    98 			QMessageBox::critical(this, 
       
    99 					tr("Error"),
       
   100 					tr("Cannot create directory %s").arg("/.hedgewars"),
       
   101 					tr("Quit"));
       
   102 		}
       
   103 		return ;
       
   104 	}
       
   105 	cfgdir.cd(".hedgewars");
       
   106 	
       
   107 	QFile settings(cfgdir.absolutePath() + "/options");
       
   108 	if (!settings.open(QIODevice::ReadOnly))
       
   109 	{
       
   110 		return ;
       
   111 	}
       
   112 	QTextStream stream(&settings);
       
   113 	stream.setCodec("UTF-8");	
       
   114 	QString str;
       
   115 	
       
   116 	while (!stream.atEnd())
       
   117 	{
       
   118 		str = stream.readLine();
       
   119 		if (str.startsWith(";")) continue;
       
   120 		if (str.startsWith("resolution "))
       
   121 		{
       
   122 			str.remove(0, 11);
       
   123 			ui.CBResolution->setCurrentIndex(str.toLong());
       
   124 		} else
       
   125 		if (str.startsWith("fullscreen "))
       
   126 		{
       
   127 			str.remove(0, 11);
       
   128 			ui.CBFullscreen->setChecked(str.toLong());
       
   129 		}
       
   130 	}
       
   131 	settings.close();
       
   132 	connect(ui.BtnSPBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
       
   133 	connect(ui.BtnSetupBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
       
   134 	connect(ui.BtnSinglePlayer, SIGNAL(clicked()), this, SLOT(GoToSinglePlayer()));
       
   135 	connect(ui.BtnSetup, SIGNAL(clicked()), this, SLOT(GoToSetup()));
       
   136 	connect(ui.BtnNewTeam, SIGNAL(clicked()), this, SLOT(NewTeam()));
       
   137 	connect(ui.BtnEditTeam, SIGNAL(clicked()), this, SLOT(EditTeam()));
       
   138 	connect(ui.BtnTeamSave, SIGNAL(clicked()), this, SLOT(TeamSave()));
       
   139 	connect(ui.BtnTeamDiscard, SIGNAL(clicked()), this, SLOT(TeamDiscard()));
       
   140 	connect(ui.BtnSimpleGame, SIGNAL(clicked()), this, SLOT(SimpleGame()));
       
   141 	connect(ui.BtnSaveOptions, SIGNAL(clicked()), this, SLOT(SaveOptions()));
       
   142 	connect(ui.CBGrave, SIGNAL(activated(const QString &)), this, SLOT(CBGrave_activated(const QString &)));
       
   143 	connect(ui.CBFort, SIGNAL(activated(const QString &)), this, SLOT(CBFort_activated(const QString &)));
       
   144 	ui.Pages->setCurrentIndex(ID_PAGE_MAIN);
       
   145 }
       
   146 
       
   147 void HWForm::GoToMain()
       
   148 {
       
   149 	ui.Pages->setCurrentIndex(ID_PAGE_MAIN);
       
   150 }
       
   151 
       
   152 void HWForm::GoToSinglePlayer()
       
   153 {
       
   154 	ui.Pages->setCurrentIndex(ID_PAGE_SINGLEPLAYER);
       
   155 }
       
   156 
       
   157 void HWForm::GoToSetup()
       
   158 {
       
   159 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
       
   160 }
       
   161 
       
   162 void HWForm::NewTeam()
       
   163 {
       
   164 	HWTeam tmpTeam(this);
       
   165 	tmpTeam.ToPage();
       
   166 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
       
   167 }
       
   168 
       
   169 void HWForm::EditTeam()
       
   170 {
       
   171 	HWTeam tmpTeam(this);
       
   172 	tmpTeam.LoadFromFile(cfgdir.absolutePath() + "/team.cfg");
       
   173 	tmpTeam.ToPage();
       
   174 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
       
   175 }
       
   176 
       
   177 void HWForm::TeamSave()
       
   178 {
       
   179 	HWTeam tmpTeam(this);
       
   180 	tmpTeam.FromPage();
       
   181 	tmpTeam.SaveToFile(cfgdir.absolutePath() + "/team.cfg");
       
   182 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
       
   183 }
       
   184 
       
   185 void HWForm::TeamDiscard()
       
   186 {
       
   187 	ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
       
   188 }
       
   189 
       
   190 void HWForm::SimpleGame()
       
   191 {
       
   192 	game = new HWGame();
       
   193 	game->AddTeam(cfgdir.absolutePath() + "/team.cfg");
       
   194 	game->AddTeam(cfgdir.absolutePath() + "/team.cfg");
       
   195 	game->Start(ui.CBResolution->currentIndex());
       
   196 }
       
   197 
       
   198 void HWForm::CBGrave_activated(const QString & gravename)
       
   199 {
       
   200 	QPixmap pix(QString(DATA_PATH) + "/Graphics/Graves/" + gravename + ".png");
       
   201 	ui.GravePreview->setPixmap(pix.copy(0, 0, 32, 32));
       
   202 }
       
   203 
       
   204 void HWForm::CBFort_activated(const QString & fortname)
       
   205 {
       
   206 	QPixmap pix(QString(DATA_PATH) + "/Forts/" + fortname + "L.png");
       
   207 	ui.FortPreview->setPixmap(pix);
       
   208 }
       
   209 
       
   210 void HWForm::SaveOptions()
       
   211 {
       
   212 	QFile settings(cfgdir.absolutePath() + "/options");
       
   213 	if (!settings.open(QIODevice::WriteOnly))
       
   214 	{
       
   215 		QMessageBox::critical(this, 
       
   216 				tr("Error"),
       
   217 				tr("Cannot save options to file %s").arg(settings.fileName()),
       
   218 				tr("Quit"));
       
   219 		return ;
       
   220 	}
       
   221 	QTextStream stream(&settings);
       
   222 	stream.setCodec("UTF-8");
       
   223 	stream << "; Generated by Hedgewars, do not modify" << endl;
       
   224 	stream << "resolution " << ui.CBResolution->currentIndex() << endl;
       
   225 	stream << "fullscreen " << ui.CBFullscreen->isChecked() << endl;
       
   226 	settings.close();
       
   227 }