File indexing completed on 2024-12-22 04:12:31

0001 /*
0002  *  SPDX-FileCopyrightText: 2011 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_node_shapes_graph.h"
0008 
0009 #include "kis_node_shape.h"
0010 
0011 
0012 KisNodeShape* KisNodeShapesGraph::addNode(KisNodeSP node, KisNodeSP parent, KisNodeSP aboveThis)
0013 {
0014     KisNodeDummy *parentDummy = 0;
0015     KisNodeDummy *aboveThisDummy = 0;
0016 
0017     KisNodeShape *parentShape = 0;
0018 
0019     if(parent) {
0020         parentDummy = nodeToDummy(parent);
0021         parentShape = parentDummy->nodeShape();
0022     }
0023 
0024     if(aboveThis) {
0025         aboveThisDummy = nodeToDummy(aboveThis);
0026     }
0027 
0028     KisNodeShape *newShape = new KisNodeShape(node);
0029     ((KoShapeLayer*)newShape)->setParent(parentShape);
0030 
0031     KisNodeDummy *newDummy = new KisNodeDummy(newShape, newShape->node());
0032 
0033     m_dummiesGraph.addNode(newDummy, parentDummy, aboveThisDummy);
0034     return newShape;
0035 }
0036 
0037 void KisNodeShapesGraph::moveNode(KisNodeSP node, KisNodeSP parent, KisNodeSP aboveThis)
0038 {
0039     KisNodeDummy *nodeDummy = nodeToDummy(node);
0040     KisNodeDummy *parentDummy = parent ? nodeToDummy(parent) : 0;
0041     KisNodeDummy *aboveThisDummy = aboveThis ? nodeToDummy(aboveThis) : 0;
0042 
0043     m_dummiesGraph.moveNode(nodeDummy, parentDummy, aboveThisDummy);
0044 }
0045 
0046 void KisNodeShapesGraph::removeNode(KisNodeSP node)
0047 {
0048     KisNodeDummy *nodeDummy = nodeToDummy(node);
0049 
0050     m_dummiesGraph.removeNode(nodeDummy);
0051 
0052     /**
0053      * The shapes remove their children automatically,
0054      * so the dummies do not own them. Delete them manually.
0055      */
0056     KisNodeShape *tempShape = nodeDummy->nodeShape();
0057     ((KoShapeLayer*)tempShape)->setParent(0);
0058 
0059     delete nodeDummy;
0060     delete tempShape;
0061 }
0062 
0063 KisNodeDummy* KisNodeShapesGraph::rootDummy() const
0064 {
0065     return m_dummiesGraph.rootDummy();
0066 }
0067 
0068 KisNodeDummy* KisNodeShapesGraph::nodeToDummy(KisNodeSP node)
0069 {
0070     return m_dummiesGraph.nodeToDummy(node);
0071 }
0072 
0073 KisNodeShape* KisNodeShapesGraph::nodeToShape(KisNodeSP node)
0074 {
0075     KisNodeDummy *dummy = nodeToDummy(node);
0076     if (dummy) {
0077         return dummy->nodeShape();
0078     }
0079     return 0;
0080 }
0081 
0082 bool KisNodeShapesGraph::containsNode(KisNodeSP node) const
0083 {
0084     return m_dummiesGraph.containsNode(node);
0085 }
0086 
0087 int KisNodeShapesGraph::shapesCount() const
0088 {
0089     return m_dummiesGraph.dummiesCount();
0090 }