[Qgis-developer] QgsMapCanvasItem and mouse events -- Python

Florian El Ahdab felahdab at gmail.com
Sun Nov 1 05:00:52 EST 2009


Hi.

It seems my previous mail didn't make it to the list with the text... weird.

Let me try again.

I am trying to subclass QgsMapCanvasItem so as do display items on top
of the maps.

I would like those items to react to mouse events.

I have implemented the handles for the mouse events as the Qt API
suggests, but my items do not seem to receive any events. The
isUnderMouse method returns coherent results when run from the python
console.

The source file for my geographicitems is included.

Thanks for your help.
-------------- next part --------------
import random
import threading
import time
import socket
from qgis import *
from qgis.core import *
from qgis.gui import *
import PyQt4.QtGui as QtGui
import PyQt4.QtCore as QtCore
		
class GeographicItem(QgsMapCanvasItem):
		
	def __init__(self,iface):
		self.iface=iface
		self.canvas = iface.mapCanvas()
		QgsMapCanvasItem.__init__(self,self.canvas)
		self.position=QgsPoint()
		self.identification=0
		self.tn=0
		self.override = False
		self.overrideColor=QtGui.QColor(0,255,255)
		
		self.setHandlesChildEvents(True)
		self.GraphicsItemFlags=QtGui.QGraphicsItem.ItemIsSelectable | QtGui.QGraphicsItem.ItemIsFocusable
		
		self.texte = QtGui.QGraphicsTextItem(QtCore.QString("Name"),self,self.canvas.scene())
		self.texte.setPos(-10,10)
		self.rec=QtGui.QGraphicsRectItem(0,0,10,10,self,self.canvas.scene())
		QtCore.QObject.connect(self.canvas,QtCore.SIGNAL( "renderComplete( QPainter * )" ),self.renderComplete)
	
	def acceptedMouseEvents(self):
		result=QtCore.Qt.LeftButton | QtCore.Qt.RightButton | QtCore.Qt.MidButton
		return result
	
	#def acceptsHoverEvents(self):
	#	return True
	
	def mousePressEvent(self, event):
		self.setTN(123)
	
	def mouseReleaseEvent(self, event):
		self.setTN(123)
		
	#def keyPressEvent(self,event):
	#	self.setTN(123)

	#def hoverEnterEvent(self,event):
	#	self.override= True
	#	self.updateStyles()
		
	#def hoverExitEvent(self,event):
	#	self.override= False
	#	self.updateStyles()

	def boundingRect(self):
		return QtCore.QRectF(0 , 0 , 10 , 10 );
	
	def getTN(self):
		return self.tn
		
	def setTN(self,tn):
		self.tn=tn
		self.canvas.scene().removeItem(self.texte)
		del(self.texte)
		self.texte = QtGui.QGraphicsTextItem(QtCore.QString(oct(self.tn)),self,self.canvas.scene())
		self.updateStyles()
	
	def setIdentification(self, identification):
		self.identification=identification
		self.updateStyles()
	
	def setPositionGeo(self,position):
		self.position=position
		self.updateStyles()
		self.setPos(self.toCanvasCoordinates(self.position))
		self.update()
	
	def updateStyles(self):
		pen=self.rec.pen()
		if not self.override:
			if self.identification < 5:
				color=QtGui.QColor(255,0,0)
			else:
				color=QtGui.QColor(0,0,255)
		else:
			color=self.overrideColor
		pen.setColor(color)
		self.texte.setDefaultTextColor(color)
		self.rec.setPen(pen)
		self.texte.update()
		self.update()
	
	def paint(self,painter,option,widget):
		return
		
	def renderComplete(self,painter):
		self.updateStyles()
		self.setPos(self.toCanvasCoordinates(self.position))
		self.update()
		
	def removeFromCanvas(self):
		self.canvas.scene().removeItem(self)

	def addToCanvas(self):
		self.canvas.scene().addItem(self)


More information about the Qgis-developer mailing list