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.

pixmapdial.py 13KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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(self.font())
  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 setWhiteText(self):
  114. self.fColor1 = QColor(0, 0, 0, 255)
  115. self.fColor2 = QColor(0, 0, 0, 0)
  116. self.fColorT = [Qt.white, Qt.darkGray]
  117. def minimumSizeHint(self):
  118. return QSize(self.fSize, self.fSize)
  119. def sizeHint(self):
  120. return QSize(self.fSize, self.fSize)
  121. def updateSizes(self):
  122. self.fWidth = self.fPixmap.width()
  123. self.fHeight = self.fPixmap.height()
  124. if self.fWidth < 1:
  125. self.fWidth = 1
  126. if self.fHeight < 1:
  127. self.fHeight = 1
  128. if self.fOrientation == self.HORIZONTAL:
  129. self.fSize = self.fHeight
  130. self.fCount = self.fWidth / self.fHeight
  131. else:
  132. self.fSize = self.fWidth
  133. self.fCount = self.fHeight / self.fWidth
  134. self.setMinimumSize(self.fSize, self.fSize + self.fLabelHeight + 5)
  135. self.setMaximumSize(self.fSize, self.fSize + self.fLabelHeight + 5)
  136. def enterEvent(self, event):
  137. self.fIsHovered = True
  138. if self.fHoverStep == self.HOVER_MIN:
  139. self.fHoverStep = self.HOVER_MIN + 1
  140. QDial.enterEvent(self, event)
  141. def leaveEvent(self, event):
  142. self.fIsHovered = False
  143. if self.fHoverStep == self.HOVER_MAX:
  144. self.fHoverStep = self.HOVER_MAX - 1
  145. QDial.leaveEvent(self, event)
  146. def paintEvent(self, event):
  147. painter = QPainter(self)
  148. event.accept()
  149. painter.save()
  150. painter.setRenderHint(QPainter.Antialiasing, True)
  151. if self.fLabel:
  152. if self.fCustomPaint == self.CUSTOM_PAINT_NULL:
  153. painter.setPen(self.fColor2)
  154. painter.setBrush(self.fLabelGradient)
  155. painter.drawRect(self.fLabelGradientRect)
  156. painter.setFont(self.fLabelFont)
  157. painter.setPen(self.fColorT[0 if self.isEnabled() else 1])
  158. painter.drawText(self.fLabelPos, self.fLabel)
  159. if self.isEnabled():
  160. current = float(self.value() - self.minimum())
  161. divider = float(self.maximum() - self.minimum())
  162. if divider == 0.0:
  163. return
  164. value = current / divider
  165. target = QRectF(0.0, 0.0, self.fSize, self.fSize)
  166. per = int((self.fCount - 1) * value)
  167. if self.fOrientation == self.HORIZONTAL:
  168. xpos = self.fSize * per
  169. ypos = 0.0
  170. else:
  171. xpos = 0.0
  172. ypos = self.fSize * per
  173. source = QRectF(xpos, ypos, self.fSize, self.fSize)
  174. painter.drawPixmap(target, self.fPixmap, source)
  175. # Custom knobs (Dry/Wet and Volume)
  176. if self.fCustomPaint in (self.CUSTOM_PAINT_CARLA_WET, self.CUSTOM_PAINT_CARLA_VOL):
  177. # knob color
  178. colorGreen = QColor(0x5D, 0xE7, 0x3D).lighter(100 + self.fHoverStep*6)
  179. colorBlue = QColor(0x3E, 0xB8, 0xBE).lighter(100 + self.fHoverStep*6)
  180. # draw small circle
  181. ballRect = QRectF(8.0, 8.0, 15.0, 15.0)
  182. ballPath = QPainterPath()
  183. ballPath.addEllipse(ballRect)
  184. #painter.drawRect(ballRect)
  185. tmpValue = (0.375 + 0.75*value)
  186. ballValue = tmpValue - floor(tmpValue)
  187. ballPoint = ballPath.pointAtPercent(ballValue)
  188. # draw arc
  189. startAngle = 216*16
  190. spanAngle = -252*16*value
  191. if self.fCustomPaint == self.CUSTOM_PAINT_CARLA_WET:
  192. painter.setBrush(colorBlue)
  193. painter.setPen(QPen(colorBlue, 0))
  194. painter.drawEllipse(QRectF(ballPoint.x(), ballPoint.y(), 2.2, 2.2))
  195. gradient = QConicalGradient(15.5, 15.5, -45)
  196. gradient.setColorAt(0.0, colorBlue)
  197. gradient.setColorAt(0.125, colorBlue)
  198. gradient.setColorAt(0.625, colorGreen)
  199. gradient.setColorAt(0.75, colorGreen)
  200. gradient.setColorAt(0.76, colorGreen)
  201. gradient.setColorAt(1.0, colorGreen)
  202. painter.setBrush(gradient)
  203. painter.setPen(QPen(gradient, 3))
  204. else:
  205. painter.setBrush(colorBlue)
  206. painter.setPen(QPen(colorBlue, 0))
  207. painter.drawEllipse(QRectF(ballPoint.x(), ballPoint.y(), 2.2, 2.2))
  208. painter.setBrush(colorBlue)
  209. painter.setPen(QPen(colorBlue, 3))
  210. painter.drawArc(4.0, 4.0, 26.0, 26.0, startAngle, spanAngle)
  211. # Custom knobs (L and R)
  212. elif self.fCustomPaint in (self.CUSTOM_PAINT_CARLA_L, self.CUSTOM_PAINT_CARLA_R):
  213. # knob color
  214. color = QColor(0xAD, 0xD5, 0x48).lighter(100 + self.fHoverStep*6)
  215. # draw small circle
  216. ballRect = QRectF(7.0, 8.0, 11.0, 12.0)
  217. ballPath = QPainterPath()
  218. ballPath.addEllipse(ballRect)
  219. #painter.drawRect(ballRect)
  220. tmpValue = (0.375 + 0.75*value)
  221. ballValue = tmpValue - floor(tmpValue)
  222. ballPoint = ballPath.pointAtPercent(ballValue)
  223. painter.setBrush(color)
  224. painter.setPen(QPen(color, 0))
  225. painter.drawEllipse(QRectF(ballPoint.x(), ballPoint.y(), 2.0, 2.0))
  226. # draw arc
  227. if self.fCustomPaint == self.CUSTOM_PAINT_CARLA_L:
  228. startAngle = 216*16
  229. spanAngle = -252.0*16*value
  230. elif self.fCustomPaint == self.CUSTOM_PAINT_CARLA_R:
  231. startAngle = 324.0*16
  232. spanAngle = 252.0*16*(1.0-value)
  233. else:
  234. return
  235. painter.setPen(QPen(color, 2))
  236. painter.drawArc(3.5, 4.5, 22.0, 22.0, startAngle, spanAngle)
  237. # Custom knobs (Color)
  238. elif self.fCustomPaint == self.CUSTOM_PAINT_COLOR:
  239. # knob color
  240. color = self.fCustomColor.lighter(100 + self.fHoverStep*6)
  241. # draw small circle
  242. ballRect = QRectF(8.0, 8.0, 15.0, 15.0)
  243. ballPath = QPainterPath()
  244. ballPath.addEllipse(ballRect)
  245. tmpValue = (0.375 + 0.75*value)
  246. ballValue = tmpValue - floor(tmpValue)
  247. ballPoint = ballPath.pointAtPercent(ballValue)
  248. # draw arc
  249. startAngle = 216*16
  250. spanAngle = -252*16*value
  251. painter.setBrush(color)
  252. painter.setPen(QPen(color, 0))
  253. painter.drawEllipse(QRectF(ballPoint.x(), ballPoint.y(), 2.2, 2.2))
  254. painter.setBrush(color)
  255. painter.setPen(QPen(color, 3))
  256. painter.drawArc(4.0, 4.0, 26.0, 26.0, startAngle, spanAngle)
  257. # Custom knobs (Zita)
  258. elif self.fCustomPaint == self.CUSTOM_PAINT_ZITA:
  259. a = value * pi * 1.5 - 2.35
  260. r = 10.0
  261. x = 10.5
  262. y = 10.5
  263. x += r * sin(a)
  264. y -= r * cos(a)
  265. painter.setBrush(Qt.black)
  266. painter.setPen(QPen(Qt.black, 2))
  267. painter.drawLine(QPointF(11.0, 11.0), QPointF(x, y))
  268. # Custom knobs
  269. else:
  270. painter.restore()
  271. return
  272. if self.HOVER_MIN < self.fHoverStep < self.HOVER_MAX:
  273. self.fHoverStep += 1 if self.fIsHovered else -1
  274. QTimer.singleShot(20, self.update)
  275. else: # isEnabled()
  276. target = QRectF(0.0, 0.0, self.fSize, self.fSize)
  277. painter.drawPixmap(target, self.fPixmap, target)
  278. painter.restore()
  279. def resizeEvent(self, event):
  280. self.updateSizes()
  281. QDial.resizeEvent(self, event)