/* * Copyright 2007-2008 Ben Boeckel * * 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 3 of the License, 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. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ // Header include #include "MapWildList.h" // Sigmod includes #include "Macros.h" #include "Map.h" #include "MapWildListEncounter.h" // Qt includes #include Sigmod::MapWildList::MapWildList(const MapWildList& wildList) : Object(wildList.parent(), wildList.id()) { *this = wildList; } Sigmod::MapWildList::MapWildList(const Map* parent, const int id) : Object(parent, id), m_name("") { } Sigmod::MapWildList::MapWildList(const MapWildList& wildList, const Map* parent, const int id) : Object(parent, id) { *this = wildList; } Sigmod::MapWildList::MapWildList(const QDomElement& xml, const Map* parent, const int id) : Object(parent, id) { LOAD_ID(); load(xml); } Sigmod::MapWildList::~MapWildList() { clear(); } void Sigmod::MapWildList::validate() { TEST_BEGIN(); if (m_name.isEmpty()) emit(error("Name is empty")); if (!encounterCount()) emit(error("There are no encounters")); QSet idChecker; TEST_SUB_BEGIN(MapWildListEncounter, encounters); TEST_SUB("encounter", id); TEST_SUB_END(); TEST_END(); } void Sigmod::MapWildList::load(const QDomElement& xml) { LOAD_BEGIN(); LOAD(name); LOAD_SUB(newEncounter, MapWildListEncounter); } QDomElement Sigmod::MapWildList::save() const { SAVE_CREATE(); SAVE(name); SAVE_SUB(MapWildListEncounter, encounters); return xml; } void Sigmod::MapWildList::setName(const QString& name) { CHECK(name); } QString Sigmod::MapWildList::name() const { return m_name; } const Sigmod::MapWildListEncounter* Sigmod::MapWildList::encounter(const int index) const { Q_ASSERT(index < encounterCount()); return m_encounters.at(index); } Sigmod::MapWildListEncounter* Sigmod::MapWildList::encounter(const int index) { Q_ASSERT(index < encounterCount()); return m_encounters[index]; } const Sigmod::MapWildListEncounter* Sigmod::MapWildList::encounterById(const int id) const { return encounter(encounterIndex(id)); } Sigmod::MapWildListEncounter* Sigmod::MapWildList::encounterById(const int id) { return encounter(encounterIndex(id)); } int Sigmod::MapWildList::encounterIndex(const int id) const { for (int i = 0; i < encounterCount(); ++i) { if (m_encounters[i]->id() == id) return i; } return INT_MAX; } int Sigmod::MapWildList::encounterCount() const { return m_encounters.size(); } Sigmod::MapWildListEncounter* Sigmod::MapWildList::newEncounter() { return newEncounter(new MapWildListEncounter(this, newEncounterId())); } Sigmod::MapWildListEncounter* Sigmod::MapWildList::newEncounter(const QDomElement& xml) { return newEncounter(new MapWildListEncounter(xml, this, newEncounterId())); } Sigmod::MapWildListEncounter* Sigmod::MapWildList::newEncounter(const MapWildListEncounter& encounter) { return newEncounter(new MapWildListEncounter(encounter, this, newEncounterId())); } Sigmod::MapWildListEncounter* Sigmod::MapWildList::newEncounter(MapWildListEncounter* encounter) { m_encounters.append(encounter); return encounter; } void Sigmod::MapWildList::deleteEncounter(const int index) { Q_ASSERT(index < encounterCount()); delete m_encounters[index]; m_encounters.removeAt(index); } void Sigmod::MapWildList::deleteEncounterById(const int id) { deleteEncounter(encounterIndex(id)); } int Sigmod::MapWildList::newEncounterId() const { int i = 0; while ((i < encounterCount()) && (encounterIndex(i) != INT_MAX)) ++i; return i; } Sigmod::MapWildList& Sigmod::MapWildList::operator=(const MapWildList& rhs) { if (this == &rhs) return *this; clear(); COPY(name); COPY_SUB(MapWildListEncounter, encounters); return *this; } void Sigmod::MapWildList::clear() { qDeleteAll(m_encounters); m_encounters.clear(); }