Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.8KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # LED Button, a custom Qt widget
  4. # Copyright (C) 2011-2020 Filipe Coelho <falktx@falktx.com>
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License as
  8. # published by the Free Software Foundation; either version 2 of
  9. # the License, or any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # For a full copy of the GNU General Public License see the doc/GPL.txt file.
  17. # ------------------------------------------------------------------------------------------------------------
  18. # Imports (Global)
  19. from PyQt5.QtCore import QRectF
  20. from PyQt5.QtGui import QPainter, QPixmap
  21. from PyQt5.QtSvg import QSvgWidget
  22. from PyQt5.QtWidgets import QPushButton
  23. # ------------------------------------------------------------------------------------------------------------
  24. # Widget Class
  25. class LEDButton(QPushButton):
  26. # unset
  27. UNSET = -1
  28. # normal
  29. OFF = 0
  30. BLUE = 1
  31. GREEN = 2
  32. RED = 3
  33. YELLOW = 4
  34. # extra
  35. CALF = 5
  36. def __init__(self, parent):
  37. QPushButton.__init__(self, parent)
  38. self.fLastColor = self.UNSET
  39. self.fColor = self.UNSET
  40. self.fImage = QSvgWidget()
  41. self.fImage.load(":/scalable/led_off.svg")
  42. self.fRect = QRectF(self.fImage.rect())
  43. self.setCheckable(True)
  44. self.setText("")
  45. self.setColor(self.BLUE)
  46. def setColor(self, color):
  47. self.fColor = color
  48. if color == self.CALF:
  49. self.fLastColor = self.UNSET
  50. if self._loadImageNowIfNeeded():
  51. if isinstance(self.fImage, QPixmap):
  52. size = self.fImage.width()
  53. else:
  54. size = self.fImage.sizeHint().width()
  55. self.fRect = QRectF(self.fImage.rect())
  56. self.setFixedSize(self.fImage.size())
  57. def paintEvent(self, event):
  58. painter = QPainter(self)
  59. event.accept()
  60. self._loadImageNowIfNeeded()
  61. if isinstance(self.fImage, QPixmap):
  62. size = self.fImage.width()
  63. rect = QRectF(0, 0, size, size)
  64. painter.drawPixmap(rect, self.fImage, rect)
  65. else:
  66. size = self.fImage.sizeHint().width()
  67. rect = QRectF(0, 0, size, size)
  68. self.fImage.renderer().render(painter, rect)
  69. def _loadImageNowIfNeeded(self):
  70. if self.isChecked():
  71. if self.fLastColor == self.fColor:
  72. return
  73. if self.fColor == self.OFF:
  74. img = ":/scalable/led_off.svg"
  75. elif self.fColor == self.BLUE:
  76. img = ":/scalable/led_blue.svg"
  77. elif self.fColor == self.GREEN:
  78. img = ":/scalable/led_green.svg"
  79. elif self.fColor == self.RED:
  80. img = ":/scalable/led_red.svg"
  81. elif self.fColor == self.YELLOW:
  82. img = ":/scalable/led_yellow.svg"
  83. elif self.fColor == self.CALF:
  84. img = ":/bitmaps/led_calf_on.png"
  85. else:
  86. return False
  87. self.fLastColor = self.fColor
  88. elif self.fLastColor != self.OFF:
  89. img = ":/bitmaps/led_calf_off.png" if self.fColor == self.CALF else ":/scalable/led_off.svg"
  90. self.fLastColor = self.OFF
  91. else:
  92. return False
  93. if img.endswith(".png"):
  94. if not isinstance(self.fImage, QPixmap):
  95. self.fImage = QPixmap()
  96. else:
  97. if not isinstance(self.fImage, QSvgWidget):
  98. self.fImage = QSvgWidget()
  99. self.fImage.load(img)
  100. self.update()
  101. return True