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.

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