From c093190584f2fea98701da5a93e24a0155747d49 Mon Sep 17 00:00:00 2001 From: falkTX Date: Thu, 4 Jun 2020 14:45:21 +0100 Subject: [PATCH] Make LEDButton compatible with old pixmaps Signed-off-by: falkTX --- source/frontend/widgets/ledbutton.py | 94 +++++++++++++++++++--------- 1 file changed, 65 insertions(+), 29 deletions(-) diff --git a/source/frontend/widgets/ledbutton.py b/source/frontend/widgets/ledbutton.py index 66d3f8bab..90e14fcb1 100644 --- a/source/frontend/widgets/ledbutton.py +++ b/source/frontend/widgets/ledbutton.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # LED Button, a custom Qt widget -# Copyright (C) 2011-2019 Filipe Coelho +# Copyright (C) 2011-2020 Filipe Coelho # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -19,14 +19,17 @@ # ------------------------------------------------------------------------------------------------------------ # Imports (Global) +from PyQt5.QtCore import QRectF +from PyQt5.QtGui import QPainter, QPixmap from PyQt5.QtSvg import QSvgWidget from PyQt5.QtWidgets import QPushButton # ------------------------------------------------------------------------------------------------------------ # Widget Class - class LEDButton(QPushButton): + # unset + UNSET = -1 # normal OFF = 0 BLUE = 1 @@ -39,50 +42,83 @@ class LEDButton(QPushButton): def __init__(self, parent): QPushButton.__init__(self, parent) - self.fSvgWidget = QSvgWidget(self) + self.fLastColor = self.UNSET + self.fColor = self.UNSET + + self.fImage = QSvgWidget() + self.fImage.load(":/scalable/led_off.svg") + self.fRect = QRectF(self.fImage.rect()) self.setCheckable(True) self.setText("") - self.fLastColor = self.OFF - self.fSvgWidget.load(":/scalable/led_off.svg") - self.setColor(self.BLUE) def setColor(self, color): self.fColor = color - self._loadSVGNow() - # Fix calf off if color == self.CALF: - self.fSvgWidget.load(":/bitmaps/led_calf_off.png") + self.fLastColor = self.UNSET + + if self._loadImageNowIfNeeded(): + if isinstance(self.fImage, QPixmap): + size = self.fImage.width() + else: + size = self.fImage.sizeHint().width() + + self.fRect = QRectF(self.fImage.rect()) + self.setFixedSize(self.fImage.size()) def paintEvent(self, event): + painter = QPainter(self) event.accept() - self._loadSVGNow() + self._loadImageNowIfNeeded() - def _loadSVGNow(self): + if isinstance(self.fImage, QPixmap): + size = self.fImage.width() + rect = QRectF(0, 0, size, size) + painter.drawPixmap(rect, self.fImage, rect) + else: + size = self.fImage.sizeHint().width() + rect = QRectF(0, 0, size, size) + self.fImage.renderer().render(painter, rect) + def _loadImageNowIfNeeded(self): if self.isChecked(): - if self.fLastColor != self.fColor: - if self.fColor == self.OFF: - self.fSvgWidget.load(":/scalable/led_off.svg") - elif self.fColor == self.BLUE: - self.fSvgWidget.load(":/scalable/led_blue.svg") - elif self.fColor == self.GREEN: - self.fSvgWidget.load(":/scalable/led_green.svg") - elif self.fColor == self.RED: - self.fSvgWidget.load(":/scalable/led_red.svg") - elif self.fColor == self.YELLOW: - self.fSvgWidget.load(":/scalable/led_yellow.svg") - elif self.fColor == self.CALF: - self.fSvgWidget.load(":/bitmaps/led_calf_on.png") - else: - return - - self.fLastColor = self.fColor + if self.fLastColor == self.fColor: + return + if self.fColor == self.OFF: + img = ":/scalable/led_off.svg" + elif self.fColor == self.BLUE: + img = ":/scalable/led_blue.svg" + elif self.fColor == self.GREEN: + img = ":/scalable/led_green.svg" + elif self.fColor == self.RED: + img = ":/scalable/led_red.svg" + elif self.fColor == self.YELLOW: + img = ":/scalable/led_yellow.svg" + elif self.fColor == self.CALF: + img = ":/bitmaps/led_calf_on.png" + else: + return False + self.fLastColor = self.fColor elif self.fLastColor != self.OFF: - self.fSvgWidget.load(":/scalable/led_calf_off.png" if self.fColor == self.CALF else ":/scalable/led_off.svg") + img = ":/bitmaps/led_calf_off.png" if self.fColor == self.CALF else ":/scalable/led_off.svg" self.fLastColor = self.OFF + + else: + return False + + if img.endswith(".png"): + if not isinstance(self.fImage, QPixmap): + self.fImage = QPixmap() + else: + if not isinstance(self.fImage, QSvgWidget): + self.fImage = QSvgWidget() + + self.fImage.load(img) + self.update() + + return True