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.

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