Пример #1
0
def map_extent():
    composers = iface.activeComposers()
    composer = composers[0]
    composition = composer.composition()
    map = composition.getComposerMapById(0)

    return map.currentMapExtent()
Пример #2
0
    def geometriePDF(self, titre, taillePlan):
        # Afficher un assistant de saisie pour le choix du composeur
        dlgConfigComposers = DICTDialogComposer(taillePlan)
        dlgConfigComposers.show()
        result = dlgConfigComposers.exec_()

        idx_plan = []

        if result:
            idx_plan = dlgConfigComposers.listComposers.selectedItems()
            # id_plan = dlgConfigComposers.listComposers.currentRow()
        # Sortie du plan en PDF
        composers = iface.activeComposers()

        out = []
        if len(idx_plan) > 0:

            for r in idx_plan:
                id_plan = dlgConfigComposers.listComposers.row(r)
                c = composers[id_plan].composition()

                # À configurer
                c_map_lists = c.composerMapItems()
                if (len(c_map_lists) == 0):
                    return out
                else:
                    c_map = c_map_lists[0]

                if c_map:
                    c_map.zoomToExtent(self._geomBB)

                    # A faire correctement
                    s = c_map.scale()
                    scal = self.__findScale(s)
                    iface.mapCanvas().zoomScale(scal)
                    c_map.setNewScale(scal)
                    c_map.updateCachedImage()
                    c_map.renderModeUpdateCachedImage()

                    # Sortie
                    out_dir = QSettings().value("/DICT/configRep")
                    if QDir(out_dir).exists() is False or out_dir is None:
                        out_dir = str(QDir.homePath())

                    pdf = out_dir + "/" + \
                        QSettings().value("/DICT/prefPlan", u"") + titre + \
                        QSettings().value("/DICT/sufPlan", u"") + \
                        str(id_plan) + ".pdf"

                    c.exportAsPDF(pdf)
                    out.append(pdf)

        return out
def map_bounds(composer_title, map_id):
 """
 Returns a rectangle with the bounds of a map
 from a specific composer
 """
 composers = iface.activeComposers()
 for composer_view in composers:
  composer_window = composer_view.composerWindow()
  window_title = composer_window.windowTitle()
  if window_title == composer_title:
   composition = composer_view.composition()
   map = composition.getComposerMapById(map_id)
   if map:
    extent = map.currentMapExtent()
    break
 else:
  extent = None
 
 return extent
    def writePrintFiles(self, appdef, folder, app, progress):
        progress.setText("Writing print layout files")
        progress.setProgress(0)
        printFolder = os.path.join(folder, "resources", "print")
        if not QDir(printFolder).exists():
            QDir().mkpath(printFolder)
        dpis = [72, 150, 300]
        layoutDefs = []
        def getBasicInfo(item):
            coords = {}
            pos = item.pos()
            coords["x"] = pos.x()
            coords["y"] = pos.y()
            rect = item.rect()
            coords["width"] = rect.width()
            coords["height"] = rect.height()
            coords["id"] = str(uuid.uuid4())
            return coords
        composers = iface.activeComposers()
        for i, composer in enumerate(composers):
            name = composer.composerWindow().windowTitle()
            layoutSafeName = safeName(name)
            layoutDef = {}
            composition = composer.composition()
            img = composition.printPageAsRaster(0)
            img = img.scaledToHeight(100, Qt.SmoothTransformation)
            img.save(os.path.join(printFolder, "%s_thumbnail.png" % layoutSafeName))
            layoutDef["width"] = composition.paperWidth()
            layoutDef["height"] = composition.paperHeight()
            elements = []
            layoutDef["thumbnail"] = "%s_thumbnail.png" % layoutSafeName
            layoutDef["name"] = name
            layoutDef["elements"] = elements
            for item in composition.items():
                element = None
                if isinstance(item, (QgsComposerLegend, QgsComposerShape, QgsComposerScaleBar, QgsComposerArrow)):
                    element = getBasicInfo(item)
                    for dpi in dpis:
                        dpmm = dpi / 25.4
                        s = QSize(item.rect().width() * dpmm, item.rect().height() * dpmm)
                        img = QImage(s, QImage.Format_ARGB32_Premultiplied)
                        img.fill(Qt.transparent)
                        painter = QPainter(img)
                        painter.scale(dpmm, dpmm)
                        item.paint(painter, None, None)
                        painter.end()
                        img.save(os.path.join(printFolder, "%s_%s_%s.png" %
                                (layoutSafeName, element["id"], str(dpi))))
                elif isinstance(item, QgsComposerLabel):
                    element = getBasicInfo(item)
                    element["name"] = item.text()
                    element["size"] = item.font().pointSize()
                    element["font"] = item.font().rawName()
                elif isinstance(item, QgsComposerMap):
                    element = getBasicInfo(item)
                    grid = item.grid()
                    if grid is not None:
                        element["grid"] = {}
                        element["grid"]["intervalX"] = grid.intervalX()
                        element["grid"]["intervalY"] = grid.intervalY()
                        element["grid"]["crs"] = grid.crs().authid()
                        element["grid"]["annotationEnabled"] = grid.annotationEnabled()
                elif isinstance(item, QgsComposerPicture):
                    filename = os.path.basename(item.picturePath())
                    if os.path.exists(filename):
                        element = getBasicInfo(item)
                        shutil.copy(item.pictureFile(), os.path.join(printFolder, filename))
                        element["file"] = filename
                if element is not None:
                    element["type"] = item.__class__.__name__[11:].lower()
                    elements.append(element)

            layoutDefs.append(layoutDef)
            progress.setProgress(int((i+1)*100.0/len(composers)))

        app.variables.append("var printLayouts = %s;" % json.dumps(layoutDefs))
