Exemplo n.º 1
0
class MarkerLabel(QgsMapCanvasItem):
    '''
    Visual representation of the markers/mobiles name
    '''
    def __init__(self, canvas, params={}):
        '''
        Constructor
        :param iface: An interface instance that will be passed to this class
            which provides the hook by which you can manipulate the QGIS
            application at run time.
        :type iface: QgsInterface
        :param params: A dictionary defining all the properties of the position marker
        :type params: dictionary
        '''
        self.canvas = canvas
        self.label = params.get('Name', 'Item')
        self.LABEL_DISTANCE = 50
        self.labelDistance = params.get('labelDistance', self.LABEL_DISTANCE)
        self.labelRect = QRectF(self.canvas.fontMetrics().boundingRect(
            self.label)).translated(
                QPointF(self.labelDistance, -self.labelDistance))
        self.labelRect.setBottomLeft(QPointF(0, 0))
        self.color = self.getColor(params.get('color', 'black'))
        self.position = None
        super(MarkerLabel, self).__init__(canvas)

    def boundingRect(self):
        return self.labelRect

    def paint(self, painter, xxx, xxx2):
        if not self.position:
            return
        pen = QPen(self.color)
        pen.setWidth(1)
        painter.setPen(pen)
        painter.drawLine(QPointF(0, 0),
                         QPointF(self.labelDistance, -self.labelDistance / 2))
        painter.drawText(
            QPointF(self.labelDistance + 2, -self.labelDistance / 2 + 2),
            self.label)

    def setMapPosition(self, pos):
        if self.position != pos:
            self.position = pos
            self.setPos(self.toCanvasCoordinates(self.position))
            self.update()

    def resetPoition(self):
        self.position = None

    def updatePosition(self):
        if self.position:
            self.setPos(self.toCanvasCoordinates(self.position))

    def getColor(self, value):
        try:
            return QColor.fromRgba(int(value))
        except ValueError:
            return QColor(value)