Draft: Edit, moved resetTrackers for context menu actions to main module

The wrapper allows to call resetTrackers in the main module after the callback to the GuiTools is executed.

This is the last commit, many thanks to @matthijskooijman for having menthored me :)

I think it's helpful to have @matthijskooijman explanation on this use of the wrapper:

This defines a new wrapper function, that calls the original callback and then calls resetTrackers. Note that this creates a new function for every loop iteration, so each of these wrapper functions captures potentially different callback, self and obj values so things work as expected. Note I did something weird with the callback value there: Since functions like these capture a variable, not its value at the time of function definition, and loop variables like label and callback are a single variable shared between all loop iterations, capturing callback directly ends up with all wrappers calling the last callback (i.e. they all capture the same variable and by the time the wrappers are called, that variable will contain the last of the callbacks). This is commonly solved by using a default value in the function definition, since such a default value uses the value of the (in this case) callback variable, not capturing the variable.
This commit is contained in:
carlopav
2021-05-25 00:04:38 +02:00
parent 4db4d1c34f
commit d2d570f875
2 changed files with 14 additions and 47 deletions

View File

@@ -703,8 +703,12 @@ class Edit(gui_base_original.Modifier):
return
for (label, callback) in actions:
def wrapper(callback=callback):
callback()
self.resetTrackers(obj)
action = self.tracker_menu.addAction(label)
action.setData(callback)
action.setData(wrapper)
self.tracker_menu.popup(Gui.getMainWindow().cursor().pos())

View File

@@ -106,27 +106,15 @@ class DraftWireGuiTools(GuiTools):
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)),
("delete point", lambda: self.delete_point(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)),
("reverse wire", lambda: self.handle_reverse_wire(edit_command, obj, position)),
("add point", lambda: self.add_point(edit_command, obj, position)),
("reverse wire", lambda: self.reverse_wire(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 handle_reverse_wire(self, edit_command, obj, pos):
self.reverse_wire(obj)
edit_command.resetTrackers(obj)
def init_preview_object(self, obj):
return trackers.wireTracker(obj.Shape)
@@ -402,13 +390,9 @@ class DraftCircleGuiTools(GuiTools):
def get_edit_obj_context_menu(self, edit_command, obj, position):
return [
("invert arc", lambda: self.handle_invert_arc(edit_command, obj, position)),
("invert arc", lambda: self.arcInvert(obj)),
]
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()
@@ -616,38 +600,17 @@ class DraftBezCurveGuiTools(GuiTools):
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)),
("make sharp", lambda: self.smoothBezPoint(obj, node_idx, 'Sharp')),
("make tangent", lambda: self.smoothBezPoint(obj, node_idx, 'Tangent')),
("make symmetric", lambda: self.smoothBezPoint(obj, node_idx, 'Symmetric')),
("delete point", lambda: self.delete_point(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)),
("add point", lambda: self.add_point(edit_command, obj, position)),
]
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):
return trackers.bezcurveTracker()