Changeset View
Changeset View
Standalone View
Standalone View
karbon/ui/KarbonDocumentMergeCommand.cpp
Show All 14 Lines | |||||
15 | * along with this library; see the file COPYING.LIB. If not, write to | 15 | * along with this library; see the file COPYING.LIB. If not, write to | ||
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
17 | * Boston, MA 02110-1301, USA. | 17 | * Boston, MA 02110-1301, USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "KarbonDocumentMergeCommand.h" | 20 | #include "KarbonDocumentMergeCommand.h" | ||
21 | #include "KarbonPart.h" | 21 | #include "KarbonPart.h" | ||
22 | #include "KarbonDocument.h" | 22 | #include "KarbonDocument.h" | ||
23 | | ||||
24 | #include <KoShape.h> | ||||
23 | #include "KoShapeLayer.h" | 25 | #include "KoShapeLayer.h" | ||
26 | #include <KoPAPageBase.h> | ||||
27 | #include <KoPAPage.h> | ||||
28 | #include <KoPAMasterPage.h> | ||||
29 | | ||||
24 | #include <klocalizedstring.h> | 30 | #include <klocalizedstring.h> | ||
25 | 31 | | |||
26 | class KarbonDocumentMergeCommand::Private | 32 | class MergePageCommand : public KUndo2Command | ||
27 | { | 33 | { | ||
28 | public: | 34 | public: | ||
29 | Private() : hasMerged(false) | 35 | MergePageCommand(KoPADocument *doc, KoPAPageBase *targetPage, KoPAPageBase *sourcePage, KUndo2Command *parent) | ||
36 | : KUndo2Command(parent) | ||||
37 | , mine(true) | ||||
38 | , doc(doc) | ||||
39 | , targetPage(targetPage) | ||||
30 | { | 40 | { | ||
41 | layers = sourcePage->shapes(); | ||||
42 | sourcePage->removeAllShapes(); | ||||
31 | } | 43 | } | ||
32 | 44 | ~MergePageCommand() { | |||
33 | ~Private() | 45 | if (mine) { | ||
34 | { | | |||
35 | if (!hasMerged) { | | |||
36 | qDeleteAll(layers); | 46 | qDeleteAll(layers); | ||
37 | qDeleteAll(shapes); | | |||
38 | } | 47 | } | ||
39 | } | 48 | } | ||
40 | 49 | void redo() { | |||
41 | KarbonDocument * targetPart; | 50 | for (int i = 0; i < layers.count(); ++i) { | ||
42 | QList<KoShapeLayer*> layers; | 51 | targetPage->addShape(layers.at(i)); | ||
43 | QList<KoShape*> shapes; | 52 | } | ||
44 | bool hasMerged; | 53 | mine = false; | ||
54 | doc->emitUpdate(targetPage); | ||||
55 | } | ||||
56 | void undo() { | ||||
57 | for (int i = 0; i < layers.count(); ++i) { | ||||
58 | targetPage->removeShape(layers.at(i)); | ||||
59 | } | ||||
60 | mine = true; | ||||
61 | doc->emitUpdate(targetPage); | ||||
62 | } | ||||
63 | | ||||
64 | private: | ||||
65 | bool mine; | ||||
66 | KoPADocument *doc; | ||||
67 | KoPAPageBase *targetPage; | ||||
68 | QList<KoShape*> layers; | ||||
45 | }; | 69 | }; | ||
46 | 70 | | |||
47 | KarbonDocumentMergeCommand::KarbonDocumentMergeCommand(KarbonDocument *targetPart, KarbonDocument *sourcePart) | 71 | class AddPageCommand : public KUndo2Command | ||
48 | : KUndo2Command(0), d(new Private()) | | |||
49 | { | 72 | { | ||
50 | d->targetPart = targetPart; | 73 | public: | ||
51 | d->layers = sourcePart->layers(); | 74 | AddPageCommand(KarbonDocument *doc, KoPAPageBase *sourcePage, KUndo2Command *parent) | ||
52 | d->shapes = sourcePart->shapes(); | 75 | : KUndo2Command(parent) | ||
53 | foreach(KoShapeLayer * layer, d->layers) { | 76 | , mine(true) | ||
54 | sourcePart->removeShape(layer); | 77 | , doc(doc) | ||
78 | { | ||||
79 | newPage = doc->newPage(dynamic_cast<KoPAMasterPage*>(doc->pages(true).value(0))); | ||||
80 | QList<KoShape*> layers = sourcePage->shapes(); | ||||
81 | sourcePage->removeAllShapes(); | ||||
82 | for (int i = 0; i < layers.count(); ++i) { | ||||
83 | newPage->addShape(layers.at(i)); | ||||
55 | } | 84 | } | ||
56 | foreach(KoShape * shape, d->shapes) { | | |||
57 | sourcePart->removeShape(shape); | | |||
58 | } | 85 | } | ||
59 | setText(kundo2_i18n("Insert graphics")); | 86 | ~AddPageCommand() { | ||
87 | if (mine) { | ||||
88 | delete newPage; | ||||
60 | } | 89 | } | ||
61 | 90 | } | |||
62 | KarbonDocumentMergeCommand::~KarbonDocumentMergeCommand() | 91 | void redo() { | ||
63 | { | 92 | doc->insertPage(newPage, doc->pages().count()); | ||
64 | delete d; | 93 | mine = false; | ||
94 | } | ||||
95 | void undo() { | ||||
96 | doc->takePage(newPage); | ||||
97 | mine = true; | ||||
65 | } | 98 | } | ||
66 | 99 | | |||
67 | void KarbonDocumentMergeCommand::redo() | 100 | private: | ||
101 | bool mine; | ||||
102 | KarbonDocument *doc; | ||||
103 | KoPAPageBase *newPage; | ||||
104 | }; | ||||
105 | | ||||
106 | KarbonDocumentMergeCommand::KarbonDocumentMergeCommand(KarbonDocument *targetPart, KarbonDocument &sourcePart, KUndo2Command *parent) | ||||
107 | : KUndo2Command(parent) | ||||
68 | { | 108 | { | ||
69 | if (!d->hasMerged) { | 109 | QList<KoPAPageBase*> pages; | ||
70 | foreach(KoShapeLayer * layer, d->layers) { | 110 | for(int i = 0; i < sourcePart.pages().count(); ++i) { | ||
71 | d->targetPart->addShape(layer); | 111 | KoPAPageBase *sourcePage = sourcePart.pages().at(i); | ||
112 | pages << sourcePage; | ||||
113 | if (i < targetPart->pages().count()) { | ||||
114 | KoPAPageBase *targetPage = targetPart->pages().at(i); | ||||
115 | new MergePageCommand(targetPart, targetPage, sourcePage, this); | ||||
116 | } else { | ||||
117 | new AddPageCommand(targetPart, sourcePage, this); | ||||
72 | } | 118 | } | ||
73 | foreach(KoShape * shape, d->shapes) { | | |||
74 | d->targetPart->addShape(shape); | | |||
75 | } | 119 | } | ||
76 | d->hasMerged = true; | 120 | setText(kundo2_i18n("Insert graphics")); | ||
77 | } | 121 | } | ||
78 | 122 | | |||
123 | void KarbonDocumentMergeCommand::redo() | ||||
124 | { | ||||
79 | KUndo2Command::redo(); | 125 | KUndo2Command::redo(); | ||
80 | } | 126 | } | ||
81 | 127 | | |||
82 | void KarbonDocumentMergeCommand::undo() | 128 | void KarbonDocumentMergeCommand::undo() | ||
83 | { | 129 | { | ||
84 | KUndo2Command::undo(); | 130 | KUndo2Command::undo(); | ||
85 | | ||||
86 | if (d->hasMerged) { | | |||
87 | foreach(KoShapeLayer * layer, d->layers) { | | |||
88 | d->targetPart->removeShape(layer); | | |||
89 | } | | |||
90 | foreach(KoShape * shape, d->shapes) { | | |||
91 | d->targetPart->removeShape(shape); | | |||
92 | } | | |||
93 | d->hasMerged = false; | | |||
94 | } | | |||
95 | } | 131 | } |