QTfrontend/gameuiconfig.cpp
author unc0rr
Wed, 26 Jul 2006 12:25:48 +0000
changeset 87 ff213e443336
parent 85 44d9045b26ff
child 97 e7c1df9cce2c
permissions -rw-r--r--
Convert all pages to classes
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"
87
ff213e443336 Convert all pages to classes
unc0rr
parents: 85
diff changeset
    37
#include "hwform.h"
ff213e443336 Convert all pages to classes
unc0rr
parents: 85
diff changeset
    38
#include "pages.h"
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    39
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
    40
GameUIConfig::GameUIConfig(HWForm * FormWidgets)
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    41
	: QObject()
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    42
{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    43
	Form = FormWidgets;
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    44
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    45
	cfgdir.setPath(cfgdir.homePath());
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    46
	if (!cfgdir.exists(".hedgewars"))
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    47
	{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    48
		if (!cfgdir.mkdir(".hedgewars"))
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    49
		{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    50
			QMessageBox::critical(0,
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    51
					tr("Error"),
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    52
					tr("Cannot create directory %1").arg("/.hedgewars"),
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    53
					tr("Quit"));
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    54
			return ;
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    55
		}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    56
	}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    57
	cfgdir.cd(".hedgewars");
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    58
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    59
	QFile settings(cfgdir.absolutePath() + "/options");
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    60
	if (settings.open(QIODevice::ReadOnly))
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    61
	{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    62
		QTextStream stream(&settings);
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    63
		stream.setCodec("UTF-8");
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    64
		QString str;
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    65
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    66
		while (!stream.atEnd())
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    67
		{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    68
			str = stream.readLine();
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    69
			if (str.startsWith(";")) continue;
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    70
			if (str.startsWith("resolution "))
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    71
			{
87
ff213e443336 Convert all pages to classes
unc0rr
parents: 85
diff changeset
    72
				Form->ui.pageOptions->CBResolution->setCurrentIndex(str.mid(11).toLong());
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    73
			} else
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    74
			if (str.startsWith("fullscreen "))
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    75
			{
87
ff213e443336 Convert all pages to classes
unc0rr
parents: 85
diff changeset
    76
				Form->ui.pageOptions->CBFullscreen->setChecked(str.mid(11).toLong());
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    77
			} else
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    78
			if (str.startsWith("sound "))
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    79
			{
87
ff213e443336 Convert all pages to classes
unc0rr
parents: 85
diff changeset
    80
				Form->ui.pageOptions->CBEnableSound->setChecked(str.mid(6).toLong());
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    81
			} else
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    82
			if (str.startsWith("nick "))
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    83
			{
87
ff213e443336 Convert all pages to classes
unc0rr
parents: 85
diff changeset
    84
				Form->ui.pageOptions->editNetNick->setText(str.mid(5));
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    85
			}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    86
		}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    87
		settings.close();
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    88
	}
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
QStringList GameUIConfig::GetTeamsList()
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    92
{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    93
	return cfgdir.entryList(QStringList("*.cfg"));
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    94
}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    95
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
    96
void GameUIConfig::SaveOptions()
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    97
{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    98
	QFile settings(cfgdir.absolutePath() + "/options");
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
    99
	if (!settings.open(QIODevice::WriteOnly))
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   100
	{
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   101
		QMessageBox::critical(0,
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   102
				tr("Error"),
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   103
				tr("Cannot save options to file %1").arg(settings.fileName()),
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   104
				tr("Quit"));
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   105
		return ;
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   106
	}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   107
	QTextStream stream(&settings);
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   108
	stream.setCodec("UTF-8");
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   109
	stream << "; Generated by Hedgewars, do not modify" << endl;
87
ff213e443336 Convert all pages to classes
unc0rr
parents: 85
diff changeset
   110
	stream << "resolution " << Form->ui.pageOptions->CBResolution->currentIndex() << endl;
ff213e443336 Convert all pages to classes
unc0rr
parents: 85
diff changeset
   111
	stream << "fullscreen " << Form->ui.pageOptions->CBFullscreen->isChecked() << endl;
ff213e443336 Convert all pages to classes
unc0rr
parents: 85
diff changeset
   112
	stream << "sound " << Form->ui.pageOptions->CBEnableSound->isChecked() << endl;
ff213e443336 Convert all pages to classes
unc0rr
parents: 85
diff changeset
   113
	stream << "nick " << Form->ui.pageOptions->editNetNick->text() << endl;
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   114
	settings.close();
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   115
}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   116
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
   117
int GameUIConfig::vid_Resolution()
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   118
{
87
ff213e443336 Convert all pages to classes
unc0rr
parents: 85
diff changeset
   119
	return Form->ui.pageOptions->CBResolution->currentIndex();
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   120
}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   121
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
   122
bool GameUIConfig::vid_Fullscreen()
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   123
{
87
ff213e443336 Convert all pages to classes
unc0rr
parents: 85
diff changeset
   124
	return Form->ui.pageOptions->CBFullscreen->isChecked();
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   125
}
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   126
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
   127
bool GameUIConfig::isSoundEnabled()
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   128
{
87
ff213e443336 Convert all pages to classes
unc0rr
parents: 85
diff changeset
   129
	return Form->ui.pageOptions->CBEnableSound->isChecked();
84
0f6669da2fcb Start rewrite ui
unc0rr
parents:
diff changeset
   130
}