summaryrefslogtreecommitdiffstats
path: root/pokemodr/PokeModrUI.cpp
diff options
context:
space:
mode:
authorBen Boeckel <MathStuf@gmail.com>2008-05-21 19:59:39 +0000
committerBen Boeckel <MathStuf@gmail.com>2008-05-21 19:59:39 +0000
commitc2d90a8019125dc260db1882ebc292e4e52f3969 (patch)
tree1ec02bf2731e7dde2a0405d1716c2e179b78cbce /pokemodr/PokeModrUI.cpp
parentfa85318e91ea9bbe4638092767b3b8e0ea64f57c (diff)
downloadsigen-c2d90a8019125dc260db1882ebc292e4e52f3969.tar.gz
sigen-c2d90a8019125dc260db1882ebc292e4e52f3969.tar.xz
sigen-c2d90a8019125dc260db1882ebc292e4e52f3969.zip
[FIX] Closing PokeMods works now
git-svn-id: https://pokegen.svn.sourceforge.net/svnroot/pokegen/trunk@162 6ecfd1a5-f3ed-3746-8530-beee90d26b22
Diffstat (limited to 'pokemodr/PokeModrUI.cpp')
-rw-r--r--pokemodr/PokeModrUI.cpp149
1 files changed, 84 insertions, 65 deletions
diff --git a/pokemodr/PokeModrUI.cpp b/pokemodr/PokeModrUI.cpp
index bfe9c1a9..a673d2e8 100644
--- a/pokemodr/PokeModrUI.cpp
+++ b/pokemodr/PokeModrUI.cpp
@@ -59,6 +59,7 @@ PokeModrUI::PokeModrUI(KConfigGroup config, KConfigGroup history, QWidget* paren
menuFile->addAction(KStandardAction::openNew(this, SLOT(newPokemod()), menuFile));
menuFile->addAction(KStandardAction::open(this, SLOT(openPokemod()), menuFile));
menuFile->addAction(KStandardAction::openRecent(this, SLOT(openPokemod(const KUrl&)), menuFile));
+ menuFile->addAction(KStandardAction::close(this, SLOT(closePokemod()), menuFile));
menuFile->addSeparator();
menuFile->addAction(KStandardAction::save(this, SLOT(savePokemod()), menuFile));
menuFile->addAction(KStandardAction::saveAs(this, SLOT(saveAsPokemod()), menuFile));
@@ -91,6 +92,7 @@ PokeModrUI::PokeModrUI(KConfigGroup config, KConfigGroup history, QWidget* paren
toolbar->addAction(KStandardAction::openNew(this, SLOT(newPokemod()), toolbar));
toolbar->addAction(KStandardAction::open(this, SLOT(openPokemod()), toolbar));
toolbar->addAction(KStandardAction::openRecent(this, SLOT(openPokemod(const KUrl&)), toolbar));
+ toolbar->addAction(KStandardAction::close(this, SLOT(closePokemod()), toolbar));
toolbar->addSeparator();
toolbar->addAction(KStandardAction::save(this, SLOT(savePokemod()), toolbar));
toolbar->addAction(KStandardAction::saveAs(this, SLOT(saveAsPokemod()), toolbar));
@@ -114,7 +116,6 @@ PokeModrUI::PokeModrUI(KConfigGroup config, KConfigGroup history, QWidget* paren
toolbar->addAction(KStandardAction::quit(this, SLOT(quit()), toolbar));
addToolBar(toolbar);
splitter->setSizes(QList<int>() << config.readEntry("treeWidth", 100) << config.readEntry("panelWidth", 100));
- treePokemod->setModel(new PokemodTreeModel(treePokemod));
connect(buttonApply, SIGNAL(clicked()), this, SLOT(update()));
KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
if (args)
@@ -153,6 +154,12 @@ void PokeModrUI::setChangedTitle(const bool changed)
setWindowTitle(QString::fromUtf8("%1%2 - PokéModr").arg(treePokemod->description(treePokemod->currentIndex()), changed ? "*" : ""));
}
+void PokeModrUI::setDirty(const bool dirty)
+{
+ const Pokemod* pokemod = static_cast<const Pokemod*>(static_cast<Object*>(treePokemod->currentIndex().internalPointer())->pokemod());
+ treePokemod->setDirty(pokemod, dirty);
+}
+
void PokeModrUI::newPokemod()
{
treePokemod->addPokemod(new Pokemod());
@@ -218,96 +225,94 @@ bool PokeModrUI::openPokemod(const QString& path)
void PokeModrUI::savePokemod()
{
- // TODO: safer way of doing this?
- const Pokemod* pokemod = static_cast<const Pokemod*>(static_cast<Object*>(treePokemod->currentIndex().internalPointer())->pokemod());
- KUrl url = treePokemod->url(pokemod);
- if (url.isEmpty())
- saveAsPokemod();
- else
- savePokemod(url);
+ const Pokemod* pokemod = currentPokemod();
+ if (pokemod)
+ {
+ KUrl url = treePokemod->url(pokemod);
+ if (url.isEmpty())
+ saveAsPokemod();
+ else
+ savePokemod(url);
+ }
}
void PokeModrUI::saveAsPokemod()
{
- // TODO: safer way of doing this?
- Pokemod* pokemod = const_cast<Pokemod*>(static_cast<const Pokemod*>(static_cast<Object*>(treePokemod->currentIndex().internalPointer())->pokemod()));
- KUrl url = KFileDialog::getSaveUrl(KUrl("kfiledialog:///pokemod"), QString::fromUtf8("*.pmod|PokéMod files"), this, QString::fromUtf8("Open PokéMod"));
- if (!url.isEmpty())
+ const Pokemod* pokemod = currentPokemod();
+ if (pokemod)
{
- if (savePokemod(url))
- treePokemod->setUrl(pokemod, url);
+ KUrl url = KFileDialog::getSaveUrl(KUrl("kfiledialog:///pokemod"), QString::fromUtf8("*.pmod|PokéMod files"), this, QString::fromUtf8("Open PokéMod"));
+ if (!url.isEmpty())
+ {
+ if (savePokemod(url))
+ treePokemod->setUrl(pokemod, url);
+ }
}
}
bool PokeModrUI::savePokemod(const KUrl& url)
{
- const Pokemod* pokemod = static_cast<const Pokemod*>(static_cast<Object*>(treePokemod->currentIndex().internalPointer())->pokemod());
- if (url.isLocalFile())
+ const Pokemod* pokemod = currentPokemod();
+ if (pokemod)
{
- QFile file(url.path());
- if (file.open(QIODevice::ReadWrite))
+ if (url.isLocalFile())
{
- file.write(Object::xml(pokemod).toByteArray());
- file.close();
- return true;
+ QFile file(url.path());
+ if (file.open(QIODevice::ReadWrite))
+ {
+ file.write(Object::xml(pokemod).toByteArray());
+ file.close();
+ return true;
+ }
+ else
+ KMessageBox::error(this, QString::fromUtf8("Error saving the PokéMod!"), "Save error");
}
else
- KMessageBox::error(this, QString::fromUtf8("Error saving the PokéMod!"), "Save error");
- }
- else
- {
- KTemporaryFile temp;
- if (temp.open())
{
- temp.write(Object::xml(pokemod).toByteArray());
- if (KIO::NetAccess::upload(temp.fileName(), url, this))
- return true;
+ KTemporaryFile temp;
+ if (temp.open())
+ {
+ temp.write(Object::xml(pokemod).toByteArray());
+ if (KIO::NetAccess::upload(temp.fileName(), url, this))
+ return true;
+ else
+ KMessageBox::error(this, KIO::NetAccess::lastErrorString(), "KIO Error");
+ }
else
- KMessageBox::error(this, KIO::NetAccess::lastErrorString(), "KIO Error");
+ KMessageBox::error(this, QString::fromUtf8("Error saving the PokéMod!"), "Save error");
}
- else
- KMessageBox::error(this, QString::fromUtf8("Error saving the PokéMod!"), "Save error");
}
return false;
}
-void PokeModrUI::closePokeMod()
+void PokeModrUI::closePokemod()
+{
+ closePokemod(currentPokemod());
+}
+
+void PokeModrUI::closePokemod(const Pokemod* pokemod)
{
- // TODO: Close the Pokemod with the current editor open
-// if (formPanel->widget())
-// {
-// ObjectUI* o = static_cast<ObjectUI*>(formPanel->widget());
-// const Pokemod* p = o->original()->pokemod();
-// if (!o->close())
-// return;
-// switch (KMessageBox::questionYesNoCancel(this, "You have unsaved changes, would you like to save them?", "Unsaved PokéMod"))
-// {
-// case KMessageBox::Yes:
-// on_actionSave_triggered();
-// case KMessageBox::No:
-// break;
-// case KMessageBox::Cancel:
-// return;
-// }
-// for (QMutableListIterator<Pokemod*> i(m_pokemods); i.hasNext(); i.next())
-// {
-// if (i.value() == p)
-// {
-// i.remove();
-// break;
-// }
-// }
-// delete p;
-// }
+ if (pokemod)
+ {
+ if (treePokemod->dirty(pokemod))
+ {
+ switch (KMessageBox::questionYesNoCancel(this, "You have unsaved changes, would you like to save them?", QString::fromUtf8("Unsaved PokéMod")))
+ {
+ case KMessageBox::Yes:
+ savePokemod();
+ case KMessageBox::No:
+ break;
+ case KMessageBox::Cancel:
+ return;
+ }
+ }
+ treePokemod->deletePokemod(currentPokemod());
+ }
}
void PokeModrUI::quit()
{
- // TODO: clean up the tree
-// while (m_pokemods.size())
-// closePokeMod();
-// if (m_clipboard)
-// delete m_clipboard;
+ // TODO: close all pokemods and cancel if any are left open
close();
}
@@ -360,6 +365,8 @@ void PokeModrUI::on_treePokemod_clicked(const QModelIndex& index)
connect(widget, SIGNAL(changed(bool)), this, SLOT(setChangedTitle(bool)));
connect(widget, SIGNAL(changed(bool)), boxButtons, SLOT(setEnabled(bool)));
connect(buttonApply, SIGNAL(clicked()), widget, SLOT(apply()));
+ // TODO: set as dirty
+// connect(buttonApply, SIGNAL(clicked(bool)), this, SLOT(setDirty()));
connect(buttonDiscard, SIGNAL(clicked()), widget, SLOT(discard()));
setChangedTitle(false);
formPanel->setWidget(widget);
@@ -373,3 +380,15 @@ void PokeModrUI::on_treePokemod_customContextMenuRequested(const QPoint& positio
if (menu)
menu->popup(mapToGlobal(position));
}
+
+const Pokemod* PokeModrUI::currentPokemod() const
+{
+ QModelIndex index = treePokemod->currentIndex();
+ if (index.isValid())
+ {
+ const Object* object = static_cast<BaseModel*>(index.internalPointer())->object();
+ if (object)
+ return static_cast<const Pokemod*>(object->pokemod());
+ }
+ return NULL;
+}