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 "AddRemoveNodesCommand.h"
21 
22 #include "CollectionUtils.h"
23 #include "Macros.h"
24 #include "Model/Node.h"
25 #include "View/MapDocumentCommandFacade.h"
26 
27 #include <cassert>
28 
29 namespace TrenchBroom {
30     namespace View {
31         const Command::CommandType AddRemoveNodesCommand::Type = Command::freeType();
32 
add(Model::Node * parent,const Model::NodeList & children)33         AddRemoveNodesCommand::Ptr AddRemoveNodesCommand::add(Model::Node* parent, const Model::NodeList& children) {
34             assert(parent != NULL);
35             Model::ParentChildrenMap nodes;
36             nodes[parent] = children;
37 
38             return add(nodes);
39         }
40 
add(const Model::ParentChildrenMap & nodes)41         AddRemoveNodesCommand::Ptr AddRemoveNodesCommand::add(const Model::ParentChildrenMap& nodes) {
42             return Ptr(new AddRemoveNodesCommand(nodes));
43         }
44 
remove(const Model::NodeList & nodes)45         AddRemoveNodesCommand::Ptr AddRemoveNodesCommand::remove(const Model::NodeList& nodes) {
46             return Ptr(new AddRemoveNodesCommand(nodes));
47         }
48 
~AddRemoveNodesCommand()49         AddRemoveNodesCommand::~AddRemoveNodesCommand() {
50             MapUtils::clearAndDelete(m_nodesToAdd);
51         }
52 
AddRemoveNodesCommand(const Model::ParentChildrenMap & nodesToAdd)53         AddRemoveNodesCommand::AddRemoveNodesCommand(const Model::ParentChildrenMap& nodesToAdd) :
54         DocumentCommand(Type, makeName(Action_Add)),
55         m_action(Action_Add),
56         m_nodesToAdd(nodesToAdd) {}
57 
AddRemoveNodesCommand(const Model::NodeList & nodesToRemove)58         AddRemoveNodesCommand::AddRemoveNodesCommand(const Model::NodeList& nodesToRemove) :
59         DocumentCommand(Type, makeName(Action_Remove)),
60         m_action(Action_Remove),
61         m_nodesToRemove(nodesToRemove) {}
62 
addedNodes() const63         const Model::NodeList& AddRemoveNodesCommand::addedNodes() const {
64             return m_nodesToRemove;
65         }
66 
makeName(const Action action)67         String AddRemoveNodesCommand::makeName(const Action action) {
68             switch (action) {
69                 case Action_Add:
70                     return "Add Objects";
71                 case Action_Remove:
72                     return "Remove Objects";
73 				switchDefault()
74             }
75         }
76 
doPerformDo(MapDocumentCommandFacade * document)77         bool AddRemoveNodesCommand::doPerformDo(MapDocumentCommandFacade* document) {
78             switch (m_action) {
79                 case Action_Add:
80                     m_nodesToRemove = document->performAddNodes(m_nodesToAdd);
81                     m_nodesToAdd.clear();
82                     break;
83                 case Action_Remove:
84                     m_nodesToAdd = document->performRemoveNodes(m_nodesToRemove);
85                     m_nodesToRemove.clear();
86                     break;
87             }
88             return true;
89         }
90 
doPerformUndo(MapDocumentCommandFacade * document)91         bool AddRemoveNodesCommand::doPerformUndo(MapDocumentCommandFacade* document) {
92             switch (m_action) {
93                 case Action_Add:
94                     m_nodesToAdd = document->performRemoveNodes(m_nodesToRemove);
95                     m_nodesToRemove.clear();
96                     break;
97                 case Action_Remove:
98                     m_nodesToRemove = document->performAddNodes(m_nodesToAdd);
99                     m_nodesToAdd.clear();
100                     break;
101             }
102             return true;
103         }
104 
doIsRepeatable(MapDocumentCommandFacade * document) const105         bool AddRemoveNodesCommand::doIsRepeatable(MapDocumentCommandFacade* document) const {
106             return false;
107         }
108 
doCollateWith(UndoableCommand::Ptr command)109         bool AddRemoveNodesCommand::doCollateWith(UndoableCommand::Ptr command) {
110             return false;
111         }
112     }
113 }
114