Advertisement
Woobinda

Pattern Mediator

May 25th, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Copyright © 2012-13 Qtrac Ltd. All rights reserved.
  3. # This program or module is free software: you can redistribute it
  4. # and/or modify it under the terms of the GNU General Public License as
  5. # published by the Free Software Foundation, either version 3 of the
  6. # License, or (at your option) any later version. It is provided for
  7. # educational purposes and is distributed in the hope that it will be
  8. # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. # General Public License for more details.
  11.  
  12. from Qtrac import coroutine
  13.  
  14.  
  15. def main():
  16.     form = Form()
  17.     test_user_interaction_with(form)
  18.  
  19.  
  20. class Form:
  21.  
  22.     def __init__(self):
  23.         self.create_widgets()
  24.         self.create_mediator()
  25.  
  26.  
  27.     def create_widgets(self):
  28.         self.nameText = Text()
  29.         self.emailText = Text()
  30.         self.okButton = Button("OK")
  31.         self.cancelButton = Button("Cancel")
  32.  
  33.  
  34.     def create_mediator(self):
  35.         self.mediator = self._update_ui_mediator(self._clicked_mediator())
  36.         for widget in (self.nameText, self.emailText, self.okButton,
  37.                 self.cancelButton):
  38.             widget.mediator = self.mediator
  39.         self.mediator.send(None)
  40.  
  41.  
  42.     @coroutine
  43.     def _update_ui_mediator(self, successor=None):
  44.         while True:
  45.             widget = (yield)
  46.             self.okButton.enabled = (bool(self.nameText.text) and
  47.                                      bool(self.emailText.text))
  48.             if successor is not None:
  49.                 successor.send(widget)
  50.  
  51.  
  52.     @coroutine
  53.     def _clicked_mediator(self, successor=None):
  54.         while True:
  55.             widget = (yield)
  56.             if widget == self.okButton:
  57.                 print("OK")
  58.             elif widget == self.cancelButton:
  59.                 print("Cancel")
  60.             elif successor is not None:
  61.                 successor.send(widget)
  62.  
  63.  
  64. class Mediated:
  65.  
  66.     def __init__(self):
  67.         self.mediator = None
  68.  
  69.  
  70.     def on_change(self):
  71.         if self.mediator is not None:
  72.             self.mediator.send(self)
  73.  
  74.  
  75. class Button(Mediated):
  76.  
  77.     def __init__(self, text=""):
  78.         super().__init__()
  79.         self.enabled = True
  80.         self.text = text
  81.    
  82.  
  83.     def click(self):
  84.         if self.enabled:
  85.             self.on_change()
  86.  
  87.  
  88.     def __str__(self):
  89.         return "Button({!r}) {}".format(self.text,
  90.                 "enabled" if self.enabled else "disabled")
  91.  
  92.  
  93. class Text(Mediated):
  94.  
  95.     def __init__(self, text=""):
  96.         super().__init__()
  97.         self.__text = text
  98.    
  99.  
  100.     @property
  101.     def text(self):
  102.         return self.__text
  103.  
  104.  
  105.     @text.setter
  106.     def text(self, text):
  107.         if self.text != text:
  108.             self.__text = text
  109.             self.on_change()
  110.  
  111.  
  112.     def __str__(self):
  113.         return "Text({!r})".format(self.text)
  114.  
  115.  
  116. def test_user_interaction_with(form):
  117.     form.okButton.click()           # Ignored because it is disabled
  118.     print(form.okButton.enabled)    # False
  119.     form.nameText.text = "Fred"
  120.     print(form.okButton.enabled)    # False
  121.     form.emailText.text = "fred@bloggers.com"
  122.     print(form.okButton.enabled)    # True
  123.     form.okButton.click()           # OK
  124.     form.emailText.text = ""
  125.     print(form.okButton.enabled)    # False
  126.     form.cancelButton.click()       # Cancel
  127.  
  128.  
  129. if __name__ == "__main__":
  130.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement