0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 03:31:50 +02:00

fix(color_button): inheritance changed to QWidget

This commit is contained in:
2024-08-29 15:13:50 +02:00
committed by wyzula_j
parent 9d76d8bf6c
commit 3c0e501c56
4 changed files with 41 additions and 17 deletions

View File

@ -3,9 +3,13 @@ from __future__ import annotations
from typing import Literal
import pyqtgraph as pg
from qtpy.QtCore import Qt
from qtpy.QtWidgets import QHBoxLayout, QWidget
from bec_widgets.qt_utils.error_popups import SafeSlot
class ColorButton(pg.ColorButton):
class ColorButton(QWidget):
"""
A ColorButton that opens a dialog to select a color. Inherits from pyqtgraph.ColorButton.
Patches event loop of the ColorDialog, if opened in another QDialog.
@ -16,11 +20,31 @@ class ColorButton(pg.ColorButton):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def selectColor(self):
self.origColor = self.color()
self.colorDialog.setCurrentColor(self.color())
self.colorDialog.open()
self.colorDialog.exec()
self.layout = QHBoxLayout(self)
self.layout.setSpacing(0)
self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.setAlignment(Qt.AlignmentFlag.AlignVCenter)
self.button = pg.ColorButton()
self.button.setFlat(True)
self.button.clicked.connect(self.select_color)
self.layout.addWidget(self.button)
@SafeSlot()
def select_color(self):
self.origColor = self.button.color()
self.button.colorDialog.setCurrentColor(self.button.color())
self.button.colorDialog.open()
self.button.colorDialog.exec()
def set_color(self, color: tuple | str):
"""
Set the color of the button.
Args:
color(tuple|str): The color to set.
"""
self.button.setColor(color)
def get_color(self, format: Literal["RGBA", "HEX"] = "RGBA") -> tuple | str:
"""
@ -33,13 +57,13 @@ class ColorButton(pg.ColorButton):
tuple|str: The color in the specified format.
"""
if format == "RGBA":
return self.color().getRgb()
return self.button.color().getRgb()
if format == "HEX":
return self.color().name()
return self.button.color().name()
def cleanup(self):
"""
Clean up the ColorButton.
"""
self.colorDialog.close()
self.colorDialog.deleteLater()
self.button.colorDialog.close()
self.button.colorDialog.deleteLater()

View File

@ -27,7 +27,7 @@ class MotorMapSettings(SettingWidget):
background_intensity = int((config["background_value"] / 255) * 100)
WidgetIO.set_value(self.ui.background_value, background_intensity)
color = config["color"]
self.ui.color.setColor(color)
self.ui.color.set_color(color)
@Slot()
def accept_changes(self):

View File

@ -123,7 +123,7 @@ class CurveSettings(SettingWidget):
colors = Colors.golden_angle_color(colormap=cm, num=max(10, rows + 1), format="HEX")
color_button_col = 2 if target == "scan" else 3
for row in range(rows):
table.cellWidget(row, color_button_col).setColor(colors[row])
table.cellWidget(row, color_button_col).set_color(colors[row])
@Slot()
def accept_changes(self):
@ -275,7 +275,7 @@ class DialogRow(QObject):
if self.config is not None:
self.device_line_edit.setText(self.config.signals.y.name)
self.entry_line_edit.setText(self.config.signals.y.entry)
self.color_button.setColor(self.config.color)
self.color_button.set_color(self.config.color)
self.style_combo.setCurrentText(self.config.pen_style)
self.width.setValue(self.config.pen_width)
self.symbol_size.setValue(self.config.symbol_size)
@ -284,7 +284,7 @@ class DialogRow(QObject):
colormap="magma", num=max(10, self.row + 1), format="HEX"
)
default_color = default_colors[self.row]
self.color_button.setColor(default_color)
self.color_button.set_color(default_color)
self.table_widget.setCellWidget(self.row, 0, self.device_line_edit)
self.table_widget.setCellWidget(self.row, 1, self.entry_line_edit)
@ -299,7 +299,7 @@ class DialogRow(QObject):
self.device_line_edit.setText(self.config.signals.y.name)
self.entry_line_edit.setText(self.config.signals.y.entry)
self.dap_combo.setCurrentText(self.config.signals.dap)
self.color_button.setColor(self.config.color)
self.color_button.set_color(self.config.color)
self.style_combo.setCurrentText(self.config.pen_style)
self.width.setValue(self.config.pen_width)
self.symbol_size.setValue(self.config.symbol_size)
@ -308,7 +308,7 @@ class DialogRow(QObject):
colormap="magma", num=max(10, self.row + 1), format="HEX"
)
default_color = default_colors[self.row]
self.color_button.setColor(default_color)
self.color_button.set_color(default_color)
self.table_widget.setCellWidget(self.row, 0, self.device_line_edit)
self.table_widget.setCellWidget(self.row, 1, self.entry_line_edit)

View File

@ -151,7 +151,7 @@ def test_display_current_settings(motor_map_settings):
}
with patch("bec_widgets.utils.widget_io.WidgetIO.set_value") as mock_set_value:
with patch.object(motor_map_settings.ui.color, "setColor") as mock_set_color:
with patch.object(motor_map_settings.ui.color, "set_color") as mock_set_color:
motor_map_settings.display_current_settings(config)
mock_set_value.assert_any_call(motor_map_settings.ui.max_points, config["max_points"])
mock_set_value.assert_any_call(