QTfrontend/team.cpp
author unc0rr
Wed, 09 Nov 2005 18:31:11 +0000
changeset 20 ccd2c45f043d
child 21 dff476dcaaa3
permissions -rw-r--r--
frontend rewrite for QT4
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
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    39
HWTeam::HWTeam(HWForm * hwform)
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
	form = hwform;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    46
	for(int i = 0; i < BINDS_NUMBER; i++)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    47
	{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    48
		binds[i].action = cbinds[i].action;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    49
		binds[i].strbind = cbinds[i].strbind;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    50
	}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    51
	
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    52
}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    53
	
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    54
bool HWTeam::LoadFromFile(const QString & filename)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    55
{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    56
	QFile cfgfile(filename);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    57
	if (!cfgfile.open(QIODevice::ReadOnly)) return false;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    58
	QTextStream stream(&cfgfile);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    59
	stream.setCodec("UTF-8");	
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    60
	QString str;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    61
	QString action;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    62
	
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    63
	while (!stream.atEnd())
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    64
	{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    65
		str = stream.readLine();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    66
		if (str.startsWith(";")) continue;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    67
		if (str.startsWith("name team "))
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    68
		{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    69
			str.remove(0, 10);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    70
			TeamName = str;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    71
		} else
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    72
		if (str.startsWith("name hh"))
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    73
		{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    74
			str.remove(0, 7);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    75
			long i = str.left(1).toLong();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    76
			if ((i < 0) || (i > 7)) continue;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    77
			str.remove(0, 2);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    78
			HHName[i] = str;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    79
		} else
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    80
		if (str.startsWith("grave "))
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    81
		{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    82
			str.remove(0, 6);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    83
			Grave = str;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    84
		} else
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    85
		if (str.startsWith("fort "))
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    86
		{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    87
			str.remove(0, 5);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    88
			Fort = str;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    89
		} else
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    90
		if (str.startsWith("bind "))
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    91
		{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    92
			str.remove(0, 5);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    93
			action = str.section(' ', 1);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    94
			str = str.section(' ', 0, 0);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    95
			str.truncate(15);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    96
			for (int i = 0; i < BINDS_NUMBER; i++)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    97
				if (action == binds[i].action)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    98
				{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
    99
					binds[i].strbind = str;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   100
					break;
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
	}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   104
	cfgfile.close();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   105
	return true;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   106
}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   107
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   108
bool HWTeam::SaveToFile(const QString & filename)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   109
{			
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   110
	QFile cfgfile(filename);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   111
	if (!cfgfile.open(QIODevice::WriteOnly)) return false;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   112
	QTextStream stream(&cfgfile);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   113
	stream.setCodec("UTF-8");
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   114
	stream << "; Generated by Hedgewars, do not modify" << endl;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   115
	stream << "name team " << TeamName << endl;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   116
	for (int i = 0; i < BINDS_NUMBER; i++)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   117
		stream << "name hh" << i << " " << HHName[i] << endl;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   118
	stream << "grave " << Grave << endl;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   119
	stream << "fort " << Fort << endl;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   120
	for(int i = 0; i < BINDS_NUMBER; i++)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   121
	{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   122
		stream << "bind " << binds[i].strbind << " " << binds[i].action << endl;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   123
	}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   124
	cfgfile.close();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   125
	return true;
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   126
}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   127
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   128
void HWTeam::ToPage()
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   129
{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   130
	form->TeamNameEdit->setText(TeamName);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   131
	for(int i = 0; i < 8; i++)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   132
	{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   133
		form->HHNameEdit[i]->setText(HHName[i]);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   134
	}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   135
	form->ui.CBGrave->setCurrentIndex(form->ui.CBGrave->findText(Grave));
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   136
	form->CBGrave_activated(Grave);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   137
	
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   138
	form->ui.CBFort->setCurrentIndex(form->ui.CBFort->findText(Fort));
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   139
	form->CBFort_activated(Fort);
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   140
	
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   141
	for(int i = 0; i < BINDS_NUMBER; i++)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   142
	{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   143
		form->CBBind[i]->setCurrentIndex(form->CBBind[i]->findText(binds[i].strbind));
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   144
	}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   145
}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   146
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   147
void HWTeam::FromPage()
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   148
{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   149
	TeamName  = form->TeamNameEdit->text();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   150
	for(int i = 0; i < 8; i++)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   151
	{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   152
		HHName[i] = form->HHNameEdit[i]->text();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   153
	}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   154
	
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   155
	Grave = form->ui.CBGrave->currentText();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   156
	Fort = form->ui.CBFort->currentText();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   157
	for(int i = 0; i < 8; i++)
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   158
	{
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   159
		binds[i].strbind = form->CBBind[i]->currentText();
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   160
	}
ccd2c45f043d frontend rewrite for QT4
unc0rr
parents:
diff changeset
   161
}