1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/****************************************************************
**
** Attal : Lords of Doom
**
** lordArtefactsConfiguration.cpp
** Configuration of artefacts for lords
**
** Version : $Id: lordArtefactsConfiguration.cpp,v 1.1 2003/12/06 14:47:19 audoux Exp $
**
** Author(s) : Pascal Audoux
**
** Date : 05/01/2002
**
** Licence :
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2, or (at your option)
** any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
****************************************************************/
#include "lordArtefactsConfiguration.h"
// generic include files
// include files for QT
#include <qfile.h>
// application specific include files
#include "libCommon/dataTheme.h"
#include "libCommon/log.h"
extern DataTheme DataTheme;
extern QString DATA_PATH;
//
// ----- ArtefactPosition -----
//
ArtefactPosition::ArtefactPosition()
{
_name = "Unknown";
_coordX.setAutoDelete( true );
_coordY.setAutoDelete( true );
_nb = 0;
}
void ArtefactPosition::setNumber( uint nb )
{
_coordX.clear();
_coordY.clear();
_nb = nb;
for( uint i = 0; i < nb; i++ ) {
_coordX.append( new uint(0) );
_coordY.append( new uint(0) );
}
if( _nb == 0 ) {
_coordX.append( new uint(0) );
_coordY.append( new uint(0) );
}
}
uint ArtefactPosition::getNumber()
{
return _nb;
}
void ArtefactPosition::setCoord( uint nb, uint x, uint y )
{
if( nb < _nb ) {
*( _coordX.at( nb ) ) = x;
*( _coordY.at( nb ) ) = y;
} else if( _nb == 0 ) {
*( _coordX.at( 0 ) ) = x;
*( _coordY.at( 0 ) ) = y;
}
}
uint ArtefactPosition::getCoordX( uint nb )
{
if( _nb < nb ) {
return *( _coordX.at( nb ) );
} else {
return *( _coordX.at( 0 ) );
}
}
uint ArtefactPosition::getCoordY( uint nb )
{
if( _nb < nb ) {
return *( _coordY.at( nb ) );
} else {
return *( _coordY.at( 0 ) );
}
}
//
// ----- LordartefactsConfiguration -----
//
LordArtefactsConfiguration::LordArtefactsConfiguration()
{
_artefacts.setAutoDelete( true );
}
void LordArtefactsConfiguration::appendPosition( ArtefactPosition * position )
{
_artefacts.append( position );
}
uint LordArtefactsConfiguration::getNbPosition()
{
return _artefacts.count();
}
QString LordArtefactsConfiguration::getPositionName( uint pos )
{
QString ret = "";
if( pos < _artefacts.count() ) {
ret = _artefacts.at( pos )->getName();
}
return ret;
}
uint LordArtefactsConfiguration::getPositionNumber( uint pos )
{
uint ret = 0;
if( pos < _artefacts.count() ) {
ret = _artefacts.at( pos )->getNumber();
}
return ret;
}
void LordArtefactsConfiguration::setPositionCoord( uint pos, uint nb, uint x, uint y )
{
if( pos < _artefacts.count() ) {
_artefacts.at( pos )->setCoord( nb, x, y );
}
}
uint LordArtefactsConfiguration::getPositionCoordX( uint pos, uint nb )
{
uint ret = 0;
if( pos < _artefacts.count() ) {
ret = _artefacts.at( pos )->getCoordX( nb );
}
return ret;
}
uint LordArtefactsConfiguration::getPositionCoordY( uint pos, uint nb )
{
uint ret = 0;
if( pos < _artefacts.count() ) {
ret = _artefacts.at( pos )->getCoordY( nb );
}
return ret;
}
bool LordArtefactsConfiguration::init()
{
clear();
ArtefactsConfigurationHandler handler( this );
QFile file( DATA_PATH + "lordArtefacts.dat" );
QXmlInputSource source( file );
QXmlSimpleReader reader;
reader.setContentHandler( &handler );
reader.setErrorHandler( &handler );
bool ok = reader.parse( source );
file.close();
if ( !ok ) {
logEE( "Parse Error (%s) : %s", QString( DATA_PATH + "lordArtefacts.dat" ).latin1(), handler.errorProtocol().latin1() );
return false;
}
return true;
}
void LordArtefactsConfiguration::clear()
{
_artefacts.clear();
}
//
// ----- ArtefactsConfigurationHandler -----
//
ArtefactsConfigurationHandler::ArtefactsConfigurationHandler( LordArtefactsConfiguration * config )
{
_config = config;
}
bool ArtefactsConfigurationHandler::startDocument()
{
// at the beginning of parsing: do some initialization
_errorProt = "";
_config->clear();
_state = StateInit;
return true;
}
bool ArtefactsConfigurationHandler::startElement( const QString&, const QString&, const QString& qName, const QXmlAttributes& atts)
{
if( qName == "lordArtefacts" && _state == StateInit ) {
_state = StateDocument;
} else if( qName == "artefact" && _state == StateDocument ) {
_state = StateArtefact;
_position = new ArtefactPosition();
_cpt = 0;
} else if( qName == "name" && _state == StateArtefact ) {
_state = StateName;
} else if( qName == "number" && _state == StateArtefact ) {
_state = StateNumber;
_position->setNumber( atts.value( "value" ).toInt() );
} else if( qName == "position" && _state == StateNumber ) {
_state = StatePosition;
_position->setCoord( _cpt, atts.value( "x" ).toInt(), atts.value( "y" ).toInt() );
_cpt++;
} else {
// error
return false;
}
return true;
}
bool ArtefactsConfigurationHandler::endElement( const QString&, const QString&, const QString& )
{
switch ( _state ) {
case StateArtefact:
_config->appendPosition( _position );
_state = StateDocument;
break;
case StateName:
_state = StateArtefact;
break;
case StateNumber:
_state = StateArtefact;
break;
case StatePosition:
_state = StateNumber;
break;
default:
// do nothing
break;
}
return true;
}
bool ArtefactsConfigurationHandler::characters( const QString& ch )
{
QString ch_simplified = ch.simplifyWhiteSpace();
if ( ch_simplified.isEmpty() )
return true;
switch( _state ) {
case StateName:
_position->setName( ch_simplified );
break;
default:
return false;
}
return true;
}
bool ArtefactsConfigurationHandler::fatalError( const QXmlParseException& exception )
{
_errorProt += QString( "fatal parsing error: %1 in line %2, column %3\n" )
.arg( exception.message() )
.arg( exception.lineNumber() )
.arg( exception.columnNumber() );
return QXmlDefaultHandler::fatalError( exception );
}
|