File indexing completed on 2024-05-12 16:34:22

0001 /* This file is part of the KDE project
0002    Copyright 2009 Thorsten Zachmann <zachmann@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 #include "ChangeImageCommand.h"
0020 
0021 #include <cmath>
0022 
0023 #include <klocalizedstring.h>
0024 
0025 #include <KoImageData.h>
0026 
0027 #include "PictureDebug.h"
0028 
0029 ChangeImageCommand::ChangeImageCommand(PictureShape *shape, KoImageData *newImageData, KUndo2Command *parent):
0030     KUndo2Command(parent),
0031     m_imageChanged(true),
0032     m_shape(shape),
0033     m_newImageData(newImageData),
0034     m_oldCroppingRect(shape->cropRect()),
0035     m_newCroppingRect(0, 0, 1, 1),
0036     m_oldColorMode(shape->colorMode()),
0037     m_newColorMode(shape->colorMode())
0038 {
0039     setText(kundo2_i18n("Change image"));
0040 
0041     // we need new here as setUserData deletes the old data
0042     m_oldImageData = m_shape->imageData() ? new KoImageData(*m_shape->imageData()): 0;
0043 }
0044 
0045 ChangeImageCommand::ChangeImageCommand(PictureShape *shape, const QRectF &croppingRect, KUndo2Command *parent):
0046     KUndo2Command(parent),
0047     m_imageChanged(false),
0048     m_shape(shape),
0049     m_oldImageData(0),
0050     m_newImageData(0),
0051     m_oldCroppingRect(shape->cropRect()),
0052     m_newCroppingRect(croppingRect),
0053     m_oldColorMode(shape->colorMode()),
0054     m_newColorMode(shape->colorMode())
0055 {
0056     setText(kundo2_i18n("Crop image"));
0057 }
0058 
0059 ChangeImageCommand::ChangeImageCommand(PictureShape *shape, PictureShape::ColorMode colorMode, KUndo2Command *parent):
0060     KUndo2Command(parent),
0061     m_imageChanged(false),
0062     m_shape(shape),
0063     m_oldImageData(0),
0064     m_newImageData(0),
0065     m_oldCroppingRect(shape->cropRect()),
0066     m_newCroppingRect(shape->cropRect()),
0067     m_oldColorMode(shape->colorMode()),
0068     m_newColorMode(colorMode)
0069 {
0070     setText(kundo2_i18n("Change image color mode"));
0071 }
0072 
0073 ChangeImageCommand::~ChangeImageCommand()
0074 {
0075     delete m_oldImageData;
0076     delete m_newImageData;
0077 }
0078 
0079 void ChangeImageCommand::redo()
0080 {
0081     if (m_imageChanged) {
0082         // we need new here as setUserData deletes the old data
0083         m_shape->setUserData(m_newImageData ? new KoImageData(*m_newImageData): 0);
0084     }
0085 
0086     m_shape->setColorMode(m_newColorMode);
0087     m_shape->setCropRect(m_newCroppingRect);
0088     emit sigExecuted();
0089 }
0090 
0091 void ChangeImageCommand::undo()
0092 {
0093     if (m_imageChanged) {
0094         // we need new here as setUserData deletes the old data
0095         m_shape->setUserData(m_oldImageData ? new KoImageData(*m_oldImageData): 0);
0096     }
0097 
0098     m_shape->setColorMode(m_oldColorMode);
0099     m_shape->setCropRect(m_oldCroppingRect);
0100     emit sigExecuted();
0101 }