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.

286 lines
8.2KB

  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 PyQt5.QtCore import QPointF, QRectF
  20. from PyQt5.QtWidgets import QGraphicsItem
  21. # ------------------------------------------------------------------------------------------------------------
  22. # Imports (Theme)
  23. from .theme import getDefaultThemeName
  24. # ------------------------------------------------------------------------------------------------------------
  25. # Maximum Id for a plugin, treated as invalid/zero if above this value
  26. MAX_PLUGIN_ID_ALLOWED = 0x7FF
  27. # Port Mode
  28. PORT_MODE_NULL = 0
  29. PORT_MODE_INPUT = 1
  30. PORT_MODE_OUTPUT = 2
  31. # Port Type
  32. PORT_TYPE_NULL = 0
  33. PORT_TYPE_AUDIO_JACK = 1
  34. PORT_TYPE_MIDI_JACK = 2
  35. PORT_TYPE_MIDI_ALSA = 3
  36. PORT_TYPE_PARAMETER = 4
  37. # Callback Action
  38. ACTION_GROUP_INFO = 0 # group_id, N, N
  39. ACTION_GROUP_RENAME = 1 # group_id, N, N
  40. ACTION_GROUP_SPLIT = 2 # group_id, N, N
  41. ACTION_GROUP_JOIN = 3 # group_id, N, N
  42. ACTION_PORT_INFO = 4 # group_id, port_id, N
  43. ACTION_PORT_RENAME = 5 # group_id, port_id, N
  44. ACTION_PORTS_CONNECT = 6 # N, N, "outG:outP:inG:inP"
  45. ACTION_PORTS_DISCONNECT = 7 # conn_id, N, N
  46. ACTION_PLUGIN_CLONE = 8 # plugin_id, N, N
  47. ACTION_PLUGIN_EDIT = 9 # plugin_id, N, N
  48. ACTION_PLUGIN_RENAME = 10 # plugin_id, N, N
  49. ACTION_PLUGIN_REPLACE = 11 # plugin_id, N, N
  50. ACTION_PLUGIN_REMOVE = 12 # plugin_id, N, N
  51. ACTION_PLUGIN_SHOW_UI = 13 # plugin_id, N, N
  52. ACTION_BG_RIGHT_CLICK = 14 # N, N, N
  53. ACTION_INLINE_DISPLAY = 15 # plugin_id, N, N
  54. # Icon
  55. ICON_APPLICATION = 0
  56. ICON_HARDWARE = 1
  57. ICON_DISTRHO = 2
  58. ICON_FILE = 3
  59. ICON_PLUGIN = 4
  60. ICON_LADISH_ROOM = 5
  61. # Split Option
  62. SPLIT_UNDEF = 0
  63. SPLIT_NO = 1
  64. SPLIT_YES = 2
  65. # Antialiasing Option
  66. ANTIALIASING_NONE = 0
  67. ANTIALIASING_SMALL = 1
  68. ANTIALIASING_FULL = 2
  69. # Eye-Candy Option
  70. EYECANDY_NONE = 0
  71. EYECANDY_SMALL = 1
  72. EYECANDY_FULL = 2
  73. # ------------------------------------------------------------------------------------------------------------
  74. # object types
  75. CanvasBoxType = QGraphicsItem.UserType + 1
  76. CanvasIconType = QGraphicsItem.UserType + 2
  77. CanvasPortType = QGraphicsItem.UserType + 3
  78. CanvasLineType = QGraphicsItem.UserType + 4
  79. CanvasBezierLineType = QGraphicsItem.UserType + 5
  80. CanvasLineMovType = QGraphicsItem.UserType + 6
  81. CanvasBezierLineMovType = QGraphicsItem.UserType + 7
  82. CanvasRubberbandType = QGraphicsItem.UserType + 8
  83. # ------------------------------------------------------------------------------------------------------------
  84. # Canvas options
  85. class options_t(object):
  86. __slots__ = [
  87. 'theme_name',
  88. 'auto_hide_groups',
  89. 'auto_select_items',
  90. 'use_bezier_lines',
  91. 'antialiasing',
  92. 'eyecandy',
  93. 'inline_displays'
  94. ]
  95. # Canvas features
  96. class features_t(object):
  97. __slots__ = [
  98. 'group_info',
  99. 'group_rename',
  100. 'port_info',
  101. 'port_rename',
  102. 'handle_group_pos'
  103. ]
  104. # Main Canvas object
  105. class Canvas(object):
  106. def __init__(self):
  107. self.qobject = None
  108. self.settings = None
  109. self.theme = None
  110. self.initiated = False
  111. self.group_list = []
  112. self.port_list = []
  113. self.connection_list = []
  114. self.animation_list = []
  115. self.group_plugin_map = {}
  116. self.callback = self.callback
  117. self.debug = False
  118. self.scene = None
  119. self.last_z_value = 0
  120. self.last_connection_id = 0
  121. self.initial_pos = QPointF(0, 0)
  122. self.size_rect = QRectF()
  123. def callback(self, action, value1, value2, value_str):
  124. print("Canvas::callback({}, {}, {}, {})".format(action, value1, value2, value_str))
  125. # ------------------------------------------------------------------------------------------------------------
  126. # object lists
  127. class group_dict_t(object):
  128. __slots__ = [
  129. 'group_id',
  130. 'group_name',
  131. 'split',
  132. 'icon',
  133. 'plugin_id',
  134. 'plugin_ui',
  135. 'plugin_inline',
  136. 'widgets'
  137. ]
  138. class port_dict_t(object):
  139. __slots__ = [
  140. 'group_id',
  141. 'port_id',
  142. 'port_name',
  143. 'port_mode',
  144. 'port_type',
  145. 'is_alternate',
  146. 'widget'
  147. ]
  148. class connection_dict_t(object):
  149. __slots__ = [
  150. 'connection_id',
  151. 'group_in_id',
  152. 'port_in_id',
  153. 'group_out_id',
  154. 'port_out_id',
  155. 'widget'
  156. ]
  157. class animation_dict_t(object):
  158. __slots__ = [
  159. 'animation',
  160. 'item'
  161. ]
  162. # ------------------------------------------------------------------------------------------------------------
  163. # Internal functions
  164. def bool2str(check):
  165. return "True" if check else "False"
  166. def port_mode2str(port_mode):
  167. if port_mode == PORT_MODE_NULL:
  168. return "PORT_MODE_NULL"
  169. elif port_mode == PORT_MODE_INPUT:
  170. return "PORT_MODE_INPUT"
  171. elif port_mode == PORT_MODE_OUTPUT:
  172. return "PORT_MODE_OUTPUT"
  173. else:
  174. return "PORT_MODE_???"
  175. def port_type2str(port_type):
  176. if port_type == PORT_TYPE_NULL:
  177. return "PORT_TYPE_NULL"
  178. elif port_type == PORT_TYPE_AUDIO_JACK:
  179. return "PORT_TYPE_AUDIO_JACK"
  180. elif port_type == PORT_TYPE_MIDI_JACK:
  181. return "PORT_TYPE_MIDI_JACK"
  182. elif port_type == PORT_TYPE_MIDI_ALSA:
  183. return "PORT_TYPE_MIDI_ALSA"
  184. elif port_type == PORT_TYPE_PARAMETER:
  185. return "PORT_TYPE_MIDI_PARAMETER"
  186. else:
  187. return "PORT_TYPE_???"
  188. def icon2str(icon):
  189. if icon == ICON_APPLICATION:
  190. return "ICON_APPLICATION"
  191. elif icon == ICON_HARDWARE:
  192. return "ICON_HARDWARE"
  193. elif icon == ICON_DISTRHO:
  194. return "ICON_DISTRHO"
  195. elif icon == ICON_FILE:
  196. return "ICON_FILE"
  197. elif icon == ICON_PLUGIN:
  198. return "ICON_PLUGIN"
  199. elif icon == ICON_LADISH_ROOM:
  200. return "ICON_LADISH_ROOM"
  201. else:
  202. return "ICON_???"
  203. def split2str(split):
  204. if split == SPLIT_UNDEF:
  205. return "SPLIT_UNDEF"
  206. elif split == SPLIT_NO:
  207. return "SPLIT_NO"
  208. elif split == SPLIT_YES:
  209. return "SPLIT_YES"
  210. else:
  211. return "SPLIT_???"
  212. # ------------------------------------------------------------------------------------------------------------
  213. # Global objects
  214. canvas = Canvas()
  215. options = options_t()
  216. options.theme_name = getDefaultThemeName()
  217. options.auto_hide_groups = False
  218. options.auto_select_items = False
  219. options.use_bezier_lines = True
  220. options.antialiasing = ANTIALIASING_SMALL
  221. options.eyecandy = EYECANDY_SMALL
  222. options.inline_displays = False
  223. features = features_t()
  224. features.group_info = False
  225. features.group_rename = False
  226. features.port_info = False
  227. features.port_rename = False
  228. features.handle_group_pos = False
  229. # PatchCanvas API
  230. def setOptions(new_options):
  231. if canvas.initiated: return
  232. options.theme_name = new_options.theme_name
  233. options.auto_hide_groups = new_options.auto_hide_groups
  234. options.auto_select_items = new_options.auto_select_items
  235. options.use_bezier_lines = new_options.use_bezier_lines
  236. options.antialiasing = new_options.antialiasing
  237. options.eyecandy = new_options.eyecandy
  238. options.inline_displays = new_options.inline_displays
  239. def setFeatures(new_features):
  240. if canvas.initiated: return
  241. features.group_info = new_features.group_info
  242. features.group_rename = new_features.group_rename
  243. features.port_info = new_features.port_info
  244. features.port_rename = new_features.port_rename
  245. features.handle_group_pos = new_features.handle_group_pos