From 341592785fbd9820f3d102f08a46832e4b9382fb Mon Sep 17 00:00:00 2001 From: ldam <ldam@passcal.nmt.edu> Date: Thu, 2 Nov 2023 09:18:02 -0600 Subject: [PATCH] supper class for editing valueColors dialog and dialog to edit valueColors for upDownDots plot_type --- .../edit_value_color_dialog/__init__.py | 0 .../edit_value_color_dialog_supper_class.py | 60 +++++++++++++ .../edit_value_color_dialog/up_down_dialog.py | 88 +++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 sohstationviewer/view/db_config/value_color_helper/edit_value_color_dialog/__init__.py create mode 100644 sohstationviewer/view/db_config/value_color_helper/edit_value_color_dialog/edit_value_color_dialog_supper_class.py create mode 100644 sohstationviewer/view/db_config/value_color_helper/edit_value_color_dialog/up_down_dialog.py diff --git a/sohstationviewer/view/db_config/value_color_helper/edit_value_color_dialog/__init__.py b/sohstationviewer/view/db_config/value_color_helper/edit_value_color_dialog/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/sohstationviewer/view/db_config/value_color_helper/edit_value_color_dialog/edit_value_color_dialog_supper_class.py b/sohstationviewer/view/db_config/value_color_helper/edit_value_color_dialog/edit_value_color_dialog_supper_class.py new file mode 100644 index 000000000..aea20f0ee --- /dev/null +++ b/sohstationviewer/view/db_config/value_color_helper/edit_value_color_dialog/edit_value_color_dialog_supper_class.py @@ -0,0 +1,60 @@ +from PySide2 import QtWidgets, QtGui +from PySide2.QtWidgets import QWidget, QDialog + + +def display_color(color_label: QtWidgets.QLabel, color: str): + """ + Display color on color_label. + Display the given color on the color_label + :param color_label: the label to display color + :param color: the color that is given to update the color_label + """ + palette = color_label.palette() + palette.setColor(QtGui.QPalette.Background, QtGui.QColor(color)) + color_label.setPalette(palette) + + +class EditValueColorDialog(QDialog): + def __init__(self, parent: QWidget, value_color_str: str): + """ + Dialog to edit color for Line/Dot Plot + + :param parent: the parent widget + :param value_color_str: string for value color to be saved in DB + """ + super(EditValueColorDialog, self).__init__(parent) + self.value_color_str = value_color_str + self.main_layout = QtWidgets.QGridLayout() + self.setLayout(self.main_layout) + + self.cancel_btn = QtWidgets.QPushButton('CANCEL', self) + self.save_colors_btn = QtWidgets.QPushButton('SAVE COLORS', self) + + self.setup_ui() + self.set_value() + self.connect_signals() + + def setup_complete_buttons(self, row_total) -> None: + """ + :param row_total: total of rows to edit + """ + self.main_layout.addWidget(self.cancel_btn, row_total, 0, 1, 1) + self.main_layout.addWidget(self.save_colors_btn, row_total, 3, 1, 1) + + def connect_signals(self) -> None: + self.cancel_btn.clicked.connect(self.close) + self.save_colors_btn.clicked.connect(self.on_save_color) + + def on_select_color(self, color_label: QtWidgets.QLabel): + """ + When clicking on Select Color button, Color Picker will pop up with + the default color is color_label's color. + User will select a color then save to update the selected color to + the color_label. + :param color_label: the label that display the color of the obj_type + """ + color = color_label.palette().window().color() + new_color = QtWidgets.QColorDialog.getColor(color) + if new_color.isValid(): + display_color(color_label, new_color.name()) + self.raise_() diff --git a/sohstationviewer/view/db_config/value_color_helper/edit_value_color_dialog/up_down_dialog.py b/sohstationviewer/view/db_config/value_color_helper/edit_value_color_dialog/up_down_dialog.py new file mode 100644 index 000000000..89aa70a31 --- /dev/null +++ b/sohstationviewer/view/db_config/value_color_helper/edit_value_color_dialog/up_down_dialog.py @@ -0,0 +1,88 @@ +import sys +import platform +import os + +from PySide2 import QtWidgets +from PySide2.QtWidgets import QWidget + +from sohstationviewer.view.db_config.value_color_helper.\ + edit_value_color_dialog.edit_value_color_dialog_supper_class import \ + EditValueColorDialog, display_color + + +class UpDownDialog(EditValueColorDialog): + def __init__(self, parent: QWidget, value_color_str: str): + """ + Dialog to edit color for Up/Down Plot + + :param parent: the parent widget + :param value_color_str: string for value color to be saved in DB + """ + # Widget that allow user to add/edit up's color + self.select_up_color_btn = QtWidgets.QPushButton("Select Color") + # Widget to display up's color + self.up_color_label = QtWidgets.QLabel() + self.up_color_label.setFixedWidth(30) + self.up_color_label.setAutoFillBackground(True) + # Widget that allow user to add/edit down's color + self.select_down_color_btn = QtWidgets.QPushButton("Select Color") + # Widget to display down's color + self.down_color_label = QtWidgets.QLabel() + self.down_color_label.setFixedWidth(30) + self.down_color_label.setAutoFillBackground(True) + + super(UpDownDialog, self).__init__(parent, value_color_str) + self.setWindowTitle("Edit Up/Down Plotting's Colors") + + def setup_ui(self) -> None: + self.main_layout.addWidget(QtWidgets.QLabel('Up Color'), 0, 0, 1, 1) + self.main_layout.addWidget(self.up_color_label, 0, 1, 1, 1) + self.main_layout.addWidget(self.select_up_color_btn, 0, 2, 1, 1) + + self.main_layout.addWidget(QtWidgets.QLabel('Down Color'), 1, 0, 1, 1) + self.main_layout.addWidget(self.down_color_label, 1, 1, 1, 1) + self.main_layout.addWidget(self.select_down_color_btn, 1, 2, 1, 1) + + self.setup_complete_buttons(2) + + def connect_signals(self) -> None: + self.select_up_color_btn.clicked.connect( + lambda: self.on_select_color(self.up_color_label)) + self.select_down_color_btn.clicked.connect( + lambda: self.on_select_color(self.down_color_label)) + super().connect_signals() + + def set_value(self): + """ + Change the corresponding color_labels's color according to the color + from value_color_str. + """ + if self.value_color_str == "": + return + vc_parts = self.value_color_str.split('|') + for vc_str in vc_parts: + obj_type, color = vc_str.split(':') + if obj_type == 'Up': + display_color(self.up_color_label, color) + if obj_type == 'Down': + display_color(self.down_color_label, color) + + def on_save_color(self): + """ + Create value_color_str from GUI's info and close the GUI with color + is the hex color got from color_labels' color + """ + up_color = self.up_color_label.palette().window().color().name() + down_color = self.down_color_label.palette().window().color().name() + self.value_color_str = f"Up:{up_color}|Down:{down_color}" + self.close() + + +if __name__ == '__main__': + os_name, version, *_ = platform.platform().split('-') + if os_name == 'macOS': + os.environ['QT_MAC_WANTS_LAYER'] = '1' + app = QtWidgets.QApplication(sys.argv) + test = UpDownDialog(None, 'Down:#FF0000|Up:#00FF00') + test.exec_() + sys.exit(app.exec_()) -- GitLab