11import os
22from typing import Optional
33
4+ import numpy as np
45from PyQt5 import uic
56from PyQt5 .QtCore import pyqtSignal
67from PyQt5 .QtWidgets import QWidget
@@ -24,12 +25,29 @@ 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 (
31+ isinstance (colour , tuple )
32+ or isinstance (colour , list )
33+ or isinstance (colour , np .ndarray )
34+ ) and len (colour ) == 3 :
35+ # Convert (r, g, b) to "#RRGGBB"
36+ if all (isinstance (c , float ) and 0.0 <= c <= 1.0 for c in colour ):
37+ rgb = [int (c * 255 ) for c in colour ]
38+ else :
39+ rgb = [int (c ) for c in colour ]
40+ self .colour = "#{:02x}{:02x}{:02x}" .format (* rgb )
41+ else :
42+ self .colour = str (colour )
43+ else :
44+ self .colour = ""
2845 self .thickness = thickness # Optional thickness attribute
2946 # Add delete button
3047 self .buttonDelete .clicked .connect (self .request_delete )
3148 self .lineEditName .editingFinished .connect (self .onNameChanged )
3249 self .spinBoxThickness .valueChanged .connect (self .onThicknessChanged )
50+ self .setStyleSheet (f"background-color: { self .colour } ;" if self .colour else "" )
3351
3452 @property
3553 def name (self ):
@@ -58,7 +76,8 @@ def onColourSelectClicked(self):
5876 color = QColorDialog .getColor ()
5977 if color .isValid ():
6078 self .colour = color .name ()
61- self .buttonColor .setStyleSheet (f"background-color: { self .colour } ;" )
79+ self .setStyleSheet (f"background-color: { self .colour } ;" )
80+ self .colourChanged .emit (self .colour )
6281
6382 def onThicknessChanged (self , thickness : float ):
6483 """Handle changes to the thickness spinbox.
@@ -84,6 +103,10 @@ def request_delete(self):
84103
85104 self .deleteRequested .emit (self )
86105
106+ def mouseDoubleClickEvent (self , event ):
107+ """Handle double-click event to open color selection dialog."""
108+ self .onColourSelectClicked ()
109+
87110 def validateFields (self ):
88111 """Validate the widget fields and update UI hints."""
89112 # Reset all styles first
@@ -111,11 +134,13 @@ def setData(self, data: Optional[dict] = None):
111134 self .name = str (data .get ("name" , "" ))
112135 self .colour = data .get ("colour" , "" )
113136 self .lineEditName .setText (self .name )
137+ self .setStyleSheet (f"background-color: { self .colour } ;" if self .colour else "" )
114138 # self.lineEditColour.setText(self.colour)
115139 else :
116140 self .name = ""
117141 self .colour = ""
118142 self .lineEditName .clear ()
143+ self .setStyleSheet ("" )
119144 # self.lineEditColour.clear()
120145
121146 self .validateFields ()
0 commit comments