1- from PyQt5 .QtWidgets import QGraphicsEllipseItem , QGraphicsTextItem , QColorDialog , QToolTip
1+ from PyQt5 .QtWidgets import (
2+ QGraphicsEllipseItem , QGraphicsTextItem , QToolTip ,
3+ QDialog , QVBoxLayout , QPushButton , QHBoxLayout
4+ )
25from PyQt5 .QtGui import QBrush , QColor , QFont
36from PyQt5 .QtCore import Qt
47
58class NodeItem (QGraphicsEllipseItem ):
69 def __init__ (self , node_id , pos , parent_level ):
7- super ().__init__ (- 20 , - 20 , 40 , 40 ) # 40x40 circle
10+ super ().__init__ (- 20 , - 20 , 40 , 40 )
811 self .node_id = node_id
912 self .parent_level = parent_level
1013 self .setBrush (QBrush (Qt .gray ))
@@ -13,24 +16,49 @@ def __init__(self, node_id, pos, parent_level):
1316 self .setPos (pos )
1417 self .locked = False
1518
16- # Node label
1719 self .text = QGraphicsTextItem (str (node_id ))
1820 self .text .setDefaultTextColor (Qt .white )
1921 self .text .setFont (QFont ("Arial" , 10 , QFont .Bold ))
2022 self .text .setParentItem (self )
2123 self .text .setPos (- 8 , - 10 )
2224
25+ # Define the allowed color palette
26+ self .allowed_colors = {
27+ "Red" : "#E74C3C" ,
28+ "Green" : "#2ECC71" ,
29+ " Blue" : "#3498DB" ,
30+ "Yellow" : "#F1C40F"
31+ }
32+
2333 def mousePressEvent (self , event ):
2434 if not self .locked and not self .parent_level .committed :
25- color = QColorDialog .getColor ()
26- if color .isValid ():
27- self .setBrush (QBrush (color ))
28- self .parent_level .node_colors [self .node_id ] = color .name ()
29- self .parent_level .update_narration (f"🎨 Prover colored node { self .node_id } as a secret." )
35+ self .show_color_selection_dialog ()
3036 super ().mousePressEvent (event )
3137
38+ def show_color_selection_dialog (self ):
39+ dialog = QDialog ()
40+ dialog .setWindowTitle (f"Select a color for Node { self .node_id } " )
41+ layout = QVBoxLayout ()
42+
43+ button_row = QHBoxLayout ()
44+ for label , hex_code in self .allowed_colors .items ():
45+ btn = QPushButton (label )
46+ btn .setStyleSheet (f"background-color: { hex_code } ; color: black; font-weight: bold;" )
47+ btn .clicked .connect (lambda _ , c = hex_code : self .select_color (dialog , c ))
48+ button_row .addWidget (btn )
49+
50+ layout .addLayout (button_row )
51+ dialog .setLayout (layout )
52+ dialog .exec_ ()
53+
54+ def select_color (self , dialog , color_hex ):
55+ self .setBrush (QBrush (QColor (color_hex )))
56+ self .parent_level .node_colors [self .node_id ] = color_hex
57+ self .parent_level .update_narration (f"🎨 Prover colored node { self .node_id } with color." )
58+ dialog .accept ()
59+
3260 def hoverEnterEvent (self , event ):
3361 if self .locked :
3462 QToolTip .showText (event .screenPos (), "🔒 This node is committed" , self .parent_level )
3563 else :
36- QToolTip .showText (event .screenPos (), f" Node { self .node_id } : Click to color" , self .parent_level )
64+ QToolTip .showText (event .screenPos (), f" Node { self .node_id } : Click to choose a color" , self .parent_level )
0 commit comments