Layer.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from gi.repository import Gtk, GLib
  2. class Layer:
  3. def __init__(self, base, name, on_change):
  4. print("init", name)
  5. self.mask = []
  6. self.tools = []
  7. self.tool_map = {}
  8. self.name = name
  9. self.enabled = True
  10. self.selected_tool = 0
  11. self.editable = not base
  12. self.selector_row = None
  13. self.tool_box = Gtk.FlowBox()
  14. self.tool_box.set_orientation(1)
  15. self.tool_stack = Gtk.Stack()
  16. self.tool_stack.set_transition_type(6)
  17. self.tool_stack.set_homogeneous(False)
  18. self.layer_changed = on_change
  19. def add_tool(self, tool):
  20. print("add tool", tool)
  21. self.tool_box.add(tool.tool_button)
  22. self.tool_stack.add(tool.widget)
  23. self.tool_map[tool.tool_button] = tool
  24. tool.tool_button.connect("clicked", self.on_tool_button_clicked)
  25. tool.connect_on_change(self.on_tool_change)
  26. self.tools += [tool,]
  27. if(len(self.tools) == 1):
  28. tool.tool_button.set_active(True)
  29. def on_tool_change(self, tool, prop):
  30. self.layer_changed(self)
  31. def on_tool_button_clicked(self, sender):
  32. if(sender.get_active()):
  33. self.tool_stack.set_visible_child(self.tool_map[sender].widget)
  34. for key in self.tool_map:
  35. if(key != sender):
  36. key.set_active(False)
  37. elif(self.tool_stack.get_visible_child() == self.tool_map[sender].widget):
  38. sender.set_active(True)
  39. def get_layer_dict(self):
  40. layerDict = {}
  41. # Get tool values
  42. toolDict = {}
  43. for tool in self.tools:
  44. toolDict[tool.id] = tool.get_properties_as_dict()
  45. layerDict["tools"] = toolDict
  46. layerDict["name"] = self.name
  47. layerDict["mask"] = self.mask
  48. layerDict["enabled"] = self.enabled
  49. return layerDict
  50. def set_from_layer_dict(self, dict):
  51. print("set_from_layer_dict", dict)
  52. # Load Tool Data
  53. for tool in self.tools:
  54. if(tool.id in dict["tools"]):
  55. GLib.idle_add(tool.load_properties, dict["tools"][tool.id])
  56. # The base layer only has tools.
  57. if(self.editable):
  58. # Load Mask Vectors
  59. self.mask = dict["mask"]
  60. # Load Layer Name
  61. self.name = dict["name"]
  62. # Load Enabled State
  63. self.enabled = dict["enabled"]
  64. def render_layer(self, baseImage, image, callback=None):
  65. # We are passed a base image (original)
  66. # Make a copy of this to pass through the tools
  67. layer = baseImage.copy()
  68. # Base layer needs no mask processing
  69. if(not self.editable):
  70. ntools = len(self.tools)
  71. count = 1
  72. for tool in self.tools:
  73. if (callback != None):
  74. # For showing progress
  75. callback(tool.name, ntools, count - 1)
  76. # Call the tool's image processing function
  77. layer = tool.on_update(layer)
  78. count += 1
  79. image = layer
  80. ## Here we would blend with the mask
  81. return image
  82. def show_all(self):
  83. self.tool_box.show_all()
  84. self.tool_stack.show_all()
  85. for tool in self.tools:
  86. tool.widget.show_all()
  87. tool.tool_button.show_all()
  88. def reset_tools(self):
  89. for tool in self.tools:
  90. tool.reset()