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