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.

283 lines
8.0KB

  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. ]
  94. # Canvas features
  95. class features_t(object):
  96. __slots__ = [
  97. 'group_info',
  98. 'group_rename',
  99. 'port_info',
  100. 'port_rename',
  101. 'handle_group_pos'
  102. ]
  103. # Main Canvas object
  104. class Canvas(object):
  105. def __init__(self):
  106. self.qobject = None
  107. self.settings = None
  108. self.theme = None
  109. self.initiated = False
  110. self.group_list = []
  111. self.port_list = []
  112. self.connection_list = []
  113. self.animation_list = []
  114. self.group_plugin_map = {}
  115. self.callback = self.callback
  116. self.debug = False
  117. self.scene = None
  118. self.last_z_value = 0
  119. self.last_connection_id = 0
  120. self.initial_pos = QPointF(0, 0)
  121. self.size_rect = QRectF()
  122. def callback(self, action, value1, value2, value_str):
  123. print("Canvas::callback({}, {}, {}, {})".fmt(action, value1, value2, value_str))
  124. # ------------------------------------------------------------------------------------------------------------
  125. # object lists
  126. class group_dict_t(object):
  127. __slots__ = [
  128. 'group_id',
  129. 'group_name',
  130. 'split',
  131. 'icon',
  132. 'plugin_id',
  133. 'plugin_ui',
  134. 'plugin_inline',
  135. 'widgets'
  136. ]
  137. class port_dict_t(object):
  138. __slots__ = [
  139. 'group_id',
  140. 'port_id',
  141. 'port_name',
  142. 'port_mode',
  143. 'port_type',
  144. 'is_alternate',
  145. 'widget'
  146. ]
  147. class connection_dict_t(object):
  148. __slots__ = [
  149. 'connection_id',
  150. 'group_in_id',
  151. 'port_in_id',
  152. 'group_out_id',
  153. 'port_out_id',
  154. 'widget'
  155. ]
  156. class animation_dict_t(object):
  157. __slots__ = [
  158. 'animation',
  159. 'item'
  160. ]
  161. # ------------------------------------------------------------------------------------------------------------
  162. # Internal functions
  163. def bool2str(check):
  164. return "True" if check else "False"
  165. def port_mode2str(port_mode):
  166. if port_mode == PORT_MODE_NULL:
  167. return "PORT_MODE_NULL"
  168. elif port_mode == PORT_MODE_INPUT:
  169. return "PORT_MODE_INPUT"
  170. elif port_mode == PORT_MODE_OUTPUT:
  171. return "PORT_MODE_OUTPUT"
  172. else:
  173. return "PORT_MODE_???"
  174. def port_type2str(port_type):
  175. if port_type == PORT_TYPE_NULL:
  176. return "PORT_TYPE_NULL"
  177. elif port_type == PORT_TYPE_AUDIO_JACK:
  178. return "PORT_TYPE_AUDIO_JACK"
  179. elif port_type == PORT_TYPE_MIDI_JACK:
  180. return "PORT_TYPE_MIDI_JACK"
  181. elif port_type == PORT_TYPE_MIDI_ALSA:
  182. return "PORT_TYPE_MIDI_ALSA"
  183. elif port_type == PORT_TYPE_PARAMETER:
  184. return "PORT_TYPE_MIDI_PARAMETER"
  185. else:
  186. return "PORT_TYPE_???"
  187. def icon2str(icon):
  188. if icon == ICON_APPLICATION:
  189. return "ICON_APPLICATION"
  190. elif icon == ICON_HARDWARE:
  191. return "ICON_HARDWARE"
  192. elif icon == ICON_DISTRHO:
  193. return "ICON_DISTRHO"
  194. elif icon == ICON_FILE:
  195. return "ICON_FILE"
  196. elif icon == ICON_PLUGIN:
  197. return "ICON_PLUGIN"
  198. elif icon == ICON_LADISH_ROOM:
  199. return "ICON_LADISH_ROOM"
  200. else:
  201. return "ICON_???"
  202. def split2str(split):
  203. if split == SPLIT_UNDEF:
  204. return "SPLIT_UNDEF"
  205. elif split == SPLIT_NO:
  206. return "SPLIT_NO"
  207. elif split == SPLIT_YES:
  208. return "SPLIT_YES"
  209. else:
  210. return "SPLIT_???"
  211. # ------------------------------------------------------------------------------------------------------------
  212. # Global objects
  213. canvas = Canvas()
  214. options = options_t()
  215. options.theme_name = getDefaultThemeName()
  216. options.auto_hide_groups = False
  217. options.auto_select_items = False
  218. options.use_bezier_lines = True
  219. options.antialiasing = ANTIALIASING_SMALL
  220. options.eyecandy = EYECANDY_SMALL
  221. features = features_t()
  222. features.group_info = False
  223. features.group_rename = False
  224. features.port_info = False
  225. features.port_rename = False
  226. features.handle_group_pos = False
  227. # PatchCanvas API
  228. def setOptions(new_options):
  229. if canvas.initiated: return
  230. options.theme_name = new_options.theme_name
  231. options.auto_hide_groups = new_options.auto_hide_groups
  232. options.auto_select_items = new_options.auto_select_items
  233. options.use_bezier_lines = new_options.use_bezier_lines
  234. options.antialiasing = new_options.antialiasing
  235. options.eyecandy = new_options.eyecandy
  236. def setFeatures(new_features):
  237. if canvas.initiated: return
  238. features.group_info = new_features.group_info
  239. features.group_rename = new_features.group_rename
  240. features.port_info = new_features.port_info
  241. features.port_rename = new_features.port_rename
  242. features.handle_group_pos = new_features.handle_group_pos