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 "ChangeBrushFaceAttributesCommand.h"
21 
22 #include "Model/BrushFace.h"
23 #include "Model/Snapshot.h"
24 #include "View/MapDocumentCommandFacade.h"
25 
26 namespace TrenchBroom {
27     namespace View {
28         const Command::CommandType ChangeBrushFaceAttributesCommand::Type = Command::freeType();
29 
command(const Model::ChangeBrushFaceAttributesRequest & request)30         ChangeBrushFaceAttributesCommand::Ptr ChangeBrushFaceAttributesCommand::command(const Model::ChangeBrushFaceAttributesRequest& request) {
31             return Ptr(new ChangeBrushFaceAttributesCommand(request));
32         }
33 
ChangeBrushFaceAttributesCommand(const Model::ChangeBrushFaceAttributesRequest & request)34         ChangeBrushFaceAttributesCommand::ChangeBrushFaceAttributesCommand(const Model::ChangeBrushFaceAttributesRequest& request) :
35         DocumentCommand(Type, request.name()),
36         m_request(request),
37         m_snapshot(NULL) {}
38 
~ChangeBrushFaceAttributesCommand()39         ChangeBrushFaceAttributesCommand::~ChangeBrushFaceAttributesCommand() {
40             delete m_snapshot;
41             m_snapshot = NULL;
42         }
43 
doPerformDo(MapDocumentCommandFacade * document)44         bool ChangeBrushFaceAttributesCommand::doPerformDo(MapDocumentCommandFacade* document) {
45             const Model::BrushFaceList faces = document->allSelectedBrushFaces();
46             assert(!faces.empty());
47 
48             assert(m_snapshot == NULL);
49             m_snapshot = new Model::Snapshot(faces.begin(), faces.end());
50 
51             document->performChangeBrushFaceAttributes(m_request);
52             return true;
53         }
54 
doPerformUndo(MapDocumentCommandFacade * document)55         bool ChangeBrushFaceAttributesCommand::doPerformUndo(MapDocumentCommandFacade* document) {
56             document->restoreSnapshot(m_snapshot);
57             delete m_snapshot;
58             m_snapshot = NULL;
59             return true;
60         }
61 
doIsRepeatable(MapDocumentCommandFacade * document) const62         bool ChangeBrushFaceAttributesCommand::doIsRepeatable(MapDocumentCommandFacade* document) const {
63             return document->hasSelectedBrushFaces();
64         }
65 
doRepeat(MapDocumentCommandFacade * document) const66         UndoableCommand::Ptr ChangeBrushFaceAttributesCommand::doRepeat(MapDocumentCommandFacade* document) const {
67             return UndoableCommand::Ptr(new ChangeBrushFaceAttributesCommand(*this));
68         }
69 
doCollateWith(UndoableCommand::Ptr command)70         bool ChangeBrushFaceAttributesCommand::doCollateWith(UndoableCommand::Ptr command) {
71             ChangeBrushFaceAttributesCommand* other = static_cast<ChangeBrushFaceAttributesCommand*>(command.get());
72             return m_request.collateWith(other->m_request);
73         }
74     }
75 }
76