Last active
April 22, 2019 15:41
-
-
Save dridk/12730b46a958349a1e979c756a9cf60e to your computer and use it in GitHub Desktop.
dispatcher cutevariant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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