File indexing completed on 2024-06-23 05:33:18

0001 /***************************************************************************
0002  *   Copyright (C) 2017 by Renaud Guezennec                                *
0003  *   http://www.rolisteam.org/contact                   *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "setbackgroundimage.h"
0021 #include "addpagecommand.h"
0022 #include "controllers/editorcontroller.h"
0023 
0024 #include <QDebug>
0025 
0026 SetBackgroundCommand::SetBackgroundCommand(int i, EditorController* ctrl, const QUrl& url, QUndoCommand* parent)
0027     : QUndoCommand(parent), m_image(QPixmap(url.toLocalFile())), m_idx(i), m_ctrl(ctrl), m_filename(url.toLocalFile())
0028 {
0029     if(m_image.isNull())
0030     {
0031         qInfo() << QStringLiteral("Oops! Something is wrong with that file: %1 - No image read").arg(url.toLocalFile());
0032         return;
0033     }
0034     init();
0035 }
0036 SetBackgroundCommand::SetBackgroundCommand(int i, EditorController* ctrl, const QPixmap& pix, const QString& fileName,
0037                                            QUndoCommand* parent)
0038     : QUndoCommand(parent), m_image(QPixmap(pix)), m_idx(i), m_ctrl(ctrl), m_filename(fileName)
0039 {
0040     init();
0041 }
0042 
0043 void SetBackgroundCommand::init()
0044 {
0045     if(m_ctrl->pageCount() <= static_cast<unsigned int>(m_idx))
0046     {
0047         m_subCommand= new AddPageCommand(m_ctrl, this);
0048     }
0049 
0050     if(nullptr == m_subCommand)
0051         setText(QObject::tr("Set background on Page #%1").arg(m_idx + 1));
0052     else
0053         setText(QObject::tr("Add Page #%1 and Set background on it").arg(m_idx + 1));
0054 }
0055 
0056 void SetBackgroundCommand::undo()
0057 {
0058     QUndoCommand::undo();
0059 
0060     m_ctrl->setImageBackground(m_idx, QPixmap(), "");
0061 }
0062 
0063 void SetBackgroundCommand::redo()
0064 {
0065     QUndoCommand::redo();
0066     m_ctrl->setImageBackground(m_idx, m_image, m_filename);
0067 }
0068 
0069 AddBackGroundImagesCommand::AddBackGroundImagesCommand(EditorController* ctrl, const QList<QImage>& images,
0070                                                        QUndoCommand* parent)
0071     : m_ctrl(ctrl), m_images(images)
0072 {
0073 
0074     for(int i= m_ctrl->pageCount(); i < images.size(); ++i)
0075     {
0076         m_subCmds << new AddPageCommand(m_ctrl, this);
0077     }
0078 }
0079 
0080 void AddBackGroundImagesCommand::undo()
0081 {
0082     QUndoCommand::undo();
0083     for(const auto &info : m_oldData)
0084         m_ctrl->setImageBackground(info.index, info.oldImg, "");
0085 }
0086 
0087 void AddBackGroundImagesCommand::redo()
0088 {
0089     QUndoCommand::redo();
0090     quint64 i= 0;
0091 
0092     for(const auto& img : m_images)
0093     {
0094         m_ctrl->setImageBackground(i, QPixmap::fromImage(img), "");
0095         m_oldData << OldImageInfo{static_cast<int>(i), m_ctrl->backgroundFromIndex(i)};
0096         ++i;
0097     }
0098 }