1 /*
2  Copyright (C) 2010-2014 Kristian Duske
3 
4  This file is part of TrenchBroom.
5 
6  TrenchBroom is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  TrenchBroom is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "ReparentNodesCommand.h"
21 
22 #include "CollectionUtils.h"
23 #include "View/MapDocumentCommandFacade.h"
24 
25 #include <cassert>
26 
27 namespace TrenchBroom {
28     namespace View {
29         const Command::CommandType ReparentNodesCommand::Type = Command::freeType();
30 
reparent(Model::Node * newParent,const Model::NodeList & children)31         ReparentNodesCommand::Ptr ReparentNodesCommand::reparent(Model::Node* newParent, const Model::NodeList& children) {
32             assert(newParent != NULL);
33             assert(!children.empty());
34 
35             Model::ParentChildrenMap map;
36             map[newParent] = children;
37             return Ptr(new ReparentNodesCommand(map));
38         }
39 
reparent(const Model::ParentChildrenMap & nodes)40         ReparentNodesCommand::Ptr ReparentNodesCommand::reparent(const Model::ParentChildrenMap& nodes) {
41             assert(!nodes.empty());
42             return Ptr(new ReparentNodesCommand(nodes));
43         }
44 
ReparentNodesCommand(const Model::ParentChildrenMap & nodes)45         ReparentNodesCommand::ReparentNodesCommand(const Model::ParentChildrenMap& nodes) :
46         DocumentCommand(Type, "Reparent Objects"),
47         m_nodes(nodes) {}
48 
~ReparentNodesCommand()49         ReparentNodesCommand::~ReparentNodesCommand() {
50             MapUtils::clearAndDelete(m_removedNodes);
51         }
52 
doPerformDo(MapDocumentCommandFacade * document)53         bool ReparentNodesCommand::doPerformDo(MapDocumentCommandFacade* document) {
54             const MapDocumentCommandFacade::ReparentResult result = document->performReparentNodes(m_nodes, MapDocumentCommandFacade::RemoveEmptyNodes);
55             m_nodes = result.movedNodes;
56             m_removedNodes = result.removedNodes;
57             return true;
58         }
59 
doPerformUndo(MapDocumentCommandFacade * document)60         bool ReparentNodesCommand::doPerformUndo(MapDocumentCommandFacade* document) {
61             document->addNodes(m_removedNodes);
62 
63             const MapDocumentCommandFacade::ReparentResult result = document->performReparentNodes(m_nodes, MapDocumentCommandFacade::KeepEmptyNodes);
64             m_nodes = result.movedNodes;
65             m_removedNodes = result.removedNodes;
66             assert(m_removedNodes.empty());
67             return true;
68         }
69 
doIsRepeatable(MapDocumentCommandFacade * document) const70         bool ReparentNodesCommand::doIsRepeatable(MapDocumentCommandFacade* document) const {
71             return false;
72         }
73 
doCollateWith(UndoableCommand::Ptr command)74         bool ReparentNodesCommand::doCollateWith(UndoableCommand::Ptr command) {
75             return false;
76         }
77     }
78 }
79