QTfrontend/team.cpp
author unc0rr
Mon, 05 Dec 2005 21:46:15 +0000
changeset 24 79c411363184
parent 21 dff476dcaaa3
child 25 27aa8030322b
permissions -rw-r--r--
Add theme objects to land
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
     1
/*
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
     2
 * Hedgewars, a worms-like game
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
     4
 *
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
     5
 * Distributed under the terms of the BSD-modified licence:
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
     6
 *
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
     7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
     8
 * of this software and associated documentation files (the "Software"), to deal
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
     9
 * with the Software without restriction, including without limitation the
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    11
 * sell copies of the Software, and to permit persons to whom the Software is
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    12
 * furnished to do so, subject to the following conditions:
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    13
 *
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    14
 * 1. Redistributions of source code must retain the above copyright notice,
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    15
 *    this list of conditions and the following disclaimer.
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    16
 * 2. Redistributions in binary form must reproduce the above copyright notice,
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    17
 *    this list of conditions and the following disclaimer in the documentation
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    18
 *    and/or other materials provided with the distribution.
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    19
 * 3. The name of the author may not be used to endorse or promote products
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    20
 *    derived from this software without specific prior written permission.
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    21
 *
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    23
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    24
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    25
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    28
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    29
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    30
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    31
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    32
 */
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    33
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    34
#include <QFile>
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    35
#include <QTextStream>
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    36
#include "team.h"
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    37
#include "hwform.h"
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    38
21
dff476dcaaa3 - Many improvements to frontend
unc0rr
parents: 20
diff changeset
    39
HWTeam::HWTeam()
20
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    40
{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    41
	TeamName = "unnamed";
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    42
	for (int i = 0; i < 8; i++) HHName[i].sprintf("hedgehog %d", i);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    43
	Grave = "Simple";
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    44
	Fort = "Barrelhouse";
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    45
	for(int i = 0; i < BINDS_NUMBER; i++)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    46
	{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    47
		binds[i].action = cbinds[i].action;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    48
		binds[i].strbind = cbinds[i].strbind;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    49
	}
21
dff476dcaaa3 - Many improvements to frontend
unc0rr
parents: 20
diff changeset
    50
	dir = "";
20
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    51
}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    52
	
21
dff476dcaaa3 - Many improvements to frontend
unc0rr
parents: 20
diff changeset
    53
bool HWTeam::LoadFromFile()
20
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    54
{
21
dff476dcaaa3 - Many improvements to frontend
unc0rr
parents: 20
diff changeset
    55
	QFile cfgfile(dir + "/" + TeamName + ".cfg");
20
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    56
	if (!cfgfile.open(QIODevice::ReadOnly)) return false;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    57
	QTextStream stream(&cfgfile);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    58
	stream.setCodec("UTF-8");	
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    59
	QString str;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    60
	QString action;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    61
	
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    62
	while (!stream.atEnd())
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    63
	{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    64
		str = stream.readLine();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    65
		if (str.startsWith(";")) continue;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    66
		if (str.startsWith("name team "))
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    67
		{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    68
			str.remove(0, 10);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    69
			TeamName = str;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    70
		} else
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    71
		if (str.startsWith("name hh"))
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    72
		{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    73
			str.remove(0, 7);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    74
			long i = str.left(1).toLong();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    75
			if ((i < 0) || (i > 7)) continue;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    76
			str.remove(0, 2);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    77
			HHName[i] = str;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    78
		} else
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    79
		if (str.startsWith("grave "))
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    80
		{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    81
			str.remove(0, 6);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    82
			Grave = str;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    83
		} else
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    84
		if (str.startsWith("fort "))
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    85
		{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    86
			str.remove(0, 5);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    87
			Fort = str;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    88
		} else
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    89
		if (str.startsWith("bind "))
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    90
		{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    91
			str.remove(0, 5);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    92
			action = str.section(' ', 1);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    93
			str = str.section(' ', 0, 0);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    94
			str.truncate(15);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    95
			for (int i = 0; i < BINDS_NUMBER; i++)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    96
				if (action == binds[i].action)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    97
				{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    98
					binds[i].strbind = str;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    99
					break;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   100
				}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   101
		}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   102
	}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   103
	cfgfile.close();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   104
	return true;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   105
}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   106
21
dff476dcaaa3 - Many improvements to frontend
unc0rr
parents: 20
diff changeset
   107
