Skip to content

Commit

Permalink
Gui: [skip ci] let view provider to decide to whether open a transact…
Browse files Browse the repository at this point in the history
…ion 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
  • Loading branch information
wwmayer committed Oct 24, 2020
1 parent f16f974 commit 690cc3e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
38 changes: 26 additions & 12 deletions src/Gui/Tree.cpp
Expand Up @@ -1315,22 +1315,36 @@ void TreeWidget::mouseDoubleClickEvent (QMouseEvent * event)
}
else if (item->type() == TreeWidget::ObjectType) {
DocumentObjectItem* objitem = static_cast<DocumentObjectItem*>(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())
ss << Command::getObjectCmd(vp->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);

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();
Expand Down
6 changes: 5 additions & 1 deletion src/Gui/ViewProvider.h
Expand Up @@ -446,7 +446,11 @@ class GuiExport ViewProvider : public App::TransactionalObject

/// 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);
Expand Down
5 changes: 5 additions & 0 deletions src/Gui/ViewProviderDocumentObject.cpp
Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions src/Gui/ViewProviderDocumentObject.h
Expand Up @@ -73,6 +73,10 @@ class GuiExport ViewProviderDocumentObject : public ViewProvider
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<std::string> &) const override;
Expand Down
6 changes: 5 additions & 1 deletion src/Mod/Sketcher/Gui/ViewProviderSketch.h
Expand Up @@ -219,7 +219,11 @@ class SketcherGuiExport ViewProviderSketch : public PartGui::ViewProvider2DObjec
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<std::string> &);
/// 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);
Expand Down

0 comments on commit 690cc3e

Please sign in to comment.