diff --git a/src/doc/doc/about/rba_notation.xml b/src/doc/doc/about/rba_notation.xml index 03bd646fc..dc4fbac5c 100644 --- a/src/doc/doc/about/rba_notation.xml +++ b/src/doc/doc/about/rba_notation.xml @@ -86,7 +86,7 @@ diff --git a/src/doc/doc/programming/events.xml b/src/doc/doc/programming/events.xml index 827102a7c..044ea331b 100644 --- a/src/doc/doc/programming/events.xml +++ b/src/doc/doc/programming/events.xml @@ -59,10 +59,12 @@

- Here is the code: + Here is the code. + This example demonstrates how the "get" method is reimplemented to deliver the actual text.

-
module MyMacro
+  
+module MyMacro
   
   include RBA
   
@@ -78,15 +80,32 @@
   dialog.home = "int:0"
   dialog.exec
 
-end
+end +
+ + The Python version is this: + +
+from pya import BrowserSource, BrowserDialog
+
+class MyBrowserSource(BrowserSource):
+  def get(self, url):
+    next_url = "int:" + str(int(url.split(":")[1]) + 1)
+    return f"This is {url}. <a href='{next_url}'>Goto next ({next_url})</a>>"
+
+dialog = BrowserDialog()
+dialog.home = "int:0"
+dialog.source = MyBrowserSource()
+dialog.exec_()
+

- This example demonstrates how the "get" method is reimplemented to deliver the actual text. Ruby even allows reimplementation of a method without deriving a new class, because it allows - to define methods per instance: + defining methods per instance:

-
module MyMacro
+  
+module MyMacro
   
   include RBA
   
@@ -101,7 +120,8 @@ end
dialog.home = "int:0" dialog.exec -end
+end +

Events

@@ -117,7 +137,8 @@ end clicked, it displays a message box:

-
module MyMacro
+  
+module MyMacro
   
   include RBA
   
@@ -129,14 +150,33 @@ end
Application::instance.main_window.menu.insert_item("@toolbar.end", "my_action", action) -end
+end + + +

+ The Python version is: +

+ +
+from pya import Action, MessageBox, Application
+
+def on_triggered():
+  MessageBox.info("A message", "The action was triggered", MessageBox.Ok)
+
+action = Action()
+action.on_triggered = on_triggered
+action.title = "My Action"
+
+Application.instance().main_window().menu().insert_item("@toolbar.end", "my_action", action)
+

Specifying a block to an event will make the event only execute that block. A more flexible way of controlling the code attached to events is available through the += and -= operators:

-
module MyMacro
+  
+module MyMacro
 
   include RBA
 
@@ -157,7 +197,12 @@ end
# to clear all event handlers use: action.on_triggered.clear -
+ + +

+ Synonyms for the += operator are add and connect. The latter makes code more familiar for PyQt users. + In the same way, synonyms for the -= operator are remove and disconnect. +

If the Qt binding is available (see ), Qt signals @@ -166,7 +211,8 @@ end input field to the label below:

-
module MyMacro
+  
+module MyMacro
   
   include RBA
   
@@ -182,13 +228,38 @@ end
dialog.exec -end
+end + + +

+ The Python version is: +

+ +
+from pya import QDialog, QVBoxLayout, QLineEdit, QLabel, Application
+
+dialog = QDialog(Application.instance().main_window())
+layout = QVBoxLayout(dialog)
+input = QLineEdit(dialog)
+label = QLabel(dialog)
+layout.addWidget(input)
+layout.addWidget(label)
+
+def text_changed(text):
+  label.text = text
+
+# implement the textChanged signal as event:
+input.textChanged = text_changed
+
+dialog.exec_()
+

Using the += operator on the event, multiple handlers can be added to a signal:

-
module MyMacro
+  
+module MyMacro
   
   include RBA
   
@@ -207,7 +278,37 @@ end
dialog.exec -end
+end + + +

+ with the Python version: +

+ +
+from pya import QDialog, QVBoxLayout, QLineEdit, QLabel, Application
+
+dialog = QDialog(Application.instance().main_window())
+layout = QVBoxLayout(dialog)
+input = QLineEdit(dialog)
+label1 = QLabel(dialog)
+label2 = QLabel(dialog)
+layout.addWidget(input)
+layout.addWidget(label1)
+layout.addWidget(label2)
+
+def text_changed1(text):
+  label1.text = text
+
+def text_changed2(text):
+  label2.text = text[::-1]
+
+# two signal consumers:
+input.textChanged += text_changed1
+input.textChanged += text_changed2
+
+dialog.exec_()
+