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

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2015 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 "Entity.h"
0021 #include "Document.h"
0022 
0023 namespace tikz {
0024 namespace core {
0025 
0026 /**
0027  * Private data and helper functions of class Entity.
0028  */
0029 class EntityPrivate
0030 {
0031 public:
0032     // unique id, or -1 (negative)
0033     Uid uid = Uid();
0034 };
0035 
0036 Entity::Entity()
0037     : ConfigObject()
0038     , d(new EntityPrivate())
0039 {
0040 }
0041 
0042 Entity::Entity(const Uid & uid)
0043     : ConfigObject()
0044     , d(new EntityPrivate())
0045 {
0046     Q_ASSERT(uid.isValid());
0047     d->uid = uid;
0048 }
0049 
0050 Entity::~Entity()
0051 {
0052 }
0053 
0054 Uid Entity::uid() const
0055 {
0056     return d->uid;
0057 }
0058 
0059 bool Entity::objectNameSet() const
0060 {
0061     return !objectName().isEmpty();
0062 }
0063 
0064 void Entity::unsetObjectName()
0065 {
0066     setObjectName(QString());
0067 }
0068 
0069 void Entity::load(const QJsonObject & json)
0070 {
0071     if (json.contains("uid")) {
0072         d->uid = Uid(json["uid"].toString(), d->uid.document());
0073     }
0074 
0075     if (json.contains("entityType")) {
0076         Q_ASSERT(entityType() == toEntityType(json["entityType"].toString()));
0077     }
0078 
0079     if (json.contains("objectName")) {
0080         setObjectName(json["objectName"].toString());
0081     }
0082 
0083     // load payload
0084     const QJsonObject data = json["data"].toObject();
0085     loadData(data);
0086 }
0087 
0088 QJsonObject Entity::save() const
0089 {
0090     QJsonObject json;
0091 
0092     // entity data
0093     json["uid"] = d->uid.toString();
0094     json["entityType"] = toString(entityType());
0095     json["objectName"] = objectName();
0096 
0097     // save payload
0098     json["data"] = saveData();
0099 
0100     return json;
0101 }
0102 
0103 QJsonObject Entity::saveData() const
0104 {
0105     return QJsonObject();
0106 }
0107 
0108 void Entity::loadData(const QJsonObject & json)
0109 {
0110     Q_UNUSED(json)
0111 }
0112 
0113 Document * Entity::document() const
0114 {
0115     return d->uid.document();
0116 }
0117 
0118 }
0119 }
0120 
0121 // kate: indent-width 4; replace-tabs on;