summaryrefslogtreecommitdiffstats
path: root/sigmodr/SigmodTreeModel.cpp
diff options
context:
space:
mode:
authorBen Boeckel <MathStuf@gmail.com>2008-09-06 04:12:30 +0000
committerBen Boeckel <MathStuf@gmail.com>2008-09-06 04:12:30 +0000
commit0b4b89cf8efdc15e5a8d4b6cb24a5c8a025227d9 (patch)
treea2031b9d0016fcbd49a51c0d1a2292d1f2d8b566 /sigmodr/SigmodTreeModel.cpp
parentb81f5bffa2772eb9bd3c67fb35485ab1ee2d96e7 (diff)
downloadsigen-0b4b89cf8efdc15e5a8d4b6cb24a5c8a025227d9.tar.gz
sigen-0b4b89cf8efdc15e5a8d4b6cb24a5c8a025227d9.tar.xz
sigen-0b4b89cf8efdc15e5a8d4b6cb24a5c8a025227d9.zip
[FIX] Renamed everything (in use) away from Poké- prefixes
git-svn-id: https://pokegen.svn.sourceforge.net/svnroot/pokegen/trunk@250 6ecfd1a5-f3ed-3746-8530-beee90d26b22
Diffstat (limited to 'sigmodr/SigmodTreeModel.cpp')
-rw-r--r--sigmodr/SigmodTreeModel.cpp159
1 files changed, 159 insertions, 0 deletions
diff --git a/sigmodr/SigmodTreeModel.cpp b/sigmodr/SigmodTreeModel.cpp
new file mode 100644
index 00000000..a2d8f5a0
--- /dev/null
+++ b/sigmodr/SigmodTreeModel.cpp
@@ -0,0 +1,159 @@
+/*
+ * Copyright 2008 Ben Boeckel <MathStuf@gmail.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+// Header include
+#include "SigmodTreeModel.h"
+
+// Model includes
+#include "models/RootModel.h"
+
+// Qt includes
+#include <QtCore/QMimeData>
+#include <QtCore/QStringList>
+#include <QtXml/QDomDocument>
+
+Sigmodr::SigmodTreeModel::SigmodTreeModel(QObject* parent) :
+ QAbstractItemModel(parent),
+ m_root(new RootModel)
+{
+}
+
+Sigmodr::SigmodTreeModel::~SigmodTreeModel()
+{
+ delete m_root;
+}
+
+QVariant Sigmodr::SigmodTreeModel::data(const QModelIndex& index, int role) const
+{
+ if (!index.isValid())
+ return QVariant();
+ BaseModel* object = static_cast<BaseModel*>(index.internalPointer());
+ return object->data(role);
+}
+
+QVariant Sigmodr::SigmodTreeModel::headerData(int /*section*/, Qt::Orientation /*orientation*/, int /*role = Qt::DisplayRole*/) const
+{
+ return QVariant();
+}
+
+QModelIndex Sigmodr::SigmodTreeModel::index(int row, int column, const QModelIndex& parent) const
+{
+ if ((row < -1) || (column < -1))
+ return QModelIndex();
+ if (row == -1)
+ return createIndex(row, 0, m_root);
+ BaseModel* model = getItem(parent);
+ if ((model->rowCount() <= row) || (1 <= column))
+ return QModelIndex();
+ BaseModel* object = model->childItem(row);
+ return createIndex(row, 0, object);
+}
+
+QModelIndex Sigmodr::SigmodTreeModel::parent(const QModelIndex& index) const
+{
+ if (!index.isValid())
+ return QModelIndex();
+ BaseModel* parent = static_cast<BaseModel*>(index.internalPointer())->parent();
+ if (!parent || (parent == m_root))
+ return QModelIndex();
+ return createIndex(parent->indexNumber(), 0, parent);
+}
+
+int Sigmodr::SigmodTreeModel::rowCount(const QModelIndex& parent) const
+{
+ BaseModel* object = getItem(parent);
+ return object->rowCount();
+}
+
+int Sigmodr::SigmodTreeModel::columnCount(const QModelIndex& /*parent*/) const
+{
+ return 1;
+}
+
+Qt::ItemFlags Sigmodr::SigmodTreeModel::flags(const QModelIndex& index) const
+{
+ if (!index.isValid())
+ return 0;
+ BaseModel* object = static_cast<BaseModel*>(index.internalPointer());
+ return object->flags();
+}
+
+bool Sigmodr::SigmodTreeModel::setData(const QModelIndex& index, const QVariant& value, int role)
+{
+ if (!index.isValid())
+ return false;
+ BaseModel* object = static_cast<BaseModel*>(index.internalPointer());
+ emit(layoutAboutToBeChanged());
+ bool success = object->setData(value, role);
+ emit(dataChanged(index, index));
+ emit(layoutChanged());
+ return success;
+}
+
+Sigmodr::BaseModel* Sigmodr::SigmodTreeModel::getItem(const QModelIndex& index) const
+{
+ if (index.isValid())
+ {
+ BaseModel* object = static_cast<BaseModel*>(index.internalPointer());
+ if (object)
+ return object;
+ }
+ return m_root;
+}
+
+QStringList Sigmodr::SigmodTreeModel::mimeTypes() const
+{
+ return QStringList() << "application/x-sigmod+xml";
+}
+
+QMimeData* Sigmodr::SigmodTreeModel::mimeData(const QModelIndexList& indexes) const
+{
+ QMimeData *mimeData = new QMimeData();
+ if ((indexes.size() == 1) && indexes[0].isValid())
+ {
+ QDomDocument xml;
+ xml.setContent(data(indexes[0], Sigmodr::BaseModel::XmlRole).toString());
+ mimeData->setData("application/x-sigmod+xml", xml.toByteArray());
+ }
+ return mimeData;
+}
+
+bool Sigmodr::SigmodTreeModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int /*row*/, int /*column*/, const QModelIndex& parent)
+{
+ if (action == Qt::IgnoreAction)
+ return true;
+ if (!data->hasFormat("application/x-sigmod+xml"))
+ return false;
+ if (parent.isValid())
+ return setData(parent, data->data("application/x-sigmod+xml"), Sigmodr::BaseModel::XmlRole);
+ emit(layoutAboutToBeChanged());
+ bool success = m_root->setData(data->data("application/x-sigmod+xml"), Sigmodr::BaseModel::XmlRole);
+ emit(layoutChanged());
+ return success;
+}
+
+void Sigmodr::SigmodTreeModel::addSigmod(Sigmod::Sigmod* sigmod)
+{
+ m_root->addSigmod(sigmod);
+ emit(reset());
+}
+
+void Sigmodr::SigmodTreeModel::deleteSigmod(const Sigmod::Sigmod* sigmod)
+{
+ m_root->deleteSigmod(sigmod);
+ emit(reset());
+}