/* * 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" // Sigmod includes #include "CoinListObject.h" #include "Macros.h" #include "Sigmod.h" // Qt includes #include Sigmod::CoinList::CoinList(const CoinList& coinList) : Object(coinList.parent(), coinList.id()) { *this = coinList; } Sigmod::CoinList::CoinList(const Sigmod* parent, const int id) : Object(parent, id), m_name(""), m_script("", "") { } Sigmod::CoinList::CoinList(const CoinList& coinList, const Sigmod* parent, const int id) : Object(parent, id) { *this = coinList; } Sigmod::CoinList::CoinList(const QDomElement& xml, const Sigmod* parent, const int id) : Object(parent, id) { LOAD_ID(); load(xml); } Sigmod::CoinList::~CoinList() { clear(); } void Sigmod::CoinList::validate() { TEST_BEGIN(); if (m_name.isEmpty()) emit(error("Name is empty")); if (!objectCount()) emit(error("There are no objects")); QSet idChecker; QSet itemChecker; QSet speciesChecker; TEST_SUB_BEGIN(CoinListObject, objects); TEST_SUB("object", id); if (object->type() == CoinListObject::Item) { TEST_SUB_RAW(item, "object item", object); } else if (object->type() == CoinListObject::Species) { TEST_SUB_RAW(species, "object species", object); } TEST_SUB_END(); // 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()); // } // } TEST_END(); } void Sigmod::CoinList::load(const QDomElement& xml) { LOAD_BEGIN(); LOAD(name); LOAD(script); LOAD_SUB(newObject, CoinListObject); } QDomElement Sigmod::CoinList::save() const { SAVE_CREATE(); SAVE(name); SAVE(script); SAVE_SUB(CoinListObject, objects); return xml; } void Sigmod::CoinList::setName(const QString& name) { CHECK(name); } void Sigmod::CoinList::setScript(const Sigcore::Script& script) { CHECK(script); } QString Sigmod::CoinList::name() const { return m_name; } Sigcore::Script Sigmod::CoinList::script() const { return m_script; } const Sigmod::CoinListObject* Sigmod::CoinList::object(const int index) const { if (index < objectCount()) return m_objects.at(index); return NULL; } Sigmod::CoinListObject* Sigmod::CoinList::object(const int index) { if (index < objectCount()) return m_objects[index]; return NULL; } const Sigmod::CoinListObject* Sigmod::CoinList::objectById(const int id) const { return object(objectIndex(id)); } Sigmod::CoinListObject* Sigmod::CoinList::objectById(const int id) { return object(objectIndex(id)); } int Sigmod::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 Sigmod::CoinList::objectCount() const { return m_objects.size(); } Sigmod::CoinListObject* Sigmod::CoinList::newObject() { return newObject(new CoinListObject(this, objectId())); } Sigmod::CoinListObject* Sigmod::CoinList::newObject(const QDomElement& xml) { return newObject(new CoinListObject(xml, this, objectId())); } Sigmod::CoinListObject* Sigmod::CoinList::newObject(const CoinListObject& object) { return newObject(new CoinListObject(object, this, objectId())); } Sigmod::CoinListObject* Sigmod::CoinList::newObject(CoinListObject* object) { m_objects.append(object); return object; } void Sigmod::CoinList::deleteObject(const int index) { if (index < objectCount()) { delete m_objects[index]; m_objects.removeAt(index); } } void Sigmod::CoinList::deleteObjectById(const int id) { deleteObject(objectIndex(id)); } int Sigmod::CoinList::objectId() const { int i = 0; while ((i < objectCount()) && (objectIndex(i) != INT_MAX)) ++i; return i; } Sigmod::CoinList& Sigmod::CoinList::operator=(const CoinList& rhs) { if (this == &rhs) return *this; clear(); COPY(name); COPY(script); COPY_SUB(CoinListObject, objects); return *this; } void Sigmod::CoinList::clear() { qDeleteAll(m_objects); m_objects.clear(); }