Skip to content

Commit 014c1ad

Browse files
committed
upgrade color picker widget
1 parent b2478c2 commit 014c1ad

File tree

2 files changed

+26
-49
lines changed

2 files changed

+26
-49
lines changed

NodeGraphQt/widgets/properties.py

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,77 +30,52 @@ def get_value(self):
3030
raise NotImplementedError
3131

3232

33-
class _ColorSolid(QtWidgets.QWidget):
34-
35-
def __init__(self, parent=None, color=None):
36-
super(_ColorSolid, self).__init__(parent)
37-
self.setFixedSize(15, 15)
38-
self.color = color or (0, 0, 0)
39-
40-
def paintEvent(self, event):
41-
size = self.geometry()
42-
rect = QtCore.QRect(1, 1, size.width() - 2, size.height() - 2)
43-
painter = QtGui.QPainter(self)
44-
painter.setPen(QtCore.Qt.NoPen)
45-
painter.setBrush(QtGui.QColor(*self._color))
46-
painter.drawRoundedRect(rect, 1, 1)
47-
48-
@property
49-
def color(self):
50-
return self._color
51-
52-
@color.setter
53-
def color(self, color):
54-
self._color = color
55-
hex = '#{0:02x}{1:02x}{2:02x}'.format(*self._color)
56-
self.setToolTip('rgb: {}\nhex: {}'.format(self._color[0:3], hex))
57-
self.update()
58-
59-
6033
class PropColorPicker(BaseProperty):
61-
6234
def __init__(self, parent=None):
6335
super(PropColorPicker, self).__init__(parent)
6436
self._color = (0, 0, 0)
65-
self._label = QtWidgets.QLabel()
6637
self._button = QtWidgets.QPushButton()
38+
self._vector = PropVector3()
39+
self._vector.set_value([0, 0, 0])
6740
self._update_color()
6841

6942
self._button.clicked.connect(self._on_select_color)
43+
self._vector.value_changed.connect(self._on_vector_changed)
7044
layout = QtWidgets.QHBoxLayout(self)
71-
layout.setContentsMargins(0, 0, 8, 0)
45+
layout.setContentsMargins(0, 0, 0, 0)
7246
layout.setSpacing(4)
73-
layout.addWidget(self._label, 0, QtCore.Qt.AlignCenter)
74-
layout.addWidget(self._button, 1, QtCore.Qt.AlignLeft)
47+
layout.addWidget(self._button, 0, QtCore.Qt.AlignLeft)
48+
layout.addWidget(self._vector, 1, QtCore.Qt.AlignLeft)
49+
50+
def _on_vector_changed(self, o, value):
51+
self._color = tuple(value)
52+
self._update_color()
53+
self.value_changed.emit(self.toolTip(), value)
54+
55+
def _update_vector(self):
56+
self._vector.set_value(list(self._color))
7557

7658
def _on_select_color(self):
77-
color = QtWidgets.QColorDialog.getColor(QtGui.QColor(*self.get_value()),
78-
options=QtWidgets.QColorDialog.ShowAlphaChannel)
59+
color = QtWidgets.QColorDialog.getColor(QtGui.QColor.fromRgbF(*self.get_value()))
7960
if color.isValid():
8061
self.set_value(color.getRgb())
8162

8263
def _update_color(self):
83-
hex = self.hex_color()
84-
self._label.setText(hex)
85-
self._label.setAlignment(QtCore.Qt.AlignCenter)
86-
self._label.setMinimumWidth(60)
87-
64+
c = [int(max(min(i, 255), 0)) for i in self._color]
65+
hex_color = '#{0:02x}{1:02x}{2:02x}'.format(*c)
8866
self._button.setStyleSheet(
8967
'''QPushButton {{background-color: rgba({0}, {1}, {2}, 255);}}
90-
QPushButton::hover {{background-color: rgba({0}, {1}, {2}, 200);}}'''
91-
.format(*self._color))
92-
self._button.setToolTip('rgb: {}\nhex: {}'.format(self._color[0:3], hex))
93-
94-
def hex_color(self):
95-
return '#{0:02x}{1:02x}{2:02x}'.format(*self._color)
68+
QPushButton::hover {{background-color: rgba({0}, {1}, {2}, 200);}}'''.format(*c))
69+
self._button.setToolTip('rgb: {}\nhex: {}'.format(self._color[:3], hex_color))
9670

9771
def get_value(self):
98-
return self._color
72+
return self._color[:3]
9973

10074
def set_value(self, value):
10175
if value != self.get_value():
10276
self._color = value
10377
self._update_color()
78+
self._update_vector()
10479
self.value_changed.emit(self.toolTip(), value)
10580

10681

@@ -619,7 +594,7 @@ def _on_value_change(self, value=None, index=None):
619594

620595
def _update_items(self):
621596
for index, value in enumerate(self._value):
622-
if self._items[index].value() != value:
597+
if index < len(self._items) and self._items[index].value() != value:
623598
self._items[index].setValue(value)
624599

625600
def get_value(self):

NodeGraphQt/widgets/properties_bin.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ def __init__(self, parent=None):
4444
self.setShowGrid(False)
4545
self.verticalHeader().hide()
4646
self.horizontalHeader().hide()
47-
self.horizontalHeader().setResizeMode(QtWidgets.QHeaderView.Stretch)
48-
self.verticalHeader().setResizeMode(QtWidgets.QHeaderView.Stretch)
47+
QtCompat.QHeaderView.setSectionResizeMode(
48+
self.verticalHeader(), QtWidgets.QHeaderView.Stretch)
49+
QtCompat.QHeaderView.setSectionResizeMode(
50+
self.horizontalHeader(), 0, QtWidgets.QHeaderView.Stretch)
4951
self.setVerticalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)
5052

5153
def wheelEvent(self, event):

0 commit comments

Comments
 (0)