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.

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