diff options
Diffstat (limited to 'sigmodr/widgets/CoinListItemUI.cpp')
| -rw-r--r-- | sigmodr/widgets/CoinListItemUI.cpp | 127 |
1 files changed, 63 insertions, 64 deletions
diff --git a/sigmodr/widgets/CoinListItemUI.cpp b/sigmodr/widgets/CoinListItemUI.cpp index b3efe4de..c141a40b 100644 --- a/sigmodr/widgets/CoinListItemUI.cpp +++ b/sigmodr/widgets/CoinListItemUI.cpp @@ -17,6 +17,7 @@ // Header include #include "CoinListItemUI.h" +#include "CoinListItemUI_p.h" // Sigmod includes #include <sigmod/CoinListItem.h> @@ -29,101 +30,99 @@ #include <KIntNumInput> // Qt includes -#include <QtCore/QFile> #include <QtGui/QButtonGroup> #include <QtGui/QRadioButton> -#include <QtGui/QVBoxLayout> -#include <QtUiTools/QUiLoader> using namespace Sigmod; using namespace Sigmodr::Widgets; CoinListItemUI::CoinListItemUI(CoinListItem* item, QWidget* parent) : - ObjectUI(parent), - ui_type(new QButtonGroup(this)), - m_lastType(-1) + ObjectUI(item, parent), + d(new Private(new CoinListItem(*item))) { - setObjects(item, new CoinListItem(*item)); + setWidget(d->makeWidgets(this)); } -void CoinListItemUI::initGui() +void CoinListItemUI::apply() { - QFile file(":/gui/coinlistitem.ui"); - file.open(QFile::ReadOnly); - QWidget *formWidget = QUiLoader().load(&file, this); - file.close(); - QRadioButton* item = formWidget->findChild<QRadioButton*>("varItem"); - QRadioButton* species = formWidget->findChild<QRadioButton*>("varSpecies"); - ui_object = formWidget->findChild<KComboBox*>("varObject"); - ui_cost = formWidget->findChild<KIntNumInput*>("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); + *qobject_cast<CoinListItem*>(m_object) = *d->m_item; + ObjectUI::apply(); } -void CoinListItemUI::setGui() +void CoinListItemUI::discard() { - bool resetObjects = (qobject_cast<CoinListItem*>(modified())->type() != m_lastType); - ui_type->button(qobject_cast<CoinListItem*>(modified())->type())->setChecked(true); - m_lastType = qobject_cast<CoinListItem*>(modified())->type(); - if (resetObjects) - { - const bool blocked = ui_object->blockSignals(true); - ui_object->clear(); - if (qobject_cast<CoinListItem*>(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<CoinListItem*>(modified())->object())); - else - ui_object->setCurrentIndex(game()->speciesIndex(qobject_cast<CoinListItem*>(modified())->object())); - ui_cost->setValue(qobject_cast<CoinListItem*>(modified())->cost()); + *d->m_item = *qobject_cast<CoinListItem*>(m_object); + d->resetGui(); + ObjectUI::discard(); } -void CoinListItemUI::apply() +CoinListItemUI::Private::Private(CoinListItem* item) : + ObjectUIPrivate(item), + m_item(item) { - *qobject_cast<CoinListItem*>(original()) = *qobject_cast<CoinListItem*>(modified()); - emit(changed(false)); } -void CoinListItemUI::discard() +CoinListItemUI::Private::~Private() +{ + delete m_item; +} + +QWidget* CoinListItemUI::Private::makeWidgets(ObjectUI* widget) { - *qobject_cast<CoinListItem*>(modified()) = *qobject_cast<CoinListItem*>(original()); - setGui(); - emit(changed(false)); + QWidget *form = openUiFile(":/gui/coinlistitem.ui", widget); + QRadioButton* item = form->findChild<QRadioButton*>("varItem"); + QRadioButton* species = form->findChild<QRadioButton*>("varSpecies"); + ui_object = form->findChild<KComboBox*>("varObject"); + ui_cost = form->findChild<KIntNumInput*>("varCost"); + ui_type = new QButtonGroup(widget); + 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))); + return form; } -void CoinListItemUI::typeChanged(const int type) +void CoinListItemUI::Private::resetGui() { - qobject_cast<CoinListItem*>(modified())->setType(static_cast<CoinListItem::Type>(type)); + ui_type->button(m_item->type())->setChecked(true); + if (m_item->type() == CoinListItem::Item) + ui_object->setCurrentIndex(m_item->game()->itemIndex(m_item->object())); + else + ui_object->setCurrentIndex(m_item->game()->speciesIndex(m_item->object())); + ui_cost->setValue(m_item->cost()); +} + +void CoinListItemUI::Private::typeChanged(const int type) +{ + m_item->setType(static_cast<CoinListItem::Type>(type)); + const bool blocked = ui_object->blockSignals(true); + ui_object->clear(); + if (m_item->type() == CoinListItem::Item) + { + for (int i = 0; i < m_item->game()->itemCount(); ++i) + ui_object->addItem(m_item->game()->item(i)->name()); + } + else + { + for (int i = 0; i < m_item->game()->speciesCount(); ++i) + ui_object->addItem(m_item->game()->species(i)->name()); + } + ui_object->blockSignals(blocked); } -void CoinListItemUI::objectChanged(const int object) +void CoinListItemUI::Private::objectChanged(const int object) { if (0 <= object) { - if (qobject_cast<CoinListItem*>(modified())->type() == CoinListItem::Item) - qobject_cast<CoinListItem*>(modified())->setObject(game()->item(object)->id()); + if (m_item->type() == CoinListItem::Item) + m_item->setObject(m_item->game()->item(object)->id()); else - qobject_cast<CoinListItem*>(modified())->setObject(game()->species(object)->id()); + m_item->setObject(m_item->game()->species(object)->id()); } } -void CoinListItemUI::costChanged(const int cost) +void CoinListItemUI::Private::costChanged(const int cost) { - qobject_cast<CoinListItem*>(modified())->setCost(cost); + m_item->setCost(cost); } |
