|
1 | 1 | #!/usr/bin/python |
2 | 2 | from Qt import QtWidgets, QtCore, QtGui |
3 | 3 |
|
4 | | -from .custom_widget_vectors import PropVector3 |
| 4 | +from .custom_widget_vectors import PropVector3, PropVector4 |
5 | 5 | from .prop_widgets_abstract import BaseProperty |
6 | 6 |
|
7 | 7 |
|
@@ -62,3 +62,64 @@ def set_value(self, value): |
62 | 62 | self._update_color() |
63 | 63 | self._update_vector() |
64 | 64 | self.value_changed.emit(self.toolTip(), value) |
| 65 | + |
| 66 | + |
| 67 | +class PropColorPickerRGBA(BaseProperty): |
| 68 | + """ |
| 69 | + Color4 (rgba) picker widget for a node property. |
| 70 | + """ |
| 71 | + |
| 72 | + def __init__(self, parent=None): |
| 73 | + super(PropColorPickerRGBA, self).__init__(parent) |
| 74 | + self._color = (0, 0, 0, 255) |
| 75 | + self._button = QtWidgets.QPushButton() |
| 76 | + self._vector = PropVector4() |
| 77 | + self._vector.set_value([0, 0, 0, 255]) |
| 78 | + self._update_color() |
| 79 | + |
| 80 | + self._button.clicked.connect(self._on_select_color) |
| 81 | + self._vector.value_changed.connect(self._on_vector_changed) |
| 82 | + |
| 83 | + layout = QtWidgets.QHBoxLayout(self) |
| 84 | + layout.setContentsMargins(0, 0, 0, 0) |
| 85 | + layout.addWidget(self._button, 0, QtCore.Qt.AlignLeft) |
| 86 | + layout.addWidget(self._vector, 1, QtCore.Qt.AlignLeft) |
| 87 | + |
| 88 | + def _on_vector_changed(self, _, value): |
| 89 | + self._color = tuple(value) |
| 90 | + self._update_color() |
| 91 | + self.value_changed.emit(self.toolTip(), value) |
| 92 | + |
| 93 | + def _on_select_color(self): |
| 94 | + current_color = QtGui.QColor(*self.get_value()) |
| 95 | + alpha_option = QtWidgets.QColorDialog.ShowAlphaChannel |
| 96 | + color = QtWidgets.QColorDialog.getColor( |
| 97 | + current_color, self, options=alpha_option) |
| 98 | + if color.isValid(): |
| 99 | + self.set_value(color.getRgb()) |
| 100 | + |
| 101 | + def _update_vector(self): |
| 102 | + self._vector.set_value(self._color) |
| 103 | + |
| 104 | + def _update_color(self): |
| 105 | + c = [int(max(min(i, 255), 0)) for i in self._color] |
| 106 | + hex_color = '#{0:02x}{1:02x}{2:02x}{3:03x}'.format(*c) |
| 107 | + self._button.setStyleSheet( |
| 108 | + ''' |
| 109 | + QPushButton {{background-color: rgba({0}, {1}, {2}, {3});}} |
| 110 | + QPushButton::hover {{background-color: rgba({0}, {1}, {2}, {3});}} |
| 111 | + '''.format(*c) |
| 112 | + ) |
| 113 | + self._button.setToolTip( |
| 114 | + 'rgba: {}\nhex: {}'.format(self._color, hex_color) |
| 115 | + ) |
| 116 | + |
| 117 | + def get_value(self): |
| 118 | + return self._color[:4] |
| 119 | + |
| 120 | + def set_value(self, value): |
| 121 | + if value != self.get_value(): |
| 122 | + self._color = value |
| 123 | + self._update_color() |
| 124 | + self._update_vector() |
| 125 | + self.value_changed.emit(self.toolTip(), value) |
0 commit comments