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.

276 lines
11KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Pixmap Dial, a custom Qt widget
  4. # Copyright (C) 2011-2022 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 math import cos, floor, pi, sin
  20. from PyQt5.QtCore import pyqtSlot, Qt, QEvent, QPointF, QRectF, QTimer, QSize
  21. from PyQt5.QtGui import QColor, QConicalGradient, QFontMetrics, QPainterPath, QPen, QPixmap
  22. from PyQt5.QtWidgets import QDial
  23. from .commondial import CommonDial
  24. # ---------------------------------------------------------------------------------------------------------------------
  25. # Widget Class
  26. class PixmapDial(CommonDial):
  27. def __init__(self, parent, index=0):
  28. CommonDial.__init__(self, parent, index)
  29. self.fPixmap = QPixmap(":/bitmaps/dial_01d.png")
  30. self.fPixmapNum = "01"
  31. if self.fPixmap.width() > self.fPixmap.height():
  32. self.fPixmapOrientation = self.HORIZONTAL
  33. else:
  34. self.fPixmapOrientation = self.VERTICAL
  35. self.updateSizes()
  36. def getBaseSize(self):
  37. return self.fPixmapBaseSize
  38. def updateSizes(self):
  39. self.fPixmapWidth = self.fPixmap.width()
  40. self.fPixmapHeight = self.fPixmap.height()
  41. if self.fPixmapWidth < 1:
  42. self.fPixmapWidth = 1
  43. if self.fPixmapHeight < 1:
  44. self.fPixmapHeight = 1
  45. if self.fPixmapOrientation == self.HORIZONTAL:
  46. self.fPixmapBaseSize = self.fPixmapHeight
  47. self.fPixmapLayersCount = self.fPixmapWidth / self.fPixmapHeight
  48. else:
  49. self.fPixmapBaseSize = self.fPixmapWidth
  50. self.fPixmapLayersCount = self.fPixmapHeight / self.fPixmapWidth
  51. self.setMinimumSize(self.fPixmapBaseSize, self.fPixmapBaseSize + self.fLabelHeight + 5)
  52. self.setMaximumSize(self.fPixmapBaseSize, self.fPixmapBaseSize + self.fLabelHeight + 5)
  53. if not self.fLabel:
  54. self.fLabelHeight = 0
  55. self.fLabelWidth = 0
  56. return
  57. self.fLabelWidth = QFontMetrics(self.fLabelFont).width(self.fLabel)
  58. self.fLabelHeight = QFontMetrics(self.fLabelFont).height()
  59. self.fLabelPos.setX(float(self.fPixmapBaseSize)/2.0 - float(self.fLabelWidth)/2.0)
  60. if self.fPixmapNum in ("01", "02", "07", "08", "09", "10"):
  61. self.fLabelPos.setY(self.fPixmapBaseSize + self.fLabelHeight)
  62. elif self.fPixmapNum in ("11",):
  63. self.fLabelPos.setY(self.fPixmapBaseSize + self.fLabelHeight*2/3)
  64. else:
  65. self.fLabelPos.setY(self.fPixmapBaseSize + self.fLabelHeight/2)
  66. self.fLabelGradient.setStart(0, float(self.fPixmapBaseSize)/2.0)
  67. self.fLabelGradient.setFinalStop(0, self.fPixmapBaseSize + self.fLabelHeight + 5)
  68. self.fLabelGradientRect = QRectF(float(self.fPixmapBaseSize)/8.0, float(self.fPixmapBaseSize)/2.0, float(self.fPixmapBaseSize*3)/4.0, self.fPixmapBaseSize+self.fLabelHeight+5)
  69. def setPixmap(self, pixmapId):
  70. self.fPixmapNum = "%02i" % pixmapId
  71. self.fPixmap.load(":/bitmaps/dial_%s%s.png" % (self.fPixmapNum, "" if self.isEnabled() else "d"))
  72. if self.fPixmap.width() > self.fPixmap.height():
  73. self.fPixmapOrientation = self.HORIZONTAL
  74. else:
  75. self.fPixmapOrientation = self.VERTICAL
  76. # special pixmaps
  77. if self.fCustomPaintMode == self.CUSTOM_PAINT_MODE_NULL:
  78. # reserved for carla-wet, carla-vol, carla-pan and color
  79. if self.fPixmapNum == "03":
  80. self.fCustomPaintMode = self.CUSTOM_PAINT_MODE_COLOR
  81. # reserved for carla-L and carla-R
  82. elif self.fPixmapNum == "04":
  83. self.fCustomPaintMode = self.CUSTOM_PAINT_MODE_CARLA_L
  84. # reserved for zita
  85. elif self.fPixmapNum == "06":
  86. self.fCustomPaintMode = self.CUSTOM_PAINT_MODE_ZITA
  87. self.updateSizes()
  88. self.update()
  89. @pyqtSlot()
  90. def slot_updatePixmap(self):
  91. self.setPixmap(int(self.fPixmapNum))
  92. def minimumSizeHint(self):
  93. return QSize(self.fPixmapBaseSize, self.fPixmapBaseSize)
  94. def sizeHint(self):
  95. return QSize(self.fPixmapBaseSize, self.fPixmapBaseSize)
  96. def changeEvent(self, event):
  97. CommonDial.changeEvent(self, event)
  98. # Force pixmap update if enabled state changes
  99. if event.type() == QEvent.EnabledChange:
  100. self.setPixmap(int(self.fPixmapNum))
  101. def paintDial(self, painter):
  102. if self.isEnabled():
  103. normValue = float(self.fRealValue - self.fMinimum) / float(self.fMaximum - self.fMinimum)
  104. target = QRectF(0.0, 0.0, self.fPixmapBaseSize, self.fPixmapBaseSize)
  105. curLayer = int((self.fPixmapLayersCount - 1) * normValue)
  106. if self.fPixmapOrientation == self.HORIZONTAL:
  107. xpos = self.fPixmapBaseSize * curLayer
  108. ypos = 0.0
  109. else:
  110. xpos = 0.0
  111. ypos = self.fPixmapBaseSize * curLayer
  112. source = QRectF(xpos, ypos, self.fPixmapBaseSize, self.fPixmapBaseSize)
  113. painter.drawPixmap(target, self.fPixmap, source)
  114. # Custom knobs (Dry/Wet and Volume)
  115. if self.fCustomPaintMode in (self.CUSTOM_PAINT_MODE_CARLA_WET, self.CUSTOM_PAINT_MODE_CARLA_VOL):
  116. # knob color
  117. colorGreen = QColor(0x5D, 0xE7, 0x3D).lighter(100 + self.fHoverStep*6)
  118. colorBlue = QColor(0x3E, 0xB8, 0xBE).lighter(100 + self.fHoverStep*6)
  119. # draw small circle
  120. ballRect = QRectF(8.0, 8.0, 15.0, 15.0)
  121. ballPath = QPainterPath()
  122. ballPath.addEllipse(ballRect)
  123. #painter.drawRect(ballRect)
  124. tmpValue = (0.375 + 0.75*normValue)
  125. ballValue = tmpValue - floor(tmpValue)
  126. ballPoint = ballPath.pointAtPercent(ballValue)
  127. # draw arc
  128. startAngle = 216*16
  129. spanAngle = -252*16*normValue
  130. if self.fCustomPaintMode == self.CUSTOM_PAINT_MODE_CARLA_WET:
  131. painter.setBrush(colorBlue)
  132. painter.setPen(QPen(colorBlue, 0))
  133. painter.drawEllipse(QRectF(ballPoint.x(), ballPoint.y(), 2.2, 2.2))
  134. gradient = QConicalGradient(15.5, 15.5, -45)
  135. gradient.setColorAt(0.0, colorBlue)
  136. gradient.setColorAt(0.125, colorBlue)
  137. gradient.setColorAt(0.625, colorGreen)
  138. gradient.setColorAt(0.75, colorGreen)
  139. gradient.setColorAt(0.76, colorGreen)
  140. gradient.setColorAt(1.0, colorGreen)
  141. painter.setBrush(gradient)
  142. painter.setPen(QPen(gradient, 3))
  143. else:
  144. painter.setBrush(colorBlue)
  145. painter.setPen(QPen(colorBlue, 0))
  146. painter.drawEllipse(QRectF(ballPoint.x(), ballPoint.y(), 2.2, 2.2))
  147. painter.setBrush(colorBlue)
  148. painter.setPen(QPen(colorBlue, 3))
  149. painter.drawArc(4.0, 4.0, 26.0, 26.0, startAngle, spanAngle)
  150. # Custom knobs (L and R)
  151. elif self.fCustomPaintMode in (self.CUSTOM_PAINT_MODE_CARLA_L, self.CUSTOM_PAINT_MODE_CARLA_R):
  152. # knob color
  153. color = QColor(0xAD, 0xD5, 0x48).lighter(100 + self.fHoverStep*6)
  154. # draw small circle
  155. ballRect = QRectF(7.0, 8.0, 11.0, 12.0)
  156. ballPath = QPainterPath()
  157. ballPath.addEllipse(ballRect)
  158. #painter.drawRect(ballRect)
  159. tmpValue = (0.375 + 0.75*normValue)
  160. ballValue = tmpValue - floor(tmpValue)
  161. ballPoint = ballPath.pointAtPercent(ballValue)
  162. painter.setBrush(color)
  163. painter.setPen(QPen(color, 0))
  164. painter.drawEllipse(QRectF(ballPoint.x(), ballPoint.y(), 2.0, 2.0))
  165. # draw arc
  166. if self.fCustomPaintMode == self.CUSTOM_PAINT_MODE_CARLA_L:
  167. startAngle = 216*16
  168. spanAngle = -252.0*16*normValue
  169. else:
  170. startAngle = 324.0*16
  171. spanAngle = 252.0*16*(1.0-normValue)
  172. painter.setPen(QPen(color, 2))
  173. painter.drawArc(3.5, 4.5, 22.0, 22.0, startAngle, spanAngle)
  174. # Custom knobs (Color)
  175. elif self.fCustomPaintMode == self.CUSTOM_PAINT_MODE_COLOR:
  176. # knob color
  177. color = self.fCustomPaintColor.lighter(100 + self.fHoverStep*6)
  178. # draw small circle
  179. ballRect = QRectF(8.0, 8.0, 15.0, 15.0)
  180. ballPath = QPainterPath()
  181. ballPath.addEllipse(ballRect)
  182. tmpValue = (0.375 + 0.75*normValue)
  183. ballValue = tmpValue - floor(tmpValue)
  184. ballPoint = ballPath.pointAtPercent(ballValue)
  185. # draw arc
  186. startAngle = 216*16
  187. spanAngle = -252*16*normValue
  188. painter.setBrush(color)
  189. painter.setPen(QPen(color, 0))
  190. painter.drawEllipse(QRectF(ballPoint.x(), ballPoint.y(), 2.2, 2.2))
  191. painter.setBrush(color)
  192. painter.setPen(QPen(color, 3))
  193. painter.drawArc(4.0, 4.0, 26.0, 26.0, startAngle, spanAngle)
  194. # Custom knobs (Zita)
  195. elif self.fCustomPaintMode == self.CUSTOM_PAINT_MODE_ZITA:
  196. a = normValue * pi * 1.5 - 2.35
  197. r = 10.0
  198. x = 10.5
  199. y = 10.5
  200. x += r * sin(a)
  201. y -= r * cos(a)
  202. painter.setBrush(Qt.black)
  203. painter.setPen(QPen(Qt.black, 2))
  204. painter.drawLine(QPointF(11.0, 11.0), QPointF(x, y))
  205. # Custom knobs
  206. else:
  207. painter.restore()
  208. return
  209. if self.HOVER_MIN < self.fHoverStep < self.HOVER_MAX:
  210. self.fHoverStep += 1 if self.fIsHovered else -1
  211. QTimer.singleShot(20, self.update)
  212. else: # isEnabled()
  213. target = QRectF(0.0, 0.0, self.fPixmapBaseSize, self.fPixmapBaseSize)
  214. painter.drawPixmap(target, self.fPixmap, target)
  215. # ---------------------------------------------------------------------------------------------------------------------