Draft: Edit, rewriting context menu system

get_edit_point_context_menu(self, edit_command, obj, node_idx)
get_edit_obj_context_menu(self, edit_command, obj, position)
that are called depending if the user is over an editpoint or over another part of the object.
This commit is contained in:
carlopav
2021-05-24 23:44:48 +02:00
parent cc87ab4913
commit 28ced10d50
3 changed files with 118 additions and 80 deletions

View File

@@ -678,7 +678,6 @@ class Edit(gui_base_original.Modifier):
def display_tracker_menu(self, event):
self.tracker_menu = QtGui.QMenu()
self.event = event
actions = None
if self.overNode:
@@ -689,22 +688,23 @@ class Edit(gui_base_original.Modifier):
obj_gui_tools = self.get_obj_gui_tools(obj)
if obj_gui_tools:
actions = obj_gui_tools.get_edit_point_context_menu(obj, ep)
actions = obj_gui_tools.get_edit_point_context_menu(self, obj, ep)
else:
# try if user is over an edited object
pos = self.event.getPosition()
pos = event.getPosition()
obj = self.get_selected_obj_at_position(pos)
if utils.get_type(obj) in ["Line", "Wire", "BSpline", "BezCurve"]:
actions = ["add point"]
elif utils.get_type(obj) in ["Circle"] and obj.FirstAngle != obj.LastAngle:
actions = ["invert arc"]
obj_gui_tools = self.get_obj_gui_tools(obj)
if obj_gui_tools:
actions = obj_gui_tools.get_edit_obj_context_menu(self, obj, pos)
if actions is None:
return
for a in actions:
self.tracker_menu.addAction(a)
for (label, callback) in actions:
action = self.tracker_menu.addAction(label)
action.setData(callback)
self.tracker_menu.popup(Gui.getMainWindow().cursor().pos())
@@ -713,32 +713,9 @@ class Edit(gui_base_original.Modifier):
self.evaluate_menu_action)
def evaluate_menu_action(self, labelname):
action_label = str(labelname.text())
doc = None
obj = None
idx = None
if self.overNode:
doc = self.overNode.get_doc_name()
obj = App.getDocument(doc).getObject(self.overNode.get_obj_name())
idx = self.overNode.get_subelement_index()
obj_gui_tools = self.get_obj_gui_tools(obj)
if obj and obj_gui_tools:
actions = obj_gui_tools.evaluate_context_menu_action(self, obj, idx, action_label)
elif action_label == "add point":
if obj and obj_gui_tools:
obj_gui_tools.add_point(self.event)
elif action_label == "invert arc":
pos = self.event.getPosition()
obj = self.get_selected_obj_at_position(pos)
obj_gui_tools.arcInvert(obj)
del self.event
def evaluate_menu_action(self, action):
callback = action.data()
callback()
# -------------------------------------------------------------------------

View File

