File indexing completed on 2024-05-12 04:35:06

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2013 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "DeserializeVisitor.h"
0021 
0022 #include "Document.h"
0023 #include "Node.h"
0024 #include "Path.h"
0025 #include "EdgePath.h"
0026 #include "EllipsePath.h"
0027 #include "Style.h"
0028 
0029 #include <QJsonDocument>
0030 #include <QStringList>
0031 #include <QTextStream>
0032 #include <QMetaProperty>
0033 #include <QFile>
0034 #include <QDebug>
0035 
0036 namespace tikz {
0037 namespace core {
0038 
0039 DeserializeVisitor::DeserializeVisitor()
0040     : Visitor()
0041 {
0042 }
0043 
0044 DeserializeVisitor::~DeserializeVisitor()
0045 {
0046 }
0047 
0048 bool DeserializeVisitor::load(const QString & filename)
0049 {
0050     // open file
0051     QFile file(filename);
0052     if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
0053          return false;
0054     }
0055 
0056     QJsonDocument json = QJsonDocument::fromJson(file.readAll());
0057     m_root = json.object();
0058 
0059     m_nodes = m_root["nodes"].toObject();
0060     m_paths = m_root["paths"].toObject();
0061     m_styles = m_root["styles"].toObject();
0062 
0063 //     qDebug() << m_nodes << m_paths;
0064 
0065     return true;
0066 }
0067 
0068 void DeserializeVisitor::visit(Document * doc)
0069 {
0070     // aggregate node ids
0071     QStringList list = m_root["node-ids"].toString().split(',');
0072     for (const QString & uid : qAsConst(list)) {
0073         doc->createEntity(Uid(uid, doc), tikz::EntityType::Node);
0074     }
0075 
0076     // aggregate edge ids
0077     list = m_root["path-ids"].toString().split(',');
0078     for (const QString & idAsStr : qAsConst(list)) {
0079         const Uid uid(idAsStr, doc);
0080         const QString type = m_root["paths"].toObject()["path-" + uid.toString()].toObject()["type"].toString();
0081 //         qDebug() << type << pathType(type);
0082         doc->createPath(toEnum<PathType>(type), uid);
0083     }
0084 
0085     // aggregate style ids
0086     list = m_root["style-ids"].toString().split(',');
0087     for (const QString & uid : qAsConst(list)) {
0088         doc->createEntity(Uid(uid, doc), tikz::EntityType::Style);
0089     }
0090 
0091     // load document style
0092     doc->style()->load(m_root["document-style"].toObject());
0093 }
0094 
0095 void DeserializeVisitor::visit(Node * node)
0096 {
0097     node->load(m_nodes["node-" + node->uid().toString()].toObject());
0098 }
0099 
0100 void DeserializeVisitor::visit(Path * path)
0101 {
0102     path->load(m_paths["path-" + path->uid().toString()].toObject());
0103 }
0104 
0105 void DeserializeVisitor::visit(Style * style)
0106 {
0107     style->load(m_styles["style-" + style->uid().toString()].toObject());
0108 }
0109 
0110 }
0111 }
0112 
0113 // kate: indent-width 4; replace-tabs on;