def __init__(self, **kwargs): Widget.__init__(self) Container.__init__(self) QtWidgets.QListWidget.__init__(self, self._owner) _convert_all_signals_to_awaitables(self) self.current_changed = Event("ListWidget.current_changed") self._show(kwargs)
class Thread(threading.Thread): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.event = Event('Thread.event') def run(self): time.sleep(1) self.event.post()
class TableView(Widget, QtWidgets.QTableView): @dispatch() def __init__(self, **kwargs): super().__init__() QtWidgets.QTableView.__init__(self, self._owner) _convert_all_signals_to_awaitables(self) self.current_changed = Event("TableView.current_changed") self._show(kwargs) def currentChanged(self, current, previous): self.current_changed.send(QtEvent(self, current, previous))
def __init__(self, *args, **kwargs): Frame.__init__(self) Gtk.Button.__init__(self, *args, **kwargs) find_parent(Window).add(self) self.clicked = Event("Button.clicked") def send_clicked_event(*args): self.clicked.send(args) self.connect("clicked", send_clicked_event)
def __init__(self, size=None, title=None, modal=False): QMainWindow.__init__(self) Container.__init__(self) self.keys = Keys() if size: self.resize(*size) if title is not None: self.setWindowTitle(title) if modal: self.setWindowModality(Qt.ApplicationModal) self.closed = Event("MainWindow.closed") self.resized = Event("MainWindow.resized")
def __init__(self, layout=None, size=None, title=None): QDialog.__init__(self) Container.__init__(self, layout=layout) self.keys = Keys() if size: self.resize(*size) if title is not None: self.setWindowTitle(title) self.closed = Event("Dialog.closed") def onfinished(result): self._result = result self.remove() self.finished.connect(onfinished)
class ListWidget(Container, Widget, QtWidgets.QListWidget): @dispatch() def __init__(self, **kwargs): Widget.__init__(self) Container.__init__(self) QtWidgets.QListWidget.__init__(self, self._owner) _convert_all_signals_to_awaitables(self) self.current_changed = Event("ListWidget.current_changed") self._show(kwargs) def currentChanged(self, current, previous): self.current_changed.send(QtEvent(self, current, previous)) def add_widget(self, widget, **kwargs): self.addItem(widget) def remove_widget(self, widget): pass
def _convert_all_signals_to_awaitables(obj): for key in dir(obj.__class__): try: signal = getattr(obj, key) except TypeError: continue if type(signal) == QtCore.pyqtBoundSignal: awaitable = Event("{}.{}".format(obj.__class__.__name__, key)) #signal.connect(functools.partial(awaitable.post, obj)) def sig(awaitable, obj, *args): awaitable.post(QtEvent(obj, *args)) signal.connect(functools.partial(sig, awaitable, obj)) awaitable.connect = signal.connect # Preserve pyqtBoundSignal.connect() awaitable.emit = signal.emit # Preserve pyqtBoundSignal.emit() setattr(obj, key, awaitable)
class MainWindow(Container, QMainWindow): def __init__(self, size=None, title=None, modal=False): QMainWindow.__init__(self) Container.__init__(self) self.keys = Keys() if size: self.resize(*size) if title is not None: self.setWindowTitle(title) if modal: self.setWindowModality(Qt.ApplicationModal) self.closed = Event("MainWindow.closed") self.resized = Event("MainWindow.resized") def create(self, framefunc, *frameargs, size=None, title=None, **framekwargs): if size: self.resize(*size) if title is not None: self.setWindowTitle(title) super().create(framefunc, *frameargs, **framekwargs) self.show() def _ondispose(self): self.deleteLater() super()._ondispose() def closeEvent(self, event): self.closed.send(event) if event.isAccepted(): # If event wasn't canceled by user self.remove() def resizeEvent(self, event): self.resized.send(QtEvent(self, event)) def add_widget(self, widget, **kwargs): self.setCentralWidget(widget) def remove_widget(self, widget): pass def keyPressEvent(self, event): self.keys._onkeyevent(event.key(), True, event) def keyReleaseEvent(self, event): self.keys._onkeyevent(event.key(), False, event)
def _connect_events(obj, cls): for signal_name in GObject.signal_list_names(cls): signal_name = signal_name.replace('-', '_') event = Event(obj.__class__.__name__ + '.' + signal_name) setattr(obj, signal_name, event) def send_event(event, *args): event.send(args) obj.connect(signal_name, partial(send_event, event))
class Dialog(Container, QDialog): def __init__(self, layout=None, size=None, title=None): QDialog.__init__(self) Container.__init__(self, layout=layout) self.keys = Keys() if size: self.resize(*size) if title is not None: self.setWindowTitle(title) self.closed = Event("Dialog.closed") def onfinished(result): self._result = result self.remove() self.finished.connect(onfinished) def create(self, framefunc, *frameargs, size=None, title=None, **framekwargs): if size: self.resize(*size) if title is not None: self.setWindowTitle(title) super().create(framefunc, *frameargs, **framekwargs) self.show() def _ondispose(self): self.deleteLater() super()._ondispose() def closeEvent(self, event): self.closed.send(event) if event.isAccepted(): # If event wasn't canceled by user self.remove() def keyPressEvent(self, event): self.keys._onkeyevent(event.key(), True, event) def keyReleaseEvent(self, event): self.keys._onkeyevent(event.key(), False, event)
class Widget(Primitive): def __init__(self): super().__init__(Container) self.mouse_pressed = Event(f"{self.__class__.__name__}.mouse_pressed") self.mouse_released = Event(f"{self.__class__.__name__}.mouse_released") self.mouse_moved = Event(f"{self.__class__.__name__}.mouse_moved") self.double_clicked = Event(f"{self.__class__.__name__}.double_clicked") def _ondispose(self): self._owner.remove_widget(self) self.setParent(None) self.deleteLater() super()._ondispose() def _show(self, kwargs): self.resize(self.sizeHint()) self._owner.add_widget(self, **kwargs) self.show() def mousePressEvent(self, event): qtevent = QtEvent(self, event) self.mouse_pressed.send(qtevent) if not getattr(qtevent, "handled", False): super().mousePressEvent(event) def mouseReleaseEvent(self, event): qtevent = QtEvent(self, event) self.mouse_released.send(qtevent) if not getattr(qtevent, "handled", False): super().mouseReleaseEvent(event) def mouseMoveEvent(self, event): qtevent = QtEvent(self, event) self.mouse_moved.send(qtevent) if not getattr(qtevent, "handled", False): super().mouseMoveEvent(event) def mouseDoubleClickEvent(self, event): qtevent = QtEvent(self, event) self.double_clicked.send(qtevent) if not getattr(qtevent, "handled", False): super().mouseDoubleClickEvent(event)
def __init__(self, **kwargs): super().__init__() QtWidgets.QTableView.__init__(self, self._owner) _convert_all_signals_to_awaitables(self) self.current_changed = Event("TableView.current_changed") self._show(kwargs)
def __init__(self): super().__init__(Container) self.mouse_pressed = Event(f"{self.__class__.__name__}.mouse_pressed") self.mouse_released = Event(f"{self.__class__.__name__}.mouse_released") self.mouse_moved = Event(f"{self.__class__.__name__}.mouse_moved") self.double_clicked = Event(f"{self.__class__.__name__}.double_clicked")
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.event = Event('Thread.event')