@@ -61,14 +61,33 @@ class GuiTools:
"""
pass
def get_edit_point_context_menu(self, obj, node_idx):
""" Return a list of Draft_Edit context menu actions.
def get_edit_point_context_menu(self, edit_command, obj, node_idx):
""" Get the context menu associated to edit points (user is over an editpoint)
Return a list of tuples containig menu labels and associated functions:
return [
("action label", lambda: self.handle_action_label(edit_command, obj, node_idx)),
]
Parameters:
edit_command: running Draft_Edit command
obj: the edited object
node_idx: number of the edited node
"""
pass
def evaluate_context_menu_action(self, edit_command, obj, node_idx, action):
""" Do something when a Draft_Edit context menu action is triggered over a node.
def get_edit_obj_context_menu(self, edit_command, obj, position):
""" Get the context menu associated to edited object (user is over the object)
Return a list of tuples containig menu labels and associated functions:
return [
("action label", lambda: self.handle_action_label(edit_command, obj, position)),
]
Parameters:
edit_command: running Draft_Edit command
obj: the edited object
position: position of the cursor on the screen (x, y)
"""
pass

View File

@@ -104,15 +104,23 @@ class DraftWireGuiTools(GuiTools):
pts[node_idx] = v
obj.Points = pts
def get_edit_point_context_menu(self, edit_command, obj, node_idx):
return [
("delete point", lambda: self.handle_delete_point(edit_command, obj, node_idx)),
]
def get_edit_point_context_menu(self, obj, node_idx):
actions = ["delete point"]
return actions
def evaluate_context_menu_action(self, edit_command, obj, node_idx, action):
if action == "delete point":
self.delete_point(obj, node_idx)
edit_command.resetTrackers(obj)
def get_edit_obj_context_menu(self, edit_command, obj, position):
return [
("add point", lambda: self.handle_add_point(edit_command, obj, position)),
]
def handle_delete_point(self, edit_command, obj, node_idx):
self.delete_point(obj, node_idx)
edit_command.resetTrackers(obj)
def handle_add_point(self, edit_command, obj, pos):
self.add_point(edit_command, obj, pos)
edit_command.resetTrackers(obj)
def init_preview_object(self, obj):
return trackers.wireTracker(obj.Shape)
@@ -348,26 +356,50 @@ class DraftCircleGuiTools(GuiTools):
obj.Radius = v.Length
def get_edit_point_context_menu(self, obj, node_idx):
def get_edit_point_context_menu(self, edit_command, obj, node_idx):
actions = None
if obj.FirstAngle != obj.LastAngle:
if node_idx == 0: # user is over arc start point
actions = ["move arc"]
return [
("move arc", lambda: self.handle_move_arc(edit_command, obj, node_idx)),
]
elif node_idx == 1: # user is over arc start point
actions = ["set first angle"]
return [
("mset first angle", lambda: self.handle_set_first_angle(edit_command, obj, node_idx)),
]
elif node_idx == 2: # user is over arc end point
actions = ["set last angle"]
return [
("set last angle", lambda: self.handle_set_last_angle(edit_command, obj, node_idx)),
]
elif node_idx == 3: # user is over arc mid point
actions = ["set radius"]
if actions:
return actions
return [
("set radius", lambda: self.handle_set_radius(edit_command, obj, node_idx)),
]
def handle_move_arc(self, edit_command, obj, node_idx):
edit_command.alt_edit_mode = 1
edit_command.startEditing(obj, node_idx)
def evaluate_context_menu_action(self, edit_command, obj, node_idx, action):
if action in ("move arc", "set radius",
"set first angle", "set last angle"):
edit_command.alt_edit_mode = 1
edit_command.startEditing(obj, node_idx)
def handle_set_first_angle(self, edit_command, obj, node_idx):
edit_command.alt_edit_mode = 1
edit_command.startEditing(obj, node_idx)
def handle_set_last_angle(self, edit_command, obj, node_idx):
edit_command.alt_edit_mode = 1
edit_command.startEditing(obj, node_idx)
def handle_set_radius(self, edit_command, obj, node_idx):
edit_command.alt_edit_mode = 1
edit_command.startEditing(obj, node_idx)
def get_edit_obj_context_menu(self, edit_command, obj, position):
return [
("invert arc", lambda: self.handle_invert_arc(edit_command, obj, position)),
]
def handle_invert_arc(self, edit_command, obj, position):
self.arcInvert(obj)
edit_command.resetTrackers(obj)
def init_preview_object(self, obj):
return trackers.arcTracker()
@@ -574,28 +606,38 @@ class DraftBezCurveGuiTools(GuiTools):
obj.Points = pts
def get_edit_point_context_menu(self, obj, node_idx):
if utils.get_type(obj) in ["Line", "Wire", "BSpline"]:
actions = ["delete point"]
elif utils.get_type(obj) in ["BezCurve"]:
actions = ["make sharp", "make tangent",
"make symmetric", "delete point"]
return actions
def get_edit_point_context_menu(self, edit_command, obj, node_idx):
return [
("make sharp", lambda: self.handle_make_sharp(edit_command, obj, node_idx)),
("make tangent", lambda: self.handle_make_tangent(edit_command, obj, node_idx)),
("make symmetric", lambda: self.handle_make_symmetric(edit_command, obj, node_idx)),
("delete point", lambda: self.handle_delete_point(edit_command, obj, node_idx)),
]
def get_edit_obj_context_menu(self, edit_command, obj, position):
return [
("add point", lambda: self.handle_add_point(edit_command, obj, position)),
]
def evaluate_context_menu_action(self, edit_command, obj, node_idx, action):
if action == "delete point":
self.delete_point(obj, node_idx)
edit_command.resetTrackers(obj)
# Bezier curve menu
elif action in ["make sharp", "make tangent", "make symmetric"]:
if action == "make sharp":
self.smoothBezPoint(obj, node_idx, 'Sharp')
elif action == "make tangent":
self.smoothBezPoint(obj, node_idx, 'Tangent')
elif action == "make symmetric":
self.smoothBezPoint(obj, node_idx, 'Symmetric')
edit_command.resetTrackers(obj)
def handle_make_sharp(self, edit_command, obj, node_idx):
self.smoothBezPoint(obj, node_idx, 'Sharp')
edit_command.resetTrackers(obj)
def handle_make_tangent(self, edit_command, obj, node_idx):
self.smoothBezPoint(obj, node_idx, 'Tangent')
edit_command.resetTrackers(obj)
def handle_make_symmetric(self, edit_command, obj, node_idx):
self.smoothBezPoint(obj, node_idx, 'Symmetric')
edit_command.resetTrackers(obj)
def handle_delete_point(self, edit_command, obj, node_idx):
self.delete_point(obj, node_idx)
edit_command.resetTrackers(obj)
def handle_add_point(self, edit_command, obj, pos):
self.add_point(edit_command, obj, pos)
edit_command.resetTrackers(obj)
def init_preview_object(self, obj):