FreeCAD

Форк
0
/
MergeDocuments.cpp 
155 строк · 4.6 Кб
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
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
# include <stack>
26
#endif
27

28
#include <QCoreApplication>
29
#include <App/Document.h>
30
#include <Base/Reader.h>
31
#include <Base/Writer.h>
32

33
#include "MergeDocuments.h"
34

35

36
using namespace App;
37
namespace sp = std::placeholders;
38

39
namespace App {
40

41
class XMLMergeReader : public Base::XMLReader
42
{
43
public:
44
    XMLMergeReader(std::map<std::string, std::string>& name, const char* FileName, std::istream& str)
45
      : Base::XMLReader(FileName, str), nameMap(name)
46
    {}
47

48
    void addName(const char* s1, const char* s2) override
49
    {
50
        nameMap[s1] = s2;
51
    }
52
    const char* getName(const char* name) const override
53
    {
54
        std::map<std::string, std::string>::const_iterator it = nameMap.find(name);
55
        if (it != nameMap.end())
56
            return it->second.c_str();
57
        else
58
            return name;
59
    }
60
    bool doNameMapping() const override
61
    {
62
        return true;
63
    }
64
protected:
65

66

67
private:
68
    std::map<std::string, std::string>& nameMap;
69
    using PropertyTag = std::pair<std::string, std::string>;
70
    std::stack<PropertyTag> propertyStack;
71
};
72
}
73

74
MergeDocuments::MergeDocuments(App::Document* doc)
75
    : appdoc(doc)
76
{
77
    //NOLINTBEGIN
78
    connectExport = doc->signalExportObjects.connect
79
        (std::bind(&MergeDocuments::exportObject, this, sp::_1, sp::_2));
80
    connectImport = doc->signalImportObjects.connect
81
        (std::bind(&MergeDocuments::importObject, this, sp::_1, sp::_2));
82
    //NOLINTEND
83

84
    QCoreApplication* app = QCoreApplication::instance();
85
    if (app && app->inherits("QApplication")) {
86
        guiup = true;
87
    }
88
}
89

90
MergeDocuments::~MergeDocuments()
91
{
92
    connectExport.disconnect();
93
    connectImport.disconnect();
94
}
95

96
unsigned int MergeDocuments::getMemSize () const
97
{
98
    return 0;
99
}
100

101
std::vector<App::DocumentObject*>
102
MergeDocuments::importObjects(std::istream& input)
103
{
104
    this->nameMap.clear();
105
    this->stream = new zipios::ZipInputStream(input);
106
    XMLMergeReader reader(this->nameMap,"<memory>", *stream);
107
    reader.setVerbose(isVerbose());
108
    std::vector<App::DocumentObject*> objs = appdoc->importObjects(reader);
109

110
    delete this->stream;
111
    this->stream = nullptr;
112

113
    return objs;
114
}
115

116
void MergeDocuments::importObject(const std::vector<App::DocumentObject*>& o, Base::XMLReader & r)
117
{
118
    objects = o;
119
    Restore(r);
120
    r.readFiles(*this->stream);
121
}
122

123
void MergeDocuments::exportObject(const std::vector<App::DocumentObject*>& o, Base::Writer & w)
124
{
125
    objects = o;
126
    Save(w);
127
}
128

129
void MergeDocuments::Save (Base::Writer & w) const
130
{
131
    // Save view provider stuff
132
    if (guiup) {
133
        w.addFile("GuiDocument.xml", this);
134
    }
135
}
136

137
void MergeDocuments::Restore(Base::XMLReader &r)
138
{
139
    // Restore view provider stuff
140
    if (guiup) {
141
        r.addFile("GuiDocument.xml", this);
142
    }
143
}
144

145
void MergeDocuments::SaveDocFile (Base::Writer & w) const
146
{
147
    // Save view provider stuff
148
    appdoc->signalExportViewObjects(this->objects, w);
149
}
150

151
void MergeDocuments::RestoreDocFile(Base::Reader & r)
152
{
153
    // Restore view provider stuff
154
    appdoc->signalImportViewObjects(this->objects, r, this->nameMap);
155
}
156

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.