/* * Copyright 2008 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 "ObjectUI.h" // Pokemod includes #include "../pokemod/Object.h" #include "../pokemod/Pokemod.h" // Qt includes #include #include #include // KDE includes #include #include #include #include #include #include // #include Pokemodr::ObjectUI::ObjectUI(QWidget* parent) : QWidget(parent), m_changed(false), m_object(NULL), m_object_mod(NULL), m_valTree(NULL) { setContextMenuPolicy(Qt::CustomContextMenu); connect(this, SIGNAL(changed(bool)), SLOT(setChanged(bool))); connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(contextMenu(const QPoint&))); } Pokemodr::ObjectUI::~ObjectUI() { delete m_object_mod; } void Pokemodr::ObjectUI::reload() { refreshGui(); setGui(); } bool Pokemodr::ObjectUI::isChanged() const { return m_changed; } const Pokemod::Pokemod* Pokemodr::ObjectUI::pokemod() const { return qobject_cast(m_object->pokemod()); } const Pokemod::Pokemod::Object* Pokemodr::ObjectUI::original() const { return m_object; } Pokemod::Object* Pokemodr::ObjectUI::original() { return m_object; } const Pokemod::Object* Pokemodr::ObjectUI::modified() const { return m_object_mod; } Pokemod::Object* Pokemodr::ObjectUI::modified() { return m_object_mod; } void Pokemodr::ObjectUI::setChanged(const bool changed) { m_changed = changed; } void Pokemodr::ObjectUI::errorMessage(const QString& message) { KMessageBox::error(this, message, "Error"); } void Pokemodr::ObjectUI::warningMessage(const QString& message) { KMessageBox::warningContinueCancel(this, message, "Warning"); } void Pokemodr::ObjectUI::initGui() { } void Pokemodr::ObjectUI::refreshGui() { } void Pokemodr::ObjectUI::contextMenu(const QPoint& pos) { KMenu* menu = new KMenu; menu->addAction("&Apply", this, SLOT(apply())); menu->addAction("&Discard", this, SLOT(discard())); menu->addSeparator(); KAction* validate = new KAction("&Validate", this); connect(validate, SIGNAL(triggered()), this, SLOT(validate())); validate->setEnabled(!m_changed); menu->addAction(validate); menu->popup(mapToGlobal(pos)); } void Pokemodr::ObjectUI::validate() { KProgressDialog* progress = new KProgressDialog(this, "Validating", "Please wait"); progress->progressBar()->setRange(0, 0); progress->setAllowCancel(false); progress->setModal(true); progress->show(); if (!m_valTree) m_valTree = new QTreeWidget; m_valTree->setHeaderHidden(true); m_parents.clear(); m_warnings = 0; m_errors = 0; m_valTree->clear(); connect(m_object, SIGNAL(valMessage(const QString&)), this, SLOT(addMessage(const QString&))); connect(m_object, SIGNAL(valWarning(const QString&)), this, SLOT(addWarning(const QString&))); connect(m_object, SIGNAL(valError(const QString&)), this, SLOT(addError(const QString&))); m_object->validate(); delete progress; KDialog* dialog = new KDialog(this); dialog->setCaption("Validation Messages"); dialog->setButtons(KDialog::Ok); QWidget* widget = new QWidget(this); QVBoxLayout* layout = new QVBoxLayout(widget); layout->addWidget(new QLabel(QString("Warnings: %1\nErrors: %2").arg(m_warnings).arg(m_errors), widget)); layout->addWidget(m_valTree); dialog->setMainWidget(widget); dialog->exec(); delete dialog; m_valTree = NULL; disconnect(m_object, SIGNAL(valMessage(const QString&)), this, SLOT(addMessage(const QString&))); disconnect(m_object, SIGNAL(valWarning(const QString&)), this, SLOT(addWarning(const QString&))); disconnect(m_object, SIGNAL(valError(const QString&)), this, SLOT(addError(const QString&))); } void Pokemodr::ObjectUI::addMessage(const QString& msg) { QTreeWidgetItem* item; if (msg.startsWith("++")) { if (m_parents.size()) item = new QTreeWidgetItem(m_parents.top(), QStringList(msg.mid(2))); else item = new QTreeWidgetItem(m_valTree, QStringList(msg.mid(2))); m_parents.push(item); } else if (msg.startsWith("--")) m_parents.pop(); else { item = new QTreeWidgetItem(m_parents.top(), QStringList(msg)); item->setBackground(0, KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground).brush(QPalette::Normal)); } } void Pokemodr::ObjectUI::addWarning(const QString& msg) { ++m_warnings; QTreeWidgetItem* item; item = new QTreeWidgetItem(m_parents.top(), QStringList(msg)); item->setBackground(0, KStatefulBrush(KColorScheme::View, KColorScheme::NeutralBackground).brush(QPalette::Normal)); } void Pokemodr::ObjectUI::addError(const QString& msg) { ++m_errors; QTreeWidgetItem* item; item = new QTreeWidgetItem(m_parents.top(), QStringList(msg)); item->setBackground(0, KStatefulBrush(KColorScheme::View, KColorScheme::NegativeBackground).brush(QPalette::Normal)); } void Pokemodr::ObjectUI::setObjects(Pokemod::Object* original, Pokemod::Object* modified) { m_object = original; m_object_mod = modified; connect(m_object_mod, SIGNAL(changed()), this, SIGNAL(changed())); connect(m_object_mod, SIGNAL(changed()), this, SLOT(setGui())); connect(m_object_mod, SIGNAL(error(const QString&)), this, SLOT(setGui())); connect(m_object_mod, SIGNAL(error(const QString&)), this, SLOT(errorMessage(const QString&))); connect(m_object_mod, SIGNAL(warning(const QString&)), this, SLOT(warningMessage(const QString&))); init(); } void Pokemodr::ObjectUI::init() { initGui(); reload(); emit(changed(false)); }