def icon_from_resources(self, path: Union[str, ColoredIcon]) -> QIcon: "Fetch icon from Qt resources, and invert if in night mode." if self.night_mode: cache = self._icon_cache_light else: cache = self._icon_cache_dark if isinstance(path, str): key = path else: key = f"{path.path}-{path.color}" icon = cache.get(key) if icon: return icon if isinstance(path, str): # default black/white icon = QIcon(path) if self.night_mode: img = icon.pixmap(self._icon_size, self._icon_size).toImage() img.invertPixels() icon = QIcon(QPixmap(img)) else: # specified colours icon = QIcon(path.path) pixmap = icon.pixmap(16) painter = QPainter(pixmap) painter.setCompositionMode(QPainter.CompositionMode_SourceIn) painter.fillRect(pixmap.rect(), QColor(path.current_color(self.night_mode))) painter.end() icon = QIcon(pixmap) return icon return cache.setdefault(path, icon)
def paintEvent(self, event: QPaintEvent): qp = QPainter() qp.begin(self) self.scoreBoard(qp) self.placeFood(qp) self.drawSnake(qp) self.scoreText(event, qp) if self.isOver: self.gameOver(event, qp) qp.end()
def eventFilter(self, obj, evt): if not (evt.type() == QEvent.Paint and self.save_png): return super().eventFilter(obj, evt) filename, oldsize = self.save_png self.save_png = () size = self._page.contentsSize().toSize() image = QImage(size, QImage.Format_ARGB32) painter = QPainter(image) self.render(painter) painter.end() success = image.save(filename, "png") self.resize(oldsize) mw.progress.finish() if success: showInfo("Image saved to %s!" % os.path.abspath(filename)) else: showCritical("Failed to save the image.") return super().eventFilter(obj, evt)
def eventFilter(self, obj, evt): if not(evt.type() == QEvent.Paint and self.save_png): return super().eventFilter(obj, evt) filename, oldsize = self.save_png self.save_png = () size = self._page.contentsSize().toSize() image = QImage(size, QImage.Format_ARGB32) painter = QPainter(image) self.render(painter) painter.end() success = image.save(filename, "png") self.resize(oldsize) mw.progress.finish() if success: showInfo("Image saved to %s!" % os.path.abspath(filename)) else: showCritical("Failed to save the image.") return super().eventFilter(obj, evt)