11import enum
22import types
33import typing
4+ from dataclasses import dataclass
45
56from PyQt5 import QtCore , QtGui , QtWidgets
67
7- from melid .store import State
8+ from melid .store import StatefulWidget
89from melid .types import AppEvents
910
1011
11- class Widget (QtWidgets .QGroupBox ):
12+ class Widget (QtWidgets .QGroupBox , StatefulWidget ):
1213
1314 STYLESHEET_PATH = ""
1415 STYLESHEET_TYPE = "CSS"
@@ -19,8 +20,8 @@ def __init__(
1920 ):
2021 super ().__init__ ()
2122
22- if qt_window_resized :
23- qt_window_resized .connect (self .onWindowResized )
23+ # if qt_window_resized:
24+ # qt_window_resized.connect(self.onWindowResized)
2425
2526 self .setObjectName (self .__class__ .__name__ )
2627 self .setContentsMargins (0 , 0 , 0 , 0 )
@@ -90,38 +91,6 @@ def onWindowResized(self, size: QtCore.QSize):
9091 def updateStyleSheet (self , style : str ):
9192 self .setStyleSheet (self .styleSheet () + "\n %s" % style )
9293
93- def state (
94- self ,
95- state : State ,
96- condition : typing .Union [bool , typing .Callable [[], bool ]] = True ,
97- attribute : str = None ,
98- params : typing .Union [typing .List , typing .Dict , typing .Callable ] = None ,
99- ):
100- def resolve_params (p ):
101- if callable (p ):
102- return resolve_params (p ())
103-
104- p1 = p if isinstance (p , list ) else []
105- p2 = p if isinstance (p , dict ) else {}
106-
107- return p1 , p2
108-
109- def resolve_condition (c ):
110- if callable (c ):
111- return resolve_condition (c ())
112-
113- return c
114-
115- def on_state_change ():
116- p1 , p2 = resolve_params (params )
117-
118- if resolve_condition (condition ) and attribute and hasattr (self , attribute ):
119- getattr (self , attribute )(* p1 , ** p2 )
120-
121- state .subscribe (on_state_change )
122-
123- return self
124-
12594
12695class Label (QtWidgets .QLabel , Widget ):
12796 def __init__ (self , text : str , style : str = "" , * args , ** kwargs ):
@@ -136,21 +105,25 @@ def __init__(self, text: str, style: str = "", *args, **kwargs):
136105class Button (QtWidgets .QPushButton , Widget ):
137106 def __init__ (
138107 self ,
139- text : typing .Optional [typing .Union [str , QtWidgets .QWidget ]] = "" ,
108+ child : typing .Optional [typing .Union [str , QtWidgets .QWidget , QtGui . QIcon ]] = "" ,
140109 on_click : typing .Optional [types .FunctionType ] = None ,
141110 style : typing .Optional [str ] = "" ,
142111 * args ,
143112 ** kwargs ,
144113 ):
145114 super (Button , self ).__init__ (* args , ** kwargs )
146115
147- if isinstance (text , str ):
148- self .setText (text )
116+ self .setObjectName (self .__class__ .__name__ )
117+
118+ if child :
119+ if isinstance (child , str ):
120+ self .setText (child )
121+ if isinstance (child , QtGui .QIcon ):
122+ self .setIcon (child )
149123
150124 if on_click :
151125 self .clicked .connect (lambda : on_click ())
152126
153- self .setObjectName (self .__class__ .__name__ )
154127 self .setStyleSheet (style )
155128 self .setCursor (QtGui .QCursor (QtCore .Qt .PointingHandCursor ))
156129
@@ -182,8 +155,87 @@ def __init__(
182155
183156 self .setObjectName (self .__class__ .__name__ )
184157 self .setStyleSheet (style )
185- self .setSizePolicy (
186- QtWidgets .QSizePolicy (
187- QtWidgets .QSizePolicy .Expanding , QtWidgets .QSizePolicy .Expanding
188- )
158+
159+
160+ class TextArea (QtWidgets .QTextEdit , Widget ):
161+ def __init__ (
162+ self ,
163+ on_change : typing .Optional [typing .Callable ] = None ,
164+ style : typing .Optional [str ] = "" ,
165+ ):
166+ super ().__init__ ()
167+
168+ if on_change :
169+ self .textChanged .connect (on_change )
170+
171+ self .setObjectName (self .__class__ .__name__ )
172+ self .setStyleSheet (style )
173+
174+
175+ @dataclass
176+ class Option :
177+ label : str
178+ value : typing .Any
179+
180+
181+ class Select (QtWidgets .QComboBox , Widget ):
182+ def __init__ (
183+ self ,
184+ options : typing .Optional [typing .List [Option ]] = None ,
185+ on_change : typing .Optional [typing .Callable ] = None ,
186+ style : typing .Optional [str ] = "" ,
187+ ):
188+ super ().__init__ ()
189+
190+ if options :
191+ self .addItems (list (map (lambda x : x .label , options )))
192+
193+ if on_change :
194+ self .activated .connect (lambda idx : on_change (options [idx ].value ))
195+
196+ # Set placeholder text for the combo box
197+ self .setEditable (True )
198+ self .lineEdit ().setPlaceholderText ("Select an option..." )
199+ self .lineEdit ().setAlignment (QtCore .Qt .AlignCenter )
200+ self .setEditable (False ) # Disable edit after setting placeholder
201+
202+ # Set custom background color and styling using CSS
203+ # self.setStyleSheet(
204+ # """
205+ # QComboBox {
206+ # background-color: #EFEFEF;
207+ # border: 1px solid #AFAFAF;
208+ # padding: 5px;
209+ # border-radius: 5px;
210+ # }
211+ # QComboBox::drop-down {
212+ # subcontrol-origin: padding;
213+ # subcontrol-position: top right;
214+ # width: 20px;
215+ # border-left-width: 1px;
216+ # border-left-color: darkgray;
217+ # border-left-style: solid; /* Just a single line */
218+ # border-top-right-radius: 3px; /* same radius as the QComboBox */
219+ # border-bottom-right-radius: 3px;
220+ # }
221+ # QComboBox::down-arrow {
222+ # image: url(icons/down_arrow.png); /* Custom down-arrow icon */
223+ # }
224+ # QComboBox QAbstractItemView {
225+ # background-color: white;
226+ # selection-background-color: lightgray;
227+ # selection-color: black;
228+ # }
229+ # """
230+ # )
231+ self .setStyleSheet (
232+ style
233+ + """
234+ Select::drop-down {
235+ subcontrol-origin: padding;
236+ subcontrol-position: top right;
237+ width: 5px;
238+ padding: 5px;
239+ }
240+ """
189241 )
0 commit comments