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