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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. # FIXME stop using self.m_ctrl_down
  225. if event.modifiers() & Qt.ControlModifier:
  226. self.m_ctrl_down = True
  227. if event.button() == Qt.MidButton and self.m_ctrl_down:
  228. self.m_mid_button_down = True
  229. self.startConnectionCut()
  230. pos = event.scenePos()
  231. self.m_pointer_border.moveTo(floor(pos.x()), floor(pos.y()))
  232. items = self.items(self.m_pointer_border)
  233. for item in items:
  234. if item and item.type() in (CanvasLineType, CanvasBezierLineType, CanvasPortType):
  235. item.triggerDisconnect()
  236. QGraphicsScene.mousePressEvent(self, event)
  237. def mouseMoveEvent(self, event):
  238. if self.m_mouse_down_init:
  239. self.m_mouse_down_init = False
  240. topmost = self.itemAt(event.scenePos(), self.m_view.transform())
  241. self.m_mouse_rubberband = not (topmost and topmost.type() in (CanvasBoxType,
  242. CanvasIconType,
  243. CanvasPortType))
  244. if self.m_mouse_rubberband:
  245. event.accept()
  246. pos = event.scenePos()
  247. pos_x = pos.x()
  248. pos_y = pos.y()
  249. if not self.m_rubberband_selection:
  250. self.m_rubberband.show()
  251. self.m_rubberband_selection = True
  252. self.m_rubberband_orig_point = pos
  253. rubberband_orig_point = self.m_rubberband_orig_point
  254. x = min(pos_x, rubberband_orig_point.x())
  255. y = min(pos_y, rubberband_orig_point.y())
  256. lineHinting = canvas.theme.rubberband_pen.widthF() / 2
  257. self.m_rubberband.setRect(x+lineHinting,
  258. y+lineHinting,
  259. abs(pos_x - rubberband_orig_point.x()),
  260. abs(pos_y - rubberband_orig_point.y()))
  261. return
  262. if self.m_mid_button_down and self.m_ctrl_down:
  263. trail = QPolygonF([event.scenePos(), event.lastScenePos(), event.scenePos()])
  264. items = self.items(trail)
  265. for item in items:
  266. if item and item.type() in (CanvasLineType, CanvasBezierLineType):
  267. item.triggerDisconnect()
  268. QGraphicsScene.mouseMoveEvent(self, event)
  269. def mouseReleaseEvent(self, event):
  270. if self.m_scale_area and not self.m_rubberband_selection:
  271. self.m_scale_area = False
  272. self.m_view.viewport().unsetCursor()
  273. if self.m_rubberband_selection:
  274. if self.m_scale_area:
  275. self.m_scale_area = False
  276. self.m_view.viewport().unsetCursor()
  277. rect = self.m_rubberband.rect()
  278. self.m_view.fitInView(rect.x(), rect.y(), rect.width(), rect.height(), Qt.KeepAspectRatio)
  279. self.fixScaleFactor()
  280. else:
  281. items_list = self.items()
  282. for item in items_list:
  283. if item and item.isVisible() and item.type() == CanvasBoxType:
  284. item_rect = item.sceneBoundingRect()
  285. item_top_left = QPointF(item_rect.x(), item_rect.y())
  286. item_bottom_right = QPointF(item_rect.x() + item_rect.width(),
  287. item_rect.y() + item_rect.height())
  288. if self.m_rubberband.contains(item_top_left) and self.m_rubberband.contains(item_bottom_right):
  289. item.setSelected(True)
  290. self.m_rubberband.hide()
  291. self.m_rubberband.setRect(0, 0, 0, 0)
  292. self.m_rubberband_selection = False
  293. else:
  294. items_list = self.selectedItems()
  295. for item in items_list:
  296. if item and item.isVisible() and item.type() == CanvasBoxType:
  297. item.checkItemPos()
  298. if len(items_list) > 1:
  299. canvas.scene.update()
  300. self.m_mouse_down_init = False
  301. self.m_mouse_rubberband = False
  302. if event.button() == Qt.MidButton:
  303. self.m_mid_button_down = False
  304. # Connection cut mode off
  305. if self.m_ctrl_down:
  306. self.m_view.viewport().unsetCursor()
  307. QGraphicsScene.mouseReleaseEvent(self, event)
  308. def zoom_wheel(self, delta):
  309. transform = self.m_view.transform()
  310. scale = transform.m11()
  311. if (delta > 0 and scale < self.m_scale_max) or (delta < 0 and scale > self.m_scale_min):
  312. factor = 1.41 ** (delta / 240.0)
  313. transform.scale(factor, factor)
  314. self.fixScaleFactor(transform)
  315. self.m_view.setTransform(transform)
  316. self.scaleChanged.emit(transform.m11())
  317. def wheelEvent(self, event):
  318. if not self.m_view:
  319. event.ignore()
  320. return
  321. if self.m_ctrl_down:
  322. event.accept()
  323. self.zoom_wheel(event.delta())
  324. return
  325. QGraphicsScene.wheelEvent(self, event)
  326. def contextMenuEvent(self, event):
  327. if self.m_ctrl_down:
  328. event.accept()
  329. self.triggerRubberbandScale()
  330. return
  331. if len(self.selectedItems()) == 0:
  332. event.accept()
  333. canvas.callback(ACTION_BG_RIGHT_CLICK, 0, 0, "")
  334. return
  335. QGraphicsScene.contextMenuEvent(self, event)
  336. # ------------------------------------------------------------------------------------------------------------