summaryrefslogtreecommitdiffstats
path: root/sigmodr/widgets/ItemUI.cpp
blob: 200edd81bbd5e3b3f369c33ccaa8ca88fd2c8103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright 2008-2009 Ben Boeckel <MathStuf@gmail.com>
 * 
 * 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 <http://www.gnu.org/licenses/>.
 */

// Header include
#include "ItemUI.h"
#include "ItemUI_p.h"

// Sigmodr core widget includes
#include <sigmodr/corewidgets/ScriptWidget.h>

// Sigmod includes
#include <sigmod/Game.h>
#include <sigmod/Item.h>
#include <sigmod/ItemType.h>
#include <sigmod/Rules.h>

// KDE includes
#include <KComboBox>
#include <KIntNumInput>
#include <KLineEdit>

// Qt includes
#include <QtGui/QCheckBox>

// Standard includes
#include <climits>

using namespace Sigcore;
using namespace Sigmod;
using namespace Sigmodr::CoreWidgets;
using namespace Sigmodr::Widgets;

ItemUI::ItemUI(Item* item, QWidget* parent) :
        ObjectUI(item, parent),
        d(new Private(new Item(*item)))
{
    setPrivate(d);
}

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)));
    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)));
    return form;
}

void ItemUI::Private::refreshGui()
{
    const bool blocked = ui_type->blockSignals(true);
    ui_type->clear();
    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(m_item->game()->rules()->maxMoney());
    ObjectUIPrivate::refreshGui();
}

void ItemUI::Private::resetGui()
{
    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::Private::nameChanged(const QString& name)
{
    const int cursor = ui_name->cursorPosition();
    m_item->setName(name);
    ui_name->setCursorPosition(cursor);
}

void ItemUI::Private::sellableChanged(const bool sellable)
{
    m_item->setSellable(sellable);
    ui_sellPrice->setEnabled(sellable);
}

void ItemUI::Private::typeChanged(const int type)
{
    if (0 <= type)
    {
        const ItemType* itemType = m_item->game()->itemType(type);
        m_item->setType(itemType->id());
        ui_weight->setMaximum(itemType->maxWeight());
    }
    else
        ui_weight->setMaximum(INT_MAX);
}

void ItemUI::Private::priceChanged(const int price)
{
    m_item->setPrice(price);
}

void ItemUI::Private::sellPriceChanged(const int sellPrice)
{
    m_item->setSellPrice(sellPrice);
}

void ItemUI::Private::weightChanged(const int weight)
{
    m_item->setWeight(weight);
}

void ItemUI::Private::descriptionChanged(const QString& description)
{
    const int cursor = ui_description->cursorPosition();
    m_item->setDescription(description);
    ui_description->setCursorPosition(cursor);
}

void ItemUI::Private::scriptChanged(const Script& script)
{
    m_item->setScript(script);
}