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.

352 lines
12KB

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