This patch adds support of recomputation with external linked object, as well as external document auto loading and partial loading. Application: * Modified new/openDocument()/signalNewDocument to choose whether to signal GUI for creating a view for the document. This makes it possible to suppress view creation when opening external documents. * New API openDocuments() which does the actual job of loading the document together with any external dependencies. There are afew extra arguments to allow setting FileName property differently from the actual file path, which are required when auto loading dependencies during document recovery (with future patch to Gui::DocumentRecovery) * openDocumentPrivate() is an internal helper for opening individual document. * New signalStart/FinishOpenDocument to be signaled before and after opening a document. There may be multiple depending documents actually opened in between these two signals. * New signalBeforeRecomputeDocument signaled before recompute a document. * New API addPendingDocument() for use by external capable link properties' to queue up external documents. * isRestoring/isClosingAll(), for convenience status reporting. Document: * signalFinishImport/RestoreObjects, new signal triggered after imported or restored all input objects * signalBeforeRecompute, signaled before start recomputing this document * Modified signalRecomputed with additional recomputed objects, this is to make it more efficient for Gui::TreeWidget to check recomputation result. * signalSkipRecompute, signal to inform which objects are skipped during recomputation because of their owner document SkipRecompute setting. * restore/save/read/writeObjects() modified to suport partial loading. See [here](https://git.io/fj6PY) for more information. * afterRestore(), internal function called to finish restore. The function is separated from restore() because there is quite a few critical steps needed to fully restore a document with external linking. See [here](https://git.io/fj6P4) for more information. * DocumentP::_RecomputeLog is modified to store more accurate object recomputation error, including those happened during restore/import. * isExporting(), new API for checking if an object is exporting. External linking properties will use this function to decide how to export. * copyObject(), modified to support external linking objects, and accepts multiple input objects. * moveObject(), modified to support arbitary object moves. The original implementation may cause crash if undo/redo is enabled. Furthermore, because the original information fakes the object's deletion to break its dependency, it does not work for objects that may auto delete their children when being deleted. The new implementation copy the object, and than paste it to the other document. It then deletes the input objects from the original document. In case of recursive move, it only deletes the depending object if it has an empty in list. * importLinks(), new API to import any external object linked by the input objects into this document. It will auto correct all link references after importing. * getDependencyList/_rebuildDependencyList(), these two APIs are unified and implemented by an internal function _buildDependencyList() with a new algorithm to handle external objects. The returned dependency list will now include objects from external documents. In case of cyclic dependencies, getDpendencyList() will report the actual objects involved in dependency loops. * mustExecute(), new API to check if there are any object requires recomputation. This function will call _buildDependencyList() and check for external objects as well. * addRecomputeObject(), new API for marking changes during document restore. It only marks the object but does not actually recompute them for performance reason. One use case is for geo feature to request for recomputation to generate geometry topological names. * recompute(), support partial, external, and inverse dependency recomputation. Improve error handling during recomputation. See [here](https://git.io/fj6PO) for more information. * _recomputeFeature(), suppoert user abort. * getDependentDocuments/getInList(), new API to obtain an optional dependency sorted list of depending documents. DocumentObject: * Add various ObjectStatus flags * isExporting/getExportName(), return a safe name for exporting, in the form of <ObjName>@<DocName>, which is guarrenteed to be unique. Various link property will save linked object using this name if the the linked object is exported together with the owner object, see [PropertyLinkBase::restoreLabelReference()](https://git.io/fj6XO) for more information. * recomputeFeature(), add option to recompute this object together with all its dependent objects. * canLoadPartial(), new API for [partial document loading](https://git.io/fj6PY). MergeDocuments: * Move object name mapping logic to various link properties. See Base::Sequencer: * Add new API checkAbort() for checking user abort.
206 lines
7.3 KiB
C++
206 lines
7.3 KiB
C++
/***************************************************************************
|
|
* Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> *
|
|
* *
|
|
* This file is part of the FreeCAD CAx development system. *
|
|
* *
|
|
* This library is free software; you can redistribute it and/or *
|
|
* modify it under the terms of the GNU Library General Public *
|
|
* License as published by the Free Software Foundation; either *
|
|
* version 2 of the License, or (at your option) any later version. *
|
|
* *
|
|
* This library is distributed in the hope that it will be useful, *
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
* GNU Library General Public License for more details. *
|
|
* *
|
|
* You should have received a copy of the GNU Library General Public *
|
|
* License along with this library; see the file COPYING.LIB. If not, *
|
|
* write to the Free Software Foundation, Inc., 59 Temple Place, *
|
|
* Suite 330, Boston, MA 02111-1307, USA *
|
|
* *
|
|
***************************************************************************/
|
|
|
|
#include "PreCompiled.h"
|
|
#ifndef _PreComp_
|
|
# include <stack>
|
|
# include <boost/bind.hpp>
|
|
#endif
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include "MergeDocuments.h"
|
|
#include <Base/Console.h>
|
|
#include <Base/Reader.h>
|
|
#include <Base/Writer.h>
|
|
#include <App/Document.h>
|
|
#include <App/DocumentObject.h>
|
|
|
|
using namespace App;
|
|
|
|
namespace App {
|
|
|
|
class XMLMergeReader : public Base::XMLReader
|
|
{
|
|
public:
|
|
XMLMergeReader(std::map<std::string, std::string>& name, const char* FileName, std::istream& str)
|
|
: Base::XMLReader(FileName, str), nameMap(name)
|
|
{}
|
|
|
|
void addName(const char* s1, const char* s2)
|
|
{
|
|
nameMap[s1] = s2;
|
|
}
|
|
const char* getName(const char* name) const
|
|
{
|
|
std::map<std::string, std::string>::const_iterator it = nameMap.find(name);
|
|
if (it != nameMap.end())
|
|
return it->second.c_str();
|
|
else
|
|
return name;
|
|
}
|
|
bool doNameMapping() const
|
|
{
|
|
return true;
|
|
}
|
|
protected:
|
|
|
|
// It is not safe to change potential object name reference at this level.
|
|
// For example, a LinkSub with sub element name Face1 may also be some
|
|
// object's name that may potentially be mapped. In addition, with the
|
|
// introduction of full quanlified SubName reference, the Sub value inside
|
|
// LinkSub may require customized mapping. So we move the mapping logic to
|
|
// various link property's Restore() function.
|
|
#if 0
|
|
void startElement(const XMLCh* const uri, const XMLCh* const localname,
|
|
const XMLCh* const qname,
|
|
const XERCES_CPP_NAMESPACE_QUALIFIER Attributes& attrs)
|
|
{
|
|
Base::XMLReader::startElement(uri, localname, qname, attrs);
|
|
if (LocalName == "Property")
|
|
propertyStack.push(std::make_pair(AttrMap["name"],AttrMap["type"]));
|
|
|
|
if (!propertyStack.empty()) {
|
|
// replace the stored object name with the real one
|
|
if (LocalName == "Link" || LocalName == "LinkSub" || (LocalName == "String" && propertyStack.top().first == "Label")) {
|
|
for (std::map<std::string, std::string>::iterator it = AttrMap.begin(); it != AttrMap.end(); ++it) {
|
|
std::map<std::string, std::string>::const_iterator jt = nameMap.find(it->second);
|
|
if (jt != nameMap.end())
|
|
it->second = jt->second;
|
|
}
|
|
}
|
|
// update the expression if name of the object is used
|
|
else if (LocalName == "Expression") {
|
|
std::map<std::string, std::string>::iterator it = AttrMap.find("expression");
|
|
if (it != AttrMap.end()) {
|
|
// search for the part before the first dot that should be the object name.
|
|
std::string expression = it->second;
|
|
std::string::size_type dotpos = expression.find_first_of(".");
|
|
if (dotpos != std::string::npos) {
|
|
std::string name = expression.substr(0, dotpos);
|
|
std::map<std::string, std::string>::const_iterator jt = nameMap.find(name);
|
|
if (jt != nameMap.end()) {
|
|
std::string newexpression = jt->second;
|
|
newexpression += expression.substr(dotpos);
|
|
it->second = newexpression;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void endElement(const XMLCh* const uri, const XMLCh *const localname, const XMLCh *const qname)
|
|
{
|
|
Base::XMLReader::endElement(uri, localname, qname);
|
|
if (LocalName == "Property")
|
|
propertyStack.pop();
|
|
}
|
|
#endif
|
|
|
|
private:
|
|
std::map<std::string, std::string>& nameMap;
|
|
typedef std::pair<std::string, std::string> PropertyTag;
|
|
std::stack<PropertyTag> propertyStack;
|
|
};
|
|
}
|
|
|
|
MergeDocuments::MergeDocuments(App::Document* doc) : guiup(false), verbose(true), stream(0), appdoc(doc)
|
|
{
|
|
connectExport = doc->signalExportObjects.connect
|
|
(boost::bind(&MergeDocuments::exportObject, this, _1, _2));
|
|
connectImport = doc->signalImportObjects.connect
|
|
(boost::bind(&MergeDocuments::importObject, this, _1, _2));
|
|
|
|
QCoreApplication* app = QCoreApplication::instance();
|
|
if (app && app->inherits("QApplication")) {
|
|
guiup = true;
|
|
}
|
|
}
|
|
|
|
MergeDocuments::~MergeDocuments()
|
|
{
|
|
connectExport.disconnect();
|
|
connectImport.disconnect();
|
|
}
|
|
|
|
unsigned int MergeDocuments::getMemSize (void) const
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
std::vector<App::DocumentObject*>
|
|
MergeDocuments::importObjects(std::istream& input)
|
|
{
|
|
this->nameMap.clear();
|
|
this->stream = new zipios::ZipInputStream(input);
|
|
XMLMergeReader reader(this->nameMap,"<memory>", *stream);
|
|
reader.setVerbose(isVerbose());
|
|
std::vector<App::DocumentObject*> objs = appdoc->importObjects(reader);
|
|
|
|
delete this->stream;
|
|
this->stream = 0;
|
|
|
|
return objs;
|
|
}
|
|
|
|
void MergeDocuments::importObject(const std::vector<App::DocumentObject*>& o, Base::XMLReader & r)
|
|
{
|
|
objects = o;
|
|
Restore(r);
|
|
r.readFiles(*this->stream);
|
|
}
|
|
|
|
void MergeDocuments::exportObject(const std::vector<App::DocumentObject*>& o, Base::Writer & w)
|
|
{
|
|
objects = o;
|
|
Save(w);
|
|
}
|
|
|
|
void MergeDocuments::Save (Base::Writer & w) const
|
|
{
|
|
// Save view provider stuff
|
|
if (guiup) {
|
|
w.addFile("GuiDocument.xml", this);
|
|
}
|
|
}
|
|
|
|
void MergeDocuments::Restore(Base::XMLReader &r)
|
|
{
|
|
// Restore view provider stuff
|
|
if (guiup) {
|
|
r.addFile("GuiDocument.xml", this);
|
|
}
|
|
}
|
|
|
|
void MergeDocuments::SaveDocFile (Base::Writer & w) const
|
|
{
|
|
// Save view provider stuff
|
|
appdoc->signalExportViewObjects(this->objects, w);
|
|
}
|
|
|
|
void MergeDocuments::RestoreDocFile(Base::Reader & r)
|
|
{
|
|
// Restore view provider stuff
|
|
appdoc->signalImportViewObjects(this->objects, r, this->nameMap);
|
|
}
|