File indexing completed on 2024-05-12 16:30:48

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2009 Jan Hambrecht <jaham@gmx.net>
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 
0020 #include "KarbonDocumentMergeCommand.h"
0021 #include "KarbonPart.h"
0022 #include "KarbonDocument.h"
0023 
0024 #include <KoShape.h>
0025 #include "KoShapeLayer.h"
0026 #include <KoPAPageBase.h>
0027 #include <KoPAPage.h>
0028 #include <KoPAMasterPage.h>
0029 
0030 #include <klocalizedstring.h>
0031 
0032 class MergePageCommand : public KUndo2Command
0033 {
0034 public:
0035     MergePageCommand(KoPADocument *doc, KoPAPageBase *targetPage, KoPAPageBase *sourcePage, KUndo2Command *parent)
0036     : KUndo2Command(parent)
0037     , mine(true)
0038     , doc(doc)
0039     , targetPage(targetPage)
0040     {
0041         layers = sourcePage->shapes();
0042         sourcePage->removeAllShapes();
0043     }
0044     ~MergePageCommand() override {
0045         if (mine) {
0046             qDeleteAll(layers);
0047         }
0048     }
0049     void redo() override {
0050         for (int i = 0; i < layers.count(); ++i) {
0051             targetPage->addShape(layers.at(i));
0052         }
0053         mine = false;
0054         doc->emitUpdate(targetPage);
0055     }
0056     void undo() override {
0057         for (int i = 0; i < layers.count(); ++i) {
0058             targetPage->removeShape(layers.at(i));
0059         }
0060         mine = true;
0061         doc->emitUpdate(targetPage);
0062     }
0063 
0064 private:
0065     bool mine;
0066     KoPADocument *doc;
0067     KoPAPageBase *targetPage;
0068     QList<KoShape*> layers;
0069 };
0070 
0071 class AddPageCommand : public KUndo2Command
0072 {
0073 public:
0074     AddPageCommand(KarbonDocument *doc, KoPAPageBase *sourcePage, KUndo2Command *parent)
0075     : KUndo2Command(parent)
0076     , mine(true)
0077     , doc(doc)
0078     {
0079         newPage = doc->newPage(dynamic_cast<KoPAMasterPage*>(doc->pages(true).value(0)));
0080         QList<KoShape*> layers = sourcePage->shapes();
0081         sourcePage->removeAllShapes();
0082         for (int i = 0; i < layers.count(); ++i) {
0083             newPage->addShape(layers.at(i));
0084         }
0085     }
0086     ~AddPageCommand() override {
0087         if (mine) {
0088             delete newPage;
0089         }
0090     }
0091     void redo() override {
0092         doc->insertPage(newPage, doc->pages().count());
0093         mine = false;
0094     }
0095     void undo() override {
0096         doc->takePage(newPage);
0097         mine = true;
0098     }
0099 
0100 private:
0101     bool mine;
0102     KarbonDocument *doc;
0103     KoPAPageBase *newPage;
0104 };
0105 
0106 KarbonDocumentMergeCommand::KarbonDocumentMergeCommand(KarbonDocument *targetPart, KarbonDocument &sourcePart, KUndo2Command *parent)
0107     : KUndo2Command(parent)
0108 {
0109     QList<KoPAPageBase*> pages;
0110     for(int i = 0; i < sourcePart.pages().count(); ++i) {
0111         KoPAPageBase *sourcePage = sourcePart.pages().at(i);
0112         pages << sourcePage;
0113         if (i < targetPart->pages().count()) {
0114             KoPAPageBase *targetPage = targetPart->pages().at(i);
0115             new MergePageCommand(targetPart, targetPage, sourcePage, this);
0116         } else {
0117             new AddPageCommand(targetPart, sourcePage, this);
0118         }
0119     }
0120     setText(kundo2_i18n("Insert graphics"));
0121 }
0122 
0123 void KarbonDocumentMergeCommand::redo()
0124 {
0125     KUndo2Command::redo();
0126 }
0127 
0128 void KarbonDocumentMergeCommand::undo()
0129 {
0130     KUndo2Command::undo();
0131 }