From 1d2bd837c1a391fa877828a4f855d2aa0ea4e19d Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 3 Apr 2024 19:13:59 +0200 Subject: [PATCH] Sketch: add command to context-menu to change value of constraint The edit datum dialog is opened by double-clicking on a dimensional constraint in a sketch. However, the double-clicking event doesn't work realiably on some systems. As a workaround this PR adds the command to the context-menu. For more details see the forum thread: https://forum.freecad.org/viewtopic.php?t=71137 --- src/Mod/Sketcher/Gui/CommandConstraints.cpp | 54 +++++++++++++++++++++ src/Mod/Sketcher/Gui/ViewProviderSketch.cpp | 3 ++ 2 files changed, 57 insertions(+) diff --git a/src/Mod/Sketcher/Gui/CommandConstraints.cpp b/src/Mod/Sketcher/Gui/CommandConstraints.cpp index bcd49bbd2f..285faad264 100644 --- a/src/Mod/Sketcher/Gui/CommandConstraints.cpp +++ b/src/Mod/Sketcher/Gui/CommandConstraints.cpp @@ -9778,6 +9778,59 @@ bool CmdSketcherConstrainSnellsLaw::isActive() return isCreateConstraintActive(getActiveGuiDocument()); } +// ====================================================================================== +DEF_STD_CMD_A(CmdSketcherChangeDimensionConstraint) + +CmdSketcherChangeDimensionConstraint::CmdSketcherChangeDimensionConstraint() + : Command("Sketcher_ChangeDimensionConstraint") +{ + sAppModule = "Sketcher"; + sGroup = "Sketcher"; + sMenuText = QT_TR_NOOP("Change value"); + sToolTipText = QT_TR_NOOP("Change the value of a dimensional constraint"); + sWhatsThis = "Sketcher_ChangeDimensionConstraint"; + sStatusTip = sToolTipText; + eType = ForEdit; +} + +void CmdSketcherChangeDimensionConstraint::activated(int iMsg) +{ + Q_UNUSED(iMsg); + + auto getDimConstraint = []() { + std::vector selection{getSelection().getSelectionEx()}; + if (selection.size() != 1 || selection[0].getSubNames().size() != 1) { + throw Base::RuntimeError(); + } + + if (auto sketch = dynamic_cast(selection[0].getObject())) { + std::string subName = selection[0].getSubNames().at(0); + if (subName.size() > 10 && subName.substr(0, 10) == "Constraint") { + int ConstrId = Sketcher::PropertyConstraintList::getIndexFromConstraintName(subName); + return std::make_tuple(sketch, ConstrId); + } + } + + throw Base::RuntimeError(); + }; + + try { + auto value = getDimConstraint(); + EditDatumDialog editDatumDialog(std::get<0>(value), std::get<1>(value)); + editDatumDialog.exec(false); + } + catch (const Base::RuntimeError&) { + Gui::TranslatedUserWarning(getActiveGuiDocument()->getDocument(), + QObject::tr("Wrong selection"), + QObject::tr("Select one dimensional constraint from the sketch.")); + } +} + +bool CmdSketcherChangeDimensionConstraint::isActive() +{ + return isCommandActive(getActiveGuiDocument()); +} + // ====================================================================================== /*** Creation Mode / Toggle to or from Reference ***/ DEF_STD_CMD_AU(CmdSketcherToggleDrivingConstraint) @@ -10053,6 +10106,7 @@ void CreateSketcherCommandsConstraints() rcCmdMgr.addCommand(new CmdSketcherConstrainPointOnObject()); rcCmdMgr.addCommand(new CmdSketcherConstrainSymmetric()); rcCmdMgr.addCommand(new CmdSketcherConstrainSnellsLaw()); + rcCmdMgr.addCommand(new CmdSketcherChangeDimensionConstraint()); rcCmdMgr.addCommand(new CmdSketcherToggleDrivingConstraint()); rcCmdMgr.addCommand(new CmdSketcherToggleActiveConstraint()); rcCmdMgr.addCommand(new CmdSketcherCompDimensionTools()); diff --git a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp index d031975642..03ecc61253 100644 --- a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp +++ b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp @@ -4142,6 +4142,9 @@ void ViewProviderSketch::generateContextMenu() // context menu if only constraints are selected else if (selectedConstraints >= 1) { + if (selectedConstraints == 1) { + menu << "Sketcher_ChangeDimensionConstraint"; + } menu << "Sketcher_ToggleDrivingConstraint" << "Sketcher_ToggleActiveConstraint" << "Sketcher_SelectElementsAssociatedWithConstraints"