From be93f78f7c8081cd170920a5dc88177e946502b7 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Wed, 6 May 2020 04:56:56 +0100 Subject: [PATCH] Fix the alpha sliders not taking the correct value on startup and not syncing bidirectionally. --- flatcamGUI/GUIElements.py | 3 ++ flatcamGUI/preferences/OptionUI.py | 48 ++++++++++++++++- flatcamGUI/preferences/OptionsGroupUI.py | 2 - .../general/GeneralGUIPrefGroupUI.py | 54 ++++--------------- .../gerber/GerberGenPrefGroupUI.py | 43 ++------------- 5 files changed, 63 insertions(+), 87 deletions(-) diff --git a/flatcamGUI/GUIElements.py b/flatcamGUI/GUIElements.py index 20cc3a1d..eaf67be9 100644 --- a/flatcamGUI/GUIElements.py +++ b/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() diff --git a/flatcamGUI/preferences/OptionUI.py b/flatcamGUI/preferences/OptionUI.py index bef0b5c5..ce27dffd 100644 --- a/flatcamGUI/preferences/OptionUI.py +++ b/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): diff --git a/flatcamGUI/preferences/OptionsGroupUI.py b/flatcamGUI/preferences/OptionsGroupUI.py index 1167a8d4..2f519ea9 100644 --- a/flatcamGUI/preferences/OptionsGroupUI.py +++ b/flatcamGUI/preferences/OptionsGroupUI.py @@ -2,8 +2,6 @@ from typing import Dict from PyQt5 import QtWidgets - - from PyQt5.QtCore import QSettings import gettext diff --git a/flatcamGUI/preferences/general/GeneralGUIPrefGroupUI.py b/flatcamGUI/preferences/general/GeneralGUIPrefGroupUI.py index 6ee4a6fa..5021e26e 100644 --- a/flatcamGUI/preferences/general/GeneralGUIPrefGroupUI.py +++ b/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() diff --git a/flatcamGUI/preferences/gerber/GerberGenPrefGroupUI.py b/flatcamGUI/preferences/gerber/GerberGenPrefGroupUI.py index 4c938ebf..33acdfcc 100644 --- a/flatcamGUI/preferences/gerber/GerberGenPrefGroupUI.py +++ b/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') -