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.

100 lines
3.2KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Pixmap Button, a custom Qt widget
  4. # Copyright (C) 2013-2019 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.QtWidgets import QPushButton
  22. # ------------------------------------------------------------------------------------------------------------
  23. # Widget Class
  24. class PixmapButton(QPushButton):
  25. def __init__(self, parent):
  26. QPushButton.__init__(self, parent)
  27. self.fPixmapNormal = QPixmap()
  28. self.fPixmapDown = QPixmap()
  29. self.fPixmapHover = QPixmap()
  30. self.fPixmapRect = QRectF(0, 0, 0, 0)
  31. self.fIsHovered = False
  32. self.fTopText = ""
  33. self.fTopTextColor = QColor()
  34. self.fTopTextFont = QFont()
  35. self.setText("")
  36. def setPixmaps(self, normal, down, hover):
  37. self.fPixmapNormal.load(normal)
  38. self.fPixmapDown.load(down)
  39. self.fPixmapHover.load(hover)
  40. width = self.fPixmapNormal.width()
  41. height = self.fPixmapNormal.height()
  42. self.fPixmapRect = 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. painter.drawPixmap(self.fPixmapRect, self.fPixmapNormal, self.fPixmapRect)
  62. painter.restore()
  63. elif self.isChecked() or self.isDown():
  64. painter.drawPixmap(self.fPixmapRect, self.fPixmapDown, self.fPixmapRect)
  65. elif self.fIsHovered:
  66. painter.drawPixmap(self.fPixmapRect, self.fPixmapHover, self.fPixmapRect)
  67. else:
  68. painter.drawPixmap(self.fPixmapRect, self.fPixmapNormal, self.fPixmapRect)
  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()