bool HWTeam::SaveToFile()
20
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   108
{			
21
dff476dcaaa3 - Many improvements to frontend
unc0rr
parents: 20
diff changeset
   109
	QFile cfgfile(dir + "/" + TeamName + ".cfg");
20
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   110
	if (!cfgfile.open(QIODevice::WriteOnly)) return false;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   111
	QTextStream stream(&cfgfile);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   112
	stream.setCodec("UTF-8");
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   113
	stream << "; Generated by Hedgewars, do not modify" << endl;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   114
	stream << "name team " << TeamName << endl;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   115
	for (int i = 0; i < BINDS_NUMBER; i++)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   116
		stream << "name hh" << i << " " << HHName[i] << endl;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   117
	stream << "grave " << Grave << endl;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   118
	stream << "fort " << Fort << endl;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   119
	for(int i = 0; i < BINDS_NUMBER; i++)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   120
	{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   121
		stream << "bind " << binds[i].strbind << " " << binds[i].action << endl;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   122
	}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   123
	cfgfile.close();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   124
	return true;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   125
}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   126
21
dff476dcaaa3 - Many improvements to frontend
unc0rr
parents: 20
diff changeset
   127
void HWTeam::SetToPage(HWForm * hwform)
20
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   128
{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   129
	form->TeamNameEdit->setText(TeamName);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   130
	for(int i = 0; i < 8; i++)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   131
	{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   132
		form->HHNameEdit[i]->setText(HHName[i]);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   133
	}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   134
	form->ui.CBGrave->setCurrentIndex(form->ui.CBGrave->findText(Grave));
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   135
	form->CBGrave_activated(Grave);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   136
	
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   137
	form->ui.CBFort->setCurrentIndex(form->ui.CBFort->findText(Fort));
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   138
	form->CBFort_activated(Fort);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   139
	
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   140
	for(int i = 0; i < BINDS_NUMBER; i++)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   141
	{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   142
		form->CBBind[i]->setCurrentIndex(form->CBBind[i]->findText(binds[i].strbind));
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   143
	}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   144
}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   145
21
dff476dcaaa3 - Many improvements to frontend
unc0rr
parents: 20
diff changeset
   146
void HWTeam::GetFromPage(HWForm * hwform)
20
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   147
{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   148
	TeamName  = form->TeamNameEdit->text();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   149
	for(int i = 0; i < 8; i++)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   150
	{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   151
		HHName[i] = form->HHNameEdit[i]->text();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   152
	}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   153
	
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   154
	Grave = form->ui.CBGrave->currentText();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   155
	Fort = form->ui.CBFort->currentText();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   156
	for(int i = 0; i < 8; i++)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   157
	{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   158
		binds[i].strbind = form->CBBind[i]->currentText();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   159
	}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   160
}
21
dff476dcaaa3 - Many improvements to frontend
unc0rr
parents: 20
diff changeset
   161
dff476dcaaa3 - Many improvements to frontend
unc0rr
parents: 20
diff changeset
   162
void HWTeam::SetCfgDir(const QString & dir)
dff476dcaaa3 - Many improvements to frontend
unc0rr
parents: 20
diff changeset
   163
{
dff476dcaaa3 - Many improvements to frontend
unc0rr
parents: 20
diff changeset
   164
	this->dir = dir;
dff476dcaaa3 - Many improvements to frontend
unc0rr
parents: 20
diff changeset
   165
}