1/***************************************************************************
2 kimecommands.cpp - description
3 -------------------
4 begin : Fri May 25 2001
5 copyright : (C) 2001 by Jan Schäfer
6 email : j_schaef@informatik.uni-kl.de
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17#include <qstring.h>
18#include <kdebug.h>
19#include <klocale.h>
20
21#include "kimagemapeditor.h"
22#include "kimecommands.h"
23#include "drawzone.h"
24
25CutCommand::CutCommand(KImageMapEditor * document, const AreaSelection & a)
26 : QUndoCommand(i18n( "Cut %1", a.typeString() ))
27{
28 _document=document;
29 _cutAreaSelection=new AreaSelection();
30 _cutAreaSelection->setAreaList( a.getAreaList() );
31 _cutted=true;
32}
33
34
35CutCommand::~CutCommand()
36{
37 if (_cutted)
38 {
39 AreaListIterator it(_cutAreaSelection->getAreaList());
40 while (it.hasNext()) {
41 delete it.next();
42 }
43 }
44
45 delete _cutAreaSelection;
46}
47
48void CutCommand::redo()
49{
50 // The Area won't be really delete
51 // it only gets removed from the AreaList
52 _document->deleteArea(_cutAreaSelection );
53 _document->updateActionAccess();
54 _cutted=true;
55}
56
57void CutCommand::undo()
58{
59 if (_document) {
60 _document->addArea( _cutAreaSelection );
61 _document->select( _cutAreaSelection );
62 _document->slotAreaChanged( _cutAreaSelection );
63 _cutted=false;
64 }
65}
66
67DeleteCommand::DeleteCommand(KImageMapEditor * document, const AreaSelection & a)
68 : CutCommand(document,a)
69{
70 setText(i18n( "Delete %1", a.typeString() ));
71}
72
73PasteCommand::PasteCommand(KImageMapEditor *document, const AreaSelection & a)
74 : QUndoCommand(i18n( "Paste %1", a.typeString() ))
75{
76 _document=document;
77 _pasteAreaSelection=new AreaSelection();
78 _pasteAreaSelection->setAreaList( a.getAreaList() );
79 _pasted=true;
80 _wasUndoed=false;
81}
82
83PasteCommand::~PasteCommand ()
84{
85 if ( ! _pasted ) {
86 AreaListIterator it(_pasteAreaSelection->getAreaList());
87 while (it.hasNext()) {
88 delete it.next();
89 }
90 }
91
92 delete _pasteAreaSelection;
93}
94
95void PasteCommand::redo()
96{
97 _document->deselectAll();
98 _document->addArea( _pasteAreaSelection );
99 _document->select( _pasteAreaSelection );
100 _document->slotAreaChanged( _pasteAreaSelection );
101 _pasted=true;
102}
103
104void PasteCommand::undo()
105{
106 _document->deleteArea(_pasteAreaSelection );
107 _pasted=false;
108 _wasUndoed=true;
109}
110
111
112MoveCommand::MoveCommand (KImageMapEditor *document, AreaSelection * a, const QPoint & oldPoint)
113 : QUndoCommand(i18n( "Move %1", a->typeString() ))
114{
115 _document=document;
116 _areaSelection=new AreaSelection();
117 _areaSelection->setAreaList( a->getAreaList() );
118 _oldPoint.setX(oldPoint.x());
119 _oldPoint.setY(oldPoint.y());
120
121 _newPoint.setX(a->rect().left());
122 _newPoint.setY(a->rect().top());
123}
124
125MoveCommand::~MoveCommand () {
126 delete _areaSelection;
127}
128
129void MoveCommand::redo()
130{
131 // only for repainting reasons
132 Area* tempArea = _areaSelection->clone();
133
134 _areaSelection->moveTo( _newPoint.x(), _newPoint.y() );
135
136 if (!_areaSelection->allAreasWithin(_document->getDrawZone()->getImageRect()))
137 _areaSelection->moveTo( _oldPoint.x(), _oldPoint.y() );
138
139 _document->selected()->invalidate();
140
141
142 _document->slotAreaChanged( tempArea );
143 _document->slotAreaChanged( _areaSelection );
144
145 delete tempArea;
146
147}
148
149void MoveCommand::undo()
150{
151 // only to erase the old Area
152 Area* tempArea = _areaSelection->clone();
153
154 _areaSelection->setMoving(true);
155 _areaSelection->moveTo( _oldPoint.x(), _oldPoint.y() );
156 _areaSelection->setMoving(false);
157
158 _document->selected()->invalidate();
159
160 _document->slotAreaChanged( tempArea );
161 _document->slotAreaChanged( _areaSelection );
162
163 delete tempArea;
164
165}
166
167
168ResizeCommand::ResizeCommand (KImageMapEditor *document, AreaSelection *a, Area *oldArea)
169 :QUndoCommand(i18n( "Resize %1", a->typeString() ))
170{
171 _areaSelection=new AreaSelection();
172 _areaSelection->setAreaList( a->getAreaList() );
173
174 _newArea = a->clone();
175 _oldArea = oldArea->clone();
176 _document=document;
177}
178
179ResizeCommand::~ResizeCommand ()
180{
181 delete _newArea;
182 delete _oldArea;
183 delete _areaSelection;
184}
185
186void ResizeCommand::redo()
187{
188 _areaSelection->setArea ( *_newArea);
189 _areaSelection->setMoving(false);
190
191 _document->slotAreaChanged( _areaSelection );
192 _document->slotAreaChanged( _oldArea );
193
194
195}
196
197void ResizeCommand::undo()
198{
199 _areaSelection->setArea ( *_oldArea);
200 _areaSelection->setMoving(false);
201
202 _document->slotAreaChanged( _areaSelection );
203 _document->slotAreaChanged( _newArea );
204
205}
206
207
208
209AddPointCommand::AddPointCommand (KImageMapEditor *document, AreaSelection *a, const QPoint & p)
210 :QUndoCommand(i18n( "Add point to %1", a->typeString() ))
211{
212 if (a->type()!=Area::Polygon)
213 {
214 kDebug() << "trying to add a point to a " << a->typeString();
215 return;
216 }
217
218 _areaSelection=new AreaSelection();
219 _areaSelection->setAreaList( a->getAreaList() );
220
221 _point = p;
222 _document=document;
223}
224
225AddPointCommand::~AddPointCommand ()
226{
227 delete _areaSelection;
228}
229
230void AddPointCommand::redo()
231{
232 _coordpos = _areaSelection->addCoord(_point);
233 _areaSelection->setMoving(false);
234
235 _document->slotAreaChanged( _areaSelection );
236}
237
238void AddPointCommand::undo()
239{
240// QRect *selectionPoint = _areaSelection->onSelectionPoint(_point);
241 Area* repaintArea = _areaSelection->clone();
242
243 _areaSelection->removeCoord(_coordpos);
244 _areaSelection->setMoving(false);
245
246 _document->slotAreaChanged( _areaSelection );
247 _document->slotAreaChanged( repaintArea );
248
249 delete repaintArea;
250}
251
252RemovePointCommand::RemovePointCommand(KImageMapEditor *document,
253 AreaSelection *a, Area *oldArea)
254 : QUndoCommand(i18n( "Remove point from %1", a->typeString() ))
255{
256 if (a->type()!=Area::Polygon)
257 {
258 kDebug() << "trying to remove a point to a " << a->typeString();
259 return;
260 }
261
262 _areaSelection=new AreaSelection();
263 _areaSelection->setAreaList( a->getAreaList() );
264
265 _newArea = a->clone();
266 _oldArea = oldArea->clone();
267 _document=document;
268}
269
270RemovePointCommand::~RemovePointCommand ()
271{
272 delete _newArea;
273 delete _oldArea;
274 delete _areaSelection;
275}
276
277void RemovePointCommand::redo()
278{
279 _areaSelection->setArea ( *_newArea);
280 _areaSelection->setMoving(false);
281
282 _document->slotAreaChanged( _areaSelection );
283 _document->slotAreaChanged( _oldArea );
284
285
286}
287
288void RemovePointCommand::undo()
289{
290 _areaSelection->setArea ( *_oldArea);
291 _areaSelection->setMoving(false);
292
293 _document->slotAreaChanged( _areaSelection );
294 _document->slotAreaChanged( _newArea );
295
296}
297
298
299
300CreateCommand::CreateCommand (KImageMapEditor *document, Area *area)
301 : QUndoCommand(i18n( "Create %1", area->typeString() ))
302{
303 _document=document;
304 _area=area;
305 _created=true;
306 _wasUndoed=false;
307
308}
309
310CreateCommand::~CreateCommand ()
311{
312 if ( ! _created)
313 delete _area;
314}
315
316void CreateCommand::redo()
317{
318 if (_document) {
319
320 if ( _wasUndoed ) {
321 _document->addArea( _area );
322 _document->deselectAll();
323 _document->select( _area );
324 _document->slotAreaChanged( _area );
325 }
326 else
327 _document->addAreaAndEdit( _area );
328
329 _created=true;
330 }
331
332}
333
334void CreateCommand::undo()
335{
336 if (_document) {
337 _document->deleteArea( _area );
338 _created=false;
339 _wasUndoed=true;
340 }
341
342}
343