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.

342 lines
13KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Custom Mini Canvas Preview, a custom Qt widget
  4. # Copyright (C) 2011-2019 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 floor, ceil
  20. from PyQt5.QtCore import pyqtSignal, Qt, QRectF, QTimer, QEvent, QPoint
  21. from PyQt5.QtGui import QBrush, QColor, QCursor, QPainter, QPainterPath, QPen, QCursor, QPixmap
  22. from PyQt5.QtWidgets import QFrame, QWidget
  23. # ------------------------------------------------------------------------------------------------------------
  24. # Antialiasing settings
  25. from patchcanvas import options, ANTIALIASING_FULL
  26. # ------------------------------------------------------------------------------------------------------------
  27. # Static Variables
  28. iX = 0
  29. iY = 1
  30. iWidth = 2
  31. iHeight = 3
  32. MOUSE_MODE_NONE = 0
  33. MOUSE_MODE_MOVE = 1
  34. MOUSE_MODE_SCALE = 2
  35. # ------------------------------------------------------------------------------------------------------------
  36. # Widget Class
  37. class CanvasPreviewFrame(QFrame):
  38. miniCanvasMoved = pyqtSignal(float, float)
  39. # x = 2
  40. # y = 2
  41. # w = width-4
  42. # h = height-3
  43. # bounds -1 px
  44. kInternalWidth = 210-6 # -4 for width + -1px*2 bounds
  45. kInternalHeight = 162-5 # -3 for height + -1px*2 bounds
  46. kInternalRatio = kInternalWidth / kInternalHeight
  47. def __init__(self, parent):
  48. QFrame.__init__(self, parent)
  49. #self.setMinimumWidth(210)
  50. #self.setMinimumHeight(162)
  51. #self.setMaximumHeight(162)
  52. self.fRealParent = None
  53. self.fScene = None
  54. self.fRenderSource = QRectF(0.0, 0.0, 0.0, 0.0)
  55. self.fRenderTarget = QRectF(0.0, 0.0, 0.0, 0.0)
  56. self.fUseCustomPaint = False
  57. self.fFrameWidth = 0.0
  58. self.fInitialX = 0.0
  59. self.fInitialY = 0.0
  60. self.fScale = 1.0
  61. self.fViewBg = QColor(0, 0, 0)
  62. self.fViewBrush = QBrush(QColor(75, 75, 255, 30))
  63. self.fViewPen = QPen(Qt.blue, 1)
  64. self.fViewRect = [3.0, 3.0, 10.0, 10.0]
  65. self.fMouseMode = MOUSE_MODE_NONE
  66. self.fMouseLeftDown = False
  67. self.fMouseRightDown = False
  68. self.fMousePos = None
  69. def init(self, scene, realWidth, realHeight, useCustomPaint = False):
  70. realWidth,realHeight = float(realWidth),float(realHeight)
  71. self.fScene = scene
  72. self.fRenderSource = QRectF(0.0, 0.0, realWidth, realHeight)
  73. self.kInternalRatio = realWidth / realHeight
  74. self.updateStyle()
  75. if self.fUseCustomPaint != useCustomPaint:
  76. self.fUseCustomPaint = useCustomPaint
  77. self.repaint()
  78. def updateStyle(self):
  79. self.fFrameWidth = 1 if self.fUseCustomPaint else self.frameWidth()
  80. def changeEvent(self, event):
  81. if event.type() in (QEvent.StyleChange, QEvent.PaletteChange):
  82. self.updateStyle()
  83. QWidget.changeEvent(self, event)
  84. def setRealParent(self, parent):
  85. self.fRealParent = parent
  86. def setViewPosX(self, xp):
  87. self.fViewRect[iX] = xp * (self.kInternalWidth - self.fViewRect[iWidth]/self.fScale)
  88. self.update()
  89. def setViewPosY(self, yp):
  90. self.fViewRect[iY] = yp * (self.kInternalHeight - self.fViewRect[iHeight]/self.fScale)
  91. self.update()
  92. def setViewScale(self, scale):
  93. self.fScale = scale
  94. if self.fRealParent is not None:
  95. QTimer.singleShot(0, self.fRealParent.slot_miniCanvasCheckAll)
  96. def setViewSize(self, width, height):
  97. self.fViewRect[iWidth] = width * self.kInternalWidth
  98. self.fViewRect[iHeight] = height * self.kInternalHeight
  99. self.update()
  100. def setViewTheme(self, bgColor, brushColor, penColor):
  101. bg_black = bgColor.blackF()
  102. brush_black = brushColor.blackF()
  103. r0,g0,b0,a = bgColor.getRgb()
  104. r1,g1,b1,a = brushColor.getRgb()
  105. if brush_black < bg_black:
  106. self.fRubberBandBlending = 1
  107. brushColor = QColor(r1-r0, g1-g0, b1-b0, 40)
  108. elif bg_black < brush_black:
  109. self.fRubberBandBlending = -1
  110. brushColor = QColor(r0-r1, g0-g1, b0-b1, 40)
  111. else:
  112. bgColor.setAlpha(40)
  113. self.fRubberBandBlending = 0
  114. penColor.setAlpha(100)
  115. self.fViewBg = bgColor
  116. self.fViewBrush = QBrush(brushColor)
  117. self.fViewPen = QPen(penColor, 1)
  118. cur_color = "black" if bg_black < 0.5 else "white"
  119. self.fScaleCursors = ( QCursor(QPixmap(":/cursors/zoom-generic-"+cur_color+".png"), 8, 7),
  120. QCursor(QPixmap(":/cursors/zoom-in-"+cur_color+".png"), 8, 7),
  121. QCursor(QPixmap(":/cursors/zoom-out-"+cur_color+".png"), 8, 7) )
  122. def moveViewRect(self, x, y):
  123. x = float(x) - self.fInitialX
  124. y = float(y) - self.fInitialY
  125. fixPos = False
  126. rCentX = self.fViewRect[iWidth] / self.fScale / 2
  127. rCentY = self.fViewRect[iHeight] / self.fScale / 2
  128. if x < rCentX:
  129. x = rCentX
  130. fixPos = True
  131. elif x > self.kInternalWidth - rCentX:
  132. x = self.kInternalWidth - rCentX
  133. fixPos = True
  134. if y < rCentY:
  135. y = rCentY
  136. fixPos = True
  137. elif y > self.kInternalHeight - rCentY:
  138. y = self.kInternalHeight - rCentY
  139. fixPos = True
  140. if fixPos:
  141. globalPos = self.mapToGlobal(QPoint(self.fInitialX + x, self.fInitialY + y))
  142. self.cursor().setPos(globalPos)
  143. x = self.fRenderSource.width() * x / self.kInternalWidth
  144. y = self.fRenderSource.height() * y / self.kInternalHeight
  145. self.fScene.m_view.centerOn(x, y)
  146. def updateMouseMode(self, event=None):
  147. if self.fMouseLeftDown and self.fMouseRightDown:
  148. self.fMousePos = event.globalPos()
  149. self.setCursor(self.fScaleCursors[0])
  150. self.fMouseMode = MOUSE_MODE_SCALE
  151. elif self.fMouseLeftDown:
  152. self.setCursor(QCursor(Qt.SizeAllCursor))
  153. if self.fMouseMode == MOUSE_MODE_NONE:
  154. self.moveViewRect(event.x(), event.y())
  155. self.fMouseMode = MOUSE_MODE_MOVE
  156. else:
  157. self.unsetCursor()
  158. self.fMouseMode = MOUSE_MODE_NONE
  159. def mousePressEvent(self, event):
  160. if event.button() == Qt.LeftButton:
  161. self.fMouseLeftDown = True
  162. self.updateMouseMode(event)
  163. return event.accept()
  164. elif event.button() == Qt.RightButton:
  165. self.fMouseRightDown = True
  166. self.updateMouseMode(event)
  167. return event.accept()
  168. elif event.button() == Qt.MidButton:
  169. self.fMouseLeftDown = True
  170. self.fMouseRightDown = True
  171. self.updateMouseMode(event)
  172. return event.accept()
  173. QFrame.mouseMoveEvent(self, event)
  174. def mouseMoveEvent(self, event):
  175. if self.fMouseMode == MOUSE_MODE_MOVE:
  176. self.moveViewRect(event.x(), event.y())
  177. return event.accept()
  178. if self.fMouseMode == MOUSE_MODE_SCALE:
  179. dy = self.fMousePos.y() - event.globalY()
  180. if dy != 0:
  181. self.setCursor(self.fScaleCursors[1 if dy > 0 else 2])
  182. self.fScene.zoom_wheel(dy)
  183. self.cursor().setPos(self.fMousePos)
  184. QFrame.mouseMoveEvent(self, event)
  185. def mouseReleaseEvent(self, event):
  186. if event.button() == Qt.LeftButton:
  187. self.fMouseLeftDown = False
  188. self.updateMouseMode()
  189. return event.accept()
  190. elif event.button() == Qt.RightButton:
  191. self.fMouseRightDown = False
  192. self.updateMouseMode(event)
  193. return event.accept()
  194. elif event.button() == Qt.MidButton:
  195. self.fMouseLeftDown = event.buttons() & Qt.LeftButton
  196. self.fMouseRightDown = event.buttons() & Qt.RightButton
  197. self.updateMouseMode(event)
  198. return event.accept()
  199. QFrame.mouseReleaseEvent(self, event)
  200. def wheelEvent(self, event):
  201. self.fScene.zoom_wheel(event.angleDelta().y())
  202. return event.accept()
  203. def paintEvent(self, event):
  204. painter = QPainter(self)
  205. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing == ANTIALIASING_FULL))
  206. # Brightness-aware out-of-canvas shading
  207. bg_color = self.fViewBg
  208. bg_black = bg_color.black()
  209. bg_shade = -12 if bg_black < 127 else 12
  210. r,g,b,a = bg_color.getRgb()
  211. bg_color = QColor(r+bg_shade, g+bg_shade, b+bg_shade)
  212. frameWidth = self.fFrameWidth
  213. if self.fUseCustomPaint:
  214. # Inner shadow
  215. color = QColor.fromHsv(40, 0, 255-max(210, bg_color.black(), bg_black))
  216. painter.setBrush(Qt.transparent)
  217. painter.setPen(color)
  218. painter.drawRect(QRectF(0.5, 0.5, self.width()-1, self.height()-1))
  219. # Background
  220. painter.setBrush(bg_color)
  221. painter.setPen(bg_color)
  222. painter.drawRect(QRectF(1.5, 1.5, self.width()-3, self.height()-3))
  223. else:
  224. use_rounding = int(frameWidth > 1)
  225. rounding = 0.5 * use_rounding
  226. painter.setBrush(bg_color)
  227. painter.setPen(bg_color)
  228. painter.drawRoundedRect(QRectF(0.5+frameWidth, 0.5+frameWidth, self.width()-1-frameWidth*2, self.height()-1-frameWidth*2), rounding, rounding)
  229. clipPath = QPainterPath()
  230. rounding = 1.0 * use_rounding
  231. clipPath.addRoundedRect(QRectF(frameWidth, frameWidth, self.width()-frameWidth*2, self.height()-frameWidth*2), rounding, rounding)
  232. painter.setClipPath(clipPath)
  233. self.fScene.render(painter, self.fRenderTarget, self.fRenderSource, Qt.KeepAspectRatio)
  234. # Allow cursor frame to look joined with minicanvas frame
  235. painter.setClipping(False)
  236. width = self.fViewRect[iWidth]/self.fScale
  237. height = self.fViewRect[iHeight]/self.fScale
  238. # cursor
  239. lineHinting = self.fViewPen.widthF() / 2
  240. x = self.fViewRect[iX]+self.fInitialX
  241. y = self.fViewRect[iY]+self.fInitialY
  242. scr_x = floor(x)
  243. scr_y = floor(y)
  244. rect = QRectF(
  245. scr_x+lineHinting,
  246. scr_y+lineHinting,
  247. ceil(width+x-scr_x)-lineHinting*2,
  248. ceil(height+y-scr_y)-lineHinting*2 )
  249. if self.fRubberBandBlending == 1:
  250. painter.setCompositionMode(QPainter.CompositionMode_Plus)
  251. elif self.fRubberBandBlending == -1:
  252. painter.setCompositionMode(QPainter.CompositionMode_Difference)
  253. painter.setBrush(self.fViewBrush)
  254. painter.setPen(Qt.NoPen)
  255. painter.drawRect(rect)
  256. painter.setCompositionMode(QPainter.CompositionMode_SourceOver)
  257. painter.setBrush(Qt.NoBrush)
  258. painter.setPen(self.fViewPen)
  259. painter.drawRect(rect)
  260. if self.fUseCustomPaint:
  261. event.accept()
  262. else:
  263. QFrame.paintEvent(self, event)
  264. def resizeEvent(self, event):
  265. size = event.size()
  266. width = size.width()
  267. height = size.height()
  268. extRatio = (width - self.fFrameWidth * 2) / (height - self.fFrameWidth * 2)
  269. if extRatio >= self.kInternalRatio:
  270. self.kInternalHeight = floor(height - self.fFrameWidth * 2)
  271. self.kInternalWidth = floor(self.kInternalHeight * self.kInternalRatio)
  272. self.fInitialX = floor(float(width - self.kInternalWidth) / 2.0)
  273. self.fInitialY = self.fFrameWidth
  274. else:
  275. self.kInternalWidth = floor(width - self.fFrameWidth * 2)
  276. self.kInternalHeight = floor(self.kInternalWidth / self.kInternalRatio)
  277. self.fInitialX = self.fFrameWidth
  278. self.fInitialY = floor(float(height - self.kInternalHeight) / 2.0)
  279. self.fRenderTarget = QRectF(self.fInitialX, self.fInitialY, float(self.kInternalWidth), float(self.kInternalHeight))
  280. if self.fRealParent is not None:
  281. QTimer.singleShot(0, self.fRealParent.slot_miniCanvasCheckAll)
  282. QFrame.resizeEvent(self, event)