[TD] handle object deletions better

see https://forum.freecadweb.org/viewtopic.php?p=366902#p366902
This commit is contained in:
donovaly
2020-03-03 03:54:08 +01:00
committed by WandererFan
parent b25ca7194c
commit def5159c38
10 changed files with 175 additions and 7 deletions

View File

@@ -221,3 +221,12 @@ void ViewProviderDimension::handleChangedPropertyType(Base::XMLReader &reader, c
LineWidth.setValue(LineWidthProperty.getValue());
}
}
bool ViewProviderDimension::canDelete(App::DocumentObject *obj) const
{
// deletions of objects from a ProjGroupItem don't necesarily destroy anything
// thus we can pass this action
// we can warn the user if necessary in the object's ViewProvider in the onDelete() function
Q_UNUSED(obj)
return true;
}

View File

@@ -80,8 +80,9 @@ public:
double prefFontSize() const;
double prefWeight() const;
int prefStandardAndStyle() const;
virtual bool canDelete(App::DocumentObject* obj) const;
protected:
protected:
virtual void handleChangedPropertyType(Base::XMLReader &reader, const char *TypeName, App::Property * prop);
private:

View File

@@ -27,6 +27,8 @@
#ifndef _PreComp_
# include <QAction>
# include <QMenu>
# include <QMessageBox>
# include <QTextStream>
# include <QTimer>
#include <QList>
#include <QPointer>
@@ -191,11 +193,37 @@ void ViewProviderPage::updateData(const App::Property* prop)
Gui::ViewProviderDocumentObject::updateData(prop);
}
bool ViewProviderPage::onDelete(const std::vector<std::string> &items)
bool ViewProviderPage::onDelete(const std::vector<std::string> &)
{
bool rc = ViewProviderDocumentObject::onDelete(items);
removeMDIView();
return rc;
// warn the user if the Page is not empty
// check if there are items in the group
auto objs = claimChildren();
if (!objs.empty())
{
// generate dialog
QString bodyMessage;
QTextStream bodyMessageStream(&bodyMessage);
bodyMessageStream << qApp->translate("Std_Delete",
"The page is not empty, therefore the\n following referencing objects might be lost.\n\n"
"Are you sure you want to continue?\n");
for (auto ObjIterator : objs)
bodyMessageStream << '\n' << QString::fromUtf8(ObjIterator->Label.getValue());
// show and evaluate the dialog
int DialogResult = QMessageBox::warning(Gui::getMainWindow(),
qApp->translate("Std_Delete", "Object dependencies"), bodyMessage,
QMessageBox::Yes, QMessageBox::No);
if (DialogResult == QMessageBox::Yes) {
removeMDIView();
return true;
} else
return false;
}
else {
removeMDIView();
return true;
}
}
void ViewProviderPage::setupContextMenu(QMenu* menu, QObject* receiver, const char* member)
@@ -421,6 +449,16 @@ void ViewProviderPage::setGraphicsView(QGVPage* gv)
m_graphicsView = gv;
}
bool ViewProviderPage::canDelete(App::DocumentObject *obj) const
{
// deletions from a page don't necesarily destroy anything
// thus we can pass this action
// if an object could break something, like e.g. the template object
// its ViewProvider handles this in the onDelete() function
Q_UNUSED(obj)
return true;
}
//! Redo the whole visual page
void ViewProviderPage::onGuiRepaint(const TechDraw::DrawPage* dp)
{

View File

@@ -92,6 +92,7 @@ public:
void toggleFrameState(void);
void setTemplateMarkers(bool state);
void setGraphicsView(QGVPage* gv);
virtual bool canDelete(App::DocumentObject* obj) const;
protected:
bool setEdit(int ModNum) override;

View File

@@ -27,6 +27,8 @@
# endif
# include <QAction>
# include <QMenu>
# include <QMessageBox>
# include <QTextStream>
#endif
#include <Base/Console.h>
@@ -48,7 +50,6 @@
#include <Mod/TechDraw/App/DrawProjGroupItem.h>
#include "TaskProjGroup.h"
#include "ViewProviderProjGroup.h"
@@ -146,6 +147,45 @@ bool ViewProviderProjGroup::doubleClicked(void)
return true;
}
bool ViewProviderProjGroup::onDelete(const std::vector<std::string> &)
{
// warn the user if the ProjGroup is not empty
// check if there are items in the group
auto objs = claimChildren();
if (!objs.empty())
{
// generate dialog
QString bodyMessage;
QTextStream bodyMessageStream(&bodyMessage);
bodyMessageStream << qApp->translate("Std_Delete",
"The projection group is not empty, therefore\n the following referencing objects might be lost.\n\n"
"Are you sure you want to continue?\n");
for (auto ObjIterator : objs)
bodyMessageStream << '\n' << QString::fromUtf8(ObjIterator->Label.getValue());
// show and evaluate dialog
int DialogResult = QMessageBox::warning(Gui::getMainWindow(),
qApp->translate("Std_Delete", "Object dependencies"), bodyMessage,
QMessageBox::Yes, QMessageBox::No);
if (DialogResult == QMessageBox::Yes)
return true;
else
return false;
}
else
return true;
}
bool ViewProviderProjGroup::canDelete(App::DocumentObject *obj) const
{
// deletions of views from a ProjGroup don't necesarily destroy anything
// thus we can pass this action
// we can warn the user if necessary in the object's ViewProvider in the onDelete() function
Q_UNUSED(obj)
return true;
}
std::vector<App::DocumentObject*> ViewProviderProjGroup::claimChildren(void) const
{
// Collect any child fields

View File

@@ -60,7 +60,8 @@ public:
virtual TechDraw::DrawProjGroup* getViewObject() const;
void unsetEdit(int ModNum);
virtual void onChanged(const App::Property *prop);
virtual bool onDelete(const std::vector<std::string> &);
virtual bool canDelete(App::DocumentObject* obj) const;
protected:
bool setEdit(int ModNum);

View File

@@ -22,6 +22,8 @@
#include "PreCompiled.h"
#ifndef _PreComp_
#include <QMessageBox>
#include <QTextStream>
#endif
/// Here the FreeCAD includes sorted by Base,App,Gui......
@@ -41,6 +43,8 @@
#include <Gui/Document.h>
#include <Gui/MainWindow.h>
#include <Mod/TechDraw/App/DrawProjGroup.h>
#include <Mod/TechDraw/App/DrawProjGroupItem.h>
#include "ViewProviderProjGroupItem.h"
@@ -142,6 +146,49 @@ bool ViewProviderProjGroupItem::doubleClicked(void)
return true;
}
bool ViewProviderProjGroupItem::onDelete(const std::vector<std::string> &)
{
// we cannot delete the anchor view, thus check if the item is the front item
bool isAnchor = false;
// get the item and group
TechDraw::DrawProjGroupItem* dpgi = static_cast<TechDraw::DrawProjGroupItem*>(getViewObject());
TechDraw::DrawProjGroup* dpg = dpgi->getPGroup();
// get the projection
TechDraw::DrawProjGroupItem* proj = getObject();
// check if it is the anchor projection
if ((dpg != nullptr) && (dpg->hasProjection(proj->Type.getValueAsString()))
&& (dpg->getAnchor() == dpgi))
isAnchor = true;
if (isAnchor)
{
// generate dialog
QString bodyMessage;
QTextStream bodyMessageStream(&bodyMessage);
bodyMessageStream << qApp->translate("Std_Delete",
"You cannot delete the anchor view of a projection group!");
QMessageBox::warning(Gui::getMainWindow(),
qApp->translate("Std_Delete", "Object dependencies"), bodyMessage,
QMessageBox::Ok);
// don't allow to delete
return false;
}
else {
return true;
}
}
bool ViewProviderProjGroupItem::canDelete(App::DocumentObject *obj) const
{
// deletions of objects from a ProjGroupItem don't necesarily destroy anything
// thus we can pass this action
// we can warn the user if necessary in the object's ViewProvider in the onDelete() function
Q_UNUSED(obj)
return true;
}
TechDraw::DrawProjGroupItem* ViewProviderProjGroupItem::getViewObject() const
{
return dynamic_cast<TechDraw::DrawProjGroupItem*>(pcObject);

View File

@@ -54,6 +54,8 @@ public:
virtual TechDraw::DrawProjGroupItem* getViewObject() const;
TechDraw::DrawProjGroupItem* getObject() const;
void unsetEdit(int ModNum);
virtual bool onDelete(const std::vector<std::string> &);
virtual bool canDelete(App::DocumentObject* obj) const;
protected:
bool setEdit(int ModNum);

View File

@@ -24,6 +24,8 @@
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QMessageBox>
# include <QTextStream>
# ifdef FC_OS_WIN32
# include <windows.h>
# endif
@@ -47,6 +49,7 @@
#include <Mod/TechDraw/App/DrawSVGTemplate.h>
#include <Mod/TechDraw/App/DrawPage.h>
#include "DrawGuiUtil.h"
#include "QGITemplate.h"
#include "QGISVGTemplate.h"
#include "QGVPage.h"
@@ -182,6 +185,31 @@ void ViewProviderTemplate::setMarkers(bool state)
}
}
bool ViewProviderTemplate::onDelete(const std::vector<std::string> &)
{
// deleting the template will break the page view, thus warn the user
// get the page
auto page = getTemplate()->getParentPage();
// generate dialog
QString bodyMessage;
QTextStream bodyMessageStream(&bodyMessage);
bodyMessageStream << qApp->translate("Std_Delete",
"The following referencing objects might break.\n\n"
"Are you sure you want to continue?\n");
bodyMessageStream << '\n' << QString::fromUtf8(page->Label.getValue());
// show and evaluate dialog
int DialogResult = QMessageBox::warning(Gui::getMainWindow(),
qApp->translate("Std_Delete", "Object dependencies"), bodyMessage,
QMessageBox::Yes, QMessageBox::No);
if (DialogResult == QMessageBox::Yes)
return true;
else
return false;
}
MDIViewPage* ViewProviderTemplate::getMDIViewPage(void) const
{
MDIViewPage* myMdi = nullptr;

View File

@@ -60,6 +60,7 @@ public:
virtual Gui::MDIView *getMDIView() const override;
void setMarkers(bool state);
virtual bool onDelete(const std::vector<std::string> &);
};
} // namespace TechDrawGui