summaryrefslogtreecommitdiffstats
path: root/sigmodr/tree/NatureGroupModel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'sigmodr/tree/NatureGroupModel.cpp')
-rw-r--r--sigmodr/tree/NatureGroupModel.cpp94
1 files changed, 94 insertions, 0 deletions
diff --git a/sigmodr/tree/NatureGroupModel.cpp b/sigmodr/tree/NatureGroupModel.cpp
new file mode 100644
index 00000000..a484ccf5
--- /dev/null
+++ b/sigmodr/tree/NatureGroupModel.cpp
@@ -0,0 +1,94 @@
+/*
+ * 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 "NatureGroupModel.h"
+
+// Model includes
+#include "NatureModel.h"
+
+// Sigmod includes
+#include "../../sigmod/Nature.h"
+#include "../../sigmod/Sigmod.h"
+
+// KDE includes
+#include <KMenu>
+
+Sigmodr::NatureGroupModel::NatureGroupModel(BaseModel* parent, Sigmod::Sigmod* sigmod) :
+ GroupModel(parent, sigmod, "Natures")
+{
+ for (int i = 0; i < sigmod->natureCount(); ++i)
+ addObject(sigmod->nature(i));
+}
+
+Sigmodr::NatureGroupModel::~NatureGroupModel()
+{
+}
+
+QVariant Sigmodr::NatureGroupModel::data(const int role) const
+{
+ if (role == Sigmodr::BaseModel::ContextMenuRole)
+ {
+ KMenu* menu = new KMenu;
+ menu->addAction("&Add Nature", this, SLOT(addObject()));
+ return QVariant::fromValue(menu);
+ }
+ return Sigmodr::GroupModel::data(role);
+}
+
+bool Sigmodr::NatureGroupModel::setData(const QVariant& value, int role)
+{
+ if (role == Sigmodr::BaseModel::XmlRole)
+ {
+ QString data = value.toString();
+ if (!data.isEmpty())
+ {
+ QDomDocument xml;
+ if (loadFromData(data, &xml) && (xml.doctype().name() == "Nature"))
+ {
+ addObject(qobject_cast<Sigmod::Sigmod*>(m_object)->newNature(xml.documentElement()));
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+QString Sigmodr::NatureGroupModel::types() const
+{
+ return "Nature";
+}
+
+void Sigmodr::NatureGroupModel::addObject(Sigmod::Object* object)
+{
+ if (!object)
+ object = qobject_cast<Sigmod::Sigmod*>(m_object)->newNature();
+ if (object->className() == "Nature")
+ addChild(new NatureModel(this, qobject_cast<Sigmod::Nature*>(object)));
+}
+
+void Sigmodr::NatureGroupModel::deleteObject(BaseModel* model)
+{
+ const int index = find(model);
+ if (0 <= index)
+ {
+ qobject_cast<Sigmod::Sigmod*>(m_object)->deleteNature(index);
+ m_objects[index]->deleteLater();
+ m_objects.removeAt(index);
+ childRowChanged(index);
+ }
+}