|
| 1 | +from typing import TYPE_CHECKING |
| 2 | + |
| 3 | +from PySide6.QtGui import QColor |
| 4 | +from PySide6.QtWidgets import QColorDialog |
| 5 | +from PySide6.QtWidgets import QPushButton |
| 6 | +from PySide6.QtWidgets import QVBoxLayout |
| 7 | +from PySide6.QtWidgets import QWidget |
| 8 | + |
| 9 | +from compas.colors import Color |
| 10 | +from compas.colors.colordict import ColorDict |
| 11 | +from compas_viewer.base import Base |
| 12 | +from compas_viewer.components.combobox import ComboBox |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from compas_viewer.scene import ViewerSceneObject |
| 16 | + |
| 17 | + |
| 18 | +def remap_rgb(value, to_range_one=True): |
| 19 | + """ |
| 20 | + Remap an RGB value between the range (0, 255) and (0, 1). |
| 21 | +
|
| 22 | + Parameters |
| 23 | + ---------- |
| 24 | + value : tuple |
| 25 | + The RGB value to remap. |
| 26 | + to_range_one : bool, optional |
| 27 | + If True, remap from (0, 255) to (0, 1). If False, remap from (0, 1) to (0, 255). |
| 28 | +
|
| 29 | + Returns |
| 30 | + ------- |
| 31 | + tuple |
| 32 | + The remapped RGB value. |
| 33 | + """ |
| 34 | + factor = 1 / 255 if to_range_one else 255 |
| 35 | + return tuple(v * factor for v in value) |
| 36 | + |
| 37 | + |
| 38 | +class ColorComboBox(QWidget, Base): |
| 39 | + """ |
| 40 | + A custom QWidget for selecting colors from a predefined list and applying the selected color to an object's attribute. |
| 41 | +
|
| 42 | + Parameters |
| 43 | + ---------- |
| 44 | + obj : ViewerSceneObject, optional |
| 45 | + The object to which the selected color will be applied. Defaults to None. |
| 46 | + attr : str, optional |
| 47 | + The attribute of the object to which the selected color will be applied. Defaults to None. |
| 48 | +
|
| 49 | + Attributes |
| 50 | + ---------- |
| 51 | + obj : ViewerSceneObject |
| 52 | + The object to which the selected color will be applied. |
| 53 | + attr : str |
| 54 | + The attribute of the object to which the selected color will be applied. |
| 55 | + color_options : list of QColor |
| 56 | + A list of predefined QColor objects representing available colors. |
| 57 | + layout : QVBoxLayout |
| 58 | + The layout of the widget. |
| 59 | + color_selector : ComboBox |
| 60 | + A combo box for selecting colors. |
| 61 | +
|
| 62 | + Methods |
| 63 | + ------- |
| 64 | + change_color(color: QColor) -> None |
| 65 | + Changes the color of the object's attribute to the selected color. |
| 66 | +
|
| 67 | + Example |
| 68 | + ------- |
| 69 | + >>> color_combobox = ColorComboBox(obj=some_obj, attr="linecolor") |
| 70 | + >>> color_combobox.show() |
| 71 | + """ |
| 72 | + |
| 73 | + def __init__( |
| 74 | + self, |
| 75 | + obj: "ViewerSceneObject" = None, |
| 76 | + attr: str = None, |
| 77 | + ): |
| 78 | + super().__init__() |
| 79 | + self.obj = obj |
| 80 | + self.attr = attr |
| 81 | + |
| 82 | + self.color_options = [ |
| 83 | + QColor(255, 255, 255), # White |
| 84 | + QColor(211, 211, 211), # LightGray |
| 85 | + QColor(190, 190, 190), # Gray |
| 86 | + QColor(0, 0, 0), # Black |
| 87 | + QColor(255, 0, 0), # Red |
| 88 | + QColor(0, 255, 0), # Green |
| 89 | + QColor(0, 0, 255), # Blue |
| 90 | + QColor(255, 255, 0), # Yellow |
| 91 | + QColor(0, 255, 255), # Cyan |
| 92 | + QColor(255, 0, 255), # Magenta |
| 93 | + ] |
| 94 | + |
| 95 | + default_color = getattr(self.obj, self.attr) |
| 96 | + |
| 97 | + if isinstance(default_color, Color): |
| 98 | + default_color = default_color.rgb |
| 99 | + elif isinstance(default_color, ColorDict): |
| 100 | + default_color = default_color.default |
| 101 | + else: |
| 102 | + raise ValueError("Invalid color type.") |
| 103 | + default_color = QColor(*remap_rgb(default_color, to_range_one=False)) |
| 104 | + |
| 105 | + self.layout = QVBoxLayout(self) |
| 106 | + self.color_selector = ComboBox(self.color_options, self.change_color, paint=True) |
| 107 | + self.color_selector.setAssignedColor(default_color) |
| 108 | + self.layout.addWidget(self.color_selector) |
| 109 | + |
| 110 | + def change_color(self, color): |
| 111 | + rgb = remap_rgb(color.getRgb())[:-1] # rgba to rgb(0-1) |
| 112 | + setattr(self.obj, self.attr, Color(*rgb)) |
| 113 | + self.obj.update() |
| 114 | + |
| 115 | + |
| 116 | +class ColorDialog(QWidget): |
| 117 | + """ |
| 118 | + A custom QWidget that provides a QPushButton to open a QColorDialog for selecting colors. |
| 119 | +
|
| 120 | + This class is used to manage and display a color attribute of a ViewerSceneObject. |
| 121 | + The button shows the current color and allows the user to change the color via a color dialog. |
| 122 | +
|
| 123 | + Parameters |
| 124 | + ---------- |
| 125 | + obj : ViewerSceneObject, optional |
| 126 | + The object whose color attribute is being managed. |
| 127 | + attr : str, optional |
| 128 | + The attribute name of the color in the object. |
| 129 | +
|
| 130 | + Attributes |
| 131 | + ---------- |
| 132 | + obj : ViewerSceneObject |
| 133 | + The object whose color attribute is being managed. |
| 134 | + attr : str |
| 135 | + The attribute name of the color in the object. |
| 136 | + color_button : QPushButton |
| 137 | + The button that displays the current color and opens the color dialog when clicked. |
| 138 | + layout : QVBoxLayout |
| 139 | + The layout of the widget, which contains the color button. |
| 140 | + current_color : QColor |
| 141 | + The currently selected color. |
| 142 | +
|
| 143 | + Methods |
| 144 | + ------- |
| 145 | + open_color_dialog() |
| 146 | + Opens a QColorDialog for the user to select a new color. |
| 147 | + set_button_color(color: QColor) |
| 148 | + Sets the button's background and text to the provided color. |
| 149 | + change_color(color: QColor) |
| 150 | + Changes the color attribute of the object to the provided color and updates the object. |
| 151 | +
|
| 152 | + Example |
| 153 | + ------- |
| 154 | + >>> obj = ViewerSceneObject() # Assume this is a valid object with a color attribute |
| 155 | + >>> color_button = ColorButton(obj=obj, attr="linecolor") |
| 156 | + >>> layout = QVBoxLayout() |
| 157 | + >>> layout.addWidget(color_button) |
| 158 | + >>> window = QWidget() |
| 159 | + >>> window.setLayout(layout) |
| 160 | + >>> window.show() |
| 161 | + """ |
| 162 | + |
| 163 | + def __init__( |
| 164 | + self, |
| 165 | + obj: "ViewerSceneObject" = None, |
| 166 | + attr: str = None, |
| 167 | + ): |
| 168 | + super().__init__() |
| 169 | + |
| 170 | + self.obj = obj |
| 171 | + self.attr = attr |
| 172 | + |
| 173 | + default_color = getattr(self.obj, self.attr) |
| 174 | + if isinstance(default_color, Color): |
| 175 | + default_color = default_color.rgb |
| 176 | + elif isinstance(default_color, ColorDict): |
| 177 | + default_color = default_color.default |
| 178 | + else: |
| 179 | + raise ValueError("Invalid color type.") |
| 180 | + default_color = QColor(*remap_rgb(default_color, to_range_one=False)) |
| 181 | + |
| 182 | + self.color_button = QPushButton(self) |
| 183 | + self.layout = QVBoxLayout(self) |
| 184 | + self.layout.addWidget(self.color_button) |
| 185 | + self.color_button.clicked.connect(self.open_color_dialog) |
| 186 | + self.set_button_color(default_color) |
| 187 | + |
| 188 | + def open_color_dialog(self): |
| 189 | + color = QColorDialog.getColor() |
| 190 | + |
| 191 | + if color.isValid(): |
| 192 | + self.change_color(color) |
| 193 | + self.set_button_color(color) |
| 194 | + |
| 195 | + def set_button_color(self, color: QColor): |
| 196 | + self.color_button.setStyleSheet(f"background-color: {color.name()};") |
| 197 | + self.color_button.setText(color.name()) |
| 198 | + self.current_color = color |
| 199 | + |
| 200 | + def change_color(self, color): |
| 201 | + rgb = remap_rgb(color.getRgb())[:-1] # rgba to rgb(0-1) |
| 202 | + setattr(self.obj, self.attr, Color(*rgb)) |
| 203 | + self.obj.update() |
0 commit comments