Explorar o código

Fix the alpha sliders not taking the correct value on startup and not syncing bidirectionally.

David Robertson %!s(int64=5) %!d(string=hai) anos
pai
achega
be93f78f7c

+ 3 - 0
flatcamGUI/GUIElements.py

@@ -677,6 +677,7 @@ class FCColorEntry(QtWidgets.QFrame):
         self.entry.editingFinished.connect(self._sync_button_color)
         self.button.clicked.connect(self._on_button_clicked)
 
+
     def get_value(self) -> str:
         return self.entry.get_value()
 
@@ -733,6 +734,8 @@ class FCSliderWithSpinner(QtWidgets.QFrame):
         self.slider.valueChanged.connect(self._on_slider)
         self.spinner.valueChanged.connect(self._on_spinner)
 
+        self.valueChanged = self.spinner.valueChanged
+
     def get_value(self) -> int:
         return self.spinner.get_value()
 

+ 47 - 1
flatcamGUI/preferences/OptionUI.py

@@ -1,4 +1,4 @@
-from typing import Union, Sequence
+from typing import Union, Sequence, List
 
 from PyQt5 import QtWidgets
 from flatcamGUI.GUIElements import RadioSet, FCCheckBox, FCButton, FCComboBox, FCEntry, FCSpinner, FCColorEntry, \
@@ -7,6 +7,7 @@ from flatcamGUI.GUIElements import RadioSet, FCCheckBox, FCButton, FCComboBox, F
 import gettext
 import FlatCAMTranslation as fcTranslate
 import builtins
+
 fcTranslate.apply_language('strings')
 if '_' not in builtins.__dict__:
     _ = gettext.gettext
@@ -125,6 +126,51 @@ class SliderWithSpinnerOptionUI(BasicOptionUI):
         entry = FCSliderWithSpinner(min=self.min_value, max=self.max_value, step=self.step)
         return entry
 
+class ColorAlphaSliderOptionUI(SliderWithSpinnerOptionUI):
+    def __init__(self, applies_to: List[str], group, label_text: str, **kwargs):
+        self.applies_to = applies_to
+        self.group = group
+        super().__init__(option="__color_alpha_slider", label_text=label_text, min_value=0, max_value=255, step=1, **kwargs)
+        self.get_field().valueChanged.connect(self._on_alpha_change)
+
+    def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
+        for index, field in enumerate(self._get_target_fields()):
+            field.entry.textChanged.connect(lambda value, i=index: self._on_target_change(target_index=i))
+        return super().add_to_grid(grid, row)
+
+    def _get_target_fields(self):
+        return list(map(lambda n: self.group.option_dict()[n].get_field(), self.applies_to))
+
+    def _on_target_change(self, target_index: int):
+        field = self._get_target_fields()[target_index]
+        color = field.get_value()
+        alpha_part = color[7:]
+        if len(alpha_part) != 2:
+            return
+        alpha = int(alpha_part, 16)
+        if alpha < 0 or alpha > 255 or self.get_field().get_value() == alpha:
+            return
+        self.get_field().set_value(alpha)
+
+    def _on_alpha_change(self):
+        alpha = self.get_field().get_value()
+        for field in self._get_target_fields():
+            old_value = field.get_value()
+            new_value = self._modify_color_alpha(old_value, alpha=alpha)
+            field.set_value(new_value)
+
+    def _modify_color_alpha(self, color: str, alpha: int):
+        color_without_alpha = color[:7]
+        if alpha > 255:
+            return color_without_alpha + "FF"
+        elif alpha < 0:
+            return color_without_alpha + "00"
+        else:
+            hexalpha = hex(alpha)[2:]
+            if len(hexalpha) == 1:
+                hexalpha = "0" + hexalpha
+            return color_without_alpha + hexalpha
+
 
 class SpinnerOptionUI(BasicOptionUI):
     def __init__(self, option: str, label_text: str, min_value: int, max_value: int, step: int = 1, **kwargs):

+ 0 - 2
flatcamGUI/preferences/OptionsGroupUI.py

