summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBen Boeckel <MathStuf@gmail.com>2008-05-21 04:55:22 +0000
committerBen Boeckel <MathStuf@gmail.com>2008-05-21 04:55:22 +0000
commit66bb55e0feab92784209bdcf1b2ce5387a83100e (patch)
treea974b25d02581a31b5a4119668585f4ce6a15ba5
parent820bc54980b906042c0b5231147caea2059af7c9 (diff)
[FIX] Opening files and saving now works
git-svn-id: https://pokegen.svn.sourceforge.net/svnroot/pokegen/trunk@159 6ecfd1a5-f3ed-3746-8530-beee90d26b22
-rw-r--r--Changelog7
-rwxr-xr-xpokegen.pro1
-rw-r--r--pokemod/Object.h7
-rw-r--r--pokemodr/PokeModrUI.cpp77
-rw-r--r--pokemodr/PokeModrUI.h3
-rw-r--r--pokemodr/PokemodTree.cpp30
-rw-r--r--pokemodr/PokemodTree.h12
-rw-r--r--pokemodr/PokemodTreeModel.cpp22
-rw-r--r--pokemodr/PokemodTreeModel.h5
-rw-r--r--pokemodr/TODO2
-rw-r--r--pokemodr/models/BaseModel.cpp5
-rw-r--r--pokemodr/models/BaseModel.h2
-rw-r--r--pokemodr/models/RootModel.cpp30
-rw-r--r--pokemodr/models/RootModel.h6
14 files changed, 154 insertions, 55 deletions
diff --git a/Changelog b/Changelog
index 2d53480e..4429472d 100644
--- a/Changelog
+++ b/Changelog
@@ -1,4 +1,11 @@
-----------------
+Rev: 159
+Date: 20 May 2008
+User: MathStuf
+-----------------
+[FIX] Opening files and saving now works
+
+-----------------
Rev: 158
Date: 20 May 2008
User: MathStuf
diff --git a/pokegen.pro b/pokegen.pro
index a8c04a55..91628e10 100755
--- a/pokegen.pro
+++ b/pokegen.pro
@@ -15,6 +15,7 @@ pokemodr.depends = pokemod
distsrc.target = distsrc
distsrc.commands = ./make-tarball.sh $${VERSION}
+distsrc.depends = releases/pokegen-$${VERSION}.tar.bz2
QMAKE_EXTRA_UNIX_TARGETS += distsrc
diff --git a/pokemod/Object.h b/pokemod/Object.h
index d7f0a935..97ac325a 100644
--- a/pokemod/Object.h
+++ b/pokemod/Object.h
@@ -42,6 +42,13 @@ class Object : public QObject
int id() const;
QString className() const;
+
+ static QDomDocument xml(const Object* object)
+ {
+ QDomDocument xml(object->className());
+ xml.appendChild(object->save());
+ return xml;
+ }
signals:
void warning(const QString& message) const;
void error(const QString& message) const;
diff --git a/pokemodr/PokeModrUI.cpp b/pokemodr/PokeModrUI.cpp
index 3f103fa0..6db54597 100644
--- a/pokemodr/PokeModrUI.cpp
+++ b/pokemodr/PokeModrUI.cpp
@@ -41,6 +41,7 @@
#include <KRecentFilesAction>
#include <KStandardAction>
#include <KStandardDirs>
+#include <KTemporaryFile>
#include <KToolBar>
#include <KUrl>
#include <KIO/NetAccess>
@@ -113,6 +114,8 @@ 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)
{
@@ -126,8 +129,6 @@ PokeModrUI::PokeModrUI(KConfigGroup config, KConfigGroup history, QWidget* paren
openPokemod(m_recent->urls().at(i));
}
setAutoSaveSettings("MainWindow", true);
- treePokemod->setModel(new PokemodTreeModel(QStringList(), treePokemod));
- connect(buttonApply, SIGNAL(clicked()), this, SLOT(update()));
}
PokeModrUI::~PokeModrUI()
@@ -159,36 +160,37 @@ void PokeModrUI::newPokemod()
void PokeModrUI::openPokemod()
{
- QString path(KFileDialog::getOpenFileName(KUrl("kfiledialog:///pokemod"), QString::fromUtf8("*.pmod|PokéMod files"), this));
- if (!path.isEmpty())
+ KUrl::List urls(KFileDialog::getOpenUrls(KUrl("kfiledialog:///pokemod"), QString::fromUtf8("*.pmod|PokéMod files"), this));
+ foreach (KUrl url, urls)
{
- if (openPokemod(path))
+ if (!url.isEmpty())
{
- m_recent->addUrl(path);
- m_recent->saveEntries(m_config);
+ if (openPokemod(url))
+ {
+ m_recent->addUrl(url);
+ m_recent->saveEntries(m_config);
+ }
}
}
}
-void PokeModrUI::openPokemod(const KUrl& path)
+bool PokeModrUI::openPokemod(const KUrl& url)
{
- if (path.isLocalFile())
- openPokemod(path.path());
+ if (url.isLocalFile())
+ return openPokemod(url.path());
else
{
QString temp;
- if (KIO::NetAccess::download(path, temp, this))
+ if (KIO::NetAccess::download(url, temp, this))
{
if (openPokemod(temp))
- {
- m_recent->addUrl(path);
- m_recent->saveEntries(m_config);
- }
+ return true;
KIO::NetAccess::removeTempFile(temp);
}
else
KMessageBox::error(this, KIO::NetAccess::lastErrorString(), "KIO Error");
}
+ return false;
}
bool PokeModrUI::openPokemod(const QString& path)
@@ -216,16 +218,53 @@ bool PokeModrUI::openPokemod(const QString& path)
void PokeModrUI::savePokemod()
{
- Object* object = static_cast<Object*>(treePokemod->currentIndex().internalPointer());
- treePokemod->savePokemod(static_cast<const Pokemod*>(object->pokemod()));
+ // 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);
}
void PokeModrUI::saveAsPokemod()
{
- Object* object = static_cast<Object*>(treePokemod->currentIndex().internalPointer());
+ // 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())
- treePokemod->savePokemod(static_cast<const Pokemod*>(object->pokemod()), url);
+ {
+ 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())
+ {
+ QFile file(url.path());
+ if (file.open(QIODevice::ReadWrite))
+ {
+ file.write(Object::xml(pokemod).toByteArray());
+ file.close();
+ }
+ 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))
+ KMessageBox::error(this, KIO::NetAccess::lastErrorString(), "KIO Error");
+ }
+ else
+ KMessageBox::error(this, QString::fromUtf8("Error saving the PokéMod!"), "Save error");
+ }
}
void PokeModrUI::closePokeMod()
diff --git a/pokemodr/PokeModrUI.h b/pokemodr/PokeModrUI.h
index 2a715b21..41298479 100644
--- a/pokemodr/PokeModrUI.h
+++ b/pokemodr/PokeModrUI.h
@@ -50,10 +50,11 @@ class PokeModrUI : public KMainWindow, private Ui::formPokeModr
void newPokemod();
void openPokemod();
- void openPokemod(const KUrl& path);
+ bool openPokemod(const KUrl& url);
bool openPokemod(const QString& path);
void savePokemod();
void saveAsPokemod();
+ bool savePokemod(const KUrl& url);
void closePokeMod();
void quit();
void cutObject();
diff --git a/pokemodr/PokemodTree.cpp b/pokemodr/PokemodTree.cpp
index f94b5216..fee1d26e 100644
--- a/pokemodr/PokemodTree.cpp
+++ b/pokemodr/PokemodTree.cpp
@@ -23,9 +23,7 @@
// PokeModr includes
#include "ObjectUI.h"
-
-// KDE includes
-#include <KUrl>
+#include "PokemodTreeModel.h"
// Qt includes
#include <QHeaderView>
@@ -69,22 +67,32 @@ void PokemodTree::paste(const QModelIndex& index, const QDomDocument& data)
model()->setData(index, data.toString(), BaseModel::XmlRole);
}
-void PokemodTree::addPokemod(Pokemod* pokemod)
+KUrl PokemodTree::url(const Pokemod* pokemod) const
{
- // TODO: add the pokemod to the RootModel
+ if (m_urls.contains(pokemod))
+ return m_urls[pokemod];
+ return KUrl();
}
-void PokemodTree::deletePokemod(const Pokemod* pokemod)
+void PokemodTree::setUrl(Pokemod* pokemod, const KUrl& url)
+{
+ m_urls[pokemod] = url;
+}
+
+void PokemodTree::addPokemod(Pokemod* pokemod)
{
- // TODO: delete the pokemod from the RootModel
+ static_cast<PokemodTreeModel*>(model())->addPokemod(pokemod);
+ m_urls[pokemod] = "";
}
-void PokemodTree::savePokemod(const Pokemod* pokemod)
+void PokemodTree::addPokemod(Pokemod* pokemod, const KUrl& url)
{
- // TODO: save the pokemod
+ static_cast<PokemodTreeModel*>(model())->addPokemod(pokemod);
+ m_urls[pokemod] = url;
}
-void PokemodTree::savePokemod(const Pokemod* pokemod, const KUrl& url)
+void PokemodTree::deletePokemod(const Pokemod* pokemod)
{
- // TODO: save the pokemod
+ static_cast<PokemodTreeModel*>(model())->deletePokemod(pokemod);
+ m_urls.remove(pokemod);
}
diff --git a/pokemodr/PokemodTree.h b/pokemodr/PokemodTree.h
index 5591a6e6..bf2f4331 100644
--- a/pokemodr/PokemodTree.h
+++ b/pokemodr/PokemodTree.h
@@ -20,11 +20,14 @@
// Qt includes
#include <QDomDocument>
+#include <QMap>
#include <QTreeView>
+// KDE includes
+#include <KUrl>
+
// Forward declarations
class KMenu;
-class KUrl;
class ObjectUI;
class Pokemod;
@@ -43,10 +46,13 @@ class PokemodTree : public QTreeView
QDomDocument copy(const QModelIndex& index);
void paste(const QModelIndex& index, const QDomDocument& data);
+ KUrl url(const Pokemod* pokemod) const;
+ void setUrl(Pokemod* pokemod, const KUrl& url);
void addPokemod(Pokemod* pokemod);
+ void addPokemod(Pokemod* pokemod, const KUrl& url);
void deletePokemod(const Pokemod* pokemod);
- void savePokemod(const Pokemod* pokemod);
- void savePokemod(const Pokemod* pokemod, const KUrl& url);
+ private:
+ QMap<const Pokemod*, KUrl> m_urls;
};
#endif
diff --git a/pokemodr/PokemodTreeModel.cpp b/pokemodr/PokemodTreeModel.cpp
index 60bab37d..fb8aa4f5 100644
--- a/pokemodr/PokemodTreeModel.cpp
+++ b/pokemodr/PokemodTreeModel.cpp
@@ -26,12 +26,10 @@
#include <QMimeData>
#include <QStringList>
-PokemodTreeModel::PokemodTreeModel(const QStringList& pokemods, QObject* parent) :
- QAbstractItemModel(parent)
+PokemodTreeModel::PokemodTreeModel(QObject* parent) :
+ QAbstractItemModel(parent),
+ m_root(new RootModel)
{
- emit(layoutAboutToBeChanged());
- m_root = new RootModel(QList<QVariant>());
- emit(layoutChanged());
}
PokemodTreeModel::~PokemodTreeModel()
@@ -165,3 +163,17 @@ bool PokemodTreeModel::dropMimeData(const QMimeData* data, Qt::DropAction action
emit(layoutChanged());
return success;
}
+
+void PokemodTreeModel::addPokemod(Pokemod* pokemod)
+{
+ emit(layoutAboutToBeChanged());
+ m_root->addPokemod(pokemod);
+ emit(layoutChanged());
+}
+
+void PokemodTreeModel::deletePokemod(const Pokemod* pokemod)
+{
+ emit(layoutAboutToBeChanged());
+ m_root->deletePokemod(pokemod);
+ emit(layoutChanged());
+}
diff --git a/pokemodr/PokemodTreeModel.h b/pokemodr/PokemodTreeModel.h
index 5aec5957..a05cc7e4 100644
--- a/pokemodr/PokemodTreeModel.h
+++ b/pokemodr/PokemodTreeModel.h
@@ -33,7 +33,7 @@ class PokemodTreeModel : public QAbstractItemModel
Q_OBJECT
public:
- PokemodTreeModel(const QStringList& pokemods, QObject* parent = 0);
+ PokemodTreeModel(QObject* parent = 0);
~PokemodTreeModel();
QVariant data(const QModelIndex& index, int role) const;
@@ -56,6 +56,9 @@ class PokemodTreeModel : public QAbstractItemModel
QStringList mimeTypes() const;
QMimeData* mimeData(const QModelIndexList& indexes) const;
bool dropMimeData(const QMimeData* data, Qt::DropAction action, int /*row*/, int /*column*/, const QModelIndex& parent);
+
+ void addPokemod(Pokemod* pokemod);
+ void deletePokemod(const Pokemod* pokemod);
private:
BaseModel* getItem(const QModelIndex& index) const;
diff --git a/pokemodr/TODO b/pokemodr/TODO
index 90ed882f..bc1ccbe4 100644
--- a/pokemodr/TODO
+++ b/pokemodr/TODO
@@ -2,6 +2,8 @@ MoveEffect
Dialog command widgets
+Safeguard against closing with unsaved changes
+
Set sub class stuff by passing it on to subclasses via setData
TilemapModel is (probably) wrong
diff --git a/pokemodr/models/BaseModel.cpp b/pokemodr/models/BaseModel.cpp
index 8d76f928..789cdccb 100644
--- a/pokemodr/models/BaseModel.cpp
+++ b/pokemodr/models/BaseModel.cpp
@@ -51,6 +51,11 @@ int BaseModel::indexNumber() const
return -1;
}
+const Object * BaseModel::object() const
+{
+ return m_object;
+}
+
int BaseModel::findChild(const BaseModel* /*model*/) const
{
return -1;
diff --git a/pokemodr/models/BaseModel.h b/pokemodr/models/BaseModel.h
index 0540e859..41d5f353 100644
--- a/pokemodr/models/BaseModel.h
+++ b/pokemodr/models/BaseModel.h
@@ -53,6 +53,8 @@ class BaseModel
virtual QString type() const = 0;
+ const Object* object() const;
+
static const int TypeRole = Qt::UserRole;
static const int XmlRole = Qt::UserRole + 1;
static const int WidgetRole = Qt::UserRole + 2;
diff --git a/pokemodr/models/RootModel.cpp b/pokemodr/models/RootModel.cpp
index 7f9616e7..0dcd4f3a 100644
--- a/pokemodr/models/RootModel.cpp
+++ b/pokemodr/models/RootModel.cpp
@@ -27,10 +27,9 @@
// Test code includes
#include <QFile>
-RootModel::RootModel(const QList<QVariant>& pokemods) :
+RootModel::RootModel() :
GroupModel(NULL, NULL)
{
- setupData();
}
RootModel::~RootModel()
@@ -54,19 +53,24 @@ bool RootModel::removeRows(const int position, const int rows)
return true;
}
-void RootModel::setupData()
+void RootModel::addPokemod(Pokemod* pokemod)
{
- setupData(QList<QVariant>());
+ m_objects.append(new PokemodModel(this, pokemod));
}
-void RootModel::setupData(const QList<QVariant>& pokemods)
+void RootModel::deletePokemod(const Pokemod* pokemod)
+{
+ for (int i = 0; i < m_objects.size(); ++i)
+ {
+ if (m_objects[i]->object() == pokemod)
+ {
+ delete m_objects[i];
+ m_objects.removeAt(i);
+ break;
+ }
+ }
+}
+
+void RootModel::setupData()
{
- QFile fin("full.pmod");
- fin.open(QIODevice::ReadOnly);
- QDomDocument xml;
- xml.setContent(&fin);
- fin.close();
- Pokemod* full = new Pokemod(xml.documentElement());
- m_objects.append(new PokemodModel(this, full));
- // TODO: make sub models from data
}
diff --git a/pokemodr/models/RootModel.h b/pokemodr/models/RootModel.h
index f5539b78..9d788de5 100644
--- a/pokemodr/models/RootModel.h
+++ b/pokemodr/models/RootModel.h
@@ -27,15 +27,17 @@ class PokemodModel;
class RootModel : public GroupModel
{
public:
- RootModel(const QList<QVariant>& pokemods);
+ RootModel();
~RootModel();
bool insertRows(const int rows);
bool removeRows(const int position, const int rows);
+
+ void addPokemod(Pokemod* pokemod);
+ void deletePokemod(const Pokemod* pokemod);
protected:
void setupData();
- void setupData(const QList<QVariant>& pokemods);
};
#endif