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.

290 lines
11KB

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