#!/usr/bin/python2
from qtcanvas import *
from qt import *
import sys

class Point(QCanvasSprite):

	def __init__(self, *args):
		"""The point's constructor"""
		self.radius=5
		
		self.canvasPixmap = self.pointCanvasPixmap(Qt.darkCyan, Qt.black)
		# Comment the following line:
		self.canvasPixmap1 = self.pointCanvasPixmap(Qt.green, Qt.black)
		self.pixmapArray = QCanvasPixmapArray()
		self.pixmapArray.setImage(0, self.canvasPixmap)
		# Comment the following line:
		self.pixmapArray.setImage(1, self.canvasPixmap1)
		
		QCanvasSprite.__init__(self, self.pixmapArray, *args)
		self.setZ(0)

	def pointCanvasPixmap(self, fillColor, penColor):
		"""Returns a QCanvasPixmap for the Point with the desired color."""
		pixmap = QPixmap(2*self.radius, 2*self.radius, -1)
		pixmap.fill()
		p = QPainter(pixmap)
		p.setBrush(fillColor)
		p.setPen(penColor)
		p.drawEllipse(0, 0, 2*self.radius, 2*self.radius)
		p.end()
		pixmap.setMask(pixmap.createHeuristicMask())
		canvasPixmap = QCanvasPixmap(pixmap, QPoint(0,0))
		return canvasPixmap

	def moveTo(self, position):
		"""Use this instead of move in order to maintain accuracy.  This
		moves the center of the point; not the top right corner.  It also
		notifies all attached items that it has been moved."""
		self.move(position.x()-self.radius, position.y()-self.radius)

	def xExact(self):
		"""Use this x-value in the construction of geometric shapes.  It is
		the center of the point."""
		return self.x()+self.radius

	def yExact(self):
		"""This is the y position of the center of the point."""
		return self.y()+self.radius

	def rtti(self):
		return 1001

class MainWindow(QMainWindow):

	def __init__(self, *args):
		QMainWindow.__init__(self, *args)

		self.canvas = Canvas(self)
		self.canvas.resize(100,100)

		self.view = View(self.canvas, self)
		self.setCentralWidget(self.view)

		self.fileQuitAction = QAction("Quit", "&Quit",
								 Qt.CTRL+Qt.Key_Q, self, "quit")
		QObject.connect(self.fileQuitAction, SIGNAL("activated()"),
						qApp, SLOT("closeAllWindows()"))

		self.tools = QActionGroup(self)
		self.tools.setExclusive(1)
		self.toolPlacePointAction = QAction("Place Points", "&Place Points",
											Qt.CTRL+Qt.Key_P, self.tools,
											"placePoints", 1)
		self.toolDeletePointAction = QAction("Delete Points", "&Delete Points",
											 Qt.CTRL+Qt.Key_D, self.tools,
											 "deletePoints", 1)
		QObject.connect(self.tools, SIGNAL("selected(QAction *)"),
						self.setTool)
		
		self.fileMenu = QPopupMenu(self)
		self.toolsMenu = QPopupMenu(self)
		self.menuBar().insertItem("&File", self.fileMenu)
		self.menuBar().insertItem("&Tools", self.toolsMenu)
		self.fileQuitAction.addTo(self.fileMenu)
		self.tools.addTo(self.toolsMenu)

	def setTool(self, action):
		"""Sets the current mode in View."""
		if action == self.toolPlacePointAction:
			self.view.mode=self.view.modes['PLACE_POINTS']
		elif action == self.toolDeletePointAction:
			self.view.mode=self.view.modes['DELETE_POINTS']


class View(QCanvasView):

	def __init__(self, *args):
		QCanvasView.__init__(self, *args)
		self.modes={'PLACE_POINTS':1, 'DELETE_POINTS':4}
		self.mode=0
		self.moving=None
		self.selectedPoints=[]

	def contentsMousePressEvent(self, ev):
		if self.mode==self.modes['PLACE_POINTS']:
			self.canvas().addPoint(ev.pos())
		if self.mode==self.modes['DELETE_POINTS']:
			list = self.canvas().collisions(ev.pos())
			for x in list:
				if x.rtti() == self.canvas().rttis['point']:
					self.canvas().deletePoint(x)
					break


class Canvas(QCanvas):

	def __init__(self, *args):
		QCanvas.__init__(self, *args)
		self.rttis = {'point':1001}
		self.items=[]
		self.setDoubleBuffering(1)

	def addPoint(self, position):
		p=Point(self)
		p.moveTo(position)
		p.show()
		self.items.append(p)
		self.update()

	def deletePoint(self, point):
		print self.allItems(), '\n', self.items
		point.setCanvas(None)
		self.items.remove(point)
		self.update()
		print self.allItems(), '\n', self.items

def main(args):
	app = QApplication(args)
	win = MainWindow()
	win.setGeometry(0,0,100,100)
	win.show()
	QObject.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
	app.exec_loop()

if __name__=="__main__":
	main(sys.argv)
