/* * 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 "GameUI.h" // Sigmodr widget includes #include "TypechartModel.h" // Sigmodr core widget includes #include #include // Sigmod includes #include #include #include #include // KDE includes #include // Qt includes #include #include #include #include #include #include using namespace Sigcore; using namespace Sigmod; using namespace Sigmodr::CoreWidgets; using namespace Sigmodr::Widgets; GameUI::GameUI(Game* game, QWidget* parent) : ObjectUI(parent), m_changingMult(true) { setObjects(game, new Game(*game)); } void GameUI::initGui() { QFile file(":/gui/game.ui"); file.open(QFile::ReadOnly); QWidget *formWidget = QUiLoader().load(&file, this); file.close(); ui_title = formWidget->findChild("varTitle"); ui_version = formWidget->findChild("varVersion"); ui_description = formWidget->findChild("varDescription"); ui_singlePlayer = formWidget->findChild("varSinglePlayer"); ui_startScript = formWidget->findChild("varStartScript"); ui_typechart = formWidget->findChild("varTypechart"); ui_labelTypes = formWidget->findChild("labelTypes"); ui_effectiveness = formWidget->findChild("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_effectiveness, SIGNAL(valueChanged(Sigcore::Fraction)), this, SLOT(effectivenessChanged(Sigcore::Fraction))); QVBoxLayout* layout = new QVBoxLayout; layout->addWidget(formWidget); setLayout(layout); } void GameUI::refreshGui() { delete ui_typechart->model(); QStringList types; for (int i = 0; i < qobject_cast(original())->typeCount(); ++i) types << qobject_cast(original())->type(i)->name(); ui_typechart->setModel(new TypechartModel(qobject_cast(modified())->typechart(), types)); ui_effectiveness->setEnabled(false); } void GameUI::setGui() { ui_title->setText(qobject_cast(modified())->title()); ui_version->setText(qobject_cast(modified())->version()); ui_description->setText(qobject_cast(modified())->description()); ui_singlePlayer->setCheckState(qobject_cast(modified())->singlePlayer() ? Qt::Checked : Qt::Unchecked); ui_startScript->setValue(qobject_cast(modified())->startScript()); } void GameUI::apply() { *qobject_cast(original()) = *qobject_cast(modified()); emit(changed(false)); } void GameUI::discard() { *qobject_cast(modified()) = *qobject_cast(original()); setGui(); qobject_cast(ui_typechart->model())->discarded(); emit(changed(false)); } void GameUI::titleChanged(const QString& title) { const int cursor = ui_title->cursorPosition(); qobject_cast(modified())->setTitle(title); ui_title->setCursorPosition(cursor); } void GameUI::versionChanged(const QString& version) { const int cursor = ui_version->cursorPosition(); qobject_cast(modified())->setVersion(version); ui_version->setCursorPosition(cursor); } void GameUI::descriptionChanged(const QString& description) { const int cursor = ui_description->cursorPosition(); qobject_cast(modified())->setDescription(description); ui_description->setCursorPosition(cursor); } void GameUI::singlePlayerChanged(const bool singlePlayer) { qobject_cast(modified())->setSinglePlayer(singlePlayer); } void GameUI::startScriptChanged(const Script& startScript) { qobject_cast(modified())->setStartScript(startScript); } void GameUI::typechartChanged(const QModelIndex& index) { m_index = index; m_changingMult = 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()); } void GameUI::effectivenessChanged(const Fraction& multiplier) { ui_typechart->model()->setData(m_index, QVariant::fromValue(multiplier), Qt::EditRole); if (!m_changingMult) emit(changed()); m_changingMult = false; setGui(); }