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.

289 lines
10KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Pixmap Dial, a custom Qt4 widget
  4. # Copyright (C) 2011-2013 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 GPL.txt file
  17. # ------------------------------------------------------------------------------------------------------------
  18. # Imports (Global)
  19. from math import floor
  20. from PyQt4.QtCore import Qt, QPointF, QRectF, QTimer, QSize, SLOT
  21. from PyQt4.QtGui import QColor, QConicalGradient, QDial, QFont, QFontMetrics
  22. from PyQt4.QtGui import QLinearGradient, QPainter, QPainterPath, QPen, QPixmap
  23. # ------------------------------------------------------------------------------------------------------------
  24. # Widget Class
  25. class PixmapDial(QDial):
  26. # enum Orientation
  27. HORIZONTAL = 0
  28. VERTICAL = 1
  29. # enum CustomPaint
  30. CUSTOM_PAINT_NULL = 0
  31. CUSTOM_PAINT_CARLA_WET = 1
  32. CUSTOM_PAINT_CARLA_VOL = 2
  33. CUSTOM_PAINT_CARLA_L = 3
  34. CUSTOM_PAINT_CARLA_R = 4
  35. HOVER_MIN = 0
  36. HOVER_MAX = 9
  37. def __init__(self, parent):
  38. QDial.__init__(self, parent)
  39. self.fPixmap = QPixmap(":/bitmaps/dial_01d.png")
  40. self.fPixmapNum = "01"
  41. self.fCustomPaint = self.CUSTOM_PAINT_NULL
  42. self.fHovered = False
  43. self.fHoverStep = self.HOVER_MIN
  44. if self.fPixmap.width() > self.fPixmap.height():
  45. self.fOrientation = self.HORIZONTAL
  46. else:
  47. self.fOrientation = self.VERTICAL
  48. self.fLabel = ""
  49. self.fLabelPos = QPointF(0.0, 0.0)
  50. self.fLabelFont = QFont()
  51. self.fLabelFont.setPointSize(6)
  52. self.fLabelWidth = 0
  53. self.fLabelHeight = 0
  54. self.fLabelGradient = QLinearGradient(0, 0, 0, 1)
  55. if self.palette().window().color().lightness() > 100:
  56. # Light background
  57. c = self.palette().dark().color()
  58. self.fColor1 = c
  59. self.fColor2 = QColor(c.red(), c.green(), c.blue(), 0)
  60. self.fColorT = [self.palette().buttonText().color(), self.palette().mid().color()]
  61. else:
  62. # Dark background
  63. self.fColor1 = QColor(0, 0, 0, 255)
  64. self.fColor2 = QColor(0, 0, 0, 0)
  65. self.fColorT = [Qt.white, Qt.darkGray]
  66. self.updateSizes()
  67. def getSize(self):
  68. return self.fSize
  69. def setCustomPaint(self, paint):
  70. self.fCustomPaint = paint
  71. self.fLabelPos.setY(self.fSize + self.fLabelHeight/2)
  72. self.update()
  73. def setEnabled(self, enabled):
  74. if self.isEnabled() != enabled:
  75. self.fPixmap.load(":/bitmaps/dial_%s%s.png" % (self.fPixmapNum, "" if enabled else "d"))
  76. self.updateSizes()
  77. self.update()
  78. QDial.setEnabled(self, enabled)
  79. def setLabel(self, label):
  80. self.fLabel = label
  81. self.fLabelWidth = QFontMetrics(self.fLabelFont).width(label)
  82. self.fLabelHeight = QFontMetrics(self.fLabelFont).height()
  83. self.fLabelPos.setX(float(self.fSize)/2 - float(self.fLabelWidth)/2)
  84. self.fLabelPos.setY(self.fSize + self.fLabelHeight)
  85. self.fLabelGradient.setColorAt(0.0, self.fColor1)
  86. self.fLabelGradient.setColorAt(0.6, self.fColor1)
  87. self.fLabelGradient.setColorAt(1.0, self.fColor2)
  88. self.fLabelGradient.setStart(0, float(self.fSize)/2)
  89. self.fLabelGradient.setFinalStop(0, self.fSize + self.fLabelHeight + 5)
  90. self.fLabelGradientRect = QRectF(float(self.fSize)/8, float(self.fSize)/2, float(self.fSize)*6/8, self.fSize+self.fLabelHeight+5)
  91. self.update()
  92. def setPixmap(self, pixmapId):
  93. self.fPixmapNum = "%02i" % pixmapId
  94. self.fPixmap.load(":/bitmaps/dial_%s%s.png" % (self.fPixmapNum, "" if self.isEnabled() else "d"))
  95. if self.fPixmap.width() > self.fPixmap.height():
  96. self.fOrientation = self.HORIZONTAL
  97. else:
  98. self.fOrientation = self.VERTICAL
  99. self.updateSizes()
  100. self.update()
  101. def minimumSizeHint(self):
  102. return QSize(self.fSize, self.fSize)
  103. def sizeHint(self):
  104. return QSize(self.fSize, self.fSize)
  105. def updateSizes(self):
  106. self.fWidth = self.fPixmap.width()
  107. self.fHeight = self.fPixmap.height()
  108. if self.fWidth < 1:
  109. self.fWidth = 1
  110. if self.fHeight < 1:
  111. self.fHeight = 1
  112. if self.fOrientation == self.HORIZONTAL:
  113. self.fSize = self.fHeight
  114. self.fCount = self.fWidth / self.fHeight
  115. else:
  116. self.fSize = self.fWidth
  117. self.fCount = self.fHeight / self.fWidth
  118. self.setMinimumSize(self.fSize, self.fSize + self.fLabelHeight + 5)
  119. self.setMaximumSize(self.fSize, self.fSize + self.fLabelHeight + 5)
  120. def enterEvent(self, event):
  121. self.fHovered = True
  122. if self.fHoverStep == self.HOVER_MIN:
  123. self.fHoverStep += 1
  124. QDial.enterEvent(self, event)
  125. def leaveEvent(self, event):
  126. self.fHovered = False
  127. if self.fHoverStep == self.HOVER_MAX:
  128. self.fHoverStep -= 1
  129. QDial.leaveEvent(self, event)
  130. def paintEvent(self, event):
  131. painter = QPainter(self)
  132. painter.setRenderHint(QPainter.Antialiasing, True)
  133. event.accept()
  134. if self.fLabel:
  135. if self.fCustomPaint == self.CUSTOM_PAINT_NULL:
  136. painter.setPen(self.fColor2)
  137. painter.setBrush(self.fLabelGradient)
  138. painter.drawRect(self.fLabelGradientRect)
  139. painter.setFont(self.fLabelFont)
  140. painter.setPen(self.fColorT[0 if self.isEnabled() else 1])
  141. painter.drawText(self.fLabelPos, self.fLabel)
  142. if self.isEnabled():
  143. current = float(self.value() - self.minimum())
  144. divider = float(self.maximum() - self.minimum())
  145. if divider == 0.0:
  146. return
  147. value = current / divider
  148. target = QRectF(0.0, 0.0, self.fSize, self.fSize)
  149. per = int((self.fCount - 1) * value)
  150. if self.fOrientation == self.HORIZONTAL:
  151. xpos = self.fSize * per
  152. ypos = 0.0
  153. else:
  154. xpos = 0.0
  155. ypos = self.fSize * per
  156. source = QRectF(xpos, ypos, self.fSize, self.fSize)
  157. painter.drawPixmap(target, self.fPixmap, source)
  158. # Custom knobs (Dry/Wet and Volume)
  159. if self.fCustomPaint in (self.CUSTOM_PAINT_CARLA_WET, self.CUSTOM_PAINT_CARLA_VOL):
  160. # knob color
  161. colorGreen = QColor(0x5D, 0xE7, 0x3D, 191 + self.fHoverStep*7)
  162. colorBlue = QColor(0x3E, 0xB8, 0xBE, 191 + self.fHoverStep*7)
  163. # draw small circle
  164. ballRect = QRectF(8.0, 8.0, 15.0, 15.0)
  165. ballPath = QPainterPath()
  166. ballPath.addEllipse(ballRect)
  167. #painter.drawRect(ballRect)
  168. tmpValue = (0.375 + 0.75*value)
  169. ballValue = tmpValue - floor(tmpValue)
  170. ballPoint = ballPath.pointAtPercent(ballValue)
  171. # draw arc
  172. startAngle = 216*16
  173. spanAngle = -252*16*value
  174. if self.fCustomPaint == self.CUSTOM_PAINT_CARLA_WET:
  175. painter.setBrush(colorBlue)
  176. painter.setPen(QPen(colorBlue, 0))
  177. painter.drawEllipse(QRectF(ballPoint.x(), ballPoint.y(), 2.2, 2.2))
  178. gradient = QConicalGradient(15.5, 15.5, -45)
  179. gradient.setColorAt(0.0, colorBlue)
  180. gradient.setColorAt(0.125, colorBlue)
  181. gradient.setColorAt(0.625, colorGreen)
  182. gradient.setColorAt(0.75, colorGreen)
  183. gradient.setColorAt(0.76, colorGreen)
  184. gradient.setColorAt(1.0, colorGreen)
  185. painter.setBrush(gradient)
  186. painter.setPen(QPen(gradient, 3))
  187. else:
  188. painter.setBrush(colorBlue)
  189. painter.setPen(QPen(colorBlue, 0))
  190. painter.drawEllipse(QRectF(ballPoint.x(), ballPoint.y(), 2.2, 2.2))
  191. painter.setBrush(colorBlue)
  192. painter.setPen(QPen(colorBlue, 3))
  193. painter.drawArc(4.0, 4.0, 26.0, 26.0, startAngle, spanAngle)
  194. # Custom knobs (L and R)
  195. elif self.fCustomPaint in (self.CUSTOM_PAINT_CARLA_L, self.CUSTOM_PAINT_CARLA_R):
  196. # knob color
  197. color = QColor(0xAD + self.fHoverStep*5, 0xD5 + self.fHoverStep*4, 0x4B + self.fHoverStep*5)
  198. # draw small circle
  199. ballRect = QRectF(7.0, 8.0, 11.0, 12.0)
  200. ballPath = QPainterPath()
  201. ballPath.addEllipse(ballRect)
  202. #painter.drawRect(ballRect)
  203. tmpValue = (0.375 + 0.75*value)
  204. ballValue = tmpValue - floor(tmpValue)
  205. ballPoint = ballPath.pointAtPercent(ballValue)
  206. painter.setBrush(color)
  207. painter.setPen(QPen(color, 0))
  208. painter.drawEllipse(QRectF(ballPoint.x(), ballPoint.y(), 2.0, 2.0))
  209. # draw arc
  210. if self.fCustomPaint == self.CUSTOM_PAINT_CARLA_L:
  211. startAngle = 216*16
  212. spanAngle = -252.0*16*value
  213. elif self.fCustomPaint == self.CUSTOM_PAINT_CARLA_R:
  214. startAngle = 324.0*16
  215. spanAngle = 252.0*16*(1.0-value)
  216. else:
  217. return
  218. painter.setPen(QPen(color, 2))
  219. painter.drawArc(3.5, 4.5, 22.0, 22.0, startAngle, spanAngle)
  220. if self.HOVER_MIN < self.fHoverStep < self.HOVER_MAX:
  221. self.fHoverStep += 1 if self.fHovered else -1
  222. QTimer.singleShot(20, self, SLOT("update()"))
  223. else: # isEnabled()
  224. target = QRectF(0.0, 0.0, self.fSize, self.fSize)
  225. painter.drawPixmap(target, self.fPixmap, target)
  226. def resizeEvent(self, event):
  227. self.updateSizes()
  228. QDial.resizeEvent(self, event)