Skip to content

Instantly share code, notes, and snippets.

@dridk
Last active April 22, 2019 15:41
Show Gist options
  • Save dridk/12730b46a958349a1e979c756a9cf60e to your computer and use it in GitHub Desktop.
Save dridk/12730b46a958349a1e979c756a9cf60e to your computer and use it in GitHub Desktop.
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