From 9055cc1230f61f37d452031f07230b563f1cca12 Mon Sep 17 00:00:00 2001 From: Marius Stanciu Date: Sun, 22 May 2022 01:40:55 +0300 Subject: [PATCH] - in Gerber Editor upgraded the PadAdd GUI --- CHANGELOG.md | 4 + appEditors/AppExcEditor.py | 4 +- appEditors/AppGerberEditor.py | 199 +++++++++++++++++++++-- appEditors/exc_plugins/ExcDrillPlugin.py | 2 +- appEditors/grb_plugins/GrbPadPlugin.py | 196 ++++++++++++++++++++++ appGUI/MainGUI.py | 34 ++-- 6 files changed, 409 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dcd0a5a..c4e4dc07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ CHANGELOG for FlatCAM Evo beta ================================================= +22.05.2022 + +- in Gerber Editor upgraded the PadAdd GUI + 21.05.2022 - more code refactored in the appMain.py diff --git a/appEditors/AppExcEditor.py b/appEditors/AppExcEditor.py index 1e430d5f..2c47de85 100644 --- a/appEditors/AppExcEditor.py +++ b/appEditors/AppExcEditor.py @@ -3064,7 +3064,7 @@ class AppExcEditor(QtCore.QObject): self.ui.deltool_btn.clicked.connect(self.on_tool_delete) # self.ui.tools_table_exc.selectionModel().currentChanged.connect(self.on_row_selected) self.ui.tools_table_exc.cellPressed.connect(self.on_row_selected) - self.ui.tools_table_exc.selectionModel().selectionChanged.connect(self.on_table_selection) + self.ui.tools_table_exc.selectionModel().selectionChanged.connect(self.on_table_selection) # noqa self.app.ui.exc_add_array_drill_menuitem.triggered.connect(self.exc_add_drill_array) self.app.ui.exc_add_drill_menuitem.triggered.connect(self.exc_add_drill) @@ -4634,7 +4634,7 @@ class AppExcEditor(QtCore.QObject): self.app.plotcanvas.on_update_text_hud(self.app.dx, self.app.dy, x, y) # ## Utility geometry (animated) - self.update_utility_geometry(data=(x, y)) + # self.update_utility_geometry(data=(x, y)) self.update_utility_geometry(data=(x, y)) if self.active_tool.name in [ diff --git a/appEditors/AppGerberEditor.py b/appEditors/AppGerberEditor.py index 34968f87..eb047b9f 100644 --- a/appEditors/AppGerberEditor.py +++ b/appEditors/AppGerberEditor.py @@ -11,6 +11,8 @@ from camlib import distance, arc, three_point_circle, flatten_shapely_geometry from appGUI.GUIElements import * from appTool import AppTool +from appEditors.grb_plugins.GrbPadPlugin import PadEditorTool + from appEditors.grb_plugins.GrbBufferPlugin import BufferEditorTool from appEditors.grb_plugins.GrbTransformationPlugin import TransformEditorTool from appEditors.grb_plugins.GrbSimplificationPlugin import SimplificationTool @@ -39,6 +41,7 @@ class PadEditorGrb(ShapeToolEditorGrb): DrawTool.__init__(self, draw_app) self.name = 'pad' self.draw_app = draw_app + self.app = self.draw_app.app self.dont_execute = False try: @@ -63,6 +66,15 @@ class PadEditorGrb(ShapeToolEditorGrb): self.draw_app.select_tool('select') return + if self.app.use_3d_engine: + self.draw_app.app.plotcanvas.view.camera.zoom_callback = self.draw_cursor_data + self.draw_app.app.jump_signal.connect(lambda x: self.draw_app.update_utility_geometry(data=x)) + + self.pad_tool = PadEditorTool(self.app, self.draw_app, plugin_name=_("Pad")) + self.pad_tool.run() + self.pad_tool.length = self.draw_app.last_length + self.ui = self.pad_tool.ui + if self.radius == 0: self.draw_app.app.inform.emit('[WARNING_NOTCL] %s' % _("Aperture size is zero. It needs to be greater than zero.")) @@ -84,30 +96,57 @@ class PadEditorGrb(ShapeToolEditorGrb): except KeyError: pass - geo = self.utility_geometry(data=(self.draw_app.snap_x, self.draw_app.snap_y)) + geo = self.utility_geometry(data=self.draw_app.app.mouse_pos) if isinstance(geo, DrawToolShape) and geo.geo is not None: self.draw_app.draw_utility_geometry(geo_shape=geo) - self.draw_app.app.inform.emit(_("Click to place ...")) - self.draw_app.app.jump_signal.connect(lambda x: self.draw_app.update_utility_geometry(data=x)) - # Switch notebook to Properties page - self.draw_app.app.ui.notebook.setCurrentWidget(self.draw_app.app.ui.properties_tab) + self.app.ui.notebook.setTabText(2, _("Pad")) + if self.draw_app.app.ui.splitter.sizes()[0] == 0: + self.draw_app.app.ui.splitter.setSizes([1, 1]) + self.set_plugin_ui() + + # Signals + try: + self.ui.add_btn.clicked.disconnect() + except (AttributeError, TypeError): + pass + self.ui.add_btn.clicked.connect(self.on_add_pad) + + self.draw_app.app.inform.emit(_("Click to place ...")) self.start_msg = _("Click to place ...") + def set_plugin_ui(self): + dia = float(self.draw_app.storage_dict[self.draw_app.last_aperture_selected]['size']) + self.ui.dia_entry.set_value(dia) + self.ui.x_entry.set_value(float(self.draw_app.snap_x)) + self.ui.y_entry.set_value(float(self.draw_app.snap_y)) + def click(self, point): + self.points = point + self.draw_app.last_length = self.pad_tool.length + self.ui.x_entry.set_value(float(self.draw_app.snap_x)) + self.ui.y_entry.set_value(float(self.draw_app.snap_y)) self.make() return "Done." + def on_add_pad(self): + self.draw_app.last_length = self.pad_tool.length + self.points = self.ui.x_entry.get_value(), self.ui.y_entry.get_value() + self.make() + self.draw_app.on_grb_shape_complete(self.draw_app.current_storage) + self.draw_app.build_ui() + self.draw_app.select_tool("pad") + def utility_geometry(self, data=None): if self.dont_execute is True: self.draw_app.select_tool('select') return - self.points = data - geo_data = self.util_shape(data) + pos = data if data else self.points + geo_data = self.util_shape(pos) if geo_data: return DrawToolUtilityShape(geo_data) else: @@ -128,7 +167,6 @@ class PadEditorGrb(ShapeToolEditorGrb): self.half_height = float(self.draw_app.storage_dict[self.draw_app.last_aperture_selected]['height']) / 2 except KeyError: pass - if point[0] is None and point[1] is None: point_x = self.draw_app.x point_y = self.draw_app.y @@ -230,13 +268,129 @@ class PadEditorGrb(ShapeToolEditorGrb): self.draw_app.in_action = False self.complete = True + + try: + self.draw_app.app.jump_signal.disconnect() + except (TypeError, AttributeError): + pass + self.draw_app.app.inform.emit('[success] %s' % _("Done.")) - self.draw_app.app.jump_signal.disconnect() + + def draw_cursor_data(self, pos=None, delete=False): + if pos is None: + pos = self.draw_app.snap_x, self.draw_app.snap_y + + if delete: + if self.draw_app.app.use_3d_engine: + self.draw_app.app.plotcanvas.text_cursor.parent = None + self.draw_app.app.plotcanvas.view.camera.zoom_callback = lambda *args: None + return + + # font size + qsettings = QtCore.QSettings("Open Source", "FlatCAM") + if qsettings.contains("hud_font_size"): + fsize = qsettings.value('hud_font_size', type=int) + else: + fsize = 8 + + old_x = self.ui.x_entry.get_value() + old_y = self.ui.y_entry.get_value() + + x = pos[0] + y = pos[1] + try: + length = abs(np.sqrt((x - old_x) ** 2 + (y - old_y) ** 2)) + except IndexError: + length = self.draw_app.app.dec_format(0.0, self.draw_app.app.decimals) + units = self.draw_app.app.app_units.lower() + + x_dec = str(self.draw_app.app.dec_format(x, self.draw_app.app.decimals)) if x else '0.0' + y_dec = str(self.draw_app.app.dec_format(y, self.draw_app.app.decimals)) if y else '0.0' + length_dec = str(self.draw_app.app.dec_format(length, self.draw_app.app.decimals)) if length else '0.0' + + l1_txt = 'X: %s [%s]' % (x_dec, units) + l2_txt = 'Y: %s [%s]' % (y_dec, units) + l3_txt = 'L: %s [%s]' % (length_dec, units) + cursor_text = '%s\n%s\n\n%s' % (l1_txt, l2_txt, l3_txt) + + if self.draw_app.app.use_3d_engine: + new_pos = self.draw_app.app.plotcanvas.translate_coords_2((x, y)) + x, y, __, ___ = self.draw_app.app.plotcanvas.translate_coords((new_pos[0]+30, new_pos[1])) + + # text + self.draw_app.app.plotcanvas.text_cursor.font_size = fsize + self.draw_app.app.plotcanvas.text_cursor.text = cursor_text + self.draw_app.app.plotcanvas.text_cursor.pos = x, y + self.draw_app.app.plotcanvas.text_cursor.anchors = 'left', 'top' + + if self.draw_app.app.plotcanvas.text_cursor.parent is None: + self.draw_app.app.plotcanvas.text_cursor.parent = self.draw_app.app.plotcanvas.view.scene + + def on_key(self, key): + # Jump to coords + if key == QtCore.Qt.Key.Key_J or key == 'J': + self.draw_app.app.on_jump_to() + + if key in [str(i) for i in range(10)] + ['.', ',', '+', '-', '/', '*'] or \ + key in [QtCore.Qt.Key.Key_0, QtCore.Qt.Key.Key_0, QtCore.Qt.Key.Key_1, QtCore.Qt.Key.Key_2, + QtCore.Qt.Key.Key_3, QtCore.Qt.Key.Key_4, QtCore.Qt.Key.Key_5, QtCore.Qt.Key.Key_6, + QtCore.Qt.Key.Key_7, QtCore.Qt.Key.Key_8, QtCore.Qt.Key.Key_9, QtCore.Qt.Key.Key_Minus, + 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]: + try: + # VisPy keys + if self.pad_tool.length == self.draw_app.last_length: + self.pad_tool.length = str(key.name) + self.new_segment = False + else: + self.pad_tool.length = str(self.pad_tool.length) + str(key.name) + except AttributeError: + # Qt keys + if self.pad_tool.length == self.draw_app.last_length: + self.pad_tool.length = chr(key) + else: + self.pad_tool.length = str(self.pad_tool.length) + chr(key) + + if key == 'Enter' or key == QtCore.Qt.Key.Key_Return or key == QtCore.Qt.Key.Key_Enter: + if self.pad_tool.length != 0: + target_length = self.pad_tool.length + if target_length is None: + self.pad_tool.length = 0.0 + return _("Failed.") + + first_pt = self.ui.x_entry.get_value(), self.ui.y_entry.get_value() + 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: + self.draw_app.app.log.debug("PadEditorGrb.on_key() --> 'ENTER'. Segment is zero.") + return + try: + new_x = first_pt[0] + (last_pt[0] - first_pt[0]) / seg_length * target_length + new_y = first_pt[1] + (last_pt[1] - first_pt[1]) / seg_length * target_length + except ZeroDivisionError as err: + self.clean_up() + return '[ERROR_NOTCL] %s %s' % (_("Failed."), str(err).capitalize()) + + if first_pt != (new_x, new_y): + self.draw_app.app.on_jump_to(custom_location=(new_x, new_y), fit_center=False) + if len(self.points) > 0: + msg = '%s: %s. %s' % ( + _("Projected"), str(self.pad_tool.length), + _("Click on next Point or click right mouse button to complete ...")) + self.draw_app.app.inform.emit(msg) + # self.interpolate_length = '' + # return "Click on next point or hit ENTER to complete ..." def clean_up(self): self.draw_app.selected = [] self.draw_app.ui.apertures_table.clearSelection() self.draw_app.plot_all() + + if self.draw_app.app.use_3d_engine: + self.draw_app.app.plotcanvas.text_cursor.parent = None + self.draw_app.app.plotcanvas.view.camera.zoom_callback = lambda *args: None + try: self.draw_app.app.jump_signal.disconnect() except (TypeError, AttributeError): @@ -3275,6 +3429,20 @@ class AppGerberEditor(QtCore.QObject): self.snap_y = None self.pos = None + # ############################################################################################################# + # Plugin Attributes + # ############################################################################################################# + self.last_length = 0.0 + + self.last_parray_type = None + self.last_parray_size = None + self.last_parray_lin_dir = None + self.last_parray_circ_dir = None + self.last_parray_pitch = None + self.last_parray_lin_angle = None + self.last_parray_circ_angle = None + self.last_parray_radius = None + # used in RegionEditorGrb and TrackEditorGrb. Will store the bending mode self.bend_mode = 1 @@ -5418,13 +5586,21 @@ class AppGerberEditor(QtCore.QObject): self.app.plotcanvas.on_update_text_hud(self.app.dx, self.app.dy, x, y) self.update_utility_geometry(data=(x, y)) + if self.active_tool.name in [ + 'pad', + ]: + try: + self.active_tool.draw_cursor_data(pos=self.app.mouse_pos) + except AttributeError: + # this can happen if the method is not implemented yet for the active_tool + pass # # ## Selection area on canvas section # ## if event_is_dragging == 1 and event.button == 1: # I make an exception for RegionEditorGrb and TrackEditorGrb because clicking and dragging while making # regions can create strange issues like missing a point in a track/region if isinstance(self.active_tool, RegionEditorGrb) or isinstance(self.active_tool, TrackEditorGrb): - pass + self.app.selection_type = None else: dx = pos_canvas[0] - self.pos[0] self.app.delete_selection_shape() @@ -5557,6 +5733,9 @@ class AppGerberEditor(QtCore.QObject): self.shapes.add(shape=geometry, color=color, face_color=color, layer=0, tolerance=self.tolerance) + def on_shape_complete(self): + pass + @property def visible(self): return self.shapes.visible diff --git a/appEditors/exc_plugins/ExcDrillPlugin.py b/appEditors/exc_plugins/ExcDrillPlugin.py index b5c396cb..660e80b6 100644 --- a/appEditors/exc_plugins/ExcDrillPlugin.py +++ b/appEditors/exc_plugins/ExcDrillPlugin.py @@ -117,7 +117,7 @@ class ExcDrillEditorUI: self.drill_tool_frame.setLayout(self.editor_vbox) # Position - self.tool_lbl = FCLabel('%s' % _("Tool Diameter"), bold=True, color='blue') + self.tool_lbl = FCLabel('%s' % _("Diameter"), bold=True, color='blue') self.editor_vbox.addWidget(self.tool_lbl) # ############################################################################################################# # Diameter Frame diff --git a/appEditors/grb_plugins/GrbPadPlugin.py b/appEditors/grb_plugins/GrbPadPlugin.py index e69de29b..b11d1998 100644 --- a/appEditors/grb_plugins/GrbPadPlugin.py +++ b/appEditors/grb_plugins/GrbPadPlugin.py @@ -0,0 +1,196 @@ + +from appTool import * + +fcTranslate.apply_language('strings') +if '_' not in builtins.__dict__: + _ = gettext.gettext + + +class PadEditorTool(AppToolEditor): + """ + Simple input for buffer distance. + """ + + def __init__(self, app, draw_app, plugin_name): + AppToolEditor.__init__(self, app) + + self.draw_app = draw_app + self.decimals = app.decimals + self.plugin_name = plugin_name + + self.ui = PadEditorUI(layout=self.layout, pad_class=self, plugin_name=plugin_name) + + self.connect_signals_at_init() + self.set_tool_ui() + + def connect_signals_at_init(self): + # Signals + self.ui.clear_btn.clicked.connect(self.on_clear) + + def disconnect_signals(self): + # Signals + try: + self.ui.clear_btn.clicked.disconnect() + except (TypeError, AttributeError): + pass + + def run(self): + self.app.defaults.report_usage("Gerber Editor PadEditorTool()") + super().run() + + # if the splitter us hidden, display it + if self.app.ui.splitter.sizes()[0] == 0: + self.app.ui.splitter.setSizes([1, 1]) + + # if the Tool Tab is hidden display it, else hide it but only if the objectName is the same + found_idx = None + for idx in range(self.app.ui.notebook.count()): + if self.app.ui.notebook.widget(idx).objectName() == "plugin_tab": + found_idx = idx + break + # show the Tab + if not found_idx: + try: + self.app.ui.notebook.addTab(self.app.ui.plugin_tab, _("Plugin")) + except RuntimeError: + self.app.ui.plugin_tab = QtWidgets.QWidget() + self.app.ui.plugin_tab.setObjectName("plugin_tab") + self.app.ui.plugin_tab_layout = QtWidgets.QVBoxLayout(self.app.ui.plugin_tab) + self.app.ui.plugin_tab_layout.setContentsMargins(2, 2, 2, 2) + + self.app.ui.plugin_scroll_area = VerticalScrollArea() + self.app.ui.plugin_tab_layout.addWidget(self.app.ui.plugin_scroll_area) + self.app.ui.notebook.addTab(self.app.ui.plugin_tab, _("Plugin")) + + # focus on Tool Tab + self.app.ui.notebook.setCurrentWidget(self.app.ui.plugin_tab) + + # self.app.ui.notebook.callback_on_close = self.on_tab_close + + self.app.ui.notebook.setTabText(2, self.plugin_name) + + def set_tool_ui(self): + # Init appGUI + self.length = 0.0 + + def on_tab_close(self): + self.disconnect_signals() + self.hide_tool() + # self.app.ui.notebook.callback_on_close = lambda: None + + def on_clear(self): + self.set_tool_ui() + + @property + def length(self): + return self.ui.project_line_entry.get_value() + + @length.setter + def length(self, val): + self.ui.project_line_entry.set_value(val) + + def hide_tool(self): + self.ui.pad_tool_frame.hide() + self.app.ui.notebook.setCurrentWidget(self.app.ui.properties_tab) + if self.draw_app.active_tool.name != 'select': + self.draw_app.select_tool("select") + + +class PadEditorUI: + + def __init__(self, layout, pad_class, plugin_name): + self.pluginName = plugin_name + self.ed_class = pad_class + self.decimals = self.ed_class.app.decimals + self.layout = layout + + # Title + title_label = FCLabel("%s" % ('Editor ' + self.pluginName), size=16, bold=True) + self.layout.addWidget(title_label) + + # this way I can hide/show the frame + self.pad_tool_frame = QtWidgets.QFrame() + self.pad_tool_frame.setContentsMargins(0, 0, 0, 0) + self.layout.addWidget(self.pad_tool_frame) + self.editor_vbox = QtWidgets.QVBoxLayout() + self.editor_vbox.setContentsMargins(0, 0, 0, 0) + self.pad_tool_frame.setLayout(self.editor_vbox) + + # Position + self.tool_lbl = FCLabel('%s' % _("Diameter"), bold=True, color='blue') + self.editor_vbox.addWidget(self.tool_lbl) + # ############################################################################################################# + # Diameter Frame + # ############################################################################################################# + dia_frame = FCFrame() + self.editor_vbox.addWidget(dia_frame) + + dia_grid = GLay(v_spacing=5, h_spacing=3, c_stretch=[0, 1, 0]) + dia_frame.setLayout(dia_grid) + + # Dia Value + self.dia_lbl = FCLabel('%s:' % _("Value")) + self.dia_entry = NumericalEvalEntry(border_color='#0069A9') + self.dia_entry.setDisabled(True) + self.dia_unit = FCLabel('%s' % 'mm') + + dia_grid.addWidget(self.dia_lbl, 0, 0) + dia_grid.addWidget(self.dia_entry, 0, 1) + dia_grid.addWidget(self.dia_unit, 0, 2) + + # Position + self.pos_lbl = FCLabel('%s' % _("Position"), bold=True, color='red') + self.editor_vbox.addWidget(self.pos_lbl) + # ############################################################################################################# + # Position Frame + # ############################################################################################################# + pos_frame = FCFrame() + self.editor_vbox.addWidget(pos_frame) + + pos_grid = GLay(v_spacing=5, h_spacing=3) + pos_frame.setLayout(pos_grid) + + # X Pos + self.x_lbl = FCLabel('%s:' % _("X")) + self.x_entry = FCDoubleSpinner() + self.x_entry.set_precision(self.decimals) + self.x_entry.set_range(-10000.0000, 10000.0000) + pos_grid.addWidget(self.x_lbl, 2, 0) + pos_grid.addWidget(self.x_entry, 2, 1) + + # Y Pos + self.y_lbl = FCLabel('%s:' % _("Y")) + self.y_entry = FCDoubleSpinner() + self.y_entry.set_precision(self.decimals) + self.y_entry.set_range(-10000.0000, 10000.0000) + pos_grid.addWidget(self.y_lbl, 4, 0) + pos_grid.addWidget(self.y_entry, 4, 1) + + # ############################################################################################################# + # Projection Frame + # ############################################################################################################# + pro_frame = FCFrame() + self.editor_vbox.addWidget(pro_frame) + + pro_grid = GLay(v_spacing=5, h_spacing=3, c_stretch=[0, 1, 0]) + pro_frame.setLayout(pro_grid) + + # Project distance + self.project_line_lbl = FCLabel('%s:' % _("Projection")) + self.project_line_lbl.setToolTip( + _("Length of the current segment/move.") + ) + self.project_line_entry = NumericalEvalEntry(border_color='#0069A9') + pro_grid.addWidget(self.project_line_lbl, 0, 0) + pro_grid.addWidget(self.project_line_entry, 0, 1) + + self.clear_btn = QtWidgets.QToolButton() + self.clear_btn.setIcon(QtGui.QIcon(self.ed_class.app.resource_location + '/trash32.png')) + pro_grid.addWidget(self.clear_btn, 0, 2) + + self.add_btn = FCButton(_("Add")) + self.add_btn.setIcon(QtGui.QIcon(self.ed_class.app.resource_location + '/plus32.png')) + self.editor_vbox.addWidget(self.add_btn) + + GLay.set_common_column_size([dia_grid, pos_grid, pro_grid], 0) + self.layout.addStretch(1) diff --git a/appGUI/MainGUI.py b/appGUI/MainGUI.py index a89e9a1a..3d43c108 100644 --- a/appGUI/MainGUI.py +++ b/appGUI/MainGUI.py @@ -3868,23 +3868,23 @@ class MainGUI(QtWidgets.QMainWindow): self.on_toggle_notebook() return - # Switch to Project Tab - if key == QtCore.Qt.Key.Key_1 or key == '1': - self.app.grb_editor.launched_from_shortcuts = True - self.on_select_tab('project') - return - - # Switch to Selected Tab - if key == QtCore.Qt.Key.Key_2 or key == '2': - self.app.grb_editor.launched_from_shortcuts = True - self.on_select_tab('selected') - return - - # Switch to Tool Tab - if key == QtCore.Qt.Key.Key_3 or key == '3': - self.app.grb_editor.launched_from_shortcuts = True - self.on_select_tab('tool') - return + # # Switch to Project Tab + # if key == QtCore.Qt.Key.Key_1 or key == '1': + # self.app.grb_editor.launched_from_shortcuts = True + # self.on_select_tab('project') + # return + # + # # Switch to Selected Tab + # if key == QtCore.Qt.Key.Key_2 or key == '2': + # self.app.grb_editor.launched_from_shortcuts = True + # self.on_select_tab('selected') + # return + # + # # Switch to Tool Tab + # if key == QtCore.Qt.Key.Key_3 or key == '3': + # self.app.grb_editor.launched_from_shortcuts = True + # self.on_select_tab('tool') + # return # we do this so we can reuse the following keys while inside a Tool # the above keys are general enough so were left outside