FreeCADs property system utilises some pointer math to calculate the offset between property and base class. Due to virtual inheritance of th ePropertyContainer the memory layout has been changed to rather random, which has lead to crashes dependend on the order of object initialisation. The solution is to not make PropertyContaner virtual but a class below, Base::Persitance. Then the memory layout is random for Persistance, but it is perfectly aligned for the base class chains from PropertyContainer onwards as well as from Extension onwards. Hence the proeprty system was changed to take the offset always from those two.
206 lines
8.6 KiB
C++
206 lines
8.6 KiB
C++
/***************************************************************************
|
|
* Copyright (c) 2016 Stefan Tröger <stefantroeger@gmx.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_
|
|
#endif
|
|
|
|
//#include "ViewProviderGroupExtensionPy.h"
|
|
#include "ViewProviderGroupExtension.h"
|
|
|
|
#include "Command.h"
|
|
#include "Application.h"
|
|
#include "Document.h"
|
|
#include <App/Document.h>
|
|
#include <App/DocumentObject.h>
|
|
#include <App/GroupExtension.h>
|
|
#include <App/Expression.h>
|
|
#include <QMessageBox>
|
|
|
|
using namespace Gui;
|
|
|
|
EXTENSION_PROPERTY_SOURCE(Gui::ViewProviderGroupExtension, Gui::ViewProviderExtension)
|
|
|
|
ViewProviderGroupExtension::ViewProviderGroupExtension() : visible(false)
|
|
{
|
|
initExtension(ViewProviderGroupExtension::getClassTypeId());
|
|
|
|
}
|
|
|
|
ViewProviderGroupExtension::~ViewProviderGroupExtension()
|
|
{
|
|
}
|
|
|
|
bool ViewProviderGroupExtension::extensionCanDragObjects() const {
|
|
return true;
|
|
}
|
|
|
|
bool ViewProviderGroupExtension::extensionCanDragObject(App::DocumentObject*) const {
|
|
|
|
//we can drag anything out
|
|
return true;
|
|
}
|
|
|
|
void ViewProviderGroupExtension::extensionDragObject(App::DocumentObject* obj) {
|
|
|
|
Gui::Command::doCommand(Gui::Command::Doc,"App.getDocument(\"%s\").getObject(\"%s\").removeObject("
|
|
"App.getDocument(\"%s\").getObject(\"%s\"))",
|
|
getExtendedViewProvider()->getObject()->getDocument()->getName(), getExtendedViewProvider()->getObject()->getNameInDocument(),
|
|
obj->getDocument()->getName(), obj->getNameInDocument() );
|
|
}
|
|
|
|
bool ViewProviderGroupExtension::extensionCanDropObjects() const {
|
|
return true;
|
|
}
|
|
|
|
bool ViewProviderGroupExtension::extensionCanDropObject(App::DocumentObject* obj) const {
|
|
|
|
auto* group = getExtendedViewProvider()->getObject()->getExtensionByType<App::GroupExtension>();
|
|
|
|
//we cannot drop thing of this group into it again
|
|
if (group->hasObject(obj))
|
|
return false;
|
|
|
|
//group into group?
|
|
if (obj->hasExtension(App::GroupExtension::getClassTypeId()))
|
|
if (group->isChildOf(obj->getExtensionByType<App::GroupExtension>()))
|
|
return false;
|
|
|
|
//We need to find the correct App extension to ask if this is a supported type, there should only be one
|
|
if(group->allowObject(obj))
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void ViewProviderGroupExtension::extensionDropObject(App::DocumentObject* obj) {
|
|
|
|
// Open command
|
|
App::DocumentObject* grp = static_cast<App::DocumentObject*>(getExtendedViewProvider()->getObject());
|
|
App::Document* doc = grp->getDocument();
|
|
Gui::Document* gui = Gui::Application::Instance->getDocument(doc);
|
|
gui->openCommand("Move object");
|
|
|
|
const App::DocumentObject* par = App::GroupExtension::getGroupOfObject(obj);
|
|
if (par) {
|
|
// allow an object to be in one group only
|
|
QString cmd;
|
|
cmd = QString::fromLatin1("App.getDocument(\"%1\").getObject(\"%2\").removeObject("
|
|
"App.getDocument(\"%1\").getObject(\"%3\"))")
|
|
.arg(QString::fromLatin1(doc->getName()))
|
|
.arg(QString::fromLatin1(par->getNameInDocument()))
|
|
.arg(QString::fromLatin1(obj->getNameInDocument()));
|
|
Gui::Application::Instance->runPythonCode(cmd.toUtf8());
|
|
}
|
|
|
|
// build Python command for execution
|
|
QString cmd;
|
|
cmd = QString::fromLatin1("App.getDocument(\"%1\").getObject(\"%2\").addObject("
|
|
"App.getDocument(\"%1\").getObject(\"%3\"))")
|
|
.arg(QString::fromLatin1(doc->getName()))
|
|
.arg(QString::fromLatin1(grp->getNameInDocument()))
|
|
.arg(QString::fromLatin1(obj->getNameInDocument()));
|
|
|
|
Gui::Application::Instance->runPythonCode(cmd.toUtf8());
|
|
|
|
gui->commitCommand();
|
|
}
|
|
|
|
std::vector< App::DocumentObject* > ViewProviderGroupExtension::extensionClaimChildren(void) const {
|
|
|
|
auto* group = getExtendedViewProvider()->getObject()->getExtensionByType<App::GroupExtension>();
|
|
|
|
return std::vector<App::DocumentObject*>(group->Group.getValues());
|
|
}
|
|
|
|
|
|
void ViewProviderGroupExtension::extensionShow(void) {
|
|
|
|
// when reading the Visibility property from file then do not hide the
|
|
// objects of this group because they have stored their visibility status, too
|
|
if (!getExtendedViewProvider()->isRestoring() && !this->visible) {
|
|
auto* group = getExtendedViewProvider()->getObject()->getExtensionByType<App::GroupExtension>();
|
|
|
|
const std::vector<App::DocumentObject*> & links = group->Group.getValues();
|
|
Gui::Document* doc = Application::Instance->getDocument(group->getExtendedObject()->getDocument());
|
|
for (std::vector<App::DocumentObject*>::const_iterator it = links.begin(); it != links.end(); ++it) {
|
|
ViewProvider* view = doc->getViewProvider(*it);
|
|
if (view)
|
|
view->show();
|
|
}
|
|
}
|
|
|
|
ViewProviderExtension::extensionShow();
|
|
this->visible = true;
|
|
}
|
|
|
|
void ViewProviderGroupExtension::extensionHide(void) {
|
|
|
|
// when reading the Visibility property from file then do not hide the
|
|
// objects of this group because they have stored their visibility status, too
|
|
if (!getExtendedViewProvider()->isRestoring() && this->visible) {
|
|
|
|
auto* group = getExtendedViewProvider()->getObject()->getExtensionByType<App::GroupExtension>();
|
|
|
|
const std::vector<App::DocumentObject*> & links = group->Group.getValues();
|
|
Gui::Document* doc = Application::Instance->getDocument(getExtendedViewProvider()->getObject()->getDocument());
|
|
for (std::vector<App::DocumentObject*>::const_iterator it = links.begin(); it != links.end(); ++it) {
|
|
ViewProvider* view = doc->getViewProvider(*it);
|
|
if (view)
|
|
view->hide();
|
|
}
|
|
}
|
|
|
|
ViewProviderExtension::extensionHide();
|
|
this->visible = false;
|
|
}
|
|
|
|
bool ViewProviderGroupExtension::extensionOnDelete(const std::vector< std::string >& vec) {
|
|
|
|
auto* group = getExtendedViewProvider()->getObject()->getExtensionByType<App::GroupExtension>();
|
|
// If the group is nonempty ask the user if he wants to delete it's content
|
|
if ( group->Group.getSize () ) {
|
|
QMessageBox::StandardButton choice =
|
|
QMessageBox::question ( 0, QObject::tr ( "Delete group content?" ),
|
|
QObject::tr ( "The %1 is not empty, delete it's content as well?")
|
|
.arg ( QString::fromUtf8 ( getExtendedViewProvider()->getObject()->Label.getValue () ) ),
|
|
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes );
|
|
|
|
if ( choice == QMessageBox::Yes ) {
|
|
Gui::Command::doCommand(Gui::Command::Doc,
|
|
"App.getDocument(\"%s\").getObject(\"%s\").removeObjectsFromDocument()"
|
|
,getExtendedViewProvider()->getObject()->getDocument()->getName(), getExtendedViewProvider()->getObject()->getNameInDocument());
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
namespace Gui {
|
|
EXTENSION_PROPERTY_SOURCE_TEMPLATE(Gui::ViewProviderGroupExtensionPython, Gui::ViewProviderGroupExtension)
|
|
|
|
// explicit template instantiation
|
|
template class GuiExport ViewProviderExtensionPythonT<ViewProviderGroupExtension>;
|
|
} |