Browse Source

Make LEDButton compatible with old pixmaps

Signed-off-by: falkTX <falktx@falktx.com>
tags/v2.2.0-RC1
falkTX 4 years ago
parent
commit
c093190584
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
1 changed files with 65 additions and 29 deletions
  1. +65
    -29
      source/frontend/widgets/ledbutton.py

+ 65
- 29
source/frontend/widgets/ledbutton.py View File

@@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-

# LED Button, a custom Qt widget
# Copyright (C) 2011-2019 Filipe Coelho <falktx@falktx.com>
# Copyright (C) 2011-2020 Filipe Coelho <falktx@falktx.com>
#
# 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

Loading…
Cancel
Save