@@ -2,8 +2,6 @@ from typing import Dict
 
 from PyQt5 import QtWidgets
 
-
-
 from PyQt5.QtCore import QSettings
 
 import gettext

+ 9 - 45
flatcamGUI/preferences/general/GeneralGUIPrefGroupUI.py

@@ -11,7 +11,7 @@ if '_' not in builtins.__dict__:
 
 from flatcamGUI.preferences.OptionUI import OptionUI, CheckboxOptionUI, RadioSetOptionUI, \
     SeparatorOptionUI, HeadingOptionUI, ComboboxOptionUI, ColorOptionUI, FullWidthButtonOptionUI, \
-    SliderWithSpinnerOptionUI
+    SliderWithSpinnerOptionUI, ColorAlphaSliderOptionUI
 
 
 class GeneralGUIPrefGroupUI(OptionsGroupUI2):
@@ -41,16 +41,6 @@ class GeneralGUIPrefGroupUI(OptionsGroupUI2):
             self.hdpi_field.set_value(False)
         self.hdpi_field.stateChanged.connect(self.handle_hdpi)
 
-        self.sel_line_field = self.option_dict()["global_sel_line"].get_field()
-        self.sel_fill_field = self.option_dict()["global_sel_fill"].get_field()
-        self.sel_alpha_field = self.option_dict()["_global_sel_alpha"].get_field()
-        self.sel_alpha_field.spinner.valueChanged.connect(self.on_sel_alpha_change)
-
-        self.alt_sel_line_field = self.option_dict()["global_alt_sel_line"].get_field()
-        self.alt_sel_fill_field = self.option_dict()["global_alt_sel_fill"].get_field()
-        self.alt_sel_alpha_field = self.option_dict()["_global_alt_sel_alpha"].get_field()
-        self.alt_sel_alpha_field.spinner.valueChanged.connect(self.on_alt_sel_alpha_change)
-
     def build_options(self) -> [OptionUI]:
         return [
             RadioSetOptionUI(
@@ -127,11 +117,11 @@ class GeneralGUIPrefGroupUI(OptionsGroupUI2):
                               "First 6 digits are the color and the last 2\n"
                               "digits are for alpha (transparency) level."
             ),
-            SliderWithSpinnerOptionUI(
-                option="_global_sel_alpha",
+            ColorAlphaSliderOptionUI(
+                applies_to=["global_sel_line", "global_sel_fill"],
+                group=self,
                 label_text="Alpha",
-                label_tooltip="Set the fill transparency for the 'left to right' selection box.",
-                min_value=0, max_value=255, step=1
+                label_tooltip="Set the fill transparency for the 'left to right' selection box."
             ),
             SeparatorOptionUI(),
 
@@ -149,11 +139,11 @@ class GeneralGUIPrefGroupUI(OptionsGroupUI2):
                               "First 6 digits are the color and the last 2\n"
                               "digits are for alpha (transparency) level."
             ),
-            SliderWithSpinnerOptionUI(
-                option="_global_alt_sel_alpha",
+            ColorAlphaSliderOptionUI(
+                applies_to=["global_alt_sel_line", "global_alt_sel_fill"],
+                group=self,
                 label_text="Alpha",
-                label_tooltip="Set the fill transparency for the 'right to left' selection box.",
-                min_value=0, max_value=255, step=1
+                label_tooltip="Set the fill transparency for the 'right to left' selection box."
             ),
             SeparatorOptionUI(),
 
@@ -191,32 +181,6 @@ class GeneralGUIPrefGroupUI(OptionsGroupUI2):
             ),
         ]
 
