Drags and drop with groups

This commit is contained in:
plgarcia
2018-05-15 08:16:05 +02:00
committed by wmayer
parent 47ae980fa2
commit 77232b2141
10 changed files with 82 additions and 2 deletions

View File

@@ -702,7 +702,6 @@ void TreeWidget::dropEvent(QDropEvent *event)
Gui::Document* gui = vpTarget->getDocument();
if (da == Qt::LinkAction) {
#if 0
// Open command
gui->openCommand("Drop object");
for (QList<QTreeWidgetItem*>::Iterator it = items.begin(); it != items.end(); ++it) {
@@ -720,7 +719,6 @@ void TreeWidget::dropEvent(QDropEvent *event)
}
gui->commitCommand();
#endif
}
else {
if (!vpTarget->canDropObjects()) {

View File

@@ -716,6 +716,19 @@ void ViewProvider::dropObject(App::DocumentObject* obj)
throw Base::RuntimeError("ViewProvider::dropObject: no extension for dropping given object available.");
}
void ViewProvider::replaceObject(App::DocumentObject* oldValue, App::DocumentObject* newValue)
{
auto vector = getExtensionsDerivedFromType<Gui::ViewProviderExtension>();
for (Gui::ViewProviderExtension* ext : vector) {
if (ext->extensionCanDropObject(newValue)) {
ext->extensionReplaceObject(oldValue, newValue);
return;
}
}
throw Base::RuntimeError("ViewProvider::dropObject: no extension for dropping given object available.");
}
void ViewProvider::Restore(Base::XMLReader& reader)
{
setStatus(Gui::isRestoring, true);

View File

@@ -195,6 +195,8 @@ public:
virtual bool canDropObject(App::DocumentObject*) const;
/** Add an object to the view provider by drag and drop */
virtual void dropObject(App::DocumentObject*);
/** Replace an object to the view provider by drag and drop */
virtual void replaceObject(App::DocumentObject*, App::DocumentObject*);
//@}
/** @name Signals of the view provider */

View File

@@ -63,6 +63,7 @@ public:
virtual bool extensionCanDropObjects() const { return false; }
virtual bool extensionCanDropObject(App::DocumentObject*) const { return true; }
virtual void extensionDropObject(App::DocumentObject*) { }
virtual void extensionReplaceObject(App::DocumentObject* /*oldValue*/, App::DocumentObject* /*newValue*/) { }
/// Hides the view provider
virtual void extensionHide(void) { }

View File

@@ -110,6 +110,29 @@ void ViewProviderGroupExtension::extensionDropObject(App::DocumentObject* obj) {
Gui::Command::doCommand(Gui::Command::App, cmd.toUtf8());
}
void ViewProviderGroupExtension::extensionReplaceObject(App::DocumentObject* oldValue, App::DocumentObject* newValue) {
App::DocumentObject* grp = static_cast<App::DocumentObject*>(getExtendedViewProvider()->getObject());
App::Document* doc = grp->getDocument();
// build Python command for execution
QString cmd;
cmd = QString::fromLatin1("App.getDocument(\"%1\").getObject(\"%2\").removeObject("
"App.getDocument(\"%1\").getObject(\"%3\"))")
.arg(QString::fromLatin1(doc->getName()))
.arg(QString::fromLatin1(grp->getNameInDocument()))
.arg(QString::fromLatin1(oldValue->getNameInDocument()));
Gui::Command::doCommand(Gui::Command::App, cmd.toUtf8());
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(newValue->getNameInDocument()));
Gui::Command::doCommand(Gui::Command::App, cmd.toUtf8());
}
std::vector< App::DocumentObject* > ViewProviderGroupExtension::extensionClaimChildren(void) const {
auto* group = getExtendedViewProvider()->getObject()->getExtensionByType<App::GroupExtension>();

View File

@@ -46,6 +46,7 @@ public:
virtual bool extensionCanDropObjects() const override;
virtual bool extensionCanDropObject(App::DocumentObject*) const override;
virtual void extensionDropObject(App::DocumentObject*) override;
virtual void extensionReplaceObject(App::DocumentObject* oldValue, App::DocumentObject* newValue) override;
virtual void extensionHide(void) override;
virtual void extensionShow(void) override;

View File

@@ -152,6 +152,18 @@ bool ViewProviderBoolean::onDelete(const std::vector<std::string> &)
return true;
}
void ViewProviderBoolean::replaceObject(App::DocumentObject* oldValue, App::DocumentObject* newValue)
{
Part::Boolean* pBool = static_cast<Part::Boolean*>(getObject());
if (oldValue == pBool->Base.getValue()) {
pBool->Base.setValue(newValue);
}
else if (oldValue == pBool->Tool.getValue()) {
pBool->Tool.setValue(newValue);
}
}
PROPERTY_SOURCE(PartGui::ViewProviderMultiFuse,PartGui::ViewProviderPart)
ViewProviderMultiFuse::ViewProviderMultiFuse()
@@ -279,6 +291,13 @@ void ViewProviderMultiFuse::dropObject(App::DocumentObject* obj)
pBool->Shapes.setValues(pShapes);
}
void ViewProviderMultiFuse::replaceObject(App::DocumentObject* oldValue, App::DocumentObject* newValue)
{
Part::MultiFuse* pBool = static_cast<Part::MultiFuse*>(getObject());
std::vector<App::DocumentObject*> pShapes = pBool->Shapes.getValues();
std::replace(pShapes.begin(), pShapes.end(), oldValue, newValue);
pBool->Shapes.setValues(pShapes);
}
PROPERTY_SOURCE(PartGui::ViewProviderMultiCommon,PartGui::ViewProviderPart)
@@ -406,3 +425,11 @@ void ViewProviderMultiCommon::dropObject(App::DocumentObject* obj)
pShapes.push_back(obj);
pBool->Shapes.setValues(pShapes);
}
void ViewProviderMultiCommon::replaceObject(App::DocumentObject* oldValue, App::DocumentObject* newValue)
{
Part::MultiFuse* pBool = static_cast<Part::MultiFuse*>(getObject());
std::vector<App::DocumentObject*> pShapes = pBool->Shapes.getValues();
std::replace(pShapes.begin(), pShapes.end(), oldValue, newValue);
pBool->Shapes.setValues(pShapes);
}

