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.

scene.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. 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. # ------------------------------------------------------------------------------------------------------------