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.

423 lines
14KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # PatchBay Canvas engine using QGraphicsView/Scene
  4. # Copyright (C) 2010-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
  20. from PyQt5.QtCore import QT_VERSION, pyqtSignal, pyqtSlot, qFatal, Qt, QPointF, QRectF
  21. from PyQt5.QtGui import QCursor, QPixmap, QPolygonF
  22. from PyQt5.QtWidgets import QGraphicsRectItem, QGraphicsScene
  23. # ------------------------------------------------------------------------------------------------------------
  24. # Imports (Custom)
  25. from . import (
  26. canvas,
  27. CanvasBoxType,
  28. CanvasIconType,
  29. CanvasPortType,
  30. CanvasLineType,
  31. CanvasBezierLineType,
  32. CanvasRubberbandType,
  33. ACTION_BG_RIGHT_CLICK,
  34. MAX_PLUGIN_ID_ALLOWED,
  35. )
  36. # ------------------------------------------------------------------------------------------------------------
  37. class RubberbandRect(QGraphicsRectItem):
  38. def __init__(self, scene):
  39. QGraphicsRectItem.__init__(self, QRectF(0, 0, 0, 0))
  40. self.setZValue(-1)
  41. self.hide()
  42. scene.addItem(self)
  43. def type(self):
  44. return CanvasRubberbandType
  45. # ------------------------------------------------------------------------------------------------------------
  46. class PatchScene(QGraphicsScene):
  47. scaleChanged = pyqtSignal(float)
  48. pluginSelected = pyqtSignal(list)
  49. def __init__(self, parent, view):
  50. QGraphicsScene.__init__(self, parent)
  51. self.m_ctrl_down = False
  52. self.m_scale_area = False
  53. self.m_mouse_down_init = False
  54. self.m_mouse_rubberband = False
  55. self.m_mid_button_down = False
  56. self.m_pointer_border = QRectF(0.0, 0.0, 1.0, 1.0)
  57. self.m_scale_min = 0.1
  58. self.m_scale_max = 4.0
  59. self.m_rubberband = RubberbandRect(self)
  60. self.m_rubberband_selection = False
  61. self.m_rubberband_orig_point = QPointF(0, 0)
  62. self.m_view = view
  63. if not self.m_view:
  64. qFatal("PatchCanvas::PatchScene() - invalid view")
  65. self.curCut = None
  66. self.curZoomArea = None
  67. self.selectionChanged.connect(self.slot_selectionChanged)
  68. def getDevicePixelRatioF(self):
  69. if QT_VERSION < 0x50600:
  70. return 1.0
  71. return self.m_view.devicePixelRatioF()
  72. def getScaleFactor(self):
  73. return self.m_view.transform().m11()
  74. def getView(self):
  75. return self.m_view
  76. def fixScaleFactor(self, transform=None):
  77. fix, set_view = False, False
  78. if not transform:
  79. set_view = True
  80. view = self.m_view
  81. transform = view.transform()
  82. scale = transform.m11()
  83. if scale > self.m_scale_max:
  84. fix = True
  85. transform.reset()
  86. transform.scale(self.m_scale_max, self.m_scale_max)
  87. elif scale < self.m_scale_min:
  88. fix = True
  89. transform.reset()
  90. transform.scale(self.m_scale_min, self.m_scale_min)
  91. if set_view:
  92. if fix:
  93. view.setTransform(transform)
  94. self.scaleChanged.emit(transform.m11())
  95. return fix
  96. def updateLimits(self):
  97. w0 = canvas.size_rect.width()
  98. h0 = canvas.size_rect.height()
  99. w1 = self.m_view.width()
  100. h1 = self.m_view.height()
  101. self.m_scale_min = w1/w0 if w0/h0 > w1/h1 else h1/h0
  102. def updateTheme(self):
  103. self.setBackgroundBrush(canvas.theme.canvas_bg)
  104. self.m_rubberband.setPen(canvas.theme.rubberband_pen)
  105. self.m_rubberband.setBrush(canvas.theme.rubberband_brush)
  106. cur_color = "black" if canvas.theme.canvas_bg.blackF() < 0.5 else "white"
  107. self.curCut = QCursor(QPixmap(":/cursors/cut_"+cur_color+".png"), 1, 1)
  108. self.curZoomArea = QCursor(QPixmap(":/cursors/zoom-area_"+cur_color+".png"), 8, 7)
  109. def zoom_fit(self):
  110. min_x = min_y = max_x = max_y = None
  111. first_value = True
  112. items_list = self.items()
  113. if len(items_list) > 0:
  114. for item in items_list:
  115. if item and item.isVisible() and item.type() == CanvasBoxType:
  116. pos = item.scenePos()
  117. rect = item.boundingRect()
  118. x = pos.x()
  119. y = pos.y()
  120. if first_value:
  121. first_value = False
  122. min_x, min_y = x, y
  123. max_x = x + rect.width()
  124. max_y = y + rect.height()
  125. else:
  126. min_x = min(min_x, x)
  127. min_y = min(min_y, y)
  128. max_x = max(max_x, x + rect.width())
  129. max_y = max(max_y, y + rect.height())
  130. if not first_value:
  131. self.m_view.fitInView(min_x, min_y, abs(max_x - min_x), abs(max_y - min_y), Qt.KeepAspectRatio)
  132. self.fixScaleFactor()
  133. def zoom_in(self):
  134. view = self.m_view
  135. transform = view.transform()
  136. if transform.m11() < self.m_scale_max:
  137. transform.scale(1.2, 1.2)
  138. if transform.m11() > self.m_scale_max:
  139. transform.reset()
  140. transform.scale(self.m_scale_max, self.m_scale_max)
  141. view.setTransform(transform)
  142. self.scaleChanged.emit(transform.m11())
  143. def zoom_out(self):
  144. view = self.m_view
  145. transform = view.transform()
  146. if transform.m11() > self.m_scale_min:
  147. transform.scale(0.833333333333333, 0.833333333333333)
  148. if transform.m11() < self.m_scale_min:
  149. transform.reset()
  150. transform.scale(self.m_scale_min, self.m_scale_min)
  151. view.setTransform(transform)
  152. self.scaleChanged.emit(transform.m11())
  153. def zoom_reset(self):
  154. self.m_view.resetTransform()
  155. self.scaleChanged.emit(1.0)
  156. @pyqtSlot()
  157. def slot_selectionChanged(self):
  158. items_list = self.selectedItems()
  159. if len(items_list) == 0:
  160. self.pluginSelected.emit([])
  161. return
  162. plugin_list = []
  163. for item in items_list:
  164. if item and item.isVisible():
  165. group_item = None
  166. if item.type() == CanvasBoxType:
  167. group_item = item
  168. elif item.type() == CanvasPortType:
  169. group_item = item.parentItem()
  170. #elif item.type() in (CanvasLineType, CanvasBezierLineType, CanvasLineMovType, CanvasBezierLineMovType):
  171. #plugin_list = []
  172. #break
  173. if group_item is not None and group_item.m_plugin_id >= 0:
  174. plugin_id = group_item.m_plugin_id
  175. if plugin_id > MAX_PLUGIN_ID_ALLOWED:
  176. plugin_id = 0
  177. plugin_list.append(plugin_id)
  178. self.pluginSelected.emit(plugin_list)
  179. def triggerRubberbandScale(self):
  180. self.m_scale_area = True
  181. if self.curZoomArea:
  182. self.m_view.viewport().setCursor(self.curZoomArea)
  183. def keyPressEvent(self, event):
  184. if not self.m_view:
  185. event.ignore()
  186. return
  187. if event.key() == Qt.Key_Control:
  188. self.m_ctrl_down = True
  189. if self.m_mid_button_down:
  190. self.startConnectionCut()
  191. elif event.key() == Qt.Key_Home:
  192. event.accept()
  193. self.zoom_fit()
  194. return
  195. elif self.m_ctrl_down:
  196. if event.key() == Qt.Key_Plus:
  197. event.accept()
  198. self.zoom_in()
  199. return
  200. if event.key() == Qt.Key_Minus:
  201. event.accept()
  202. self.zoom_out()
  203. return
  204. if event.key() == Qt.Key_1:
  205. event.accept()
  206. self.zoom_reset()
  207. return
  208. QGraphicsScene.keyPressEvent(self, event)
  209. def keyReleaseEvent(self, event):
  210. if event.key() == Qt.Key_Control:
  211. self.m_ctrl_down = False
  212. # Connection cut mode off
  213. if self.m_mid_button_down:
  214. self.m_view.viewport().unsetCursor()
  215. QGraphicsScene.keyReleaseEvent(self, event)
  216. def startConnectionCut(self):
  217. if self.curCut:
  218. self.m_view.viewport().setCursor(self.curCut)
  219. def mousePressEvent(self, event):
  220. self.m_mouse_down_init = (
  221. (event.button() == Qt.LeftButton) or ((event.button() == Qt.RightButton) and self.m_ctrl_down)
  222. )
  223. self.m_mouse_rubberband = False
  224. if event.button() == Qt.MidButton and self.m_ctrl_down:
  225. self.m_mid_button_down = True
  226. self.startConnectionCut()
  227. pos = event.scenePos()
  228. self.m_pointer_border.moveTo(floor(pos.x()), floor(pos.y()))
  229. items = self.items(self.m_pointer_border)
  230. for item in items:
  231. if item and item.type() in (CanvasLineType, CanvasBezierLineType, CanvasPortType):
  232. item.triggerDisconnect()
  233. QGraphicsScene.mousePressEvent(self, event)
  234. def mouseMoveEvent(self, event):
  235. if self.m_mouse_down_init:
  236. self.m_mouse_down_init = False
  237. topmost = self.itemAt(event.scenePos(), self.m_view.transform())
  238. self.m_mouse_rubberband = not (topmost and topmost.type() in (CanvasBoxType,
  239. CanvasIconType,
  240. CanvasPortType))
  241. if self.m_mouse_rubberband:
  242. event.accept()
  243. pos = event.scenePos()
  244. pos_x = pos.x()
  245. pos_y = pos.y()
  246. if not self.m_rubberband_selection:
  247. self.m_rubberband.show()
  248. self.m_rubberband_selection = True
  249. self.m_rubberband_orig_point = pos
  250. rubberband_orig_point = self.m_rubberband_orig_point
  251. x = min(pos_x, rubberband_orig_point.x())
  252. y = min(pos_y, rubberband_orig_point.y())
  253. lineHinting = canvas.theme.rubberband_pen.widthF() / 2
  254. self.m_rubberband.setRect(x+lineHinting,
  255. y+lineHinting,
  256. abs(pos_x - rubberband_orig_point.x()),
  257. abs(pos_y - rubberband_orig_point.y()))
  258. return
  259. if self.m_mid_button_down and self.m_ctrl_down:
  260. trail = QPolygonF([event.scenePos(), event.lastScenePos(), event.scenePos()])
  261. items = self.items(trail)
  262. for item in items:
  263. if item and item.type() in (CanvasLineType, CanvasBezierLineType):
  264. item.triggerDisconnect()
  265. QGraphicsScene.mouseMoveEvent(self, event)
  266. def mouseReleaseEvent(self, event):
  267. if self.m_scale_area and not self.m_rubberband_selection:
  268. self.m_scale_area = False
  269. self.m_view.viewport().unsetCursor()
  270. if self.m_rubberband_selection:
  271. if self.m_scale_area:
  272. self.m_scale_area = False
  273. self.m_view.viewport().unsetCursor()
  274. rect = self.m_rubberband.rect()
  275. self.m_view.fitInView(rect.x(), rect.y(), rect.width(), rect.height(), Qt.KeepAspectRatio)
  276. self.fixScaleFactor()
  277. else:
  278. items_list = self.items()
  279. for item in items_list:
  280. if item and item.isVisible() and item.type() == CanvasBoxType:
  281. item_rect = item.sceneBoundingRect()
  282. item_top_left = QPointF(item_rect.x(), item_rect.y())
  283. item_bottom_right = QPointF(item_rect.x() + item_rect.width(),
  284. item_rect.y() + item_rect.height())
  285. if self.m_rubberband.contains(item_top_left) and self.m_rubberband.contains(item_bottom_right):
  286. item.setSelected(True)
  287. self.m_rubberband.hide()
  288. self.m_rubberband.setRect(0, 0, 0, 0)
  289. self.m_rubberband_selection = False
  290. else:
  291. items_list = self.selectedItems()
  292. for item in items_list:
  293. if item and item.isVisible() and item.type() == CanvasBoxType:
  294. item.checkItemPos()
  295. if len(items_list) > 1:
  296. canvas.scene.update()
  297. self.m_mouse_down_init = False
  298. self.m_mouse_rubberband = False
  299. if event.button() == Qt.MidButton:
  300. self.m_mid_button_down = False
  301. # Connection cut mode off
  302. if self.m_ctrl_down:
  303. self.m_view.viewport().unsetCursor()
  304. QGraphicsScene.mouseReleaseEvent(self, event)
  305. def zoom_wheel(self, delta):
  306. transform = self.m_view.transform()
  307. scale = transform.m11()
  308. if (delta > 0 and scale < self.m_scale_max) or (delta < 0 and scale > self.m_scale_min):
  309. factor = 1.41 ** (delta / 240.0)
  310. transform.scale(factor, factor)
  311. self.fixScaleFactor(transform)
  312. self.m_view.setTransform(transform)
  313. self.scaleChanged.emit(transform.m11())
  314. def wheelEvent(self, event):
  315. if not self.m_view:
  316. event.ignore()
  317. return
  318. if self.m_ctrl_down:
  319. event.accept()
  320. self.zoom_wheel(event.delta())
  321. return
  322. QGraphicsScene.wheelEvent(self, event)
  323. def contextMenuEvent(self, event):
  324. if self.m_ctrl_down:
  325. event.accept()
  326. self.triggerRubberbandScale()
  327. return
  328. if len(self.selectedItems()) == 0:
  329. event.accept()
  330. canvas.callback(ACTION_BG_RIGHT_CLICK, 0, 0, "")
  331. return
  332. QGraphicsScene.contextMenuEvent(self, event)
  333. # ------------------------------------------------------------------------------------------------------------