File indexing completed on 2024-10-13 03:57:39

0001 /***************************************************************************
0002                           kimecommands.cpp  -  description
0003                              -------------------
0004     begin                : Fri May 25 2001
0005     copyright            : (C) 2001 by Jan Schäfer
0006     email                : j_schaef@informatik.uni-kl.de
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 #include "kimecommands.h"
0018 
0019 #include <QString>
0020 #include "kimagemapeditor_debug.h"
0021 #include <KLocalizedString>
0022 
0023 #include "drawzone.h"
0024 
0025 CutCommand::CutCommand(KImageMapEditor * document, const AreaSelection & a)
0026   : QUndoCommand(i18n( "Cut %1", a.typeString() ))
0027 {
0028     _document=document;
0029     _cutAreaSelection=new AreaSelection();
0030     _cutAreaSelection->setAreaList( a.getAreaList() );
0031     _cutted=true;
0032 }
0033 
0034 
0035 CutCommand::~CutCommand()
0036 {
0037     if (_cutted)
0038     {
0039         AreaListIterator it(_cutAreaSelection->getAreaList());
0040     while (it.hasNext()) {
0041       delete it.next();
0042     }
0043     }
0044 
0045     delete _cutAreaSelection;
0046 }
0047 
0048 void CutCommand::redo()
0049 {
0050     // The Area won't be really delete
0051     // it only gets removed from the AreaList
0052     _document->deleteArea(_cutAreaSelection );
0053   _document->updateActionAccess();
0054     _cutted=true;
0055 }
0056 
0057 void CutCommand::undo()
0058 {
0059     if (_document) {
0060         _document->addArea( _cutAreaSelection );
0061         _document->select( _cutAreaSelection );
0062         _document->slotAreaChanged( _cutAreaSelection );
0063         _cutted=false;
0064     }
0065 }
0066 
0067 DeleteCommand::DeleteCommand(KImageMapEditor * document, const AreaSelection & a)
0068     : CutCommand(document,a)
0069 {
0070     setText(i18n( "Delete %1", a.typeString() ));
0071 }
0072 
0073 PasteCommand::PasteCommand(KImageMapEditor *document, const AreaSelection & a)
0074     : QUndoCommand(i18n( "Paste %1", a.typeString() ))
0075 {
0076     _document=document;
0077     _pasteAreaSelection=new AreaSelection();
0078     _pasteAreaSelection->setAreaList( a.getAreaList() );
0079     _pasted=true;
0080     _wasUndoed=false;
0081 }
0082 
0083 PasteCommand::~PasteCommand ()
0084 {
0085     if ( ! _pasted ) {
0086         AreaListIterator it(_pasteAreaSelection->getAreaList());
0087     while (it.hasNext()) {
0088         delete it.next();
0089     }
0090     }
0091 
0092     delete _pasteAreaSelection;
0093 }
0094 
0095 void PasteCommand::redo()
0096 {
0097     _document->deselectAll();
0098     _document->addArea( _pasteAreaSelection );
0099     _document->select( _pasteAreaSelection );
0100     _document->slotAreaChanged( _pasteAreaSelection );
0101     _pasted=true;
0102 }
0103 
0104 void PasteCommand::undo()
0105 {
0106     _document->deleteArea(_pasteAreaSelection );
0107     _pasted=false;
0108     _wasUndoed=true;
0109 }
0110 
0111 
0112 MoveCommand::MoveCommand (KImageMapEditor *document, AreaSelection * a, const QPoint & oldPoint)
0113     : QUndoCommand(i18n( "Move %1", a->typeString() ))
0114 {
0115     _document=document;
0116     _areaSelection=new AreaSelection();
0117     _areaSelection->setAreaList( a->getAreaList() );
0118     _oldPoint.setX(oldPoint.x());
0119     _oldPoint.setY(oldPoint.y());
0120 
0121     _newPoint.setX(a->rect().left());
0122     _newPoint.setY(a->rect().top());
0123 }
0124 
0125 MoveCommand::~MoveCommand () {
0126     delete _areaSelection;
0127 }
0128 
0129 void MoveCommand::redo()
0130 {
0131     // only for repainting reasons
0132     Area* tempArea = _areaSelection->clone();
0133 
0134     _areaSelection->moveTo( _newPoint.x(), _newPoint.y() );
0135 
0136   if (!_areaSelection->allAreasWithin(_document->getDrawZone()->getImageRect()))
0137     _areaSelection->moveTo( _oldPoint.x(), _oldPoint.y() );
0138 
0139   _document->selected()->invalidate();
0140 
0141 
0142     _document->slotAreaChanged( tempArea );
0143     _document->slotAreaChanged( _areaSelection );
0144 
0145     delete tempArea;
0146 
0147 }
0148 
0149 void MoveCommand::undo()
0150 {
0151   // only to erase the old Area
0152   Area* tempArea = _areaSelection->clone();
0153 
0154   _areaSelection->setMoving(true);
0155     _areaSelection->moveTo( _oldPoint.x(), _oldPoint.y() );
0156   _areaSelection->setMoving(false);
0157 
0158   _document->selected()->invalidate();
0159 
0160     _document->slotAreaChanged( tempArea );
0161     _document->slotAreaChanged( _areaSelection );
0162 
0163     delete tempArea;
0164 
0165 }
0166 
0167 
0168 ResizeCommand::ResizeCommand (KImageMapEditor *document, AreaSelection *a, Area *oldArea)
0169     :QUndoCommand(i18n( "Resize %1", a->typeString() ))
0170 {
0171     _areaSelection=new AreaSelection();
0172     _areaSelection->setAreaList( a->getAreaList() );
0173 
0174     _newArea = a->clone();
0175     _oldArea = oldArea->clone();
0176     _document=document;
0177 }
0178 
0179 ResizeCommand::~ResizeCommand ()
0180 {
0181     delete _newArea;
0182     delete _oldArea;
0183     delete _areaSelection;
0184 }
0185 
0186 void ResizeCommand::redo()
0187 {
0188   _areaSelection->setArea ( *_newArea);
0189   _areaSelection->setMoving(false);
0190 
0191     _document->slotAreaChanged( _areaSelection );
0192     _document->slotAreaChanged( _oldArea );
0193 
0194 
0195 }
0196 
0197 void ResizeCommand::undo()
0198 {
0199   _areaSelection->setArea ( *_oldArea);
0200   _areaSelection->setMoving(false);
0201 
0202     _document->slotAreaChanged( _areaSelection );
0203     _document->slotAreaChanged( _newArea );
0204 
0205 }
0206 
0207 
0208 
0209 AddPointCommand::AddPointCommand (KImageMapEditor *document, AreaSelection *a, const QPoint & p)
0210     :QUndoCommand(i18n( "Add point to %1", a->typeString() ))
0211 {
0212   if (a->type()!=Area::Polygon)
0213   {
0214      qCDebug(KIMAGEMAPEDITOR_LOG) << "trying to add a point to a " << a->typeString();
0215      return;
0216   }
0217 
0218     _areaSelection=new AreaSelection();
0219     _areaSelection->setAreaList( a->getAreaList() );
0220 
0221     _point = p;
0222     _document=document;
0223 }
0224 
0225 AddPointCommand::~AddPointCommand ()
0226 {
0227     delete _areaSelection;
0228 }
0229 
0230 void AddPointCommand::redo()
0231 {
0232   _coordpos = _areaSelection->addCoord(_point);
0233   _areaSelection->setMoving(false);
0234 
0235     _document->slotAreaChanged( _areaSelection );
0236 }
0237 
0238 void AddPointCommand::undo()
0239 {
0240 //  QRect *selectionPoint = _areaSelection->onSelectionPoint(_point);
0241   Area* repaintArea = _areaSelection->clone();
0242 
0243   _areaSelection->removeCoord(_coordpos);
0244   _areaSelection->setMoving(false);
0245 
0246     _document->slotAreaChanged( _areaSelection );
0247     _document->slotAreaChanged( repaintArea );
0248 
0249   delete repaintArea;
0250 }
0251 
0252 RemovePointCommand::RemovePointCommand(KImageMapEditor *document, 
0253        AreaSelection *a, Area *oldArea)
0254   : QUndoCommand(i18n( "Remove point from %1", a->typeString() ))
0255 {
0256   if (a->type()!=Area::Polygon)
0257   {
0258      qCDebug(KIMAGEMAPEDITOR_LOG) << "trying to remove a point to a " << a->typeString();
0259      return;
0260   }
0261 
0262     _areaSelection=new AreaSelection();
0263     _areaSelection->setAreaList( a->getAreaList() );
0264 
0265     _newArea = a->clone();
0266     _oldArea = oldArea->clone();
0267     _document=document;
0268 }
0269 
0270 RemovePointCommand::~RemovePointCommand ()
0271 {
0272     delete _newArea;
0273     delete _oldArea;
0274     delete _areaSelection;
0275 }
0276 
0277 void RemovePointCommand::redo()
0278 {
0279   _areaSelection->setArea ( *_newArea);
0280   _areaSelection->setMoving(false);
0281 
0282     _document->slotAreaChanged( _areaSelection );
0283     _document->slotAreaChanged( _oldArea );
0284 
0285 
0286 }
0287 
0288 void RemovePointCommand::undo()
0289 {
0290   _areaSelection->setArea ( *_oldArea);
0291   _areaSelection->setMoving(false);
0292 
0293     _document->slotAreaChanged( _areaSelection );
0294     _document->slotAreaChanged( _newArea );
0295 
0296 }
0297 
0298 
0299 
0300 CreateCommand::CreateCommand (KImageMapEditor *document, Area *area)
0301     : QUndoCommand(i18n( "Create %1", area->typeString() ))
0302 {
0303     _document=document;
0304     _area=area;
0305     _created=true;
0306     _wasUndoed=false;
0307 
0308 }
0309 
0310 CreateCommand::~CreateCommand ()
0311 {
0312     if ( ! _created)
0313         delete _area;
0314 }
0315 
0316 void CreateCommand::redo()
0317 {
0318     if (_document) {
0319 
0320         if ( _wasUndoed ) {
0321             _document->addArea( _area );
0322             _document->deselectAll();
0323             _document->select( _area );
0324             _document->slotAreaChanged( _area );
0325         }
0326         else
0327             _document->addAreaAndEdit( _area );
0328 
0329         _created=true;
0330     }
0331 
0332 }
0333 
0334 void CreateCommand::undo()
0335 {
0336     if (_document) {
0337         _document->deleteArea( _area );
0338         _created=false;
0339         _wasUndoed=true;
0340     }
0341 
0342 }