diff options
Diffstat (limited to 'sigmodr/widgets/GameUI.cpp')
| -rw-r--r-- | sigmodr/widgets/GameUI.cpp | 141 |
1 files changed, 73 insertions, 68 deletions
diff --git a/sigmodr/widgets/GameUI.cpp b/sigmodr/widgets/GameUI.cpp index 0cf5ce03..ab6153ee 100644 --- a/sigmodr/widgets/GameUI.cpp +++ b/sigmodr/widgets/GameUI.cpp @@ -17,6 +17,7 @@ // Header include #include "GameUI.h" +#include "GameUI_p.h" // Sigmodr widget includes #include "TypechartModel.h" @@ -35,12 +36,9 @@ #include <KLineEdit> // Qt includes -#include <QtCore/QFile> #include <QtGui/QCheckBox> #include <QtGui/QLabel> #include <QtGui/QTableView> -#include <QtGui/QVBoxLayout> -#include <QtUiTools/QUiLoader> using namespace Sigcore; using namespace Sigmod; @@ -48,116 +46,123 @@ using namespace Sigmodr::CoreWidgets; using namespace Sigmodr::Widgets; GameUI::GameUI(Game* game, QWidget* parent) : - ObjectUI(parent), - m_changingMult(true) + ObjectUI(game, parent), + d(new Private(new Game(*game))) { - setObjects(game, new Game(*game)); + setWidget(d->makeWidgets(this)); } -void GameUI::initGui() +void GameUI::apply() +{ + *qobject_cast<Game*>(m_object) = *d->m_game; + ObjectUI::apply(); +} + +void GameUI::discard() +{ + *d->m_game = *qobject_cast<Game*>(m_object); + d->resetGui(); + ObjectUI::discard(); +} + +GameUI::Private::Private(Game* game) : + ObjectUIPrivate(game), + m_game(game) +{ +} + +GameUI::Private::~Private() { - QFile file(":/gui/game.ui"); - file.open(QFile::ReadOnly); - QWidget *formWidget = QUiLoader().load(&file, this); - file.close(); - ui_title = formWidget->findChild<KLineEdit*>("varTitle"); - ui_version = formWidget->findChild<KLineEdit*>("varVersion"); - ui_description = formWidget->findChild<KLineEdit*>("varDescription"); - ui_singlePlayer = formWidget->findChild<QCheckBox*>("varSinglePlayer"); - ui_startScript = formWidget->findChild<ScriptWidget*>("varStartScript"); - ui_typechart = formWidget->findChild<QTableView*>("varTypechart"); - ui_labelTypes = formWidget->findChild<QLabel*>("labelTypes"); - ui_effectiveness = formWidget->findChild<FractionWidget*>("varEffectiveness"); + delete m_game; +} + +QWidget* GameUI::Private::makeWidgets(ObjectUI* widget) +{ + QWidget *form = openUiFile(":/gui/game.ui", widget); + ui_title = form->findChild<KLineEdit*>("varTitle"); + ui_version = form->findChild<KLineEdit*>("varVersion"); + ui_description = form->findChild<KLineEdit*>("varDescription"); + ui_singlePlayer = form->findChild<QCheckBox*>("varSinglePlayer"); + ui_startScript = form->findChild<ScriptWidget*>("varStartScript"); + ui_typechart = form->findChild<QTableView*>("varTypechart"); + ui_labelTypes = form->findChild<QLabel*>("labelTypes"); + ui_effectiveness = form->findChild<FractionWidget*>("varEffectiveness"); connect(ui_title, SIGNAL(textChanged(QString)), this, SLOT(titleChanged(QString))); connect(ui_version, SIGNAL(textChanged(QString)), this, SLOT(versionChanged(QString))); connect(ui_description, SIGNAL(textChanged(QString)), this, SLOT(descriptionChanged(QString))); connect(ui_singlePlayer, SIGNAL(toggled(bool)), this, SLOT(singlePlayerChanged(bool))); connect(ui_startScript, SIGNAL(valueChanged(Sigcore::Script)), this, SLOT(startScriptChanged(Sigcore::Script))); - connect(ui_typechart, SIGNAL(clicked(QModelIndex)), this, SLOT(typechartChanged(QModelIndex))); + connect(ui_typechart, SIGNAL(currentChanged(QModelIndex, QModelIndew)), this, SLOT(typechartChanged(QModelIndex))); connect(ui_effectiveness, SIGNAL(valueChanged(Sigcore::Fraction)), this, SLOT(effectivenessChanged(Sigcore::Fraction))); - QVBoxLayout* layout = new QVBoxLayout; - layout->addWidget(formWidget); - setLayout(layout); + return form; } -void GameUI::refreshGui() +void GameUI::Private::refreshGui() { delete ui_typechart->model(); QStringList types; - for (int i = 0; i < qobject_cast<Game*>(original())->typeCount(); ++i) - types << qobject_cast<Game*>(original())->type(i)->name(); - ui_typechart->setModel(new TypechartModel(qobject_cast<Game*>(modified())->typechart(), types)); + for (int i = 0; i < m_game->typeCount(); ++i) + types << m_game->type(i)->name(); + delete ui_typechart->model(); + ui_typechart->setModel(new TypechartModel(m_game->typechart(), types)); ui_effectiveness->setEnabled(false); } -void GameUI::setGui() -{ - ui_title->setText(qobject_cast<Game*>(modified())->title()); - ui_version->setText(qobject_cast<Game*>(modified())->version()); - ui_description->setText(qobject_cast<Game*>(modified())->description()); - ui_singlePlayer->setCheckState(qobject_cast<Game*>(modified())->singlePlayer() ? Qt::Checked : Qt::Unchecked); - ui_startScript->setValue(qobject_cast<Game*>(modified())->startScript()); -} - -void GameUI::apply() -{ - *qobject_cast<Game*>(original()) = *qobject_cast<Game*>(modified()); - emit(changed(false)); -} - -void GameUI::discard() +void GameUI::Private::resetGui() { - *qobject_cast<Game*>(modified()) = *qobject_cast<Game*>(original()); - setGui(); - qobject_cast<TypechartModel*>(ui_typechart->model())->discarded(); - emit(changed(false)); + ui_title->setText(m_game->title()); + ui_version->setText(m_game->version()); + ui_description->setText(m_game->description()); + ui_singlePlayer->setCheckState(m_game->singlePlayer() ? Qt::Checked : Qt::Unchecked); + ui_startScript->setValue(m_game->startScript()); } -void GameUI::titleChanged(const QString& title) +void GameUI::Private::titleChanged(const QString& title) { const int cursor = ui_title->cursorPosition(); - qobject_cast<Game*>(modified())->setTitle(title); + m_game->setTitle(title); ui_title->setCursorPosition(cursor); } -void GameUI::versionChanged(const QString& version) +void GameUI::Private::versionChanged(const QString& version) { const int cursor = ui_version->cursorPosition(); - qobject_cast<Game*>(modified())->setVersion(version); + m_game->setVersion(version); ui_version->setCursorPosition(cursor); } -void GameUI::descriptionChanged(const QString& description) +void GameUI::Private::descriptionChanged(const QString& description) { const int cursor = ui_description->cursorPosition(); - qobject_cast<Game*>(modified())->setDescription(description); + m_game->setDescription(description); ui_description->setCursorPosition(cursor); } -void GameUI::singlePlayerChanged(const bool singlePlayer) +void GameUI::Private::singlePlayerChanged(const bool singlePlayer) { - qobject_cast<Game*>(modified())->setSinglePlayer(singlePlayer); + m_game->setSinglePlayer(singlePlayer); } -void GameUI::startScriptChanged(const Script& startScript) +void GameUI::Private::startScriptChanged(const Script& startScript) { - qobject_cast<Game*>(modified())->setStartScript(startScript); + m_game->setStartScript(startScript); } -void GameUI::typechartChanged(const QModelIndex& index) +void GameUI::Private::typechartChanged(const QModelIndex& index) { - m_index = index; - m_changingMult = true; + m_changing = true; ui_effectiveness->setEnabled(true); - ui_labelTypes->setText(QString("%1 vs. %2").arg(game()->type(index.row())->name()).arg(game()->type(index.column())->name())); - ui_effectiveness->setValue(ui_typechart->model()->data(m_index, Qt::EditRole).value<Fraction>()); + ui_labelTypes->setText(QString("%1 vs. %2").arg(m_game->type(index.row())->name()).arg(m_game->type(index.column())->name())); + ui_effectiveness->setValue(ui_typechart->model()->data(index, Qt::EditRole).value<Fraction>()); } -void GameUI::effectivenessChanged(const Fraction& multiplier) +void GameUI::Private::effectivenessChanged(const Fraction& multiplier) { - ui_typechart->model()->setData(m_index, QVariant::fromValue(multiplier), Qt::EditRole); - if (!m_changingMult) - emit(changed()); - m_changingMult = false; - setGui(); + if (m_changing) + { + m_changing = false; + return; + } + ui_typechart->model()->setData(ui_typechart->currentIndex(), QVariant::fromValue(multiplier), Qt::EditRole); + emit(changed()); } |
