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.

299 lines
12KB

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