11import os
2+ import numpy as np
23from typing import Optional
34
45from 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