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.

99 lines
3.1KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Svg Button, a custom Qt widget
  4. # Copyright (C) 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
  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.fSvgNormal = QSvgWidget()
  29. self.fSvgDown = QSvgWidget()
  30. self.fSvgHover = QSvgWidget()
  31. self.fIsHovered = False
  32. self.fTopText = ""
  33. self.fTopTextColor = QColor()
  34. self.fTopTextFont = QFont()
  35. self.setText("")
  36. def setSvgs(self, normal, down, hover):
  37. self.fSvgNormal.load(normal)
  38. self.fSvgDown.load(down)
  39. self.fSvgHover.load(hover)
  40. width = self.fSvgNormal.sizeHint().width()
  41. height = self.fSvgNormal.sizeHint().height()
  42. self.fSvgRect = QRectF(0, 0, width, height)
  43. self.setMinimumSize(width, height)
  44. self.setMaximumSize(width, height)
  45. def setTopText(self, text, color, font):
  46. self.fTopText = text
  47. self.fTopTextColor = color
  48. self.fTopTextFont = font
  49. def enterEvent(self, event):
  50. self.fIsHovered = True
  51. QPushButton.enterEvent(self, event)
  52. def leaveEvent(self, event):
  53. self.fIsHovered = False
  54. QPushButton.leaveEvent(self, event)
  55. def paintEvent(self, event):
  56. painter = QPainter(self)
  57. event.accept()
  58. if not self.isEnabled():
  59. painter.save()
  60. painter.setOpacity(0.2)
  61. self.fSvgNormal.renderer().render(painter, self.fSvgRect)
  62. painter.restore()
  63. elif self.isChecked() or self.isDown():
  64. self.fSvgDown.renderer().render(painter, self.fSvgRect)
  65. elif self.fIsHovered:
  66. self.fSvgHover.renderer().render(painter, self.fSvgRect)
  67. else:
  68. self.fSvgNormal.renderer().render(painter, self.fSvgRect)
  69. if not self.fTopText:
  70. return
  71. painter.save()
  72. painter.setPen(self.fTopTextColor)
  73. painter.setBrush(self.fTopTextColor)
  74. painter.setFont(self.fTopTextFont)
  75. painter.drawText(QPointF(10, 16), self.fTopText)
  76. painter.restore()