/* * 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/Item.cpp */ // Header include #include "Item.h" // Sigmod includes #include "ItemType.h" #include "Macros.h" #include "Rules.h" #include "Sigmod.h" // Qt includes #include Sigmod::Item::Item(const Item& item) : Object(item.parent(), item.id()) { *this = item; } Sigmod::Item::Item(const Sigmod* parent, const int id) : Object(parent, id), m_name(""), m_sellable(false), m_type(-1), m_price(0), m_sellPrice(0), m_weight(0), m_description(""), m_script("", "") { } Sigmod::Item::Item(const Item& item, const Sigmod* parent, const int id) : Object(parent, id) { *this = item; } Sigmod::Item::Item(const QDomElement& xml, const Sigmod* parent, const int id) : Object(parent, id) { LOAD_ID(); load(xml); } void Sigmod::Item::validate() { TEST_BEGIN(); if (m_name.isEmpty()) emit(error("Name is empty")); TEST(setType, type); TEST(setPrice, price); if (m_sellable) TEST(setSellPrice, sellPrice); TEST(setWeight, weight); TEST_END(); } void Sigmod::Item::load(const QDomElement& xml) { LOAD_BEGIN(); LOAD(name); LOAD(sellable); LOAD(type); LOAD(price); LOAD(sellPrice); LOAD(weight); LOAD(description); LOAD(script); } QDomElement Sigmod::Item::save() const { SAVE_CREATE(); SAVE(name); SAVE(sellable); SAVE(type); SAVE(price); SAVE(sellPrice); SAVE(weight); SAVE(description); SAVE(script); return xml; } void Sigmod::Item::setName(const QString& name) { CHECK(name); } void Sigmod::Item::setSellable(const bool sellable) { CHECK(sellable); } void Sigmod::Item::setType(const int type) { if (!sigmod()->itemTypeById(type)) emit(error(bounds("type", type))); else CHECK(type); } void Sigmod::Item::setPrice(const int price) { if ((price <= 0) || (sigmod()->rules()->maxMoney() < price)) emit(error(bounds("price", 1, sigmod()->rules()->maxMoney(), price))); else CHECK(price); } void Sigmod::Item::setSellPrice(const int sellPrice) { if ((sellPrice < 0) || (m_price < sellPrice)) emit(error(bounds("sellPrice", 0, m_price, sellPrice))); else CHECK(sellPrice); } void Sigmod::Item::setWeight(const int weight) { const ItemType* type = sigmod()->itemTypeById(m_type); if (type) { if ((weight < 0) || (type->maxWeight() < weight)) emit(error(bounds("weight", 0, type->maxWeight(), weight))); else CHECK(weight); } } void Sigmod::Item::setDescription(const QString& description) { CHECK(description); } void Sigmod::Item::setScript(const Sigcore::Script& script) { CHECK(script); } QString Sigmod::Item::name() const { return m_name; } bool Sigmod::Item::sellable() const { return m_sellable; } int Sigmod::Item::type() const { return m_type; } int Sigmod::Item::price() const { return m_price; } int Sigmod::Item::sellPrice() const { return m_sellPrice; } int Sigmod::Item::weight() const { return m_weight; } QString Sigmod::Item::description() const { return m_description; } Sigcore::Script Sigmod::Item::script() const { return m_script; } Sigmod::Item& Sigmod::Item::operator=(const Item& rhs) { if (this == &rhs) return *this; COPY(name); COPY(sellable); COPY(type); COPY(price); COPY(sellPrice); COPY(weight); COPY(description); COPY(script); return *this; }