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.

scalablebutton.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Scalable Button, a custom Qt widget
  4. # Copyright (C) 2013-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 QPointF, QRectF
  20. from PyQt5.QtGui import QColor, QFont, QPainter, QPixmap
  21. from PyQt5.QtSvg import QSvgWidget
  22. from PyQt5.QtWidgets import QPushButton
  23. # ------------------------------------------------------------------------------------------------------------
  24. # Widget Class
  25. class ScalableButton(QPushButton):
  26. def __init__(self, parent):
  27. QPushButton.__init__(self, parent)
  28. self.fImageNormal = None
  29. self.fImageDown = None
  30. self.fImageHover = None
  31. self.fImageRect = QRectF()
  32. self.fIsHovered = False
  33. self.fTopText = ""
  34. self.fTopTextColor = QColor()
  35. self.fTopTextFont = QFont()
  36. self.setText("")
  37. def setPixmaps(self, normal, down, hover):
  38. self.fImageNormal = QPixmap()
  39. self.fImageDown = QPixmap()
  40. self.fImageHover = QPixmap()
  41. self._loadImages(normal, down, hover)
  42. def setSvgs(self, normal, down, hover):
  43. self.fImageNormal = QSvgWidget()
  44. self.fImageDown = QSvgWidget()
  45. self.fImageHover = QSvgWidget()
  46. self._loadImages(normal, down, hover)
  47. def _loadImages(self, normal, down, hover):
  48. self.fImageNormal.load(normal)
  49. self.fImageDown.load(down)
  50. self.fImageHover.load(hover)
  51. if isinstance(self.fImageNormal, QPixmap):
  52. width = self.fImageNormal.width()
  53. height = self.fImageNormal.height()
  54. else:
  55. width = self.fImageNormal.sizeHint().width()
  56. height = self.fImageNormal.sizeHint().height()
  57. self.fImageRect = QRectF(0, 0, width, height)
  58. self.setMinimumSize(width, height)
  59. self.setMaximumSize(width, height)
  60. def setTopText(self, text, color, font):
  61. self.fTopText = text
  62. self.fTopTextColor = color
  63. self.fTopTextFont = font
  64. def enterEvent(self, event):
  65. self.fIsHovered = True
  66. QPushButton.enterEvent(self, event)
  67. def leaveEvent(self, event):
  68. self.fIsHovered = False
  69. QPushButton.leaveEvent(self, event)
  70. def paintEvent(self, event):
  71. painter = QPainter(self)
  72. event.accept()
  73. if self.fImageNormal is None:
  74. return
  75. if isinstance(self.fImageNormal, QPixmap):
  76. self.paintEventPixmap(painter)
  77. else:
  78. self.paintEventSvg(painter)
  79. if not self.fTopText:
  80. return
  81. painter.save()
  82. painter.setPen(self.fTopTextColor)
  83. painter.setBrush(self.fTopTextColor)
  84. painter.setFont(self.fTopTextFont)
  85. painter.drawText(QPointF(10, 16), self.fTopText)
  86. painter.restore()
  87. def paintEventPixmap(self, painter):
  88. if not self.isEnabled():
  89. painter.save()
  90. painter.setOpacity(0.2)
  91. painter.drawPixmap(self.fImageRect, self.fImageNormal, self.fImageRect)
  92. painter.restore()
  93. elif self.isChecked() or self.isDown():
  94. painter.drawPixmap(self.fImageRect, self.fImageDown, self.fImageRect)
  95. elif self.fIsHovered:
  96. painter.drawPixmap(self.fImageRect, self.fImageHover, self.fImageRect)
  97. else:
  98. painter.drawPixmap(self.fImageRect, self.fImageNormal, self.fImageRect)
  99. def paintEventSvg(self, painter):
  100. if not self.isEnabled():
  101. painter.save()
  102. painter.setOpacity(0.2)
  103. self.fImageNormal.renderer().render(painter, self.fImageRect)
  104. painter.restore()
  105. elif self.isChecked() or self.isDown():
  106. self.fImageDown.renderer().render(painter, self.fImageRect)
  107. elif self.fIsHovered:
  108. self.fImageHover.renderer().render(painter, self.fImageRect)
  109. else:
  110. self.fImageNormal.renderer().render(painter, self.fImageRect)