Reimplement 'Duplicate' command to do the same as Copy/Paste

This commit is contained in:
wmayer 2013-05-07 23:30:17 +02:00
parent 9a9510d9e3
commit d58f317bc5

View File

@ -910,59 +910,36 @@ StdCmdDuplicateSelection::StdCmdDuplicateSelection()
void StdCmdDuplicateSelection::activated(int iMsg)
{
App::Document* act = App::GetApplication().getActiveDocument();
if (!act)
return; // no active document found
Gui::Document* doc = Gui::Application::Instance->getDocument(act);
std::vector<Gui::SelectionSingleton::SelObj> sel = Gui::Selection().getCompleteSelection();
std::vector<SelectionSingleton::SelObj> sel = Selection().getCompleteSelection();
std::vector<App::DocumentObject*> obj;
obj.reserve(sel.size());
for (std::vector<SelectionSingleton::SelObj>::iterator it = sel.begin(); it != sel.end(); ++it) {
if (!it->pObject)
continue; // should actually not happen
// create a copy of the object
App::DocumentObject* copy = act->copyObject(it->pObject, false);
if (!copy) // continue if no copy could be created
continue;
// mark all properties of the copy as "touched" which are touched in the original object
std::map<std::string,App::Property*> props;
it->pObject->getPropertyMap(props);
std::map<std::string,App::Property*> copy_props;
copy->getPropertyMap(copy_props);
for (std::map<std::string,App::Property*>::iterator jt = props.begin(); jt != props.end(); ++jt) {
if (jt->second->isTouched()) {
std::map<std::string,App::Property*>::iterator kt;
kt = copy_props.find(jt->first);
if (kt != copy_props.end()) {
kt->second->touch();
}
}
}
Gui::Document* parent = Gui::Application::Instance->getDocument(it->pObject->getDocument());
if (!parent || !doc)
continue; // should not happen
// copy the properties of the associated view providers
Gui::ViewProvider* view = parent->getViewProvider(it->pObject);
Gui::ViewProvider* copy_view = doc->getViewProvider(copy);
copy_view->addDynamicProperties(view);
if (!view || !copy_view)
continue; // should not happen
// get the properties of the view provider
props.clear();
view->getPropertyMap(props);
copy_props.clear();
copy_view->getPropertyMap(copy_props);
for (std::map<std::string,App::Property*>::iterator jt = props.begin(); jt != props.end(); ++jt) {
std::map<std::string,App::Property*>::iterator kt;
kt = copy_props.find(jt->first);
if (kt != copy_props.end()) {
std::auto_ptr<App::Property> data(jt->second->Copy());
if (data.get()) {
kt->second->Paste(*data);
}
}
if (it->pObject) {
obj.push_back(it->pObject);
}
}
if (obj.empty())
return;
Base::FileInfo fi(Base::FileInfo::getTempFileName());
{
// save stuff to file
Base::ofstream str(fi, std::ios::out | std::ios::binary);
App::Document* doc = obj.front()->getDocument();
MergeDocuments mimeView(doc);
doc->exportObjects(obj, str);
str.close();
}
App::Document* doc = App::GetApplication().getActiveDocument();
if (doc) {
// restore objects from file and add to active document
Base::ifstream str(fi, std::ios::in | std::ios::binary);
MergeDocuments mimeView(doc);
mimeView.importObjects(str);
str.close();
}
fi.deleteFile();
}
bool StdCmdDuplicateSelection::isActive(void)