Пример #5
0
    def writePrintFiles(self, appdef, folder, app, progress):
        progress.setText("Writing print layout files")
        progress.setProgress(0)
        printFolder = os.path.join(folder, "resources", "print")
        if not QDir(printFolder).exists():
            QDir().mkpath(printFolder)
        dpis = [72, 150, 300]
        layoutDefs = []
        def getBasicInfo(item):
            coords = {}
            pos = item.pos()
            coords["x"] = pos.x()
            coords["y"] = pos.y()
            rect = item.rect()
            coords["width"] = rect.width()
            coords["height"] = rect.height()
            coords["id"] = str(uuid.uuid4())
            return coords
        composers = iface.activeComposers()
        for i, composer in enumerate(composers):
            name = composer.composerWindow().windowTitle()
            layoutSafeName = safeName(name)
            layoutDef = {}
            composition = composer.composition()
            img = composition.printPageAsRaster(0)
            img = img.scaledToHeight(100, Qt.SmoothTransformation)
            img.save(os.path.join(printFolder, "%s_thumbnail.png" % layoutSafeName))
            layoutDef["width"] = composition.paperWidth()
            layoutDef["height"] = composition.paperHeight()
            elements = []
            layoutDef["thumbnail"] = "%s_thumbnail.png" % layoutSafeName
            layoutDef["name"] = name
            layoutDef["elements"] = elements
            for item in composition.items():
                element = None
                if isinstance(item, (QgsComposerLegend, QgsComposerShape, QgsComposerScaleBar, QgsComposerArrow)):
                    element = getBasicInfo(item)
                    for dpi in dpis:
                        dpmm = dpi / 25.4
                        s = QSize(item.rect().width() * dpmm, item.rect().height() * dpmm)
                        img = QImage(s, QImage.Format_ARGB32_Premultiplied)
                        img.fill(Qt.transparent)
                        painter = QPainter(img)
                        painter.scale(dpmm, dpmm)
                        item.paint(painter, None, None)
                        painter.end()
                        img.save(os.path.join(printFolder, "%s_%s_%s.png" %
                                (layoutSafeName, element["id"], str(dpi))))
                elif isinstance(item, QgsComposerLabel):
                    element = getBasicInfo(item)
                    element["name"] = item.text()
                    element["size"] = item.font().pointSize()
                    element["font"] = item.font().rawName()
                elif isinstance(item, QgsComposerMap):
                    element = getBasicInfo(item)
                    grid = item.grid()
                    if grid is not None:
                        element["grid"] = {}
                        element["grid"]["intervalX"] = grid.intervalX()
                        element["grid"]["intervalY"] = grid.intervalY()
                        element["grid"]["crs"] = grid.crs().authid()
                        element["grid"]["annotationEnabled"] = grid.annotationEnabled()
                elif isinstance(item, QgsComposerPicture):
                    filename = os.path.basename(item.picturePath())
                    if os.path.exists(filename):
                        element = getBasicInfo(item)
                        shutil.copy(item.pictureFile(), os.path.join(printFolder, filename))
                        element["file"] = filename
                if element is not None:
                    element["type"] = item.__class__.__name__[11:].lower()
                    elements.append(element)

            layoutDefs.append(layoutDef)
            progress.setProgress(int((i+1)*100.0/len(composers)))

        app.variables.append("var printLayouts = %s;" % json.dumps(layoutDefs))
Пример #6
0
 def checkProblems(self, appdef, problems, forPreview):
     composers = iface.activeComposers()
     if len(composers) == 0:
         problems.append("Print widget has been added to the web app, but no print composer is defined in the current project")
     if forPreview:
         problems.append("Print widget might not work correctly in preview mode.")