/* * 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 . */ /** * \file sigmod/CoinList.cpp */ // Header include #include "CoinList.h" // Sigmod includes #include "CoinListItem.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 (!itemCount()) emit(error("There are no items")); QSet idChecker; QSet itemChecker; QSet speciesChecker; TEST_SUB_BEGIN(CoinListItem, items); TEST_SUB("item", id); if (object->type() == CoinListItem::Item) { TEST_SUB_RAW(item, "item item", object); } else if (object->type() == CoinListItem::Species) { TEST_SUB_RAW(species, "item species", object); } TEST_SUB_END(); TEST_END(); } void Sigmod::CoinList::load(const QDomElement& xml) { LOAD_BEGIN(); LOAD(name); LOAD(script); LOAD_SUB(newItem, CoinListItem); } QDomElement Sigmod::CoinList::save() const { SAVE_CREATE(); SAVE(name); SAVE(script); SAVE_SUB(CoinListItem, items); 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::CoinListItem* Sigmod::CoinList::item(const int index) const { if (index < itemCount()) return m_items.at(index); return NULL; } Sigmod::CoinListItem* Sigmod::CoinList::item(const int index) { if (index < itemCount()) return m_items[index]; return NULL; } const Sigmod::CoinListItem* Sigmod::CoinList::itemById(const int id) const { return item(itemIndex(id)); } Sigmod::CoinListItem* Sigmod::CoinList::itemById(const int id) { return item(itemIndex(id)); } int Sigmod::CoinList::itemIndex(const int id) const { for (int i = 0; i < itemCount(); ++i) { if (m_items[i]->id() == id) return i; } return INT_MAX; } int Sigmod::CoinList::itemCount() const { return m_items.size(); } Sigmod::CoinListItem* Sigmod::CoinList::newItem() { return newItem(new CoinListItem(this, itemId())); } Sigmod::CoinListItem* Sigmod::CoinList::newItem(const QDomElement& xml) { return newItem(new CoinListItem(xml, this, itemId())); } Sigmod::CoinListItem* Sigmod::CoinList::newItem(const CoinListItem& item) { return newItem(new CoinListItem(item, this, itemId())); } Sigmod::CoinListItem* Sigmod::CoinList::newItem(CoinListItem* item) { m_items.append(item); return item; } void Sigmod::CoinList::deleteItem(const int index) { if (index < itemCount()) { delete m_items[index]; m_items.removeAt(index); } } void Sigmod::CoinList::deleteItemById(const int id) { deleteItem(itemIndex(id)); } int Sigmod::CoinList::itemId() const { int i = 0; while ((i < itemCount()) && (itemIndex(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(CoinListItem, items); return *this; } void Sigmod::CoinList::clear() { qDeleteAll(m_items); m_items.clear(); }