summaryrefslogtreecommitdiffstats
path: root/sigmodr/tree/SigmodrTree.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'sigmodr/tree/SigmodrTree.cpp')
-rw-r--r--sigmodr/tree/SigmodrTree.cpp161
1 files changed, 161 insertions, 0 deletions
diff --git a/sigmodr/tree/SigmodrTree.cpp b/sigmodr/tree/SigmodrTree.cpp
new file mode 100644
index 00000000..9437a0e6
--- /dev/null
+++ b/sigmodr/tree/SigmodrTree.cpp
@@ -0,0 +1,161 @@
+/*
+ * 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 "SigmodTree.h"
+
+// Model includes
+#include "models/BaseModel.h"
+
+// Sigmodr includes
+#include "ObjectUI.h"
+#include "SigmodTreeModel.h"
+
+// Sigmod includes
+#include "../sigmod/Object.h"
+#include "../sigmod/Sigmod.h"
+
+// Qt includes
+#include <QtGui/QHeaderView>
+
+Sigmodr::SigmodTree::SigmodTree(QWidget* parent) :
+ QTreeView(parent)
+{
+ SigmodTreeModel* model = new SigmodTreeModel(this);
+ connect(model, SIGNAL(dirty(const Sigmod::Sigmod*, bool)), this, SLOT(setDirty(const Sigmod::Sigmod*, bool)));
+ header()->hide();
+ setModel(model);
+ setRootIndex(model->index(-1, 0, QModelIndex()));
+}
+
+QString Sigmodr::SigmodTree::description(const QModelIndex& index)
+{
+ QString type = model()->data(index, BaseModel::TypeRole).toString();
+ QString name = model()->data(index, Qt::DisplayRole).toString();
+ if (type.isEmpty() && name.isEmpty())
+ return "";
+ return QString("%1: %2").arg(type).arg(name);
+}
+
+Sigmodr::ObjectUI* Sigmodr::SigmodTree::editorWidget(const QModelIndex& index)
+{
+ Sigmodr::ObjectUI* widget = qobject_cast<ObjectUI*>(model()->data(index, BaseModel::WidgetRole).value<QWidget*>());
+ return widget;
+}
+
+KMenu* Sigmodr::SigmodTree::contextMenu(const QModelIndex& index)
+{
+ return model()->data(index, BaseModel::ContextMenuRole).value<KMenu*>();
+}
+
+const Sigmod::Sigmod* Sigmodr::SigmodTree::currentSigmod() const
+{
+ QModelIndex index = currentIndex();
+ if (index.isValid())
+ {
+ const Sigmod::Object* object = static_cast<BaseModel*>(index.internalPointer())->object();
+ if (object)
+ return object->sigmod();
+ }
+ return NULL;
+}
+
+QDomDocument Sigmodr::SigmodTree::copy(const QModelIndex& index)
+{
+ QDomDocument xml;
+ xml.setContent(model()->data(index, BaseModel::XmlRole).toString());
+ return xml;
+}
+
+void Sigmodr::SigmodTree::paste(const QModelIndex& index, const QDomDocument& data)
+{
+ model()->setData(index, data.toString(), BaseModel::XmlRole);
+}
+
+QList<const Sigmod::Sigmod*> Sigmodr::SigmodTree::openedSigmods() const
+{
+ return m_sigmods.keys();
+}
+
+void Sigmodr::SigmodTree::addSigmod(Sigmod::Sigmod* sigmod, const KUrl& url)
+{
+ qobject_cast<SigmodTreeModel*>(model())->addSigmod(sigmod);
+ m_sigmods[sigmod] = UrlDirty(url, false);
+}
+
+void Sigmodr::SigmodTree::deleteSigmod(const Sigmod::Sigmod* sigmod)
+{
+ if (m_sigmods.contains(sigmod))
+ {
+ qobject_cast<SigmodTreeModel*>(model())->deleteSigmod(sigmod);
+ m_sigmods.remove(sigmod);
+ }
+}
+
+bool Sigmodr::SigmodTree::isOpen(const KUrl& url) const
+{
+ foreach (const UrlDirty& pair, m_sigmods.values())
+ {
+ if (url == pair.first)
+ return true;
+ }
+ return false;
+}
+
+KUrl Sigmodr::SigmodTree::url(const Sigmod::Sigmod* sigmod) const
+{
+ if (m_sigmods.contains(sigmod))
+ return m_sigmods[sigmod].first;
+ return KUrl();
+}
+
+QStringList Sigmodr::SigmodTree::urls() const
+{
+ QStringList urls;
+ foreach (const UrlDirty& pair, m_sigmods.values())
+ urls << pair.first.prettyUrl();
+ return urls;
+}
+
+void Sigmodr::SigmodTree::setUrl(const Sigmod::Sigmod* sigmod, const KUrl& url)
+{
+ if (m_sigmods.contains(sigmod))
+ m_sigmods[sigmod] = UrlDirty(url, false);
+}
+
+bool Sigmodr::SigmodTree::dirty(const Sigmod::Sigmod* sigmod) const
+{
+ if (m_sigmods.contains(sigmod))
+ return m_sigmods[sigmod].second;
+ return false;
+}
+
+bool Sigmodr::SigmodTree::dirty() const
+{
+ return dirty(currentSigmod());
+}
+
+void Sigmodr::SigmodTree::setDirty(const Sigmod::Sigmod* sigmod, const bool dirty)
+{
+ if (m_sigmods.contains(sigmod))
+ m_sigmods[sigmod].second = dirty;
+}
+
+void Sigmodr::SigmodTree::setDirty()
+{
+ setDirty(currentSigmod(), true);
+}