QTfrontend/gameuiconfig.cpp
author unc0rr
Tue, 14 Nov 2006 20:39:30 +0000
changeset 239 f9adf3c73bed
parent 184 f97a7a3dc8f6
child 297 279e10ec31b4
permissions -rw-r--r--
'Proto' class to make things easier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     1
/*
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     2
 * Hedgewars, a worms-like game
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     3
 * Copyright (c) 2006 Andrey Korotaev <unC0Rr@gmail.com>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     4
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     8
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    12
 * GNU General Public License for more details.
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    13
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    14
 * You should have received a copy of the GNU General Public License
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    15
 * along with this program; if not, write to the Free Software
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    17
 */
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    18
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    19
#include <QMessageBox>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    20
#include <QTextStream>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    21
#include "gameuiconfig.h"
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    22
#include "hwform.h"
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    23
#include "pages.h"
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    24
#include "hwconsts.h"
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    25
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    26
GameUIConfig::GameUIConfig(HWForm * FormWidgets)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    27
	: QObject()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    28
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    29
	Form = FormWidgets;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    30
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    31
	QFile settings(cfgdir->absolutePath() + "/options");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    32
	if (settings.open(QIODevice::ReadOnly))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    33
	{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    34
		QTextStream stream(&settings);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    35
		stream.setCodec("UTF-8");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    36
		QString str;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    37
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    38
		while (!stream.atEnd())
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    39
		{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    40
			str = stream.readLine();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    41
			if (str.startsWith(";")) continue;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    42
			if (str.startsWith("resolution "))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    43
			{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    44
				Form->ui.pageOptions->CBResolution->setCurrentIndex(str.mid(11).toLong());
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    45
			} else
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    46
			if (str.startsWith("fullscreen "))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    47
			{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    48
				Form->ui.pageOptions->CBFullscreen->setChecked(str.mid(11).toLong());
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    49
			} else
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    50
			if (str.startsWith("sound "))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    51
			{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    52
				Form->ui.pageOptions->CBEnableSound->setChecked(str.mid(6).toLong());
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    53
			} else
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    54
			if (str.startsWith("nick "))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    55
			{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    56
				Form->ui.pageNet->editNetNick->setText(str.mid(5));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    57
			} else
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    58
			if (str.startsWith("ip "))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    59
			{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    60
				Form->ui.pageNet->editIP->setText(str.mid(3));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    61
			}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    62
		}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    63
		settings.close();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    64
	}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    65
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    66
	QFile themesfile(datadir->absolutePath() + "/Themes/themes.cfg");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    67
	if (themesfile.open(QIODevice::ReadOnly)) {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    68
		QTextStream stream(&themesfile);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    69
		QString str;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    70
		while (!stream.atEnd())
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    71
		{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    72
			Themes << stream.readLine();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    73
		}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    74
		themesfile.close();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    75
	} else {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    76
		QMessageBox::critical(0, "Error", "Cannot access themes.cfg", "OK");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    77
	}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    78
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    79
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    80
QStringList GameUIConfig::GetTeamsList()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    81
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    82
	QStringList teamslist = cfgdir->entryList(QStringList("*.cfg"));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    83
	QStringList cleanedList;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    84
	for (QStringList::Iterator it = teamslist.begin(); it != teamslist.end(); ++it ) {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    85
	  QString tmpTeamStr=(*it).replace(QRegExp("^(.*).cfg$"), "\\1");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    86
	  cleanedList.push_back(tmpTeamStr);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    87
	}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    88
	return cleanedList;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    89
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    90
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    91
void GameUIConfig::SaveOptions()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    92
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    93
	QFile settings(cfgdir->absolutePath() + "/options");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    94
	if (!settings.open(QIODevice::WriteOnly))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    95
	{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    96
		QMessageBox::critical(0,
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    97
				tr("Error"),
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    98
				tr("Cannot save options to file %1").arg(settings.fileName()),
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    99
				tr("Quit"));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   100
		return ;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   101
	}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   102
	QTextStream stream(&settings);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   103
	stream.setCodec("UTF-8");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   104
	stream << "; Generated by Hedgewars, do not modify" << endl;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   105
	stream << "resolution " << Form->ui.pageOptions->CBResolution->currentIndex() << endl;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   106
	stream << "fullscreen " << Form->ui.pageOptions->CBFullscreen->isChecked() << endl;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   107
	stream << "sound " << Form->ui.pageOptions->CBEnableSound->isChecked() << endl;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   108
	stream << "nick " << Form->ui.pageNet->editNetNick->text() << endl;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   109
	stream << "ip " << Form->ui.pageNet->editIP->text() << endl;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   110
	settings.close();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   111
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   112
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   113
int GameUIConfig::vid_Resolution()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   114
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   115
	return Form->ui.pageOptions->CBResolution->currentIndex();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   116
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   117
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   118
bool GameUIConfig::vid_Fullscreen()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   119
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   120
	return Form->ui.pageOptions->CBFullscreen->isChecked();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   121
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   122
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   123
bool GameUIConfig::isSoundEnabled()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   124
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   125
	return Form->ui.pageOptions->CBEnableSound->isChecked();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   126
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   127
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   128
QString GameUIConfig::GetRandomTheme()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   129
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   130
	return (Themes.size() > 0) ? Themes[rand() % Themes.size()] : QString("steel");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   131
}