View File

@@ -44,6 +44,7 @@ public:
QIcon getIcon(void) const;
void updateData(const App::Property*);
bool onDelete(const std::vector<std::string> &);
virtual void replaceObject(App::DocumentObject*, App::DocumentObject*);
};
/// ViewProvider for the MultiFuse feature
@@ -70,6 +71,8 @@ public:
bool canDropObjects() const;
bool canDropObject(App::DocumentObject*) const;
void dropObject(App::DocumentObject*);
/** Replace an object to the view provider by drag and drop */
virtual void replaceObject(App::DocumentObject*, App::DocumentObject*);
};
/// ViewProvider for the MultiFuse feature
@@ -96,6 +99,8 @@ public:
bool canDropObjects() const;
bool canDropObject(App::DocumentObject*) const;
void dropObject(App::DocumentObject*);
/** Replace an object to the view provider by drag and drop */
virtual void replaceObject(App::DocumentObject*, App::DocumentObject*);
};

View File

@@ -154,3 +154,11 @@ void ViewProviderCompound::dropObject(App::DocumentObject* obj)
pShapes.push_back(obj);
pComp->Links.setValues(pShapes);
}
void ViewProviderCompound::replaceObject(App::DocumentObject* oldValue, App::DocumentObject* newValue)
{
Part::Compound* pBool = static_cast<Part::Compound*>(getObject());
std::vector<App::DocumentObject*> pShapes = pBool->Links.getValues();
std::replace(pShapes.begin(), pShapes.end(), oldValue, newValue);
pBool->Links.setValues(pShapes);
}

View File

@@ -48,6 +48,8 @@ public:
bool canDropObjects() const;
bool canDropObject(App::DocumentObject*) const;
void dropObject(App::DocumentObject*);
/** Replace an object to the view provider by drag and drop */
virtual void replaceObject(App::DocumentObject*, App::DocumentObject*);
protected:
void updateData(const App::Property*);