Skip to content

Commit

Permalink
Sketcher: add 3D view context menu when editing sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
realthunder committed Apr 30, 2022
1 parent d40edbc commit 36be73c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 17 deletions.
9 changes: 5 additions & 4 deletions src/Mod/Sketcher/Gui/CommandCreateGeo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6747,7 +6747,7 @@ CmdSketcherExternal::CmdSketcherExternal()
{
sAppModule = "Sketcher";
sGroup = "Sketcher";
sMenuText = QT_TR_NOOP("External geometry");
sMenuText = QT_TR_NOOP("Add external geometry");
sToolTipText = QT_TR_NOOP("Create an edge linked to an external geometry");
sWhatsThis = "Sketcher_External";
sStatusTip = sToolTipText;
Expand All @@ -6774,8 +6774,9 @@ CmdSketcherDefining::CmdSketcherDefining()
{
sAppModule = "Sketcher";
sGroup = QT_TR_NOOP("Sketcher");
sMenuText = QT_TR_NOOP("Defining geometry");
sToolTipText = QT_TR_NOOP("Create an defining edge linked to an external geometry");
sMenuText = QT_TR_NOOP("Add/toggle defining geometry");
sToolTipText = QT_TR_NOOP("Import an external geometry as defining geometry that can be use for extrusion.\n"
"Or toggle the defining status of an already imported external geometry.");
sWhatsThis = "Sketcher_Defining";
sStatusTip = sToolTipText;
sPixmap = "Sketcher_Defining";
Expand Down Expand Up @@ -7009,7 +7010,7 @@ void CmdSketcherFixExternal::activated(int iMsg)

bool CmdSketcherFixExternal::isActive(void)
{
return true;
return getExternalSelection(0)!=0;
}


Expand Down
44 changes: 31 additions & 13 deletions src/Mod/Sketcher/Gui/TaskSketcherConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,20 +432,35 @@ class ExpressionDelegate : public QStyledItemDelegate
QListWidget * view;
};

static ConstraintView *_ConstraintViewInstance;

ConstraintView::ConstraintView(QWidget *parent)
: QListWidget(parent)
{
ExpressionDelegate * delegate = new ExpressionDelegate(this);
setItemDelegate(delegate);
_ConstraintViewInstance = this;
}

ConstraintView::~ConstraintView()
{
_ConstraintViewInstance = nullptr;
}

ConstraintView *ConstraintView::getInstance()
{
return _ConstraintViewInstance;
}

void ConstraintView::contextMenuEvent (QContextMenuEvent* event)
{
QMenu menu;
populateMenu(menu);
menu.exec(event->globalPos());
}

void ConstraintView::populateMenu(QMenu &menu)
{
QListWidgetItem* item = currentItem();
QList<QListWidgetItem *> items = selectedItems();

Expand All @@ -456,18 +471,23 @@ void ConstraintView::contextMenuEvent (QContextMenuEvent* event)
// Sync the FreeCAD selection with the selection in the ConstraintView widget
if (didRelease) {
Gui::Selection().clearSelection();
std::string doc_name = static_cast<ConstraintItem*>(item)->sketchView->getSketchObject()->getDocument()->getName();
std::string obj_name = static_cast<ConstraintItem*>(item)->sketchView->getSketchObject()->getNameInDocument();

std::vector<std::string> constraintSubNames;
for (auto&& it : items) {
auto ci = static_cast<ConstraintItem*>(it);
std::string constraint_name = Sketcher::PropertyConstraintList::getConstraintName(ci->ConstraintNbr);
constraintSubNames.push_back(constraint_name.c_str());
}

if(!constraintSubNames.empty())
Gui::Selection().addSelections(doc_name.c_str(), obj_name.c_str(), constraintSubNames);
if (auto parent = qobject_cast<TaskSketcherConstraints*>(parentWidget())) {
if (auto sketchView = parent->getViewProvider()) {
std::string doc_name = sketchView->getSketchObject()->getDocument()->getName();
std::string obj_name = sketchView->getSketchObject()->getNameInDocument();

std::vector<std::string> constraintSubNames;
for (auto&& it : items) {
auto ci = static_cast<ConstraintItem*>(it);
std::string constraint_name = Sketcher::PropertyConstraintList::getConstraintName(ci->ConstraintNbr);
constraintSubNames.push_back(constraint_name.c_str());
}

if(!constraintSubNames.empty())
Gui::Selection().addSelections(doc_name.c_str(), obj_name.c_str(), constraintSubNames);
}
}
}

bool isQuantity = false;
Expand Down Expand Up @@ -524,8 +544,6 @@ void ConstraintView::contextMenuEvent (QContextMenuEvent* event)

QAction* swap = menu.addAction(tr("Swap constraint names"), this, SLOT(swapNamedOfSelectedItems()));
swap->setEnabled(items.size() == 2);

menu.exec(event->globalPos());
}

CONTEXT_MEMBER_DEF("Sketcher_SelectElementsAssociatedWithConstraints",doSelectConstraints)
Expand Down
5 changes: 5 additions & 0 deletions src/Mod/Sketcher/Gui/TaskSketcherConstraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class ConstraintView : public QListWidget
explicit ConstraintView(QWidget *parent = 0);
~ConstraintView();

static ConstraintView *getInstance();
void populateMenu(QMenu &menu);

protected:
void contextMenuEvent (QContextMenuEvent* event);

Expand Down Expand Up @@ -89,6 +92,8 @@ class TaskSketcherConstraints : public Gui::TaskView::TaskBox, public Gui::Selec
/// Observer message from the Selection
void onSelectionChanged(const Gui::SelectionChanges& msg);

ViewProviderSketch *getViewProvider() {return sketchView;}

private:
void slotConstraintsChanged(void);
bool isConstraintFiltered(QListWidgetItem * item);
Expand Down
58 changes: 58 additions & 0 deletions src/Mod/Sketcher/Gui/ViewProviderSketch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
#include <Gui/SoFCUnifiedSelection.h>
#include <Gui/Inventor/MarkerBitmaps.h>
#include <Gui/Inventor/SmSwitchboard.h>
#include <Gui/PieMenu.h>

#include <Mod/Part/App/Geometry.h>
#include <Mod/Part/App/BodyBase.h>
Expand All @@ -125,6 +126,8 @@
#include "DrawSketchHandler.h"
#include "TaskDlgEditSketch.h"
#include "TaskSketcherValidation.h"
#include "TaskSketcherConstraints.h"
#include "Workbench.h"
#include "CommandConstraints.h"
#include "ViewProviderSketchGeometryExtension.h"
#include <Mod/Sketcher/App/SolverGeometryExtension.h>
Expand Down Expand Up @@ -814,6 +817,61 @@ bool ViewProviderSketch::mouseButtonPressed(int Button, bool pressed, const SbVe
return false;
}

if (_Mode == STATUS_NONE && !pressed && Button == 2) {
QMenu menu;
if (Gui::Selection().hasPreselection()) {
auto sel = Gui::Selection().getPreselection();
if (!Gui::Selection().isSelected(sel.pDocName, sel.pObjectName, sel.pSubName, 0)) {
if (!(QApplication::queryKeyboardModifiers() & Qt::ShiftModifier))
Gui::Selection().clearSelection();
Gui::SelectionNoTopParentCheck guard;
Gui::Selection().addSelection(sel.pDocName, sel.pObjectName, sel.pSubName);
}
}
if (!edit->SelConstraintSet.empty()) {
if (auto inst = ConstraintView::getInstance())
inst->populateMenu(menu);
}
else if (!Gui::Selection().hasSelection()) {
Gui::MenuItem mitems;
addSketcherWorkbenchGeometries(mitems);
Gui::MenuManager::getInstance()->setupContextMenu(&mitems, menu);
} else {
Gui::MenuItem mitems;
Gui::MenuItem *cstrItems = new Gui::MenuItem;
cstrItems->setCommand(QT_TRANSLATE_NOOP("Sketcher", "Add constraint"));
addSketcherWorkbenchConstraints(*cstrItems);
Gui::MenuItem *toolsItems = new Gui::MenuItem;
toolsItems->setCommand(QT_TRANSLATE_NOOP("Sketcher", "Tools"));
*toolsItems << "Sketcher_Trimming"
<< "Sketcher_Extend"
<< "Sketcher_Split"
<< "Sketcher_CarbonCopy"
<< "Sketcher_ExportGeometry"
<< "Sketcher_ExportCompound"
<< "Sketcher_SwapGeometryID";
Gui::MenuItem *bsplineItems = new Gui::MenuItem;
bsplineItems->setCommand(QT_TRANSLATE_NOOP("Sketcher", "BSpline tools"));
addSketcherWorkbenchBSplines(*bsplineItems);
mitems << cstrItems
<< "Separator";
if (auto cmd = dynamic_cast<Gui::GroupCommand*>(
Gui::Application::Instance->commandManager().getCommandByName("Sketcher_ExternalCmds"))) {
for(auto c : cmd->getCommands())
mitems << c->getName();
}
mitems << "Separator"
<< "Sketcher_ToggleConstruction"
<< toolsItems
<< bsplineItems
<< "Separator";
addSketcherWorkbenchVirtualSpace(mitems);
Gui::MenuManager::getInstance()->setupContextMenu(&mitems, menu);
}
menu.exec(QCursor::pos());
return true;
}

// Both Mouse button is down, cancel current mode to avoid conflict with
// some navigation method.
auto btns = QApplication::mouseButtons();
Expand Down

1 comment on commit 36be73c

@wohltat
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking forward to try it
👍

Please sign in to comment.