Esempio n. 1
0
 def testMargins(self):
     """ test rendering an annotation with margins"""
     a = QgsHtmlAnnotation()
     a.fillSymbol().symbolLayer(0).setStrokeColor(QColor(0, 0, 0))
     a.setFrameSize(QSizeF(400, 250))
     a.setHasFixedMapPosition(False)
     a.setContentsMargin(QgsMargins(15, 10, 30, 20))
     html = TEST_DATA_DIR + "/test_html.html"
     a.setSourceFile(html)
     im = self.renderAnnotation(a, QPointF(20, 30))
     self.assertTrue(
         self.imageCheck('annotation_margins', 'annotation_margins', im))
Esempio n. 2
0
 def testHtmlAnnotationInLayout(self):
     """ test rendering a svg annotation"""
     a = QgsHtmlAnnotation()
     a.fillSymbol().symbolLayer(0).setStrokeColor(QColor(0, 0, 0))
     a.markerSymbol().symbolLayer(0).setStrokeColor(QColor(0, 0, 0))
     a.setFrameSizeMm(QSizeF(400 / 3.7795275, 200 / 3.7795275))
     a.setFrameOffsetFromReferencePointMm(
         QPointF(70 / 3.7795275, 90 / 3.7795275))
     html = TEST_DATA_DIR + "/test_html.html"
     a.setSourceFile(html)
     self.assertTrue(
         self.renderAnnotationInLayout('html_annotation_in_layout', a))
Esempio n. 3
0
    def testHtmlAnnotation(self):
        """ test rendering a html annotation"""
        a = QgsHtmlAnnotation()
        a.setFrameSize(QSizeF(400, 250))
        a.setFrameOffsetFromReferencePoint(QPointF(70, 90))
        html = TEST_DATA_DIR + "/test_html.html"
        a.setSourceFile(html)
        im = self.renderAnnotation(a, QPointF(20, 30))
        self.assertTrue(self.imageCheck('html_annotation', 'html_annotation', im))

        # check clone
        clone = a.clone()
        im = self.renderAnnotation(a, QPointF(20, 30))
        self.assertTrue(self.imageCheck('html_annotation', 'html_annotation', im))
Esempio n. 4
0
 def testHtmlAnnotationSetHtmlSource(self):
     """ test rendering html annotation where the html is set directly (not from file)"""
     a = QgsHtmlAnnotation()
     a.fillSymbol().symbolLayer(0).setStrokeColor(QColor(0, 0, 0))
     a.markerSymbol().symbolLayer(0).setStrokeColor(QColor(0, 0, 0))
     a.setFrameSizeMm(QSizeF(400 / 3.7795275, 250 / 3.7795275))
     a.setFrameOffsetFromReferencePointMm(
         QPointF(70 / 3.7795275, 90 / 3.7795275))
     htmlFile = open(TEST_DATA_DIR + "/test_html.html")
     htmlText = htmlFile.read()
     a.setHtmlSource(htmlText)
     im = self.renderAnnotation(a, QPointF(20, 30))
     self.assertTrue(
         self.imageCheck('html_annotation_html_source', 'html_annotation',
                         im))
Esempio n. 5
0
    def testHtmlAnnotation(self):
        """ test rendering a html annotation"""
        a = QgsHtmlAnnotation()
        a.fillSymbol().symbolLayer(0).setStrokeColor(QColor(0, 0, 0))
        a.markerSymbol().symbolLayer(0).setStrokeColor(QColor(0, 0, 0))
        a.setFrameSizeMm(QSizeF(400 / 3.7795275, 250 / 3.7795275))
        a.setFrameOffsetFromReferencePointMm(QPointF(70 / 3.7795275, 90 / 3.7795275))
        html = TEST_DATA_DIR + "/test_html.html"
        a.setSourceFile(html)
        im = self.renderAnnotation(a, QPointF(20, 30))
        self.assertTrue(self.imageCheck('html_annotation', 'html_annotation', im))

        # check clone
        clone = a.clone()
        im = self.renderAnnotation(clone, QPointF(20, 30))
        self.assertTrue(self.imageCheck('html_annotation', 'html_annotation', im))
Esempio n. 6
0
    def testHtmlAnnotationWithFeature(self):
        """ test rendering a html annotation with a feature"""
        layer = QgsVectorLayer("Point?crs=EPSG:3111&field=station:string&field=suburb:string",
                               'test', "memory")

        a = QgsHtmlAnnotation()
        a.setFrameSize(QSizeF(400, 250))
        a.setFrameOffsetFromReferencePoint(QPointF(70, 90))
        a.setMapLayer(layer)
        html = TEST_DATA_DIR + "/test_html_feature.html"
        a.setSourceFile(html)
        im = self.renderAnnotation(a, QPointF(20, 30))
        self.assertTrue(self.imageCheck('html_nofeature', 'html_nofeature', im))
        f = QgsFeature(layer.fields())
        f.setValid(True)
        f.setAttributes(['hurstbridge', 'somewhere'])
        a.setAssociatedFeature(f)
        im = self.renderAnnotation(a, QPointF(20, 30))
        self.assertTrue(self.imageCheck('html_feature', 'html_feature', im))
Esempio n. 7
0
    def canvasReleaseEvent(self, mouseEvent):
        self.currentMoveAction = QgsMapCanvasAnnotationItem.NoAction
        self.setCursor(self.cursor)

        results = self.identify(mouseEvent.x(), mouseEvent.y(),
                                self.selectionMode, self.VectorLayer)
        for r in results:
            # TODO: filter out features in layers that aren't relevant
            if False:
                continue

            layer = r.mLayer
            feature = r.mFeature

            a = QgsHtmlAnnotation()
            a.setMapLayer(layer)
            html = os.path.join(os.path.dirname(__file__), 'html',
                                'current_predictions_annotation.html')
            a.setSourceFile(html)
            a.setAssociatedFeature(feature)

            # TODO: this size and offset are wack. Can we dynamically calculate from the content somehow?
            a.setFrameSize(QSizeF(270, 160))
            a.setFrameOffsetFromReferencePoint(QPointF(-300, -200))
            a.setMapPosition(feature.geometry().asPoint())
            a.setMapPositionCrs(QgsCoordinateReferenceSystem(layer.crs()))

            # disable its symbol
            for symbol in a.markerSymbol().symbolLayers():
                symbol.setEnabled(False)

            QgsProject.instance().annotationManager().addAnnotation(a)

            # select the just-created annotation to make it easy to move or resize
            item = self.itemForAnnotation(a)
            if item:
                self.canvas.scene().clearSelection()
                item.setSelected(True)
            break