QTfrontend/gameuiconfig.cpp
author unc0rr
Tue, 25 Jul 2006 13:32:11 +0000
changeset 85 44d9045b26ff
parent 84 QTfrontend/gameconfig.cpp@0f6669da2fcb
child 87 ff213e443336
permissions -rw-r--r--
New GameCFGWidget. Now it's possible to set forts mode from ui
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
 *
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
     5
 * Distributed under the terms of the BSD-modified licence:
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
     6
 *
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
     7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
     8
 * of this software and associated documentation files (the "Software"), to deal
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
     9
 * with the Software without restriction, including without limitation the
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    11
 * sell copies of the Software, and to permit persons to whom the Software is
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    12
 * furnished to do so, subject to the following conditions:
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    13
 *
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    14
 * 1. Redistributions of source code must retain the above copyright notice,
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    15
 *    this list of conditions and the following disclaimer.
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    16
 * 2. Redistributions in binary form must reproduce the above copyright notice,
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    17
 *    this list of conditions and the following disclaimer in the documentation
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    18
 *    and/or other materials provided with the distribution.
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    19
 * 3. The name of the author may not be used to endorse or promote products
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    20
 *    derived from this software without specific prior written permission.
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    21
 *
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    23
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    24
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    25
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    28
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    29
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    30
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    31
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    32
 */
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    33
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    34
#include <QMessageBox>
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    35
#include <QTextStream>
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
    36
#include "gameuiconfig.h"
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    37
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
    38
GameUIConfig::GameUIConfig(HWForm * FormWidgets)
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    39
	: QObject()
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    40
{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    41
	Form = FormWidgets;
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    42
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    43
	cfgdir.setPath(cfgdir.homePath());
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    44
	if (!cfgdir.exists(".hedgewars"))
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    45
	{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    46
		if (!cfgdir.mkdir(".hedgewars"))
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    47
		{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    48
			QMessageBox::critical(0,
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    49
					tr("Error"),
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    50
					tr("Cannot create directory %1").arg("/.hedgewars"),
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    51
					tr("Quit"));
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    52
			return ;
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    53
		}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    54
	}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    55
	cfgdir.cd(".hedgewars");
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    56
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    57
	QFile settings(cfgdir.absolutePath() + "/options");
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    58
	if (settings.open(QIODevice::ReadOnly))
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    59
	{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    60
		QTextStream stream(&settings);
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    61
		stream.setCodec("UTF-8");
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    62
		QString str;
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    63
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    64
		while (!stream.atEnd())
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    65
		{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    66
			str = stream.readLine();
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    67
			if (str.startsWith(";")) continue;
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    68
			if (str.startsWith("resolution "))
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    69
			{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    70
				Form->ui.CBResolution->setCurrentIndex(str.mid(11).toLong());
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    71
			} else
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    72
			if (str.startsWith("fullscreen "))
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    73
			{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    74
				Form->ui.CBFullscreen->setChecked(str.mid(11).toLong());
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    75
			} else
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    76
			if (str.startsWith("sound "))
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    77
			{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    78
				Form->ui.CBEnableSound->setChecked(str.mid(6).toLong());
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    79
			} else
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    80
			if (str.startsWith("nick "))
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    81
			{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    82
				Form->ui.editNetNick->setText(str.mid(5));
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    83
			}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    84
		}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    85
		settings.close();
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    86
	}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    87
}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    88
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
    89
QStringList GameUIConfig::GetTeamsList()
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    90
{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    91
	return cfgdir.entryList(QStringList("*.cfg"));
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    92
}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    93
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
    94
void GameUIConfig::SaveOptions()
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    95
{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    96
	QFile settings(cfgdir.absolutePath() + "/options");
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    97
	if (!settings.open(QIODevice::WriteOnly))
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    98
	{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    99
		QMessageBox::critical(0,
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   100
				tr("Error"),
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   101
				tr("Cannot save options to file %1").arg(settings.fileName()),
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   102
				tr("Quit"));
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   103
		return ;
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   104
	}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   105
	QTextStream stream(&settings);
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   106
	stream.setCodec("UTF-8");
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   107
	stream << "; Generated by Hedgewars, do not modify" << endl;
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   108
	stream << "resolution " << Form->ui.CBResolution->currentIndex() << endl;
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   109
	stream << "fullscreen " << Form->ui.CBFullscreen->isChecked() << endl;
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   110
	stream << "sound " << Form->ui.CBEnableSound->isChecked() << endl;
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   111
	stream << "nick " << Form->ui.editNetNick->text() << endl;
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   112
	settings.close();
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   113
}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   114
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
   115
int GameUIConfig::vid_Resolution()
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   116
{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   117
	return Form->ui.CBResolution->currentIndex();
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   118
}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   119
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
   120
bool GameUIConfig::vid_Fullscreen()
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   121
{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   122
	return Form->ui.CBFullscreen->isChecked();
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   123
}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   124
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
   125
bool GameUIConfig::isSoundEnabled()
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   126
{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   127
	return Form->ui.CBEnableSound->isChecked();
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   128
}