#!/usr/bin/env python
"""
Footprint PCB layouting program
Copyright (C) 2012 Danny Milosavljevic
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

#import views.canvas
from primitives import From
import pickle

def load(source):
	return pickle.load(source) # Workbench
def save(workbench, destination):
	pickle.dump(workbench, destination)
class Workbench(object):
	def __init__(self, canvasView, toolbox):
		self.canvasView = canvasView
		self.toolbox = toolbox
		self.toolbox.toolMonitors.cons(self.handleToolChange)
	@property
	def tool(self):
		return self.toolbox.tool
	@tool.setter
	def tool(self, value):
		#if value is not self.toolbox.tool:
		self.toolbox.tool = value
		self.canvasView.tool = value
	def handleToolChange(self, *args):
		value = self.toolbox.tool
		self.canvasView.tool = value
		#self.toolbox.tool
	def repaint(self, area):
		yield From(self.canvasView.repaint(area))
		# TODO maybe show the tool somewhere.
	def handleEvent(self, operator, *operands):
		if self.canvasView.handleEvent(operator, *operands):
			return True
		if self.toolbox.handleEvent(self.canvasView, operator, *operands):
			return True
		return False
