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
This commit is contained in:
wmayer
2020-10-24 15:37:04 +02:00
parent f16f974048
commit 690cc3e180
5 changed files with 46 additions and 15 deletions

View File

@@ -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())
<< ".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();

View File

@@ -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);

View File

@@ -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))

View File

@@ -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<std::string> &) const override;

View File

@@ -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<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);