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 "ChangeEntityAttributesCommand.h" 21 22 #include "Macros.h" 23 #include "View/MapDocument.h" 24 #include "View/MapDocumentCommandFacade.h" 25 26 namespace TrenchBroom { 27 namespace View { 28 const Command::CommandType ChangeEntityAttributesCommand::Type = Command::freeType(); 29 set(const Model::AttributeName & name,const Model::AttributeValue & value)30 ChangeEntityAttributesCommand::Ptr ChangeEntityAttributesCommand::set(const Model::AttributeName& name, const Model::AttributeValue& value) { 31 Ptr command(new ChangeEntityAttributesCommand(Action_Set)); 32 command->setName(name); 33 command->setNewValue(value); 34 return command; 35 } 36 remove(const Model::AttributeName & name)37 ChangeEntityAttributesCommand::Ptr ChangeEntityAttributesCommand::remove(const Model::AttributeName& name) { 38 Ptr command(new ChangeEntityAttributesCommand(Action_Remove)); 39 command->setName(name); 40 return command; 41 } 42 rename(const Model::AttributeName & oldName,const Model::AttributeName & newName)43 ChangeEntityAttributesCommand::Ptr ChangeEntityAttributesCommand::rename(const Model::AttributeName& oldName, const Model::AttributeName& newName) { 44 Ptr command(new ChangeEntityAttributesCommand(Action_Rename)); 45 command->setName(oldName); 46 command->setNewName(newName); 47 return command; 48 } 49 setName(const Model::AttributeName & name)50 void ChangeEntityAttributesCommand::setName(const Model::AttributeName& name) { 51 m_oldName = name; 52 } 53 setNewName(const Model::AttributeName & newName)54 void ChangeEntityAttributesCommand::setNewName(const Model::AttributeName& newName) { 55 assert(m_action == Action_Rename); 56 m_newName = newName; 57 } 58 setNewValue(const Model::AttributeValue & newValue)59 void ChangeEntityAttributesCommand::setNewValue(const Model::AttributeValue& newValue) { 60 assert(m_action == Action_Set); 61 m_newValue = newValue; 62 } 63 ChangeEntityAttributesCommand(const Action action)64 ChangeEntityAttributesCommand::ChangeEntityAttributesCommand(const Action action) : 65 DocumentCommand(Type, makeName(action)), 66 m_action(action) {} 67 makeName(const Action action)68 String ChangeEntityAttributesCommand::makeName(const Action action) { 69 switch (action) { 70 case Action_Set: 71 return "Set Property"; 72 case Action_Remove: 73 return "Remove Property"; 74 case Action_Rename: 75 return "Rename Property"; 76 switchDefault() 77 } 78 } 79 doPerformDo(MapDocumentCommandFacade * document)80 bool ChangeEntityAttributesCommand::doPerformDo(MapDocumentCommandFacade* document) { 81 switch (m_action) { 82 case Action_Set: 83 m_snapshots = document->performSetAttribute(m_oldName, m_newValue); 84 break; 85 case Action_Remove: 86 m_snapshots = document->performRemoveAttribute(m_oldName); 87 break; 88 case Action_Rename: 89 document->performRenameAttribute(m_oldName, m_newName); 90 break; 91 }; 92 return true; 93 } 94 doPerformUndo(MapDocumentCommandFacade * document)95 bool ChangeEntityAttributesCommand::doPerformUndo(MapDocumentCommandFacade* document) { 96 switch (m_action) { 97 case Action_Set: 98 case Action_Remove: 99 document->restoreAttributes(m_snapshots); 100 m_snapshots.clear(); 101 break; 102 case Action_Rename: 103 document->performRenameAttribute(m_newName, m_oldName); 104 break; 105 }; 106 return true; 107 } 108 doIsRepeatable(MapDocumentCommandFacade * document) const109 bool ChangeEntityAttributesCommand::doIsRepeatable(MapDocumentCommandFacade* document) const { 110 return false; 111 } 112 doCollateWith(UndoableCommand::Ptr command)113 bool ChangeEntityAttributesCommand::doCollateWith(UndoableCommand::Ptr command) { 114 ChangeEntityAttributesCommand* other = static_cast<ChangeEntityAttributesCommand*>(command.get()); 115 if (other->m_action != m_action) 116 return false; 117 if (other->m_oldName != m_oldName) 118 return false; 119 m_newName = other->m_newName; 120 m_newValue = other->m_newValue; 121 return true; 122 } 123 } 124 } 125