From 3c7d5bc151094446c8f9f5aafb06cf77cd24b734 Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 22 Mar 2019 17:16:23 +0100 Subject: [PATCH] Improve project on surface dialog: make function undo/redoable work on document that was active when the dialog was opened handle case when object or document is deleted while dialog is open --- src/Mod/Part/Gui/DlgProjectionOnSurface.cpp | 51 +++++++++++++-------- src/Mod/Part/Gui/DlgProjectionOnSurface.h | 14 +++++- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/Mod/Part/Gui/DlgProjectionOnSurface.cpp b/src/Mod/Part/Gui/DlgProjectionOnSurface.cpp index f075908e0e..ab0a9335cb 100644 --- a/src/Mod/Part/Gui/DlgProjectionOnSurface.cpp +++ b/src/Mod/Part/Gui/DlgProjectionOnSurface.cpp @@ -132,7 +132,7 @@ DlgProjectionOnSurface::DlgProjectionOnSurface(QWidget *parent) ui->pushButtonAddFace->setCheckable(true); ui->pushButtonAddProjFace->setCheckable(true); ui->pushButtonAddWire->setCheckable(true); - + m_guiObjectVec.push_back(ui->pushButtonAddEdge); m_guiObjectVec.push_back(ui->pushButtonAddFace); m_guiObjectVec.push_back(ui->pushButtonAddProjFace); @@ -148,18 +148,19 @@ DlgProjectionOnSurface::DlgProjectionOnSurface(QWidget *parent) get_camera_direction(); disable_ui_elements(m_guiObjectVec, ui->pushButtonAddProjFace); - App::Document* activeDoc = App::GetApplication().getActiveDocument(); - if (!activeDoc) + m_partDocument = App::GetApplication().getActiveDocument(); + if (!m_partDocument) { throw Base::ValueError(QString(tr("Have no active document!!!")).toUtf8()); - return; } - m_projectionObject = dynamic_cast(activeDoc->addObject("Part::Feature", std::string(m_projectionObjectName.toUtf8()).c_str())); - if ( !m_projectionObject ) + this->attachDocument(m_partDocument); + m_partDocument->openTransaction("Project on surface"); + m_projectionObject = dynamic_cast(m_partDocument->addObject("Part::Feature", "Projection Object")); + if (!m_projectionObject) { throw Base::ValueError(QString(tr("Can not create a projection object!!!")).toUtf8()); - return; } + m_projectionObject->Label.setValue(std::string(m_projectionObjectName.toUtf8()).c_str()); on_radioButtonShowAll_clicked(); m_lastDepthVal = ui->doubleSpinBoxSolidDepth->value(); } @@ -184,15 +185,31 @@ DlgProjectionOnSurface::~DlgProjectionOnSurface() Gui::Selection().rmvSelectionGate(); } +void PartGui::DlgProjectionOnSurface::slotDeletedDocument(const App::Document& Doc) +{ + if (m_partDocument == &Doc) { + m_partDocument = nullptr; + m_projectionObject = nullptr; + } +} + +void PartGui::DlgProjectionOnSurface::slotDeletedObject(const App::DocumentObject& Obj) +{ + if (m_projectionObject == &Obj) { + m_projectionObject = nullptr; + } +} + void PartGui::DlgProjectionOnSurface::apply(void) { + if (m_partDocument) + m_partDocument->commitTransaction(); } void PartGui::DlgProjectionOnSurface::reject(void) { - App::Document* activeDoc = App::GetApplication().getActiveDocument(); - if (!activeDoc) return; - activeDoc->removeObject(m_projectionObject->getNameInDocument()); + if (m_partDocument) + m_partDocument->abortTransaction(); } void PartGui::DlgProjectionOnSurface::on_pushButtonAddFace_clicked() @@ -238,6 +255,7 @@ void PartGui::DlgProjectionOnSurface::on_pushButtonAddEdge_clicked() filterEdge = nullptr; } } + void PartGui::DlgProjectionOnSurface::on_pushButtonGetCurrentCamDir_clicked() { get_camera_direction(); @@ -311,8 +329,7 @@ void PartGui::DlgProjectionOnSurface::get_camera_direction(void) void PartGui::DlgProjectionOnSurface::store_current_selected_parts(std::vector& iStoreVec, const unsigned int iColor) { - App::Document* activeDoc = App::GetApplication().getActiveDocument(); - if (!activeDoc) return; + if (!m_partDocument) return; std::vector selObj = Gui::Selection().getSelectionEx(); if (selObj.size()) { @@ -357,7 +374,7 @@ void PartGui::DlgProjectionOnSurface::store_current_selected_parts(std::vectorShape.getName(), store, iColor); } - Gui::Selection().clearSelection(activeDoc->getName()); + Gui::Selection().clearSelection(m_partDocument->getName()); Gui::Selection().rmvPreselect(); } } @@ -528,10 +545,8 @@ void PartGui::DlgProjectionOnSurface::show_projected_shapes(const std::vectorremoveObject(m_projectionObject->getNameInDocument()); - m_projectionObject = dynamic_cast(activeDoc->addObject("Part::Feature", std::string(m_projectionObjectName.toUtf8()).c_str())); + if (!m_partDocument) return; + m_projectionObject->Shape.setValue(TopoDS_Shape()); return; } auto currentPlacement = m_projectionObject->Placement.getValue(); @@ -1003,8 +1018,8 @@ TaskProjectionOnSurface::~TaskProjectionOnSurface() bool TaskProjectionOnSurface::accept() { - return true; widget->apply(); + return true; //return (widget->result() == QDialog::Accepted); } diff --git a/src/Mod/Part/Gui/DlgProjectionOnSurface.h b/src/Mod/Part/Gui/DlgProjectionOnSurface.h index 3db1e545ed..c51137d478 100644 --- a/src/Mod/Part/Gui/DlgProjectionOnSurface.h +++ b/src/Mod/Part/Gui/DlgProjectionOnSurface.h @@ -29,6 +29,7 @@ #include #include "../App/PartFeature.h" +#include #include "TopoDS_Shape.hxx" #include "TopoDS_Edge.hxx" @@ -44,7 +45,9 @@ namespace PartGui { class DlgProjectionOnSurface; } -class DlgProjectionOnSurface : public QWidget, public Gui::SelectionObserver +class DlgProjectionOnSurface : public QWidget, + public Gui::SelectionObserver, + public App::DocumentObserver { Q_OBJECT @@ -115,6 +118,12 @@ private: void set_xyz_dir_spinbox(QDoubleSpinBox* icurrentSpinBox); void transform_shape_to_global_postion(TopoDS_Shape& ioShape, Part::Feature* iPart); +private: + /** Checks if the given document is about to be closed */ + virtual void slotDeletedDocument(const App::Document& Doc); + /** Checks if the given object is about to be removed. */ + virtual void slotDeletedObject(const App::DocumentObject& Obj); + private: Ui::DlgProjectionOnSurface *ui; std::vector m_shapeVec; @@ -127,6 +136,7 @@ private: const QString m_projectionObjectName; Part::Feature* m_projectionObject; + App::Document* m_partDocument; float m_lastDepthVal; class EdgeSelection; @@ -151,7 +161,7 @@ public: virtual QDialogButtonBox::StandardButtons getStandardButtons() const { - return QDialogButtonBox::Ok | QDialogButtonBox::Close; + return QDialogButtonBox::Ok | QDialogButtonBox::Cancel; } private: