Skip to content

Commit

Permalink
[TD] handle object deletions better
Browse files Browse the repository at this point in the history
  • Loading branch information
donovaly authored and WandererFan committed Mar 4, 2020
1 parent b38ba6e commit 5a61baf
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/Mod/TechDraw/Gui/ViewProviderDimension.cpp
Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion src/Mod/TechDraw/Gui/ViewProviderDimension.h
Expand Up @@ -80,8 +80,9 @@ class TechDrawGuiExport ViewProviderDimension : public ViewProviderDrawingView
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:
Expand Down
46 changes: 42 additions & 4 deletions src/Mod/TechDraw/Gui/ViewProviderPage.cpp
Expand Up @@ -27,6 +27,8 @@
#ifndef _PreComp_
# include <QAction>
# include <QMenu>
# include <QMessageBox>
# include <QTextStream>
# include <QTimer>
#include <QList>
#include <QPointer>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions src/Mod/TechDraw/Gui/ViewProviderPage.h
Expand Up @@ -92,6 +92,7 @@ class TechDrawGuiExport ViewProviderPage : public Gui::ViewProviderDocumentObjec
void toggleFrameState(void);
void setTemplateMarkers(bool state);
void setGraphicsView(QGVPage* gv);
virtual bool canDelete(App::DocumentObject* obj) const;

protected:
bool setEdit(int ModNum) override;
Expand Down
42 changes: 41 additions & 1 deletion src/Mod/TechDraw/Gui/ViewProviderProjGroup.cpp
Expand Up @@ -27,6 +27,8 @@
# endif
# include <QAction>
# include <QMenu>
# include <QMessageBox>
# include <QTextStream>
#endif

#include <Base/Console.h>
Expand All @@ -48,7 +50,6 @@

#include <Mod/TechDraw/App/DrawProjGroupItem.h>


#include "TaskProjGroup.h"
#include "ViewProviderProjGroup.h"

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/Mod/TechDraw/Gui/ViewProviderProjGroup.h
Expand Up @@ -60,7 +60,8 @@ class TechDrawGuiExport ViewProviderProjGroup : public ViewProviderDrawingView
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);
Expand Down
47 changes: 47 additions & 0 deletions src/Mod/TechDraw/Gui/ViewProviderProjGroupItem.cpp
Expand Up @@ -22,6 +22,8 @@

#include "PreCompiled.h"
#ifndef _PreComp_
#include <QMessageBox>
#include <QTextStream>
#endif

/// Here the FreeCAD includes sorted by Base,App,Gui......
Expand All @@ -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"

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/Mod/TechDraw/Gui/ViewProviderProjGroupItem.h
Expand Up @@ -54,6 +54,8 @@ class TechDrawGuiExport ViewProviderProjGroupItem: public ViewProviderViewPart
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);
Expand Down
28 changes: 28 additions & 0 deletions src/Mod/TechDraw/Gui/ViewProviderTemplate.cpp
Expand Up @@ -24,6 +24,8 @@
#include "PreCompiled.h"

#ifndef _PreComp_
# include <QMessageBox>
# include <QTextStream>
# ifdef FC_OS_WIN32
# include <windows.h>
# endif
Expand All @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Mod/TechDraw/Gui/ViewProviderTemplate.h
Expand Up @@ -60,6 +60,7 @@ class TechDrawGuiExport ViewProviderTemplate : public Gui::ViewProviderDocumentO
virtual Gui::MDIView *getMDIView() const override;

void setMarkers(bool state);
virtual bool onDelete(const std::vector<std::string> &);
};

} // namespace TechDrawGui
Expand Down

0 comments on commit 5a61baf

Please sign in to comment.