diff options
| author | Ben Boeckel <MathStuf@gmail.com> | 2009-03-01 17:57:05 -0500 |
|---|---|---|
| committer | Ben Boeckel <MathStuf@gmail.com> | 2009-03-01 17:57:05 -0500 |
| commit | 4ed55e72070115125732f5125d3da8efd09ffb2b (patch) | |
| tree | 2304593e75b742a7b3d5f8712a6d401fc95cddaa /sigmodr/widgets/ItemUI.cpp | |
| parent | 70f1be0257f685a2d2fce417ed8dd41ca315168f (diff) | |
Fixed more widgets to use pimpl
Diffstat (limited to 'sigmodr/widgets/ItemUI.cpp')
| -rw-r--r-- | sigmodr/widgets/ItemUI.cpp | 156 |
1 files changed, 79 insertions, 77 deletions
diff --git a/sigmodr/widgets/ItemUI.cpp b/sigmodr/widgets/ItemUI.cpp index 4dda8c51..26234204 100644 --- a/sigmodr/widgets/ItemUI.cpp +++ b/sigmodr/widgets/ItemUI.cpp @@ -17,6 +17,7 @@ // Header include #include "ItemUI.h" +#include "ItemUI_p.h" // Sigmodr core widget includes #include <sigmodr/corewidgets/ScriptWidget.h> @@ -33,10 +34,7 @@ #include <KLineEdit> // Qt includes -#include <QtCore/QFile> #include <QtGui/QCheckBox> -#include <QtGui/QVBoxLayout> -#include <QtUiTools/QUiLoader> using namespace Sigcore; using namespace Sigmod; @@ -44,26 +42,47 @@ 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<KLineEdit*>("varName"); - ui_sellable = formWidget->findChild<QCheckBox*>("varSellable"); - ui_type = formWidget->findChild<KComboBox*>("varType"); - ui_price = formWidget->findChild<KIntNumInput*>("varPrice"); - ui_sellPrice = formWidget->findChild<KIntNumInput*>("varSellPrice"); - ui_weight = formWidget->findChild<KIntNumInput*>("varWeight"); - ui_description = formWidget->findChild<KLineEdit*>("varDescription"); - ui_script = formWidget->findChild<ScriptWidget*>("varScript"); + ObjectUI(item, parent), + d(new Private(new Item(*item))) +{ + setWidget(d->makeWidgets(this)); +} + +void ItemUI::apply() +{ + *qobject_cast<Item*>(m_object) = *d->m_item; + ObjectUI::apply(); +} + +void ItemUI::discard() +{ + *d->m_item = *qobject_cast<Item*>(m_object); + d->resetGui(); + ObjectUI::discard(); +} + +ItemUI::Private::Private(Item* item) : + ObjectUIPrivate(item), + m_item(item) +{ +} + +ItemUI::Private::~Private() +{ + delete m_item; +} + +QWidget* ItemUI::Private::makeWidgets(ObjectUI* widget) +{ + QWidget *form = openUiFile(":/gui/item.ui", widget); + ui_name = form->findChild<KLineEdit*>("varName"); + ui_sellable = form->findChild<QCheckBox*>("varSellable"); + ui_type = form->findChild<KComboBox*>("varType"); + ui_price = form->findChild<KIntNumInput*>("varPrice"); + ui_sellPrice = form->findChild<KIntNumInput*>("varSellPrice"); + ui_weight = form->findChild<KIntNumInput*>("varWeight"); + ui_description = form->findChild<KLineEdit*>("varDescription"); + ui_script = form->findChild<ScriptWidget*>("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))); @@ -72,96 +91,79 @@ void ItemUI::initGui() 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); + return form; } -void ItemUI::refreshGui() +void ItemUI::Private::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()); + for (int i = 0; i < m_item->game()->itemTypeCount(); ++i) + ui_type->addItem(m_item->game()->itemType(i)->name()); ui_type->blockSignals(blocked); - ui_price->setMaximum(game()->rules()->maxMoney()); + ui_price->setMaximum(m_item->game()->rules()->maxMoney()); } -void ItemUI::setGui() +void ItemUI::Private::resetGui() { - const bool resetWeight = (qobject_cast<Item*>(modified())->type() != m_lastType); - ui_name->setText(qobject_cast<Item*>(modified())->name()); - ui_sellable->setCheckState(qobject_cast<Item*>(modified())->sellable() ? Qt::Checked : Qt::Unchecked); - ui_type->setCurrentIndex(game()->itemTypeIndex(qobject_cast<Item*>(modified())->type())); - m_lastType = qobject_cast<Item*>(modified())->type(); - ui_price->setValue(qobject_cast<Item*>(modified())->price()); - ui_sellPrice->setValue(qobject_cast<Item*>(modified())->sellPrice()); - ui_sellPrice->setEnabled(qobject_cast<Item*>(modified())->sellable()); - if (resetWeight) - { - const ItemType* itemType = game()->itemTypeById(qobject_cast<Item*>(modified())->type()); - if (itemType) - ui_weight->setMaximum(itemType->maxWeight()); - } - ui_weight->setValue(qobject_cast<Item*>(modified())->weight()); - ui_description->setText(qobject_cast<Item*>(modified())->description()); - ui_script->setValue(qobject_cast<Item*>(modified())->script()); + ui_name->setText(m_item->name()); + ui_sellable->setCheckState(m_item->sellable() ? Qt::Checked : Qt::Unchecked); + ui_type->setCurrentIndex(m_item->game()->itemTypeIndex(m_item->type())); + ui_price->setValue(m_item->price()); + ui_sellPrice->setValue(m_item->sellPrice()); + ui_sellPrice->setEnabled(m_item->sellable()); + ui_weight->setValue(m_item->weight()); + ui_description->setText(m_item->description()); + ui_script->setValue(m_item->script()); } -void ItemUI::apply() -{ - *qobject_cast<Item*>(original()) = *qobject_cast<Item*>(modified()); - emit(changed(false)); -} - -void ItemUI::discard() -{ - *qobject_cast<Item*>(modified()) = *qobject_cast<Item*>(original()); - setGui(); - emit(changed(false)); -} - -void ItemUI::nameChanged(const QString& name) +void ItemUI::Private::nameChanged(const QString& name) { const int cursor = ui_name->cursorPosition(); - qobject_cast<Item*>(modified())->setName(name); + m_item->setName(name); ui_name->setCursorPosition(cursor); } -void ItemUI::sellableChanged(const bool sellable) +void ItemUI::Private::sellableChanged(const bool sellable) { - qobject_cast<Item*>(modified())->setSellable(sellable); + m_item->setSellable(sellable); } -void ItemUI::typeChanged(const int type) +void ItemUI::Private::typeChanged(const int type) { if (0 <= type) - qobject_cast<Item*>(modified())->setType(game()->itemType(type)->id()); + { + const ItemType* itemType = m_item->game()->itemTypeById(m_item->type()); + m_item->setType(itemType->id()); + ui_weight->setMaximum(itemType->maxWeight()); + } + else + ui_weight->setMaximum(INT_MAX); } -void ItemUI::priceChanged(const int price) +void ItemUI::Private::priceChanged(const int price) { - qobject_cast<Item*>(modified())->setPrice(price); + m_item->setPrice(price); } -void ItemUI::sellPriceChanged(const int sellPrice) +void ItemUI::Private::sellPriceChanged(const int sellPrice) { - qobject_cast<Item*>(modified())->setSellPrice(sellPrice); + m_item->setSellPrice(sellPrice); } -void ItemUI::weightChanged(const int weight) +void ItemUI::Private::weightChanged(const int weight) { - qobject_cast<Item*>(modified())->setWeight(weight); + m_item->setWeight(weight); } -void ItemUI::descriptionChanged(const QString& description) +void ItemUI::Private::descriptionChanged(const QString& description) { const int cursor = ui_description->cursorPosition(); - qobject_cast<Item*>(modified())->setDescription(description); + m_item->setDescription(description); ui_description->setCursorPosition(cursor); } -void ItemUI::scriptChanged(const Script& script) +void ItemUI::Private::scriptChanged(const Script& script) { - qobject_cast<Item*>(modified())->setScript(script); + m_item->setScript(script); } |
