Skip to content

Commit 41fd222

Browse files
committed
Event listener registration can now be done by the **do** instruction instead of **connect** (that stays available for compatibility reasons).
1 parent 1a0343d commit 41fd222

25 files changed

Lines changed: 136 additions & 125 deletions

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ There is also a drag n drop GUI editor. Look at the [Editor](https://github.com/
3131

3232
Changelog
3333
===
34+
*2019 April 1*
35+
36+
Event listener registration can now be done by the **do** instruction instead of **connect** (that stays available for compatibility reasons).
37+
i.e.
38+
```python
39+
mybutton.onclick.do(myevent_listener)
40+
```
41+
42+
*Older changes*
43+
3444
The current branch includes improvements about resource files handling.
3545
App constructor accepts **static_file_path** parameter. Its value have to be a dictionary, where elements represents named resources paths.
3646

@@ -105,7 +115,7 @@ class MyApp(App):
105115
self.bt = gui.Button('Press me!')
106116

107117
# setting the listener for the onclick event of the button
108-
self.bt.onclick.connect(self.on_button_pressed)
118+
self.bt.onclick.do(self.on_button_pressed)
109119

110120
# appending a widget to another, the first argument is a string key
111121
container.append(self.lbl)
@@ -214,7 +224,7 @@ Such events are a convenient way to define the application behavior.
214224
Each widget has its own callbacks, depending on the type of user interaction it allows.
215225
The specific callbacks for the widgets will be illustrated later.
216226

217-
In order to register a function as an event listener you have to call a function like eventname.connect (i.e. onclick.connect) passing as parameters the callback that will manage the event.
227+
In order to register a function as an event listener you have to call a function like eventname.do (i.e. onclick.do) passing as parameters the callback that will manage the event.
218228
Follows an example:
219229

220230
```py
@@ -231,7 +241,7 @@ class MyApp(App):
231241
self.bt = gui.Button('Press me!')
232242

233243
# setting the listener for the onclick event of the button
234-
self.bt.onclick.connect(self.on_button_pressed)
244+
self.bt.onclick.do(self.on_button_pressed)
235245

236246
# appending a widget to another, the first argument is a string key
237247
container.append(self.lbl)
@@ -249,13 +259,13 @@ class MyApp(App):
249259
start(MyApp)
250260
```
251261

252-
In the shown example *self.bt.onclick.connect(self.on_button_pressed)* registers the self's *on_button_pressed* function as a listener for the event *onclick* exposed by the Button widget.
262+
In the shown example *self.bt.onclick.do(self.on_button_pressed)* registers the self's *on_button_pressed* function as a listener for the event *onclick* exposed by the Button widget.
253263
Simple, easy.
254264

255265
Listener's callbacks will receive the emitter's instance firstly, then all other parameters provided by the specific event.
256266

257267

258-
Besides the standard event registration (as aforementioned), it is possible to pass user parameters to listener functions. This can be achieves appending parameters to the *connect* function call.
268+
Besides the standard event registration (as aforementioned), it is possible to pass user parameters to listener functions. This can be achieves appending parameters to the *do* function call.
259269

260270
```py
261271
import remi.gui as gui
@@ -272,8 +282,8 @@ class MyApp(App):
272282
self.bt2 = gui.Button('Hello name surname!')
273283

274284
# setting the listener for the onclick event of the buttons
275-
self.bt.onclick.connect(self.on_button_pressed, "Name")
276-
self.bt2.onclick.connect(self.on_button_pressed, "Name", "Surname")
285+
self.bt.onclick.do(self.on_button_pressed, "Name")
286+
self.bt2.onclick.do(self.on_button_pressed, "Name", "Surname")
277287

278288
# appending a widget to another
279289
container.append(self.lbl)

editor/editor.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ def setup(self, refWidget, newParent):
7070

7171
def start_drag(self, emitter, x, y):
7272
self.active = True
73-
self.project.onmousemove.connect(self.on_drag)
74-
self.project.onmouseup.connect(self.stop_drag)
75-
self.project.onmouseleave.connect(self.stop_drag, 0, 0)
73+
self.project.onmousemove.do(self.on_drag)
74+
self.project.onmouseup.do(self.stop_drag)
75+
self.project.onmouseleave.do(self.stop_drag, 0, 0)
7676
self.origin_x = -1
7777
self.origin_y = -1
7878

@@ -107,7 +107,7 @@ def __init__(self, project, name_coord_x, name_coord_y, compatibility_iterable,
107107
self.set_stroke(1, 'black')
108108
self.set_fill('#ffcc00')
109109
self.compatibility_iterable = compatibility_iterable
110-
self.onmousedown.connect(self.start_drag)
110+
self.onmousedown.do(self.start_drag)
111111

112112
def setup(self, refWidget, newParent):
113113
if type(refWidget) in self.compatibility_iterable or refWidget == None:
@@ -140,7 +140,7 @@ def __init__(self, project, compatibility_iterable, **kwargs):
140140
self.set_stroke(1, 'black')
141141
self.set_fill('#ffcc00')
142142
self.compatibility_iterable = compatibility_iterable
143-
self.onmousedown.connect(self.start_drag)
143+
self.onmousedown.do(self.start_drag)
144144

145145
def setup(self, refWidget, newParent):
146146
if type(refWidget) in self.compatibility_iterable or refWidget == None:
@@ -173,7 +173,7 @@ def __init__(self, project, compatibility_iterable, **kwargs):
173173
self.set_stroke(1, 'black')
174174
self.set_fill('#ffcc00')
175175
self.compatibility_iterable = compatibility_iterable
176-
self.onmousedown.connect(self.start_drag)
176+
self.onmousedown.do(self.start_drag)
177177

178178
def setup(self, refWidget, newParent):
179179
if type(refWidget) in self.compatibility_iterable or refWidget == None:
@@ -206,7 +206,7 @@ def __init__(self, project, **kwargs):
206206
self.style['position'] = 'absolute'
207207
self.style['left']='0px'
208208
self.style['top']='0px'
209-
self.onmousedown.connect(self.start_drag)
209+
self.onmousedown.do(self.start_drag)
210210

211211
def setup(self, refWidget, newParent):
212212
if type(refWidget) in [gui.Widget, gui.Button, gui.GridBox, gui.VBox, gui.HBox,
@@ -247,7 +247,7 @@ def __init__(self, project, **kwargs):
247247
self.style['position'] = 'absolute'
248248
self.style['left']='0px'
249249
self.style['top']='0px'
250-
self.onmousedown.connect(self.start_drag)
250+
self.onmousedown.do(self.start_drag)
251251

252252
def setup(self, refWidget, newParent):
253253
if type(refWidget) in [gui.Widget, gui.Button, gui.GridBox, gui.VBox, gui.HBox,
@@ -333,7 +333,7 @@ def create_callback_copy(self, widget):
333333
#if there is a callback
334334
if getattr(widget, setOnEventListenerFuncname).callback:
335335
getattr(widget, setOnEventListenerFuncname).callback_copy = getattr(widget, setOnEventListenerFuncname).callback
336-
getattr(widget, setOnEventListenerFuncname).connect(None)
336+
getattr(widget, setOnEventListenerFuncname).do(None)
337337
for w in widget.children.values():
338338
self.create_callback_copy(w)
339339

@@ -556,20 +556,20 @@ def main(self):
556556
self.toolbar.append(grid_size)
557557

558558
self.fileOpenDialog = editor_widgets.EditorFileSelectionDialog('Open Project', 'Select the project file.<br>It have to be a python program created with this editor.', False, '.', True, False, self)
559-
self.fileOpenDialog.confirm_value.connect(self.on_open_dialog_confirm)
559+
self.fileOpenDialog.confirm_value.do(self.on_open_dialog_confirm)
560560

561561
self.fileSaveAsDialog = editor_widgets.EditorFileSaveDialog('Project Save', 'Select the project folder and type a filename', False, '.', False, True, self)
562562
self.fileSaveAsDialog.add_fileinput_field('untitled.py')
563-
self.fileSaveAsDialog.confirm_value.connect(self.on_saveas_dialog_confirm)
563+
self.fileSaveAsDialog.confirm_value.do(self.on_saveas_dialog_confirm)
564564

565-
m10.onclick.connect(self.menu_new_clicked)
566-
m11.onclick.connect(self.fileOpenDialog.show)
567-
m121.onclick.connect(self.menu_save_clicked)
568-
m122.onclick.connect(self.fileSaveAsDialog.show)
569-
m21.onclick.connect(self.menu_cut_selection_clicked)
570-
m22.onclick.connect(self.menu_paste_selection_clicked)
565+
m10.onclick.do(self.menu_new_clicked)
566+
m11.onclick.do(self.fileOpenDialog.show)
567+
m121.onclick.do(self.menu_save_clicked)
568+
m122.onclick.do(self.fileSaveAsDialog.show)
569+
m21.onclick.do(self.menu_cut_selection_clicked)
570+
m22.onclick.do(self.menu_paste_selection_clicked)
571571

572-
m3.onclick.connect(self.menu_project_config_clicked)
572+
m3.onclick.do(self.menu_project_config_clicked)
573573

574574
self.subContainer = gui.HBox(width='100%', height='96%', layout_orientation=gui.Widget.LAYOUT_HORIZONTAL)
575575
self.subContainer.style.update({'position':'relative',
@@ -595,7 +595,7 @@ def main(self):
595595
596596
return false;""" % {'evt':self.EVENT_ONDROPPPED}
597597
self.project.attributes['editor_varname'] = 'App'
598-
self.project.onkeydown.connect(self.onkeydown)
598+
self.project.onkeydown.do(self.onkeydown)
599599

600600
self.projectConfiguration = editor_widgets.ProjectConfigurationDialog('Project Configuration', 'Write here the configuration for your project.')
601601

@@ -619,7 +619,7 @@ def main(self):
619619
self.subContainerRight.add_class('RaisedFrame')
620620

621621
self.instancesWidget = editor_widgets.InstancesWidget(width='100%')
622-
self.instancesWidget.treeView.on_tree_item_selected.connect(self.on_instances_widget_selection)
622+
self.instancesWidget.treeView.on_tree_item_selected.do(self.on_instances_widget_selection)
623623

624624
self.subContainerRight.append([self.instancesWidget, self.attributeEditor])
625625

@@ -635,7 +635,7 @@ def main(self):
635635
SvgDraggablePoint(self.project, 'x', 'y', [gui.SvgRectangle, gui.SvgText]),
636636
SvgDraggableRectangleResizePoint(self.project, [gui.SvgRectangle])]
637637
for drag_helper in self.drag_helpers:
638-
drag_helper.stop_drag.connect(self.on_drag_resize_end)
638+
drag_helper.stop_drag.do(self.on_drag_resize_end)
639639

640640
self.menu_new_clicked(None)
641641

@@ -660,7 +660,7 @@ def configure_widget_for_editing(self, widget):
660660

661661
if not 'editor_varname' in widget.attributes:
662662
return
663-
widget.onclick.connect(self.on_widget_selection)
663+
widget.onclick.do(self.on_widget_selection)
664664

665665
#setup of the on_dropped function of the widget in order to manage the dragNdrop
666666
widget.__class__.on_dropped = on_dropped

editor/editor_widgets.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def append_instance(self, instance, parent):
2828
if parent==None:
2929
parent = self
3030
item.instance = instance
31-
item.onclick.connect(self.on_tree_item_selected)
31+
item.onclick.do(self.on_tree_item_selected)
3232
parent.append(item)
3333
return item
3434

@@ -110,7 +110,7 @@ def __init__(self, **kwargs):
110110
def add_command(self, imagePath, callback, title):
111111
icon = gui.Image(imagePath, height='90%', margin='0px 1px')
112112
icon.style['outline'] = '1px solid lightgray'
113-
icon.onclick.connect(callback)
113+
icon.onclick.do(callback)
114114
icon.attributes['title'] = title
115115
self.append(icon)
116116

@@ -125,7 +125,7 @@ def __init__(self, widget, listenersList, eventConnectionFuncName, eventConnecti
125125
self.label.style.update({'float':'left', 'font-size':'10px', 'overflow':'hidden', 'outline':'1px solid lightgray'})
126126

127127
self.dropdown = gui.DropDown(width='49%', height='100%')
128-
self.dropdown.onchange.connect(self.on_connection)
128+
self.dropdown.onchange.do(self.on_connection)
129129
self.append([self.label, self.dropdown])
130130
self.dropdown.style['float'] = 'right'
131131

@@ -144,14 +144,14 @@ def __init__(self, widget, listenersList, eventConnectionFuncName, eventConnecti
144144

145145
def on_connection(self, widget, dropDownValue):
146146
if self.dropdown.get_value()=='None':
147-
self.eventConnectionFunc.connect(None)
147+
self.eventConnectionFunc.do(None)
148148
else:
149149
listener = self.dropdown._selected_item.listenerInstance
150150
listener.attributes['editor_newclass'] = "True"
151151
print("Event: " + self.eventConnectionFuncName + " signal connection to: " + listener.attributes['editor_varname'] + " from:" + self.refWidget.attributes['editor_varname'])
152152
back_callback = getattr(self.refWidget, self.eventConnectionFuncName).callback
153153
listener.__class__.fakeListenerFunc = fakeListenerFunc
154-
getattr(self.refWidget, self.eventConnectionFuncName).connect(listener.fakeListenerFunc)
154+
getattr(self.refWidget, self.eventConnectionFuncName).do(listener.fakeListenerFunc)
155155
getattr(self.refWidget, self.eventConnectionFuncName).callback_copy = getattr(self.refWidget, self.eventConnectionFuncName).callback
156156
getattr(self.refWidget, self.eventConnectionFuncName).callback = back_callback
157157

@@ -295,7 +295,7 @@ def show(self, *args):
295295

296296
def add_fileinput_field(self, defaultname='untitled'):
297297
self.txtFilename = gui.TextInput()
298-
self.txtFilename.onkeydown.connect(self.on_enter_key_pressed)
298+
self.txtFilename.onkeydown.do(self.on_enter_key_pressed)
299299
self.txtFilename.set_text(defaultname)
300300

301301
self.add_field_with_label("filename","Filename",self.txtFilename)
@@ -345,7 +345,7 @@ def __init__(self, appInstance, widgetClass, **kwargs_to_widget):
345345

346346
self.optional_style_dict = {} #this dictionary will contain optional style attributes that have to be added to the widget once created
347347

348-
self.onclick.connect(self.prompt_new_widget)
348+
self.onclick.do(self.prompt_new_widget)
349349

350350
def build_widget_name_list_from_tree(self, node):
351351
if not hasattr(node, 'attributes'):
@@ -384,7 +384,7 @@ def prompt_new_widget(self, widget):
384384
self.dialog.add_field_with_label(param, param + note, editWidget)
385385

386386
self.dialog.add_field_with_label("editor_newclass", "Overload base class", gui.CheckBox())
387-
self.dialog.confirm_dialog.connect(self.on_dialog_confirm)
387+
self.dialog.confirm_dialog.do(self.on_dialog_confirm)
388388
self.dialog.show(self.appInstance)
389389

390390
def on_dropped(self, left, top):
@@ -517,7 +517,7 @@ def __init__(self, title, **kwargs):
517517
'background-repeat':'no-repeat',
518518
'background-position':'5px',
519519
'border-top':'3px solid lightgray'})
520-
self.title.onclick.connect(self.openClose)
520+
self.title.onclick.do(self.openClose)
521521
self.append(self.title, '0')
522522

523523
def openClose(self, widget):
@@ -559,8 +559,8 @@ def __init__(self, appInstance, **kwargs):
559559
attributeName = attribute[0]
560560
attributeValue = attribute[1]
561561
attributeEditor = EditorAttributeInput(attributeName, attributeValue, appInstance)
562-
attributeEditor.on_attribute_changed.connect(self.onattribute_changed)
563-
attributeEditor.on_attribute_remove.connect(self.onattribute_remove)
562+
attributeEditor.on_attribute_changed.do(self.onattribute_changed)
563+
attributeEditor.on_attribute_remove.do(self.onattribute_remove)
564564
#attributeEditor.style['display'] = 'none'
565565
if not attributeValue['group'] in self.attributeGroups.keys():
566566
groupContainer = EditorAttributesGroup(attributeValue['group'], width='100%')
@@ -610,15 +610,15 @@ def __init__(self, appInstance, **kwargs):
610610
self.style['overflow'] = 'hidden'
611611

612612
self.numInput = gui.SpinBox('0',-999999999, 999999999, 1, width='60%', height='100%')
613-
self.numInput.onchange.connect(self.onchange)
613+
self.numInput.onchange.do(self.onchange)
614614
self.numInput.style['text-align'] = 'right'
615615
self.append(self.numInput)
616616

617617
self.dropMeasureUnit = gui.DropDown(width='40%', height='100%')
618618
self.dropMeasureUnit.append( gui.DropDownItem('px'), 'px' )
619619
self.dropMeasureUnit.append( gui.DropDownItem('%'), '%' )
620620
self.dropMeasureUnit.select_by_key('px')
621-
self.dropMeasureUnit.onchange.connect(self.onchange)
621+
self.dropMeasureUnit.onchange.do(self.onchange)
622622
self.append(self.dropMeasureUnit)
623623

624624
@gui.decorate_event
@@ -654,18 +654,18 @@ def __init__(self, appInstance, **kwargs):
654654

655655
self.txtInput = gui.TextInput(width='80%', height='100%')
656656
self.txtInput.style['float'] = 'left'
657-
self.txtInput.onchange.connect(self.on_txt_changed)
657+
self.txtInput.onchange.do(self.on_txt_changed)
658658
self.append(self.txtInput)
659659

660660
self.btFileFolderSelection = gui.Widget(width='20%', height='100%')
661661
self.btFileFolderSelection.style.update({'background-repeat':'round',
662662
'background-image':"url('/res:folder.png')",
663663
'background-color':'transparent'})
664664
self.append(self.btFileFolderSelection)
665-
self.btFileFolderSelection.onclick.connect(self.on_file_selection_bt_pressed)
665+
self.btFileFolderSelection.onclick.do(self.on_file_selection_bt_pressed)
666666

667667
self.selectionDialog = gui.FileSelectionDialog('Select a file', '', False, './', True, False)
668-
self.selectionDialog.confirm_value.connect(self.file_dialog_confirmed)
668+
self.selectionDialog.confirm_value.do(self.file_dialog_confirmed)
669669

670670
@gui.decorate_event
671671
def onchange(self, widget, value):
@@ -704,7 +704,7 @@ def __init__(self, attributeName, attributeDict, appInstance=None):
704704
self.EVENT_ATTRIB_ONREMOVE = 'onremove_attribute'
705705
self.removeAttribute = gui.Image('/editor_resources:delete.png', width='5%')
706706
self.removeAttribute.attributes['title'] = 'Remove attribute from this widget.'
707-
self.removeAttribute.onclick.connect(self.on_attribute_remove)
707+
self.removeAttribute.onclick.do(self.on_attribute_remove)
708708
self.append(self.removeAttribute)
709709

710710
self.label = gui.Label(attributeName, width='45%', height=22, margin='0px')
@@ -734,7 +734,7 @@ def __init__(self, attributeName, attributeDict, appInstance=None):
734734
else: #default editor is string
735735
self.inputWidget = gui.TextInput()
736736

737-
self.inputWidget.onchange.connect(self.on_attribute_changed)
737+
self.inputWidget.onchange.do(self.on_attribute_changed)
738738
self.inputWidget.set_size('50%','22px')
739739
self.inputWidget.attributes['title'] = attributeDict['description']
740740
self.label.attributes['title'] = attributeDict['description']

editor/prototypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ def construct_ui(self):
6767

6868
proto_layout_append = "%(parentname)s.append(%(varname)s)\n "
6969

70-
proto_set_listener = "%(sourcename)s.%(register_function)s.connect(%(listenername)s.%(listener_function)s)\n "
70+
proto_set_listener = "%(sourcename)s.%(register_function)s.do(%(listenername)s.%(listener_function)s)\n "

examples/append_and_remove_widgets_app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ def main(self):
2727
main_container = gui.VBox()
2828
lbl = gui.Label("Press the buttons to add or remove labels")
2929
bt_add = gui.Button("add a label", style={'margin':'3px'})
30-
bt_add.onclick.connect(self.on_add_a_label_pressed)
30+
bt_add.onclick.do(self.on_add_a_label_pressed)
3131
bt_remove = gui.Button("remove a label", style={'margin':'3px', 'background-color':'orange'})
32-
bt_remove.onclick.connect(self.on_remove_a_label_pressed)
32+
bt_remove.onclick.do(self.on_remove_a_label_pressed)
3333
bt_empty = gui.Button("empty", style={'margin':'3px', 'background-color':'red'})
34-
bt_empty.onclick.connect(self.on_empty_pressed)
34+
bt_empty.onclick.do(self.on_empty_pressed)
3535
self.lbls_container = gui.HBox()
3636
main_container.append([lbl, bt_add, bt_remove, bt_empty, self.lbls_container])
3737

examples/closeable_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def main(self, name='world'):
3030
bt.style['margin'] = 'auto 50px'
3131
bt.style['background-color'] = 'red'
3232

33-
bt.onclick.connect(self.on_button_pressed)
33+
bt.onclick.do(self.on_button_pressed)
3434

3535
wid.append(bt)
3636
return wid

examples/gauge_app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def __init__(self, width, height, _min, _max, **kwargs):
3333
self.gauge.set_value(_min)
3434
self.append(self.gauge)
3535

36-
self.onmousedown.connect(self.confirm_value)
37-
self.onmousemove.connect(self.gauge.onmousemove)
36+
self.onmousedown.do(self.confirm_value)
37+
self.onmousemove.do(self.gauge.onmousemove)
3838

3939
@gui.decorate_event
4040
def confirm_value(self, widget, x, y):

0 commit comments

Comments
 (0)