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

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 "Path.h"
0021 #include "Style.h"
0022 #include "Visitor.h"
0023 #include "Document.h"
0024 
0025 namespace tikz {
0026 namespace core {
0027 
0028 class PathPrivate
0029 {
0030     public:
0031         // this edge's style
0032         Uid styleUid;
0033         Style * style = nullptr;
0034 };
0035 
0036 Path::Path(const Uid & uid)
0037     : Entity(uid)
0038     , d(new PathPrivate())
0039 {
0040     setStyle(Uid());
0041 }
0042 
0043 Path::~Path()
0044 {
0045     if (d->style) {
0046         delete d->style;
0047         d->style = nullptr;
0048     }
0049 
0050     delete d;
0051 }
0052 
0053 tikz::EntityType Path::entityType() const
0054 {
0055     return EntityType::Path;
0056 }
0057 
0058 PathType Path::type() const
0059 {
0060     return PathType::Invalid;
0061 }
0062 
0063 void Path::deconstruct()
0064 {
0065 }
0066 
0067 void Path::detachFromNode(Node * node)
0068 {
0069     Q_UNUSED(node)
0070 }
0071 
0072 bool Path::accept(Visitor & visitor)
0073 {
0074     visitor.visit(this);
0075     return true;
0076 }
0077 
0078 void Path::loadData(const QJsonObject & json)
0079 {
0080     ConfigTransaction transaction(this);
0081 
0082     if (json.contains("style")) {
0083         const Uid styleId(json["style"].toString(), document());
0084         setStyle(styleId);
0085     }
0086 }
0087 
0088 QJsonObject Path::saveData() const
0089 {
0090     QJsonObject json = Entity::saveData();
0091 
0092     json["style"] = style()->uid().toString();
0093 
0094     return json;
0095 }
0096 
0097 Style* Path::style() const
0098 {
0099     return d->styleUid.isValid() ? d->styleUid.entity<Style>() : d->style;
0100 }
0101 
0102 Uid Path::styleUid() const
0103 {
0104     return d->styleUid;
0105 }
0106 
0107 void Path::setStyle(const Uid & styleUid)
0108 {
0109     if (!d->styleUid.isValid()) {
0110         delete d->style;
0111         d->style = nullptr;
0112     } else {
0113         disconnect(d->styleUid.entity<Style>(), SIGNAL(changed()), this, SLOT(emitChangedIfNeeded()));
0114     }
0115 
0116     if (styleUid.isValid()) {
0117         d->styleUid = styleUid;
0118     } else {
0119         d->style = new Style();
0120         d->style->setParentStyle(document()->style()->uid());
0121     }
0122 
0123     connect(style(), SIGNAL(changed()), this, SLOT(emitChangedIfNeeded()));
0124 }
0125 
0126 // Edge * Path::createEdge(int index)
0127 // {
0128 //     // sanity check
0129 //     Q_ASSERT(index <= d->edges.size());
0130 //
0131 //     // negative index: append item
0132 //     if (index < 0) {
0133 //         index = d->edges.size();
0134 //     }
0135 //
0136 //     Edge * edge = 0;
0137 //
0138 //     if (document()->undoActive()) {
0139 //         ConfigTransaction transaction(this);
0140 //
0141 //         // create and insert edge
0142 //         edge = new Edge(this);
0143 //
0144 //         // insert edge
0145 //         d->edges.insert(index, edge);
0146 //     } else {
0147 //         // create edge via undo system
0148 //         document()->addUndoItem(new UndoCreateEdge(uid(), index, document()));
0149 //         Q_ASSERT(index < d->edges.size());
0150 //
0151 //         // return newly created edge
0152 //         edge = d->edges[index];
0153 //     }
0154 //
0155 //     return edge;
0156 // }
0157 //
0158 // void Path::deleteEdge(Edge * edge)
0159 // {
0160 //     const int index = d->edges.indexOf(edge);
0161 //     Q_ASSERT(index >= 0);
0162 //
0163 //     deleteEdge(index);
0164 // }
0165 //
0166 // void Path::deleteEdge(int index)
0167 // {
0168 //     Q_ASSERT(index >= 0);
0169 //     Q_ASSERT(index < d->edges.size());
0170 //
0171 //     if (document()->undoActive()) {
0172 //         ConfigTransaction transaction(this);
0173 //
0174 //         // get edge to delete
0175 //         Edge * edge = d->edges[index];
0176 //
0177 //         // remove edge
0178 //         d->edges.remove(index);
0179 //
0180 //         // finally delete edge
0181 //         delete edge;
0182 //     } else {
0183 //         // create edge via undo system
0184 //         document()->addUndoItem(new UndoDeleteEdge(uid(), index, document()));
0185 //     }
0186 // }
0187 //
0188 // Edge* Path::edge(int i)
0189 // {
0190 //     Q_ASSERT(i >= 0);
0191 //     Q_ASSERT(i < d->edges.count());
0192 //
0193 //     return d->edges[i];
0194 // }
0195 //
0196 // int Path::edgeIndex(const Edge * edge) const
0197 // {
0198 //     Q_ASSERT(edge != 0);
0199 //     const int index = d->edges.indexOf(const_cast<Edge*>(edge));
0200 //     Q_ASSERT(index >= 0);
0201 //     return index;
0202 // }
0203 //
0204 // int Path::edgeCount() const
0205 // {
0206 //     return d->edges.count();
0207 // }
0208 //
0209 // bool Path::isClosed() const
0210 // {
0211 //     // FIXME: implement
0212 //     return false;
0213 // }
0214 //
0215 // void Path::setClosed(bool closed)
0216 // {
0217 //     if (closed == isClosed())
0218 //         return;
0219 //
0220 //     if (closed) {
0221 //         Edge * edge = createEdge();
0222 //         // FIXME: implement
0223 //     } else {
0224 //         // FIXME: implement
0225 //     }
0226 // }
0227 
0228 }
0229 }
0230 
0231 // kate: indent-width 4; replace-tabs on;