1 /*
2  * resizetilelayer.cpp
3  * Copyright 2009, Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
4  *
5  * This file is part of Tiled.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "resizetilelayer.h"
22 
23 #include "layermodel.h"
24 #include "map.h"
25 #include "mapdocument.h"
26 #include "tilelayer.h"
27 
28 #include <QCoreApplication>
29 
30 using namespace Tiled;
31 
ResizeTileLayer(MapDocument * mapDocument,TileLayer * layer,QSize size,QPoint offset,QUndoCommand * parent)32 ResizeTileLayer::ResizeTileLayer(MapDocument *mapDocument,
33                                  TileLayer *layer,
34                                  QSize size,
35                                  QPoint offset,
36                                  QUndoCommand *parent)
37     : QUndoCommand(QCoreApplication::translate("Undo Commands",
38                                                "Resize Layer"),
39                    parent)
40     , mMapDocument(mapDocument)
41     , mDone(false)
42     , mOriginalLayer(layer)
43 {
44     // Create the resized layer (once)
45     mResizedLayer = layer->clone();
46     mResizedLayer->resize(size, offset);
47 }
48 
~ResizeTileLayer()49 ResizeTileLayer::~ResizeTileLayer()
50 {
51     if (mDone)
52         delete mOriginalLayer;
53     else
54         delete mResizedLayer;
55 }
56 
undo()57 void ResizeTileLayer::undo()
58 {
59     Q_ASSERT(mDone);
60     LayerModel *layerModel = mMapDocument->layerModel();
61     layerModel->replaceLayer(mResizedLayer, mOriginalLayer);
62     mDone = false;
63 }
64 
redo()65 void ResizeTileLayer::redo()
66 {
67     Q_ASSERT(!mDone);
68     LayerModel *layerModel = mMapDocument->layerModel();
69     layerModel->replaceLayer(mOriginalLayer, mResizedLayer);
70     mDone = true;
71 }
72