/* * Copyright 2008-2009 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 "ItemUI.h" // Sigmodr core widget includes #include // Sigmod includes #include #include #include #include // KDE includes #include #include #include // Qt includes #include #include #include #include using namespace Sigcore; using namespace Sigmod; using namespace Sigmodr::CoreWidgets; using namespace Sigmodr::Widgets; ItemUI::ItemUI(Item* item, QWidget* parent) : ObjectUI(parent), m_lastType(-1) { setObjects(item, new Item(*item)); } void ItemUI::initGui() { QFile file(":/gui/item.ui"); file.open(QFile::ReadOnly); QWidget *formWidget = QUiLoader().load(&file, this); file.close(); ui_name = formWidget->findChild("varName"); ui_sellable = formWidget->findChild("varSellable"); ui_type = formWidget->findChild("varType"); ui_price = formWidget->findChild("varPrice"); ui_sellPrice = formWidget->findChild("varSellPrice"); ui_weight = formWidget->findChild("varWeight"); ui_description = formWidget->findChild("varDescription"); ui_script = formWidget->findChild("varScript"); connect(ui_name, SIGNAL(textChanged(QString)), this, SLOT(nameChanged(QString))); connect(ui_sellable, SIGNAL(toggled(bool)), this, SLOT(sellableChanged(bool))); connect(ui_type, SIGNAL(currentIndexChanged(int)), this, SLOT(typeChanged(int))); connect(ui_price, SIGNAL(valueChanged(int)), this, SLOT(priceChanged(int))); connect(ui_sellPrice, SIGNAL(valueChanged(int)), this, SLOT(sellPriceChanged(int))); connect(ui_weight, SIGNAL(valueChanged(int)), this, SLOT(weightChanged(int))); connect(ui_description, SIGNAL(textChanged(QString)), this, SLOT(descriptionChanged(QString))); connect(ui_script, SIGNAL(valueChanged(Sigcore::Script)), this, SLOT(scriptChanged(Sigcore::Script))); QVBoxLayout* layout = new QVBoxLayout; layout->addWidget(formWidget); setLayout(layout); } void ItemUI::refreshGui() { const bool blocked = ui_type->blockSignals(true); ui_type->clear(); for (int i = 0; i < game()->itemTypeCount(); ++i) ui_type->addItem(game()->itemType(i)->name()); ui_type->blockSignals(blocked); ui_price->setMaximum(game()->rules()->maxMoney()); } void ItemUI::setGui() { const bool resetWeight = (qobject_cast(modified())->type() != m_lastType); ui_name->setText(qobject_cast(modified())->name()); ui_sellable->setCheckState(qobject_cast(modified())->sellable() ? Qt::Checked : Qt::Unchecked); ui_type->setCurrentIndex(game()->itemTypeIndex(qobject_cast(modified())->type())); m_lastType = qobject_cast(modified())->type(); ui_price->setValue(qobject_cast(modified())->price()); ui_sellPrice->setValue(qobject_cast(modified())->sellPrice()); ui_sellPrice->setEnabled(qobject_cast(modified())->sellable()); if (resetWeight) { const ItemType* itemType = game()->itemTypeById(qobject_cast(modified())->type()); if (itemType) ui_weight->setMaximum(itemType->maxWeight()); } ui_weight->setValue(qobject_cast(modified())->weight()); ui_description->setText(qobject_cast(modified())->description()); ui_script->setValue(qobject_cast(modified())->script()); } void ItemUI::apply() { *qobject_cast(original()) = *qobject_cast(modified()); emit(changed(false)); } void ItemUI::discard() { *qobject_cast(modified()) = *qobject_cast(original()); setGui(); emit(changed(false)); } void ItemUI::nameChanged(const QString& name) { const int cursor = ui_name->cursorPosition(); qobject_cast(modified())->setName(name); ui_name->setCursorPosition(cursor); } void ItemUI::sellableChanged(const bool sellable) { qobject_cast(modified())->setSellable(sellable); } void ItemUI::typeChanged(const int type) { if (0 <= type) qobject_cast(modified())->setType(game()->itemType(type)->id()); } void ItemUI::priceChanged(const int price) { qobject_cast(modified())->setPrice(price); } void ItemUI::sellPriceChanged(const int sellPrice) { qobject_cast(modified())->setSellPrice(sellPrice); } void ItemUI::weightChanged(const int weight) { qobject_cast(modified())->setWeight(weight); } void ItemUI::descriptionChanged(const QString& description) { const int cursor = ui_description->cursorPosition(); qobject_cast(modified())->setDescription(description); ui_description->setCursorPosition(cursor); } void ItemUI::scriptChanged(const Script& script) { qobject_cast(modified())->setScript(script); }