Skip to content

Instantly share code, notes, and snippets.

@dridk

dridk/dispatcher.py

Last active Apr 22, 2019
Embed
What would you like to do?
dispatcher cutevariant
class QueryPluginWidget(PluginWidget):
""" Base class for all query plugin """
query_changed = Signal() # Signal to emit if UI change the query
@property
def query(self):
return self._query
@query.setter
def query(self, query: Query):
self._query = query
def on_query_changed(self):
""" Only this method should be implemented by children """
raise NotImplemented()
class ChildPlugin(QueryPluginWidget):
def on_query_changed(self):
""" Overrided """
self.populate(self.query) # Example : Populate ui on query changed
def button_pressed(self): # Example : Action from the UI which change query
self.query.columns = .... # Update query
self.query_changed.emit() # Tell Other to update query
class QueryDispatcher(object):
def __init__(self, query: Query):
self.query = query
def add_widget(self, widget: QueryPluginWidget) :
widget.query = self.query # Link as reference ? Really ? All will point to the same query ?
widget.query_changed.connect(self.dispatch)
self.widgets.append(widget)
def dispatch(self):
# Notify All widget except the sender that query has changed
for widget in self.widgets:
if widget is not self.sender():
widget.on_query_changed()
# Usage
query = Query()
dispatcher = QueryDispacher(query)
diaspacher.addWidget(ChildWidget())
diaspacher.addWidget(Child2Widget())
diaspacher.addWidget(Child3Widget())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.