From: Niki Roo Date: Thu, 27 Mar 2025 18:08:50 +0000 (+0100) Subject: qt utils: some fixes X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=1da88a25c5afc86972aaf21cc51c3e609db25ac1;p=gamiki.git qt utils: some fixes --- diff --git a/gamiki/qt/utils.py b/gamiki/qt/utils.py index 872168e..e39c26f 100644 --- a/gamiki/qt/utils.py +++ b/gamiki/qt/utils.py @@ -17,23 +17,31 @@ def start(startable): pool.start(worker) def setBg(widget: QWidget, color: str): - if (";" in color): + if (color) and (";" in color): raise RuntimeException("No colour can contain a `;'") - if (color): + if (color): # given color bg = ("background-color: " + color + ";") - elif hasattr(widget, "bgColor"): - bg = ("background-color: " + widget.bgColor + ";") - else: + elif (color == ""): # "" = no color bg = "" + elif hasattr(widget, "bgColor"): # None = previous color (if any) + bg = ("background-color: " + widget.bgColor + ";") + else: # None + no previous color: no change + bg = None changed = False attribs = widget.styleSheet().split(";") for i, attr in enumerate(attribs): if (attr.strip().startswith("background-color:")): - attribs[i] = bg if color else "" + if (not hasattr(widget, "bgColor")): + widget.bgColor = attribs[i] + if (bg != None): + attribs[i] = bg changed = True - if (not changed) and (color): + + if (not hasattr(widget, "bgColor")): + widget.bgColor = "" + if (not changed) and (bg): attribs.append(bg) widget.setStyleSheet(";".join(attribs) + ";") @@ -49,6 +57,7 @@ def addOverlay(self: QWidget, target: QWidget): overlay.setParent(target) overlay.move(0, 0) overlay.bgColor = "rgba(0, 0, 0, 0)" + setBg(overlay, None) self.overlay.append(overlay) @@ -62,7 +71,7 @@ def fixColor(widget: QWidget): elif (widget.selected): bg = "rgba(0, 0, 100, 60)" else: - bg = "" + bg = None for overlay in widget.overlay: setBg(overlay, bg) @@ -71,64 +80,40 @@ def makeMouseAware(self: QWidget, targets: list[QWidget]): for target in targets: addOverlay(self, target) - if (hasattr(self, "resizeEvent")): - self._resizeEvent = self.resizeEvent - def resize(event): - if (hasattr(self, "_resizeEvent")): - self._resizeEvent(event) - else: - self.super().resizeEvent(event) + resize.super(self, event) for widget in self.overlay: widget.setFixedSize(widget.parent().size()) + resize.super = self.__class__.resizeEvent self.resizeEvent = resize - if (hasattr(self, "enterEvent")): - self._enterEvent = self.enterEvent - if (hasattr(self, "leaveEvent")): - self._leaveEvent = self.leaveEvent - if (hasattr(self, "mousePressEvent")): - self._mousePressEvent = self.mousePressEvent - if (hasattr(self, "mouseReleaseEvent")): - self._mouseReleaseEvent = self.mouseReleaseEvent - def enter(event): self.hovered = True fixColor(self) - if (hasattr(self, "_enterEvent")): - self._enterEvent(event) - else: - self.super().enterEvent(event) + enter.super(self, event) + enter.super = self.__class__.enterEvent self.enterEvent = enter def leave(event): self.hovered = False fixColor(self) - if (hasattr(self, "_leaveEvent")): - self._leaveEvent(event) - else: - self.super().leaveEvent(event) + leave.super(self, event) + leave.super = self.__class__.leaveEvent self.leaveEvent = leave def mousePress(event): if event.button() == Qt.LeftButton: self.clicked = True fixColor(self) - if (hasattr(self, "_mousePressEvent")): - self._mousePressEvent(event) - else: - self.super().mousePressEvent(event) + mousePress.super(self, event) + mousePress.super = self.__class__.mousePressEvent self.mousePressEvent = mousePress def mouseRelease(event): if event.button() == Qt.LeftButton: self.clicked = False fixColor(self) - if (hasattr(self, "_mouseReleaseEvent")): - self._mouseReleaseEvent(event) - else: - self.super().mouseReleaseEvent(event) + mouseRelease.super(self, event) + mouseRelease.super = self.__class__.mouseReleaseEvent self.mouseReleaseEvent = mouseRelease -if __name__ == "__main__": - print("ok")