-    def on_sel_alpha_change(self):
-        alpha = self.sel_alpha_field.get_value()
-        fill = self._modify_color_alpha(color=self.sel_fill_field.get_value(), alpha=alpha)
-        self.sel_fill_field.set_value(fill)
-        line = self._modify_color_alpha(color=self.sel_line_field.get_value(), alpha=alpha)
-        self.sel_line_field.set_value(line)
-
-    def on_alt_sel_alpha_change(self):
-        alpha = self.alt_sel_alpha_field.get_value()
-        fill = self._modify_color_alpha(color=self.alt_sel_fill_field.get_value(), alpha=alpha)
-        self.alt_sel_fill_field.set_value(fill)
-        line = self._modify_color_alpha(color=self.alt_sel_line_field.get_value(), alpha=alpha)
-        self.alt_sel_line_field.set_value(line)
-
-    def _modify_color_alpha(self, color: str, alpha: int):
-        color_without_alpha = color[:7]
-        if alpha > 255:
-            return color_without_alpha + "FF"
-        elif alpha < 0:
-            return color_without_alpha + "00"
-        else:
-            hexalpha = hex(alpha)[2:]
-            if len(hexalpha) == 1:
-                hexalpha = "0" + hexalpha
-            return color_without_alpha + hexalpha
-
     def on_theme_change(self):
         # FIXME: this should be moved out to a view model
         val = self.theme_field.get_value()

+ 4 - 39
flatcamGUI/preferences/gerber/GerberGenPrefGroupUI.py

@@ -1,4 +1,3 @@
-from PyQt5 import QtCore, QtGui
 from flatcamGUI.preferences.OptionUI import *
 from flatcamGUI.preferences.OptionsGroupUI import OptionsGroupUI2
 
@@ -16,11 +15,6 @@ class GerberGenPrefGroupUI(OptionsGroupUI2):
         super().__init__(**kwargs)
         self.setTitle(str(_("Gerber General")))
 
-        self.plot_line_field = self.option_dict()["gerber_plot_line"].get_field()
-        self.plot_fill_field = self.option_dict()["gerber_plot_fill"].get_field()
-        self.plot_alpha_field = self.option_dict()["_gerber_plot_alpha"].get_field()
-        self.plot_alpha_field.spinner.valueChanged.connect(self.on_plot_alpha_change)
-
     def build_options(self) -> [OptionUI]:
         return [
             HeadingOptionUI(label_text="Plot Options"),
@@ -102,40 +96,11 @@ class GerberGenPrefGroupUI(OptionsGroupUI2):
                               "First 6 digits are the color and the last 2\n"
                               "digits are for alpha (transparency) level."
             ),
-            SliderWithSpinnerOptionUI(
-                option="_gerber_plot_alpha",
+            ColorAlphaSliderOptionUI(
+                applies_to=["gerber_plot_line", "gerber_plot_fill"],
+                group=self,
                 label_text="Alpha",
-                label_tooltip="Set the transparency for plotted objects.",
-                min_value=0, max_value=255, step=1
+                label_tooltip="Set the transparency for plotted objects."
             )
         ]
 
-    def on_plot_alpha_change(self):
-        alpha = self.plot_alpha_field.get_value()
-        fill = self._modify_color_alpha(color=self.plot_fill_field.get_value(), alpha=alpha)
-        self.plot_fill_field.set_value(fill)
-        line = self._modify_color_alpha(color=self.plot_line_field.get_value(), alpha=alpha)
-        self.plot_line_field.set_value(line)
-
-    def _modify_color_alpha(self, color: str, alpha: int):
-        color_without_alpha = color[:7]
-        if alpha > 255:
-            return color_without_alpha + "FF"
-        elif alpha < 0:
-            return color_without_alpha + "00"
-        else:
-            hexalpha = hex(alpha)[2:]
-            if len(hexalpha) == 1:
-                hexalpha = "0" + hexalpha
-            return color_without_alpha + hexalpha
-
-
-    # def on_pf_color_alpha_spinner(self):
-    #     self.pf_color_alpha_slider.setValue(spinner_value)
-    #     self.app.defaults['gerber_plot_fill'] = \
-    #         self.app.defaults['gerber_plot_fill'][:7] + \
-    #         (hex(spinner_value)[2:] if int(hex(spinner_value)[2:], 16) > 0 else '00')
-    #     self.app.defaults['gerber_plot_line'] = \
-    #         self.app.defaults['gerber_plot_line'][:7] + \
-    #         (hex(spinner_value)[2:] if int(hex(spinner_value)[2:], 16) > 0 else '00')
-