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 "SerializeVisitor.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 SerializeVisitor::SerializeVisitor()
0040     : Visitor()
0041 {
0042 }
0043 
0044 SerializeVisitor::~SerializeVisitor()
0045 {
0046 }
0047 
0048 bool SerializeVisitor::save(const QString & filename)
0049 {
0050     QJsonObject root = m_root;
0051     root["nodes"] = m_nodes;
0052     root["paths"] = m_paths;
0053     root["styles"] = m_styles;
0054 
0055     // open file
0056     QFile target(filename);
0057     if (!target.open(QIODevice::WriteOnly | QIODevice::Text)) {
0058          return false;
0059     }
0060 
0061     // write json to text stream
0062     QJsonDocument json(root);
0063     QTextStream ts(&target);
0064     ts << json.toJson();
0065 
0066     return true;
0067 }
0068 
0069 void SerializeVisitor::visit(Document * doc)
0070 {
0071     // aggregate node ids
0072     QStringList list;
0073     for (const Uid & uid : doc->nodes()) {
0074         list.append(uid.toString());
0075     }
0076     m_root["node-ids"] = list.join(",");
0077 
0078     // aggregate path ids
0079     list.clear();
0080     for (const Uid & uid : doc->paths()) {
0081         list.append(uid.toString());
0082     }
0083     m_root["path-ids"] = list.join(",");
0084 
0085     // aggregate style ids
0086     list.clear();
0087     for (const Uid & uid : doc->entities()) {
0088         if (uid.entityType() == EntityType::Style)
0089             list.append(uid.toString());
0090     }
0091     m_root["style-ids"] = list.join(",");
0092 
0093     // save document style
0094     m_root["document-style"] = doc->style()->save();
0095 }
0096 
0097 void SerializeVisitor::visit(Node * node)
0098 {
0099     m_nodes["node-" + node->uid().toString()] = node->save();
0100 }
0101 
0102 void SerializeVisitor::visit(Path * path)
0103 {
0104     m_paths["path-" + path->uid().toString()] = path->save();
0105 }
0106 
0107 void SerializeVisitor::visit(Style * style)
0108 {
0109     m_styles["style-" + style->uid().toString()] = style->save();
0110 }
0111 
0112 }
0113 }
0114 
0115 // kate: indent-width 4; replace-tabs on;