diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bb22aa7..4dcd0a5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ CHANGELOG for FlatCAM Evo beta 21.05.2022 - more code refactored in the appMain.py +- added properties for the mouse position and mouse clicked position (and setters) and cleaned up the code regarding this data 20.05.2022 diff --git a/appEditors/AppGeoEditor.py b/appEditors/AppGeoEditor.py index d5fd6d60..99e75e16 100644 --- a/appEditors/AppGeoEditor.py +++ b/appEditors/AppGeoEditor.py @@ -646,8 +646,8 @@ class FCCircle(FCShapeTool): QtCore.Qt.Key.Key_Plus, QtCore.Qt.Key.Key_Comma, QtCore.Qt.Key.Key_Period, QtCore.Qt.Key.Key_Slash, QtCore.Qt.Key.Key_Asterisk]: - if self.draw_app.app.mouse[0] != self.points[-1][0] or ( - self.draw_app.app.mouse[1] != self.points[-1][1] and + if self.draw_app.app.mouse_pos[0] != self.points[-1][0] or ( + self.draw_app.app.mouse_pos[1] != self.points[-1][1] and self.circle_tool.ui.radius_link_btn.isChecked()): try: # VisPy keys @@ -662,8 +662,8 @@ class FCCircle(FCShapeTool): else: self.circle_tool.radius_x = str(self.circle_tool.radius_x) + chr(key) - if self.draw_app.app.mouse[1] != self.points[-1][1] or ( - self.draw_app.app.mouse[0] != self.points[-1][0] and + if self.draw_app.app.mouse_pos[1] != self.points[-1][1] or ( + self.draw_app.app.mouse_pos[0] != self.points[-1][0] and self.circle_tool.ui.radius_link_btn.isChecked()): try: # VisPy keys @@ -1180,7 +1180,7 @@ class FCRectangle(FCShapeTool): QtCore.Qt.Key.Key_Plus, QtCore.Qt.Key.Key_Comma, QtCore.Qt.Key.Key_Period, QtCore.Qt.Key.Key_Slash, QtCore.Qt.Key.Key_Asterisk]: - if self.draw_app.app.mouse[0] != self.points[-1][0]: + if self.draw_app.app.mouse_pos[0] != self.points[-1][0]: try: # VisPy keys if self.rect_tool.length == 0: @@ -1193,7 +1193,7 @@ class FCRectangle(FCShapeTool): self.rect_tool.length = chr(key) else: self.rect_tool.length = str(self.rect_tool.length) + chr(key) - if self.draw_app.app.mouse[1] != self.points[-1][1]: + if self.draw_app.app.mouse_pos[1] != self.points[-1][1]: try: # VisPy keys if self.rect_tool.width == 0: @@ -1423,7 +1423,7 @@ class FCPolygon(FCShapeTool): return _("Failed.") first_pt = self.points[-1] - last_pt = self.draw_app.app.mouse + last_pt = self.draw_app.app.mouse_pos seg_length = math.sqrt((last_pt[0] - first_pt[0])**2 + (last_pt[1] - first_pt[1])**2) if seg_length == 0.0: @@ -1636,7 +1636,7 @@ class FCPath(FCShapeTool): return _("Failed.") first_pt = self.points[-1] - last_pt = self.draw_app.app.mouse + last_pt = self.draw_app.app.mouse_pos seg_length = math.sqrt((last_pt[0] - first_pt[0])**2 + (last_pt[1] - first_pt[1])**2) if seg_length == 0.0: @@ -2172,7 +2172,7 @@ class FCMove(FCShapeTool): return _("Failed.") first_pt = self.points[-1] - last_pt = self.draw_app.app.mouse + last_pt = self.draw_app.app.mouse_pos seg_length = math.sqrt((last_pt[0] - first_pt[0])**2 + (last_pt[1] - first_pt[1])**2) if seg_length == 0.0: @@ -2688,7 +2688,7 @@ class FCCopy(FCShapeTool): return _("Failed.") first_pt = self.points[-1] - last_pt = self.draw_app.app.mouse + last_pt = self.draw_app.app.mouse_pos seg_length = math.sqrt((last_pt[0] - first_pt[0])**2 + (last_pt[1] - first_pt[1])**2) if seg_length == 0.0: @@ -4303,7 +4303,7 @@ class AppGeoEditor(QtCore.QObject): self.snap_x = x self.snap_y = y - self.app.mouse = [x, y] + self.app.mouse_pos = [x, y] if self.pos is None: self.pos = (0, 0) diff --git a/appEditors/AppGerberEditor.py b/appEditors/AppGerberEditor.py index 3dcb8d2e..34968f87 100644 --- a/appEditors/AppGerberEditor.py +++ b/appEditors/AppGerberEditor.py @@ -2846,7 +2846,7 @@ class ImportEditorGrb(QtCore.QObject, DrawTool): self.snap_x = x self.snap_y = y - self.app.mouse = [x, y] + self.app.mouse_pos = [x, y] if self.pos is None: self.pos = (0, 0) @@ -5395,7 +5395,7 @@ class AppGerberEditor(QtCore.QObject): self.snap_x = x self.snap_y = y - self.app.mouse = [x, y] + self.app.mouse_pos = [x, y] if self.pos is None: self.pos = (0, 0) diff --git a/appGUI/MainGUI.py b/appGUI/MainGUI.py index b8d6d757..a89e9a1a 100644 --- a/appGUI/MainGUI.py +++ b/appGUI/MainGUI.py @@ -3456,11 +3456,11 @@ class MainGUI(QtWidgets.QMainWindow): # Zoom In if key == QtCore.Qt.Key.Key_Equal: - self.app.plotcanvas.zoom(1 / self.app.defaults['global_zoom_ratio'], self.app.mouse) + self.app.plotcanvas.zoom(1 / self.app.defaults['global_zoom_ratio'], self.app.mouse_pos) # Zoom Out if key == QtCore.Qt.Key.Key_Minus: - self.app.plotcanvas.zoom(self.app.defaults['global_zoom_ratio'], self.app.mouse) + self.app.plotcanvas.zoom(self.app.defaults['global_zoom_ratio'], self.app.mouse_pos) # toggle display of Notebook area if key == QtCore.Qt.Key.Key_QuoteLeft: @@ -3904,8 +3904,8 @@ class MainGUI(QtWidgets.QMainWindow): self.app.inform.emit("Click on target point.") self.app.ui.add_pad_ar_btn.setChecked(True) - self.app.grb_editor.x = self.app.mouse[0] - self.app.grb_editor.y = self.app.mouse[1] + self.app.grb_editor.x = self.app.mouse_pos[0] + self.app.grb_editor.y = self.app.mouse_pos[1] self.app.grb_editor.select_tool('array') return @@ -3989,8 +3989,8 @@ class MainGUI(QtWidgets.QMainWindow): self.app.inform.emit(_("Click on target point.")) self.app.ui.add_pad_ar_btn.setChecked(True) - self.app.grb_editor.x = self.app.mouse[0] - self.app.grb_editor.y = self.app.mouse[1] + self.app.grb_editor.x = self.app.mouse_pos[0] + self.app.grb_editor.y = self.app.mouse_pos[1] self.app.grb_editor.select_tool('pad') return @@ -4177,8 +4177,8 @@ class MainGUI(QtWidgets.QMainWindow): self.app.inform.emit(_("Click on target point.")) self.app.ui.add_slot_btn.setChecked(True) - self.app.exc_editor.x = self.app.mouse[0] - self.app.exc_editor.y = self.app.mouse[1] + self.app.exc_editor.x = self.app.mouse_pos[0] + self.app.exc_editor.y = self.app.mouse_pos[1] self.app.exc_editor.select_tool('slot_add') return @@ -4202,8 +4202,8 @@ class MainGUI(QtWidgets.QMainWindow): self.app.inform.emit("Click on target point.") self.app.ui.add_drill_array_btn.setChecked(True) - self.app.exc_editor.x = self.app.mouse[0] - self.app.exc_editor.y = self.app.mouse[1] + self.app.exc_editor.x = self.app.mouse_pos[0] + self.app.exc_editor.y = self.app.mouse_pos[1] self.app.exc_editor.select_tool('drill_array') return @@ -4228,8 +4228,8 @@ class MainGUI(QtWidgets.QMainWindow): self.app.inform.emit(_("Click on target point.")) self.app.ui.add_drill_btn.setChecked(True) - self.app.exc_editor.x = self.app.mouse[0] - self.app.exc_editor.y = self.app.mouse[1] + self.app.exc_editor.x = self.app.mouse_pos[0] + self.app.exc_editor.y = self.app.mouse_pos[1] self.app.exc_editor.select_tool('drill_add') return @@ -4258,8 +4258,8 @@ class MainGUI(QtWidgets.QMainWindow): self.app.inform.emit("Click on target point.") self.app.ui.add_slot_array_btn.setChecked(True) - self.app.exc_editor.x = self.app.mouse[0] - self.app.exc_editor.y = self.app.mouse[1] + self.app.exc_editor.x = self.app.mouse_pos[0] + self.app.exc_editor.y = self.app.mouse_pos[1] self.app.exc_editor.select_tool('slot_array') return diff --git a/appMain.py b/appMain.py index f76cc75d..db9296ef 100644 --- a/appMain.py +++ b/appMain.py @@ -316,14 +316,13 @@ class App(QtCore.QObject): self.rel_point2 = (0, 0) # variable to store coordinates - self.pos = (0, 0) - self.pos_canvas = (0, 0) self.pos_jump = (0, 0) # variable to store mouse coordinates - self.mouse = [0, 0] + self._mouse_click_pos = [0, 0] + self._mouse_pos = [0, 0] - # variable to store the delta positions on cavnas + # variable to store the delta positions on canvas self.dx = 0 self.dy = 0 @@ -5132,8 +5131,8 @@ class App(QtCore.QObject): return if dia_box.reference == 'rel': - rel_x = self.mouse[0] + location[0] - rel_y = self.mouse[1] + location[1] + rel_x = self.mouse_pos[0] + location[0] + rel_y = self.mouse_pos[1] + location[1] location = (rel_x, rel_y) self.options['global_jump_ref'] = dia_box.reference except Exception: @@ -6767,27 +6766,27 @@ class App(QtCore.QObject): was clicked, the pixel coordinates and the axes coordinates. :return: None """ - self.pos = [] - event_pos = event.pos if self.use_3d_engine else (event.xdata, event.ydata) - self.pos_canvas = self.plotcanvas.translate_coords(event_pos) + pos_canvas = self.plotcanvas.translate_coords(event_pos) # So it can receive key presses self.plotcanvas.native.setFocus() if self.grid_status(): - self.pos = self.geo_editor.snap(self.pos_canvas[0], self.pos_canvas[1]) + pos = self.geo_editor.snap(pos_canvas[0], pos_canvas[1]) else: - self.pos = (self.pos_canvas[0], self.pos_canvas[1]) + pos = (pos_canvas[0], pos_canvas[1]) + + self.mouse_click_pos = [pos[0], pos[1]] try: if event.button == 1: # Reset here the relative coordinates so there is a new reference on the click position if self.rel_point1 is None: - self.rel_point1 = self.pos + self.rel_point1 = self.mouse_click_pos else: self.rel_point2 = copy(self.rel_point1) - self.rel_point1 = self.pos + self.rel_point1 = self.mouse_click_pos self.on_mouse_move_over_plot(event, origin_click=True) except Exception as e: @@ -6861,7 +6860,7 @@ class App(QtCore.QObject): self.ui.update_location_labels(self.dx, self.dy, pos[0], pos[1]) self.plotcanvas.on_update_text_hud(self.dx, self.dy, pos[0], pos[1]) - self.mouse = [pos[0], pos[1]] + self.mouse_pos = [pos[0], pos[1]] if self.options['global_selection_shape'] is False: self.selection_type = None @@ -6876,11 +6875,12 @@ class App(QtCore.QObject): if self.event_is_dragging == 1 and event.button == 1: self.delete_selection_shape() if self.dx < 0: - self.draw_moving_selection_shape(self.pos, pos, color=self.options['global_alt_sel_line'], + self.draw_moving_selection_shape(self.mouse_click_pos, self.mouse_pos, + color=self.options['global_alt_sel_line'], face_color=self.options['global_alt_sel_fill']) self.selection_type = False elif self.dx >= 0: - self.draw_moving_selection_shape(self.pos, pos) + self.draw_moving_selection_shape(self.mouse_click_pos, self.mouse_pos) self.selection_type = True else: self.selection_type = None @@ -6923,7 +6923,7 @@ class App(QtCore.QObject): # self.ui.position_label.setText("") # self.ui.rel_position_label.setText("") self.ui.update_location_labels(0.0, 0.0, 0.0, 0.0) - self.mouse = None + self.mouse_pos = [None, None] def on_mouse_click_release_over_plot(self, event): """ @@ -6965,18 +6965,31 @@ class App(QtCore.QObject): ctrl_shift_modifier_key = ctrl_modifier_key | shift_modifier_key if key_modifier == shift_modifier_key or key_modifier == ctrl_shift_modifier_key: - self.on_mouse_and_key_modifiers(position=self.pos, modifiers=key_modifier) + self.on_mouse_and_key_modifiers(position=self.mouse_click_pos, modifiers=key_modifier) self.on_plugin_mouse_click_release(pos=pos) + self.mouse_click_pos = [pos[0], pos[1]] return else: self.on_plugin_mouse_click_release(pos=pos) # the object selection on canvas will not work for App Tools or for Editors if self.call_source != 'app': + self.mouse_click_pos = [pos[0], pos[1]] return if self.doubleclick is True: - self.on_mouse_double_click() + self.doubleclick = False + if self.collection.get_selected(): + self.ui.notebook.setCurrentWidget(self.ui.properties_tab) + if self.ui.splitter.sizes()[0] == 0: + self.ui.splitter.setSizes([1, 1]) + try: + # delete the selection shape(S) as it may be in the way + self.delete_selection_shape() + self.delete_hover_shape() + except Exception as e: + self.log.error("App.on_mouse_click_release_over_plot() double click --> Error: %s" % str(e)) + self.mouse_click_pos = [pos[0], pos[1]] return # WORKAROUND for LEGACY MODE @@ -6987,11 +7000,11 @@ class App(QtCore.QObject): if self.selection_type is not None: try: - self.selection_area_handler(self.pos, pos, self.selection_type) + self.selection_area_handler(self.mouse_click_pos, pos, self.selection_type) self.selection_type = None except Exception as e: - self.log.error( - "FlatCAMApp.on_mouse_click_release_over_plot() select area --> Error: %s" % str(e)) + self.log.error("App.on_mouse_click_release_over_plot() select area --> Error: %s" % str(e)) + self.mouse_click_pos = [pos[0], pos[1]] return if key_modifier == shift_modifier_key: @@ -7014,28 +7027,11 @@ class App(QtCore.QObject): self.delete_hover_shape() except Exception as e: - self.log.error( - "FlatCAMApp.on_mouse_click_release_over_plot() select click --> Error: %s" % str(e)) + self.log.error("App.on_mouse_click_release_over_plot() select click --> Error: %s" % str(e)) + self.mouse_click_pos = [pos[0], pos[1]] return - def on_mouse_double_click(self): - """ - Called when mouse double clicking on canvas. - - :return: - """ - self.doubleclick = False - if self.collection.get_selected(): - self.ui.notebook.setCurrentWidget(self.ui.properties_tab) - if self.ui.splitter.sizes()[0] == 0: - self.ui.splitter.setSizes([1, 1]) - try: - # delete the selection shape(S) as it may be in the way - self.delete_selection_shape() - self.delete_hover_shape() - except Exception as e: - self.log.error( - "FlatCAMApp.on_mouse_click_release_over_plot() double click --> Error: %s" % str(e)) + self.mouse_click_pos = [pos[0], pos[1]] def on_mouse_and_key_modifiers(self, position, modifiers): """ @@ -7113,6 +7109,22 @@ class App(QtCore.QObject): self.ui.popmenu_move2origin.setDisabled(True) self.ui.popmenu_move.setDisabled(True) + @property + def mouse_click_pos(self) -> list[float]: + return [self._mouse_click_pos[0], self._mouse_click_pos[1]] + + @mouse_click_pos.setter + def mouse_click_pos(self, m_pos: list[float] | tuple[float]): + self._mouse_click_pos = m_pos + + @property + def mouse_pos(self) -> list[float]: + return [self._mouse_pos[0], self._mouse_pos[1]] + + @mouse_pos.setter + def mouse_pos(self, m_pos: list[float] | tuple[float]): + self._mouse_pos = m_pos + def selection_area_handler(self, start_pos, end_pos, sel_type): """ Called when the mouse selects by dragging left mouse button on canvas. @@ -7179,7 +7191,7 @@ class App(QtCore.QObject): self.objects_under_the_click_list = [] # Populate the list with the overlapped objects on the click position - curr_x, curr_y = self.pos + curr_x, curr_y = self.mouse_click_pos try: for obj in self.all_objects_list: diff --git a/appPlugins/ToolDblSided.py b/appPlugins/ToolDblSided.py index d40589e7..daddc2df 100644 --- a/appPlugins/ToolDblSided.py +++ b/appPlugins/ToolDblSided.py @@ -532,7 +532,7 @@ class DblSidedTool(AppTool): def on_point_add(self): val = self.app.options["global_point_clipboard_format"] % \ - (self.decimals, self.app.pos[0], self.decimals, self.app.pos[1]) + (self.decimals, self.app.mouse_click_pos[0], self.decimals, self.app.mouse_click_pos[1]) self.ui.point_entry.set_value(val) def on_drill_delete_last(self): diff --git a/appPlugins/ToolIsolation.py b/appPlugins/ToolIsolation.py index b3aa0e52..6e7f05b6 100644 --- a/appPlugins/ToolIsolation.py +++ b/appPlugins/ToolIsolation.py @@ -2518,7 +2518,7 @@ class ToolIsolation(AppTool, Gerber): clicked_poly = self.find_polygon(point=(curr_pos[0], curr_pos[1]), geoset=self.grb_obj.solid_geometry) if self.app.selection_type is not None: - self.selection_area_handler(self.app.pos, curr_pos, self.app.selection_type) + self.selection_area_handler(self.app.mouse_pos, curr_pos, self.app.selection_type) self.app.selection_type = None elif clicked_poly: if clicked_poly not in self.poly_dict.values():