From 18efef142da109d395d3d9761225a4936dee9e4d Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 23 Feb 2009 11:34:15 -0500 Subject: Moved the tree for sigmods in sigmodr to a library --- sigmodr/tree/BaseModel.cpp | 141 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 sigmodr/tree/BaseModel.cpp (limited to 'sigmodr/tree/BaseModel.cpp') diff --git a/sigmodr/tree/BaseModel.cpp b/sigmodr/tree/BaseModel.cpp new file mode 100644 index 00000000..643e62d3 --- /dev/null +++ b/sigmodr/tree/BaseModel.cpp @@ -0,0 +1,141 @@ +/* + * Copyright 2008-2009 Ben Boeckel + * + * 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 . + */ + +// Header include +#include "BaseModel.h" + +// Sigmod includes +#include "../../sigmod/Object.h" + +// KDE includes +#include +#include +#include +#include + +// Qt includes +#include +#include + +const int Sigmodr::BaseModel::TypeRole = Qt::UserRole; +const int Sigmodr::BaseModel::DropAcceptRole = Qt::UserRole + 1; +const int Sigmodr::BaseModel::XmlRole = Qt::UserRole + 2; +const int Sigmodr::BaseModel::WidgetRole = Qt::UserRole + 3; +const int Sigmodr::BaseModel::ContextMenuRole = Qt::UserRole + 4; + +Sigmodr::BaseModel::BaseModel(BaseModel* parent, Sigmod::Object* object, const QString& name) : + m_parent(parent), + m_object(object), + m_name(name) +{ +} + +Sigmodr::BaseModel::~BaseModel() +{ +} + +QVariant Sigmodr::BaseModel::data(int role) const +{ + if (role == Sigmodr::BaseModel::TypeRole) + return type(); + else if (role == Sigmodr::BaseModel::DropAcceptRole) + return types(); + return QVariant(); +} + +Sigmodr::BaseModel* Sigmodr::BaseModel::parent() +{ + return m_parent; +} + +int Sigmodr::BaseModel::indexNumber() const +{ + if (m_parent) + return m_parent->findChild(const_cast(this)); + return -1; +} + +QString Sigmodr::BaseModel::type() const +{ + return ""; +} + +const Sigmod::Object* Sigmodr::BaseModel::object() const +{ + return m_object; +} + +bool Sigmodr::BaseModel::loadFromData(const QString& data, QDomDocument* xml) const +{ + bool loaded = false; + QString error; + int line; + int column; + KUrl url(data); + if (url.isValid()) + { + QString path; + const bool local = url.isLocalFile(); + if (local) + path = url.path(); + else + { + if (!KIO::NetAccess::download(url, path, KApplication::kApplication()->activeWindow())) + { + KMessageBox::error(KApplication::kApplication()->activeWindow(), KIO::NetAccess::lastErrorString(), "KIO Error"); + return false; + } + } + QFile file(path); + if (file.open(QIODevice::ReadOnly)) + { + if (!xml->setContent(&file, &error, &line, &column)) + { + KMessageBox::error(KApplication::kApplication()->activeWindow(), QString("%1 at line %2, column %3").arg(error).arg(line).arg(column), "XML Error"); + loaded = false; + } + } + else + { + KMessageBox::error(KApplication::kApplication()->activeWindow(), file.errorString(), "File Error"); + loaded = false; + } + file.close(); + if (!local) + KIO::NetAccess::removeTempFile(path); + } + else if (xml->setContent(data, &error, &line, &column)) + loaded = true; + else + KMessageBox::error(KApplication::kApplication()->activeWindow(), QString("%1 at line %2, column %3").arg(error).arg(line).arg(column), "XML Error"); + return loaded; +} + +void Sigmodr::BaseModel::childRowChanged(const int row) +{ + QList indexes; + indexes << indexNumber() << row; + emit(rowsChanged(indexes)); +} + +void Sigmodr::BaseModel::childRowsChanged(const QList& rows) +{ + QList indexes; + indexes << indexNumber() << rows; + emit(rowsChanged(indexes)); +} + -- cgit