Debian Codegraph @

src/Gui/MergeDocuments.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #include <stack>
25
26
27 #include <zipios++/zipinputstream.h>
28
29 #include <App/Document.h>
30 #include <App/DocumentObject.h>
31 #include <Base/Reader.h>
32 #include <Base/Writer.h>
33 #include <Gui/Application.h>
34 #include <Gui/Document.h>
35 #include <Gui/ViewProvider.h>
36
37 #include "MergeDocuments.h"
38
39
40 using namespace Gui;
41 namespace sp = std::placeholders;
42
43 namespace Gui
44 {
45
46 class XMLMergeReader: public Base::XMLReader
47 {
48 public:
49 XMLMergeReader(std::map<std::string, std::string>& name, const char* FileName, std::istream& str)
50 : Base::XMLReader(FileName, str)
51 , nameMap(name)
52 {}
53
54 void addName(const char* s1, const char* s2) override
55 {
56 nameMap[s1] = s2;
57 }
58 const char* getName(const char* name) const override
59 {
60 std::map<std::string, std::string>::const_iterator it = nameMap.find(name);
61 if (it != nameMap.end()) {
62 return it->second.c_str();
63 }
64 else {
65 return name;
66 }
67 }
68 bool doNameMapping() const override
69 {
70 return true;
71 }
72
73 protected:
74 private:
75 std::map<std::string, std::string>& nameMap;
76 using PropertyTag = std::pair<std::string, std::string>;
77 std::stack<PropertyTag> propertyStack;
78 };
79 } // namespace Gui
80
81 MergeDocuments::MergeDocuments(App::Document* doc)
82 : appdoc(doc)
83 {
84 // NOLINTBEGIN
85 connectExport = doc->signalExportObjects.connect(
86 std::bind(&MergeDocuments::exportObject, this, sp::_1, sp::_2)
87 );
88 connectImport = doc->signalImportObjects.connect(
89 std::bind(&MergeDocuments::importObject, this, sp::_1, sp::_2)
90 );
91 document = Gui::Application::Instance->getDocument(doc);
92 // NOLINTEND
93 }
94
95 MergeDocuments::~MergeDocuments()
96 {
97 connectExport.disconnect();
98 connectImport.disconnect();
99 }
100
101 unsigned int MergeDocuments::getMemSize() const
102 {
103 return 0;
104 }
105
106 std::vector<App::DocumentObject*> MergeDocuments::importObjects(std::istream& input)
107 {
108 this->nameMap.clear();
109 this->stream = new zipios::ZipInputStream(input);
110 XMLMergeReader reader(this->nameMap, "<memory>", *stream);
111 std::vector<App::DocumentObject*> objs = appdoc->importObjects(reader);
112
113 delete this->stream;
114 this->stream = nullptr;
115
116 return objs;
117 }
118
119 void MergeDocuments::importObject(const std::vector<App::DocumentObject*>& o, Base::XMLReader& r)
120 {
121 objects = o;
122 for (auto it : objects) {
123 Gui::ViewProvider* vp = document->getViewProvider(it);
124 if (vp) {
125 vp->hide();
126 }
127 }
128 Restore(r);
129
130 r.readFiles(*this->stream);
131 }
132
133 void MergeDocuments::exportObject(const std::vector<App::DocumentObject*>& o, Base::Writer& w)
134 {
135 objects = o;
136 Save(w);
137 }
138
139 void MergeDocuments::Save(Base::Writer& w) const
140 {
141 w.addFile("GuiDocument.xml", this);
142 }
143
144 void MergeDocuments::Restore(Base::XMLReader& r)
145 {
146 r.addFile("GuiDocument.xml", this);
147 }
148
149 void MergeDocuments::SaveDocFile(Base::Writer& w) const
150 {
151 document->exportObjects(objects, w);
152 }
153
154 void MergeDocuments::RestoreDocFile(Base::Reader& r)
155 {
156 document->importObjects(objects, r, nameMap);
157 }