From 3a337212f0c0d4f289fafee8d20bc25f8cdca800 Mon Sep 17 00:00:00 2001 From: Marius Stanciu Date: Tue, 12 May 2020 01:09:00 +0300 Subject: [PATCH] - fix issue in Legacy2D graphic mode where the snap function was not accessible when the PlotCanvasLegacy class was created - modified the HUD in Legacy2D when using Dark Theme to use different colors - modified how the graphic engine change act in Preferences: now only by clicking Apply(or Save) the change will happen. And there is also a message asking for confirmation --- CHANGELOG.md | 3 + FlatCAMApp.py | 3 +- flatcamGUI/PlotCanvasLegacy.py | 86 +++++++++++++++---- .../preferences/PreferencesUIManager.py | 28 ++++-- 4 files changed, 93 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 938e7d72..8e1866d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ CHANGELOG for FlatCAM beta - fixed some minor issues (in the HUD added a separating line, missing an icon in toolbars on first launch) - made sure that the corner snap buttons are shown only in Editors - changed the HUD color when using Dark theme +- fix issue in Legacy2D graphic mode where the snap function was not accessible when the PlotCanvasLegacy class was created +- modified the HUD in Legacy2D when using Dark Theme to use different colors +- modified how the graphic engine change act in Preferences: now only by clicking Apply(or Save) the change will happen. And there is also a message asking for confirmation 10.05.2020 diff --git a/FlatCAMApp.py b/FlatCAMApp.py index 106b1496..c0007c80 100644 --- a/FlatCAMApp.py +++ b/FlatCAMApp.py @@ -981,7 +981,6 @@ class App(QtCore.QObject): # ########################################################################################################### # ######################################## GUI SETTINGS SIGNALS ############################################# # ########################################################################################################### - self.ui.general_defaults_form.general_app_group.ge_radio.activated_custom.connect(self.on_app_restart) self.ui.general_defaults_form.general_app_set_group.cursor_radio.activated_custom.connect(self.on_cursor_type) # ######################################## Tools related signals ############################################ @@ -4621,7 +4620,7 @@ class App(QtCore.QObject): if self.is_legacy is False: # HACK: enabling/disabling the cursor seams to somehow update the shapes on screen # - perhaps is a bug in VisPy implementation - if self.grid_status() is True: + if self.grid_status(): self.app_cursor.enabled = False self.app_cursor.enabled = True else: diff --git a/flatcamGUI/PlotCanvasLegacy.py b/flatcamGUI/PlotCanvasLegacy.py index 7856bb8f..0fabff1e 100644 --- a/flatcamGUI/PlotCanvasLegacy.py +++ b/flatcamGUI/PlotCanvasLegacy.py @@ -19,6 +19,8 @@ from shapely.geometry import Polygon, LineString, LinearRing from copy import deepcopy import logging +import numpy as np + import gettext import FlatCAMTranslation as fcTranslate import builtins @@ -153,8 +155,8 @@ class PlotCanvasLegacy(QtCore.QObject): else: theme_color = '#000000' tick_color = '#FFFFFF' - self.rect_hud_color = '#0000FF10' - self.text_hud_color = '#000000' + self.rect_hud_color = '#80808040' + self.text_hud_color = '#FFFFFF' # workspace lines; I didn't use the rectangle because I didn't want to add another VisPy Node, # which might decrease performance @@ -306,11 +308,6 @@ class PlotCanvasLegacy(QtCore.QObject): self.hud_enabled = False self.text_hud = self.Thud(plotcanvas=self) - # bbox_props = dict(boxstyle="round,pad=0.3", fc="blue", ec="b", lw=0) - # self.text_hud = self.figure.text(0, 0, "Direction", ha="left", va="center", rotation=0, - # size=15, - # bbox=bbox_props) - # draw a rectangle made out of 4 lines on the canvas to serve as a hint for the work area # all CNC have a limited workspace if self.app.defaults['global_workspace'] is True: @@ -336,19 +333,21 @@ class PlotCanvasLegacy(QtCore.QObject): self.p = plotcanvas units = self.p.app.defaults['units'] - self._text = 'Dx: %s [%s]\nDy: %s [%s]\nX: %s [%s]\nY: %s [%s]' % \ + self._text = 'Dx: %s [%s]\nDy: %s [%s]\n\nX: %s [%s]\nY: %s [%s]' % \ ('0.0000', units, '0.0000', units, '0.0000', units, '0.0000', units) - self.hud_holder = AnchoredText(self._text, - prop=dict(size=20), frameon=True, - loc='upper left', - ) + self.hud_holder = AnchoredText(self._text, prop=dict(size=20), frameon=True, loc='upper left') self.hud_holder.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") - self.hud_holder.patch.set_facecolor('blue') - self.hud_holder.patch.set_alpha(0.3) + fc_color = self.p.rect_hud_color[:-2] + fc_alpha = int(self.p.rect_hud_color[-2:], 16) / 255 + text_color = self.p.text_hud_color + + self.hud_holder.patch.set_facecolor(fc_color) + self.hud_holder.patch.set_alpha(fc_alpha) self.hud_holder.patch.set_edgecolor((0, 0, 0, 0)) + self. hud_holder.txt._text.set_color(color=text_color) self.text_changed.connect(self.on_text_changed) @property @@ -496,7 +495,7 @@ class PlotCanvasLegacy(QtCore.QObject): if self.big_cursor is False: try: - x, y = self.app.geo_editor.snap(x_pos, y_pos) + x, y = self.snap(x_pos, y_pos) # Pointer (snapped) # The size of the cursor is multiplied by 1.65 because that value made the cursor similar with the @@ -529,7 +528,7 @@ class PlotCanvasLegacy(QtCore.QObject): pass self.canvas.draw_idle() - self.canvas.blit(self.axes.bbox) + self.canvas.blit(self.axes.bbox) def clear_cursor(self, state): if state is True: @@ -931,7 +930,7 @@ class PlotCanvasLegacy(QtCore.QObject): self.canvas.draw_idle() # #### Temporary place-holder for cached update ##### - self.update_screen_request.emit([0, 0, 0, 0, 0]) + # self.update_screen_request.emit([0, 0, 0, 0, 0]) if self.app.defaults["global_cursor_color_enabled"] is True: self.draw_cursor(x_pos=x, y_pos=y, color=self.app.cursor_color_3D) @@ -983,6 +982,59 @@ class PlotCanvasLegacy(QtCore.QObject): return width / xpx, height / ypx + def snap(self, x, y): + """ + Adjusts coordinates to snap settings. + + :param x: Input coordinate X + :param y: Input coordinate Y + :return: Snapped (x, y) + """ + + snap_x, snap_y = (x, y) + snap_distance = np.Inf + + # ### Grid snap + if self.app.grid_status(): + if self.app.defaults["global_gridx"] != 0: + try: + snap_x_ = round(x / float(self.app.defaults["global_gridx"])) * \ + float(self.app.defaults["global_gridx"]) + except TypeError: + snap_x_ = x + else: + snap_x_ = x + + # If the Grid_gap_linked on Grid Toolbar is checked then the snap distance on GridY entry will be ignored + # and it will use the snap distance from GridX entry + if self.app.ui.grid_gap_link_cb.isChecked(): + if self.app.defaults["global_gridx"] != 0: + try: + snap_y_ = round(y / float(self.app.defaults["global_gridx"])) * \ + float(self.app.defaults["global_gridx"]) + except TypeError: + snap_y_ = y + else: + snap_y_ = y + else: + if self.app.defaults["global_gridy"] != 0: + try: + snap_y_ = round(y / float(self.app.defaults["global_gridy"])) * \ + float(self.app.defaults["global_gridy"]) + except TypeError: + snap_y_ = y + else: + snap_y_ = y + nearest_grid_distance = self.distance((x, y), (snap_x_, snap_y_)) + if nearest_grid_distance < snap_distance: + snap_x, snap_y = (snap_x_, snap_y_) + + return snap_x, snap_y + + @staticmethod + def distance(pt1, pt2): + return np.sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2) + class FakeCursor(QtCore.QObject): """ diff --git a/flatcamGUI/preferences/PreferencesUIManager.py b/flatcamGUI/preferences/PreferencesUIManager.py index c49841ba..3b7306fa 100644 --- a/flatcamGUI/preferences/PreferencesUIManager.py +++ b/flatcamGUI/preferences/PreferencesUIManager.py @@ -921,8 +921,12 @@ class PreferencesUIManager: theme = 'white' should_restart = False - val = self.ui.general_defaults_form.general_gui_group.theme_radio.get_value() - if val != theme: + theme_new_val = self.ui.general_defaults_form.general_gui_group.theme_radio.get_value() + + ge = self.defaults["global_graphic_engine"] + ge_val = self.ui.general_defaults_form.general_app_group.ge_radio.get_value() + + if theme_new_val != theme or ge != ge_val: msgbox = QtWidgets.QMessageBox() msgbox.setText(_("Are you sure you want to continue?")) msgbox.setWindowTitle(_("Application restart")) @@ -935,15 +939,22 @@ class PreferencesUIManager: msgbox.exec_() response = msgbox.clickedButton() - if response == bt_yes: - theme_settings.setValue('theme', val) + if theme_new_val != theme: + if response == bt_yes: + theme_settings.setValue('theme', theme_new_val) - # This will write the setting to the platform specific storage. - del theme_settings + # This will write the setting to the platform specific storage. + del theme_settings - should_restart = True + should_restart = True + else: + self.ui.general_defaults_form.general_gui_group.theme_radio.set_value(theme) else: - self.ui.general_defaults_form.general_gui_group.theme_radio.set_value(theme) + if response == bt_yes: + self.defaults["global_graphic_engine"] = ge_val + should_restart = True + else: + self.ui.general_defaults_form.general_app_group.ge_radio.set_value(ge) if save_to_file or should_restart is True: self.save_defaults(silent=False) @@ -1035,6 +1046,7 @@ class PreferencesUIManager: :return: None """ self.defaults.report_usage("save_defaults") + log.debug("App.PreferencesUIManager.save_defaults()") if data_path is None: data_path = self.data_path