/* * 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 "CoinList.h" // Pokemod includes #include "CoinListObject.h" #include "Item.h" #include "ItemEffect.h" #include "Pokemod.h" // Qt includes #include CoinList::CoinList(const CoinList& coinList) : Object("CoinList", coinList.parent(), coinList.id()) { *this = coinList; } CoinList::CoinList(const Pokemod* parent, const int id) : Object("CoinList", parent, id), m_name(""), m_value(0) { } CoinList::CoinList(const CoinList& coinList, const Pokemod* parent, const int id) : Object("CoinList", parent, id) { *this = coinList; } CoinList::CoinList(const QDomElement& xml, const Pokemod* parent, const int id) : Object("CoinList", parent, id) { load(xml, id); } CoinList::~CoinList() { clear(); } void CoinList::validate() { if (m_name.isEmpty()) emit(error("Name is empty")); TEST(setValue, value); if (!objectCount()) emit(error("There are no objects")); QSet idChecker; QSet itemChecker; QSet speciesChecker; foreach (CoinListObject* object, m_objects) { object->validate(); if (idChecker.contains(object->id())) emit(error(subclass("object", object->id()))); idChecker.insert(object->id()); if (object->type() == CoinListObject::Item) { if (itemChecker.contains(object->object())) emit(error(subclass("object item", object->id()))); itemChecker.insert(object->object()); } else if (object->type() == CoinListObject::Species) { if (speciesChecker.contains(object->object())) emit(error(subclass("object species", object->id()))); speciesChecker.insert(object->object()); } } } void CoinList::load(const QDomElement& xml, int id) { LOAD_ID(); LOAD(QString, name); LOAD(int, value); LOAD_SUB(newObject, CoinListObject); } QDomElement CoinList::save() const { SAVE_CREATE(); SAVE(QString, name); SAVE(int, value); SAVE_SUB(CoinListObject, objects); return xml; } void CoinList::setName(const QString& name) { m_name = name; emit(changed()); } void CoinList::setValue(const int value) { for (int i = 0; (i < static_cast(pokemod())->itemCount()); ++i) { const Item* item = static_cast(pokemod())->item(i); for (int j = 0; (j < item->effectCount()); ++j) { const ItemEffect* effect = item->effect(j); if ((effect->effect() == ItemEffect::E_CoinCase) && (effect->value1() == value)) { m_value = value; emit(changed()); return; } } } emit(error(bounds("value"))); } QString CoinList::name() const { return m_name; } int CoinList::value() const { return m_value; } const CoinListObject* CoinList::object(const int index) const { if (objectCount() <= index) return NULL; return m_objects.at(index); } CoinListObject* CoinList::object(const int index) { if (objectCount() <= index) return NULL; return m_objects[index]; } const CoinListObject* CoinList::objectById(const int id) const { return object(objectIndex(id)); } CoinListObject* CoinList::objectById(const int id) { return object(objectIndex(id)); } int CoinList::objectIndex(const int id) const { for (int i = 0; i < objectCount(); ++i) { if (m_objects[i]->id() == id) return i; } return INT_MAX; } int CoinList::objectCount() const { return m_objects.size(); } CoinListObject* CoinList::newObject() { return newObject(new CoinListObject(this, objectId())); } CoinListObject* CoinList::newObject(const QDomElement& xml) { return newObject(new CoinListObject(xml, this, objectId())); } CoinListObject* CoinList::newObject(const CoinListObject& object) { return newObject(new CoinListObject(object, this, objectId())); } CoinListObject* CoinList::newObject(CoinListObject* object) { m_objects.append(object); return object; } void CoinList::deleteObject(const int index) { if (objectCount() <= index) return; delete m_objects[index]; m_objects.removeAt(index); } void CoinList::deleteObjectById(const int id) { deleteObject(objectIndex(id)); } int CoinList::objectId() const { int i = 0; while ((i < objectCount()) && (objectIndex(i) != INT_MAX)) ++i; return i; } CoinList& CoinList::operator=(const CoinList& rhs) { if (this == &rhs) return *this; clear(); COPY(name); COPY_SUB(CoinListObject, objects); return *this; } void CoinList::clear() { while (objectCount()) deleteObject(0); }