QTfrontend/gameuiconfig.cpp
author unc0rr
Sun, 10 Dec 2006 18:57:05 +0000
changeset 297 279e10ec31b4
parent 184 f97a7a3dc8f6
child 301 29bf9c1a3ad3
permissions -rw-r--r--
'Show FPS' and 'FPS limit' options
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"
297
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
    25
#include "fpsedit.h"
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    26
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    27
GameUIConfig::GameUIConfig(HWForm * FormWidgets)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    28
	: QObject()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    29
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    30
	Form = FormWidgets;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    31
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    32
	QFile settings(cfgdir->absolutePath() + "/options");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    33
	if (settings.open(QIODevice::ReadOnly))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    34
	{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    35
		QTextStream stream(&settings);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    36
		stream.setCodec("UTF-8");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    37
		QString str;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    38
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    39
		while (!stream.atEnd())
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    40
		{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    41
			str = stream.readLine();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    42
			if (str.startsWith(";")) continue;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    43
			if (str.startsWith("resolution "))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    44
			{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    45
				Form->ui.pageOptions->CBResolution->setCurrentIndex(str.mid(11).toLong());
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    46
			} else
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    47
			if (str.startsWith("fullscreen "))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    48
			{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    49
				Form->ui.pageOptions->CBFullscreen->setChecked(str.mid(11).toLong());
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    50
			} else
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    51
			if (str.startsWith("sound "))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    52
			{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    53
				Form->ui.pageOptions->CBEnableSound->setChecked(str.mid(6).toLong());
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    54
			} else
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    55
			if (str.startsWith("nick "))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    56
			{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    57
				Form->ui.pageNet->editNetNick->setText(str.mid(5));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    58
			} else
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    59
			if (str.startsWith("ip "))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    60
			{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    61
				Form->ui.pageNet->editIP->setText(str.mid(3));
297
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
    62
			} else
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
    63
			if (str.startsWith("showfps "))
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
    64
			{
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
    65
				Form->ui.pageOptions->CBShowFPS->setChecked(str.mid(8).toLong());
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
    66
			} else
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
    67
			if (str.startsWith("interval "))
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
    68
			{
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
    69
				Form->ui.pageOptions->fpsedit->setValue(str.mid(9).toUInt());
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    70
			}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    71
		}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    72
		settings.close();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    73
	}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    74
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    75
	QFile themesfile(datadir->absolutePath() + "/Themes/themes.cfg");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    76
	if (themesfile.open(QIODevice::ReadOnly)) {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    77
		QTextStream stream(&themesfile);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    78
		QString str;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    79
		while (!stream.atEnd())
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    80
		{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    81
			Themes << stream.readLine();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    82
		}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    83
		themesfile.close();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    84
	} else {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    85
		QMessageBox::critical(0, "Error", "Cannot access themes.cfg", "OK");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    86
	}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    87
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    88
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    89
QStringList GameUIConfig::GetTeamsList()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    90
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    91
	QStringList teamslist = cfgdir->entryList(QStringList("*.cfg"));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    92
	QStringList cleanedList;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    93
	for (QStringList::Iterator it = teamslist.begin(); it != teamslist.end(); ++it ) {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    94
	  QString tmpTeamStr=(*it).replace(QRegExp("^(.*).cfg$"), "\\1");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    95
	  cleanedList.push_back(tmpTeamStr);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    96
	}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    97
	return cleanedList;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    98
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    99
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   100
void GameUIConfig::SaveOptions()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   101
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   102
	QFile settings(cfgdir->absolutePath() + "/options");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   103
	if (!settings.open(QIODevice::WriteOnly))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   104
	{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   105
		QMessageBox::critical(0,
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   106
				tr("Error"),
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   107
				tr("Cannot save options to file %1").arg(settings.fileName()),
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   108
				tr("Quit"));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   109
		return ;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   110
	}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   111
	QTextStream stream(&settings);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   112
	stream.setCodec("UTF-8");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   113
	stream << "; Generated by Hedgewars, do not modify" << endl;
297
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
   114
	stream << "resolution " << vid_Resolution() << endl;
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
   115
	stream << "fullscreen " << vid_Fullscreen() << endl;
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
   116
	stream << "sound " << isSoundEnabled() << endl;
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   117
	stream << "nick " << Form->ui.pageNet->editNetNick->text() << endl;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   118
	stream << "ip " << Form->ui.pageNet->editIP->text() << endl;
297
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
   119
	stream << "showfps " << isShowFPSEnabled() << endl;
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
   120
	stream << "interval " << Form->ui.pageOptions->fpsedit->value() << endl;
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   121
	settings.close();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   122
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   123
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   124
int GameUIConfig::vid_Resolution()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   125
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   126
	return Form->ui.pageOptions->CBResolution->currentIndex();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   127
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   128
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   129
bool GameUIConfig::vid_Fullscreen()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   130
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   131
	return Form->ui.pageOptions->CBFullscreen->isChecked();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   132
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   133
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   134
bool GameUIConfig::isSoundEnabled()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   135
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   136
	return Form->ui.pageOptions->CBEnableSound->isChecked();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   137
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   138
297
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
   139
bool GameUIConfig::isShowFPSEnabled()
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
   140
{
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
   141
	return Form->ui.pageOptions->CBShowFPS->isChecked();
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
   142
}
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
   143
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
   144
quint8 GameUIConfig::timerInterval()
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
   145
{
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
   146
	return 35 - Form->ui.pageOptions->fpsedit->value();
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
   147
}
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 184
diff changeset
   148
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   149
QString GameUIConfig::GetRandomTheme()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   150
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   151
	return (Themes.size() > 0) ? Themes[rand() % Themes.size()] : QString("steel");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   152
}