From 690cc3e180711641998f7943fd3594b9cb14f6fe Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 24 Oct 2020 15:37:04 +0200 Subject: [PATCH] Gui: [skip ci] let view provider to decide to whether open a transaction on double-click The sketcher view provider doesn't need a transaction on double-click or otherwise shows a useless entry in the undo dialog which when undoing leads to weird behaviour --- src/Gui/Tree.cpp | 40 +++++++++++++++-------- src/Gui/ViewProvider.h | 6 +++- src/Gui/ViewProviderDocumentObject.cpp | 5 +++ src/Gui/ViewProviderDocumentObject.h | 4 +++ src/Mod/Sketcher/Gui/ViewProviderSketch.h | 6 +++- 5 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/Gui/Tree.cpp b/src/Gui/Tree.cpp index 56b3a9efce..8bafbdfc07 100644 --- a/src/Gui/Tree.cpp +++ b/src/Gui/Tree.cpp @@ -1315,22 +1315,36 @@ void TreeWidget::mouseDoubleClickEvent (QMouseEvent * event) } else if (item->type() == TreeWidget::ObjectType) { DocumentObjectItem* objitem = static_cast(item); - objitem->getOwnerDocument()->document()->setActiveView(objitem->object()); + ViewProviderDocumentObject* vp = objitem->object(); + + objitem->getOwnerDocument()->document()->setActiveView(vp); auto manager = Application::Instance->macroManager(); auto lines = manager->getLines(); - auto editDoc = Application::Instance->editDocument(); - App::AutoTransaction committer("Double click", true); - std::ostringstream ss; - ss << Command::getObjectCmd(objitem->object()->getObject()) - << ".ViewObject.doubleClicked()"; - if (!objitem->object()->doubleClicked()) - QTreeWidget::mouseDoubleClickEvent(event); - else if(lines == manager->getLines()) - manager->addLine(MacroManager::Gui,ss.str().c_str()); - // If the double click starts an editing, let the transaction persist - if(!editDoc && Application::Instance->editDocument()) - committer.setEnable(false); + std::ostringstream ss; + ss << Command::getObjectCmd(vp->getObject()) + << ".ViewObject.doubleClicked()"; + + const char* commandText = vp->getTransactionText(); + if (commandText) { + auto editDoc = Application::Instance->editDocument(); + App::AutoTransaction committer(commandText, true); + + if (!vp->doubleClicked()) + QTreeWidget::mouseDoubleClickEvent(event); + else if (lines == manager->getLines()) + manager->addLine(MacroManager::Gui, ss.str().c_str()); + + // If the double click starts an editing, let the transaction persist + if (!editDoc && Application::Instance->editDocument()) + committer.setEnable(false); + } + else { + if (!vp->doubleClicked()) + QTreeWidget::mouseDoubleClickEvent(event); + else if (lines == manager->getLines()) + manager->addLine(MacroManager::Gui, ss.str().c_str()); + } } } catch (Base::Exception &e) { e.ReportException(); diff --git a/src/Gui/ViewProvider.h b/src/Gui/ViewProvider.h index a863a6e7e3..56a746f378 100644 --- a/src/Gui/ViewProvider.h +++ b/src/Gui/ViewProvider.h @@ -446,7 +446,11 @@ public: /// is called when the provider is in edit and a key event occurs. Only ESC ends edit. virtual bool keyPressed(bool pressed, int key); - /// is called by the tree if the user double click on the object + /// Is called by the tree if the user double clicks on the object. It returns the string + /// for the transaction that will be shown in the undo/redo dialog. + /// If null is returned then no transaction will be opened. + virtual const char* getTransactionText() const { return nullptr; } + /// is called by the tree if the user double clicks on the object virtual bool doubleClicked(void) { return false; } /// is called when the provider is in edit and the mouse is moved virtual bool mouseMove(const SbVec2s &cursorPos, View3DInventorViewer* viewer); diff --git a/src/Gui/ViewProviderDocumentObject.cpp b/src/Gui/ViewProviderDocumentObject.cpp index 6d2bbf1b41..5ae249139c 100644 --- a/src/Gui/ViewProviderDocumentObject.cpp +++ b/src/Gui/ViewProviderDocumentObject.cpp @@ -280,6 +280,11 @@ void ViewProviderDocumentObject::show(void) } } +const char* ViewProviderDocumentObject::getTransactionText() const +{ + return QT_TRANSLATE_NOOP("Command", "Edit"); +} + void ViewProviderDocumentObject::updateView() { if(!pcObject || testStatus(ViewStatus::UpdatingView)) diff --git a/src/Gui/ViewProviderDocumentObject.h b/src/Gui/ViewProviderDocumentObject.h index 02afd731a4..d809814138 100644 --- a/src/Gui/ViewProviderDocumentObject.h +++ b/src/Gui/ViewProviderDocumentObject.h @@ -73,6 +73,10 @@ public: virtual void hide(void) override; /// Show the object in the view virtual void show(void) override; + /// Is called by the tree if the user double clicks on the object. It returns the string + /// for the transaction that will be shown in the undo/redo dialog. + /// If null is returned then no transaction will be opened. + virtual const char* getTransactionText() const override; virtual bool canDropObjectEx(App::DocumentObject *, App::DocumentObject *, const char *, const std::vector &) const override; diff --git a/src/Mod/Sketcher/Gui/ViewProviderSketch.h b/src/Mod/Sketcher/Gui/ViewProviderSketch.h index 571ec209b8..ffb3a571e5 100644 --- a/src/Mod/Sketcher/Gui/ViewProviderSketch.h +++ b/src/Mod/Sketcher/Gui/ViewProviderSketch.h @@ -219,7 +219,11 @@ public: virtual void setupContextMenu(QMenu *menu, QObject *receiver, const char *member); /// is called when the Provider is in edit and a deletion request occurs virtual bool onDelete(const std::vector &); - /// is called by the tree if the user double click on the object + /// Is called by the tree if the user double clicks on the object. It returns the string + /// for the transaction that will be shown in the undo/redo dialog. + /// If null is returned then no transaction will be opened. + virtual const char* getTransactionText() const { return nullptr; } + /// is called by the tree if the user double clicks on the object virtual bool doubleClicked(void); /// is called when the Provider is in edit and the mouse is moved virtual bool mouseMove(const SbVec2s &pos, Gui::View3DInventorViewer *viewer);