/* * 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 "CoinListItemUI.h" // Sigmod includes #include #include #include #include // KDE includes #include #include // Qt includes #include #include #include #include #include using namespace Sigmod; using namespace Sigmodr::Widgets; CoinListItemUI::CoinListItemUI(CoinListItem* item, QWidget* parent) : ObjectUI(parent), ui_type(new QButtonGroup(this)), m_lastType(-1) { setObjects(item, new CoinListItem(*item)); } void CoinListItemUI::initGui() { QFile file(":/gui/coinlistitem.ui"); file.open(QFile::ReadOnly); QWidget *formWidget = QUiLoader().load(&file, this); file.close(); QRadioButton* item = formWidget->findChild("varItem"); QRadioButton* species = formWidget->findChild("varSpecies"); ui_object = formWidget->findChild("varObject"); ui_cost = formWidget->findChild("varCost"); ui_type->addButton(item, CoinListItem::Item); ui_type->addButton(species, CoinListItem::Species); connect(ui_type, SIGNAL(buttonClicked(int)), this, SLOT(typeChanged(int))); connect(ui_object, SIGNAL(currentIndexChanged(int)), this, SLOT(objectChanged(int))); connect(ui_cost, SIGNAL(valueChanged(int)), this, SLOT(costChanged(int))); QVBoxLayout* layout = new QVBoxLayout; layout->addWidget(formWidget); setLayout(layout); } void CoinListItemUI::setGui() { bool resetObjects = (qobject_cast(modified())->type() != m_lastType); ui_type->button(qobject_cast(modified())->type())->setChecked(true); m_lastType = qobject_cast(modified())->type(); if (resetObjects) { const bool blocked = ui_object->blockSignals(true); ui_object->clear(); if (qobject_cast(modified())->type() == CoinListItem::Item) { for (int i = 0; i < game()->itemCount(); ++i) ui_object->addItem(game()->item(i)->name()); } else { for (int i = 0; i < game()->speciesCount(); ++i) ui_object->addItem(game()->species(i)->name()); } ui_object->blockSignals(blocked); } if (m_lastType == CoinListItem::Item) ui_object->setCurrentIndex(game()->itemIndex(qobject_cast(modified())->object())); else ui_object->setCurrentIndex(game()->speciesIndex(qobject_cast(modified())->object())); ui_cost->setValue(qobject_cast(modified())->cost()); } void CoinListItemUI::apply() { *qobject_cast(original()) = *qobject_cast(modified()); emit(changed(false)); } void CoinListItemUI::discard() { *qobject_cast(modified()) = *qobject_cast(original()); setGui(); emit(changed(false)); } void CoinListItemUI::typeChanged(const int type) { qobject_cast(modified())->setType(static_cast(type)); } void CoinListItemUI::objectChanged(const int object) { if (0 <= object) { if (qobject_cast(modified())->type() == CoinListItem::Item) qobject_cast(modified())->setObject(game()->item(object)->id()); else qobject_cast(modified())->setObject(game()->species(object)->id()); } } void CoinListItemUI::costChanged(const int cost) { qobject_cast(modified())->setCost(cost); }