Skip to content

Commit 4d12ed2

Browse files
committed
trying to add colour to unit stylesheets, not working
1 parent 7a95d32 commit 4d12ed2

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

loopstructural/gui/modelling/stratigraphic_column/stratigraphic_column.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def add_unit(self, *, unit_data=None, create_new=True):
107107
for k in list(unit_data.keys()):
108108
if unit_data[k] is None:
109109
unit_data.pop(k)
110+
print(f"Adding unit with data: {unit_data}")
110111
unit_widget = StratigraphicUnitWidget(**unit_data)
111112
unit_widget.deleteRequested.connect(self.delete_unit) # Connect delete signal
112113
unit_widget.nameChanged.connect(
@@ -116,8 +117,11 @@ def add_unit(self, *, unit_data=None, create_new=True):
116117
unit_widget.thicknessChanged.connect(
117118
lambda: self.update_element(unit_widget)
118119
) # Connect thickness change signal
119-
120+
120121
unit_widget.set_thickness(unit_data.get('thickness', 0.0)) # Set initial thickness
122+
unit_widget.colourChanged.connect(
123+
lambda: self.update_element(unit_widget)
124+
) # Connect colour change signal
121125
item = QListWidgetItem()
122126
item.setSizeHint(unit_widget.sizeHint())
123127
self.unitList.addItem(item)

loopstructural/gui/modelling/stratigraphic_column/stratigraphic_unit.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import numpy as np
23
from typing import Optional
34

45
from PyQt5 import uic
@@ -24,13 +25,28 @@ def __init__(
2425
uic.loadUi(os.path.join(os.path.dirname(__file__), "stratigraphic_unit.ui"), self)
2526
self.uuid = uuid
2627
self._name = name if name is not None else ""
27-
self.colour = colour if colour is not None else ""
28+
# Convert colour from RGB tuple or string to Qt-compatible hex string
29+
if colour is not None:
30+
if (isinstance(colour, tuple) or isinstance(colour, list) or isinstance(colour, np.ndarray)) and len(colour) == 3:
31+
# Convert (r, g, b) to "#RRGGBB"
32+
if all(isinstance(c, float) and 0.0 <= c <= 1.0 for c in colour):
33+
rgb = [int(c * 255) for c in colour]
34+
else:
35+
rgb = [int(c) for c in colour]
36+
self.colour = "#{:02x}{:02x}{:02x}".format(*rgb )
37+
else:
38+
self.colour = str(colour)
39+
else:
40+
self.colour = ""
2841
self.thickness = thickness # Optional thickness attribute
42+
print("color is", self.colour)
2943
# Add delete button
3044
self.buttonDelete.clicked.connect(self.request_delete)
3145
self.lineEditName.editingFinished.connect(self.onNameChanged)
3246
self.spinBoxThickness.valueChanged.connect(self.onThicknessChanged)
47+
self.setStyleSheet(f"background-color: {self.colour};" if self.colour else "")
3348

49+
print(self.styleSheet())
3450
@property
3551
def name(self):
3652
return str(self._name)
@@ -58,7 +74,9 @@ def onColourSelectClicked(self):
5874
color = QColorDialog.getColor()
5975
if color.isValid():
6076
self.colour = color.name()
61-
self.buttonColor.setStyleSheet(f"background-color: {self.colour};")
77+
print("Selected colour:", self.colour)
78+
self.setStyleSheet(f"background-color: {self.colour};")
79+
self.colourChanged.emit(self.colour)
6280

6381
def onThicknessChanged(self, thickness: float):
6482
"""Handle changes to the thickness spinbox.
@@ -84,6 +102,10 @@ def request_delete(self):
84102

85103
self.deleteRequested.emit(self)
86104

105+
def mouseDoubleClickEvent(self, event):
106+
"""Handle double-click event to open color selection dialog."""
107+
self.onColourSelectClicked()
108+
87109
def validateFields(self):
88110
"""Validate the widget fields and update UI hints."""
89111
# Reset all styles first
@@ -111,11 +133,13 @@ def setData(self, data: Optional[dict] = None):
111133
self.name = str(data.get("name", ""))
112134
self.colour = data.get("colour", "")
113135
self.lineEditName.setText(self.name)
136+
self.setStyleSheet(f"background-color: {self.colour};" if self.colour else "")
114137
# self.lineEditColour.setText(self.colour)
115138
else:
116139
self.name = ""
117140
self.colour = ""
118141
self.lineEditName.clear()
142+
self.setStyleSheet("")
119143
# self.lineEditColour.clear()
120144

121145
self.validateFields()

0 commit comments

Comments
 (0)