File indexing completed on 2024-12-22 04:20:27
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 "EllipsePath.h" 0021 #include "UndoSetEllipsePos.h" 0022 #include "Node.h" 0023 #include "Document.h" 0024 0025 #include <QDebug> 0026 0027 namespace tikz { 0028 namespace core { 0029 0030 class EllipsePathPrivate 0031 { 0032 public: 0033 EllipsePathPrivate(Document * doc) 0034 : pos(doc) 0035 {} 0036 // meta node this ellipse 0037 MetaPos pos; 0038 }; 0039 0040 EllipsePath::EllipsePath(const Uid & uid) 0041 : Path(uid) 0042 , d(new EllipsePathPrivate(uid.document())) 0043 { 0044 connect(d->pos.notificationObject(), SIGNAL(changed(tikz::core::MetaPos*)), 0045 this, SLOT(emitChangedIfNeeded())); 0046 } 0047 0048 EllipsePath::~EllipsePath() 0049 { 0050 delete d; 0051 } 0052 0053 PathType EllipsePath::type() const 0054 { 0055 return PathType::Ellipse; 0056 } 0057 0058 void EllipsePath::deconstruct() 0059 { 0060 // just set both the start and end pos to (0, 0). 0061 // undo (i.e., creating the node again), will then restore the initial 0062 // connections correctly. 0063 ConfigTransaction transaction(this); 0064 setPos(tikz::Pos()); 0065 } 0066 0067 void EllipsePath::detachFromNode(Node * node) 0068 { 0069 Q_ASSERT(node != nullptr); 0070 0071 // disconnect from node, if currently attached 0072 if (d->pos.node() == node) { 0073 auto newPos = metaPos(); 0074 newPos.setNode(nullptr); 0075 setMetaPos(newPos); 0076 } 0077 Q_ASSERT(d->pos.node() != node); 0078 } 0079 0080 void EllipsePath::setNode(Node* node) 0081 { 0082 auto newPos = metaPos(); 0083 newPos.setNode(node); 0084 setMetaPos(newPos); 0085 0086 Q_ASSERT(d->pos.node() == node); 0087 } 0088 0089 Node* EllipsePath::node() const 0090 { 0091 return d->pos.node(); 0092 } 0093 0094 tikz::Pos EllipsePath::pos() const 0095 { 0096 return d->pos.pos(); 0097 } 0098 0099 void EllipsePath::setPos(const tikz::Pos & pos) 0100 { 0101 auto newPos = metaPos(); 0102 newPos.setPos(pos); 0103 setMetaPos(newPos); 0104 0105 Q_ASSERT(d->pos.pos() == pos); 0106 } 0107 0108 const tikz::core::MetaPos & EllipsePath::metaPos() const 0109 { 0110 return d->pos; 0111 } 0112 0113 void EllipsePath::setMetaPos(const tikz::core::MetaPos & pos) 0114 { 0115 if (d->pos == pos) { 0116 return; 0117 } 0118 0119 if (document()->undoActive()) { 0120 ConfigTransaction transaction(this); 0121 auto oldNode = node(); 0122 d->pos = pos; 0123 auto newNode = node(); 0124 if (oldNode != newNode) { 0125 Q_EMIT nodeChanged(newNode); 0126 } 0127 } else { 0128 document()->addUndoItem( 0129 new UndoSetEllipsePos(this, pos, document())); 0130 } 0131 } 0132 0133 QString EllipsePath::anchor() const 0134 { 0135 return d->pos.anchor(); 0136 } 0137 0138 void EllipsePath::setAnchor(const QString & anchor) 0139 { 0140 auto newPos = metaPos(); 0141 newPos.setAnchor(anchor); 0142 setMetaPos(newPos); 0143 0144 Q_ASSERT(d->pos.anchor() == anchor); 0145 } 0146 0147 } 0148 } 0149 0150 // kate: indent-width 4; replace-tabs on;