def test_windowsDrawingArtifacts(self): """Test that windows rendering does not make artifacts""" # sometimes spurious lines are drawn on the layout LOGGER.info('Testing windowsDrawingArtifacts') myPath = unique_filename(prefix='artifacts', suffix='.pdf', dir=temp_dir('test')) myMap = Map(IFACE) setupPrinter(myPath) myMap.setupComposition() myImage = QtGui.QImage(10, 10, QtGui.QImage.Format_RGB32) myImage.setDotsPerMeterX(dpiToMeters(300)) myImage.setDotsPerMeterY(dpiToMeters(300)) #myImage.fill(QtGui.QColor(250, 250, 250)) # Look at the output, you will see antialiasing issues around some # of the boxes drawn... # myImage.fill(QtGui.QColor(200, 200, 200)) myImage.fill(200 + 200 * 256 + 200 * 256 * 256) myFilename = os.path.join(temp_dir(), 'greyBox') myImage.save(myFilename, 'PNG') for i in range(10, 190, 10): myPicture = QgsComposerPicture(myMap.composition) myPicture.setPictureFile(myFilename) if qgisVersion() >= 10800: # 1.8 or newer myPicture.setFrameEnabled(False) else: myPicture.setFrame(False) myPicture.setItemPosition(i, # x i, # y 10, # width 10) # height myMap.composition.addItem(myPicture) # Same drawing drawn directly as a pixmap myPixmapItem = myMap.composition.addPixmap( QtGui.QPixmap.fromImage(myImage)) myPixmapItem.setOffset(i, i + 20) # Same drawing using our drawImage Helper myWidthMM = 1 myMap.drawImage(myImage, myWidthMM, i, i + 40) myImagePath, _, _ = myMap.renderComposition() # when this test no longer matches our broken render hash # we know the issue is fixed myControlImages = ['windowsArtifacts.png'] myTolerance = 0 myFlag, myMessage = checkImages(myControlImages, myImagePath, myTolerance) myMessage += ('\nWe want these images to match, if they do not ' 'there may be rendering artifacts in windows.\n') assert myFlag, myMessage
def test_windowsDrawingArtifacts(self): """Test that windows rendering does not make artifacts""" # sometimes spurious lines are drawn on the layout LOGGER.info('Testing windowsDrawingArtifacts') myPath = unique_filename(prefix='artifacts', suffix='.pdf', dir=temp_dir('test')) myMap = Map(IFACE) setupPrinter(myPath) myMap.setupComposition() myImage = QtGui.QImage(10, 10, QtGui.QImage.Format_RGB32) myImage.setDotsPerMeterX(dpiToMeters(300)) myImage.setDotsPerMeterY(dpiToMeters(300)) #myImage.fill(QtGui.QColor(250, 250, 250)) # Look at the output, you will see antialiasing issues around some # of the boxes drawn... # myImage.fill(QtGui.QColor(200, 200, 200)) myImage.fill(200 + 200 * 256 + 200 * 256 * 256) myFilename = os.path.join(temp_dir(), 'greyBox') myImage.save(myFilename, 'PNG') for i in range(10, 190, 10): myPicture = QgsComposerPicture(myMap.composition) myPicture.setPictureFile(myFilename) if qgisVersion() >= 10800: # 1.8 or newer myPicture.setFrameEnabled(False) else: myPicture.setFrame(False) myPicture.setItemPosition(i, # x i, # y 10, # width 10) # height myMap.composition.addItem(myPicture) # Same drawing drawn directly as a pixmap myPixmapItem = myMap.composition.addPixmap( QtGui.QPixmap.fromImage(myImage)) myPixmapItem.setOffset(i, i + 20) # Same drawing using our drawImage Helper myWidthMM = 1 myMap.drawImage(myImage, myWidthMM, i, i + 40) myImagePath, _, _ = myMap.renderComposition() # when this test no longer matches our broken render hash # we know the issue is fixed myTolerance = 0 myFlag, myMessage = checkImages('windowsArtifacts.png', myImagePath, myTolerance) myMessage += ('\nWe want these images to match, if they do not ' 'there may be rendering artifacts in windows.\n') assert myFlag, myMessage
def printToPdf(self, theHtml, theFilename=None): """Render an html snippet into the printer, paginating as needed. Args: * theHtml: str A string containing an html snippet. It will have a header and footer appended to it in order to make it a valid html document. The header will also apply the bootstrap theme to the document. * theFilename: str String containing a pdf file path that the output will be written to. Returns: str: The file path of the output pdf (which is the same as the theFilename parameter if it was specified. Raises: None """ LOGGER.info('InaSAFE Map printToPdf called') if theFilename is None: myHtmlPdfPath = unique_filename(prefix='table', suffix='.pdf', dir=temp_dir('work')) else: # We need to cast to python string in case we receive a QString myHtmlPdfPath = str(theFilename) self.printer = setupPrinter(myHtmlPdfPath) self.loadAndWait(theHtmlSnippet=theHtml) self.webView.print_(self.printer) return myHtmlPdfPath
def renderTemplate(self, theTemplateFilePath, theOutputFilePath): """Load a QgsComposer map from a template and render it .. note:: THIS METHOD IS EXPERIMENTAL AND CURRENTLY NON FUNCTIONAL Args: theTemplateFilePath - path to the template that should be loaded. theOutputFilePath - path for the output pdf Returns: None Raises: None """ self.setupComposition() myResolution = self.composition.printResolution() self.printer = setupPrinter(theOutputFilePath, theResolution=myResolution) if self.composition: myFile = QtCore.QFile(theTemplateFilePath) myDocument = QtXml.QDomDocument() myDocument.setContent(myFile, False) # .. todo:: fix magic param myNodeList = myDocument.elementsByTagName("Composer") if myNodeList.size() > 0: myElement = myNodeList.at(0).toElement() self.composition.readXML(myElement, myDocument) self.printToPdf(theOutputFilePath)
def printToPdf(self, theFilename): """Generate the printout for our final map. Args: theFilename: str - optional path on the file system to which the pdf should be saved. If None, a generated file name will be used. Returns: str: file name of the output file (equivalent to theFilename if provided). Raises: None """ LOGGER.debug("InaSAFE Map printToPdf called") if theFilename is None: myMapPdfPath = unique_filename(prefix="report", suffix=".pdf", dir=temp_dir("work")) else: # We need to cast to python string in case we receive a QString myMapPdfPath = str(theFilename) self.composeMap() self.printer = setupPrinter(myMapPdfPath) _, myImage, myRectangle = self.renderComposition() myPainter = QtGui.QPainter(self.printer) myPainter.drawImage(myRectangle, myImage, myRectangle) myPainter.end() return myMapPdfPath
def renderTemplate(self, theTemplateFilePath, theOutputFilePath): """Load a QgsComposer map from a template and render it .. note:: THIS METHOD IS EXPERIMENTAL AND CURRENTLY NON FUNCTIONAL Args: theTemplateFilePath - path to the template that should be loaded. theOutputFilePath - path for the output pdf Returns: None Raises: None """ self.setupComposition() myResolution = self.composition.printResolution() self.printer = setupPrinter(theOutputFilePath, theResolution=myResolution) if self.composition: myFile = QtCore.QFile(theTemplateFilePath) myDocument = QtXml.QDomDocument() myDocument.setContent(myFile, False) # .. todo:: fix magic param myNodeList = myDocument.elementsByTagName('Composer') if myNodeList.size() > 0: myElement = myNodeList.at(0).toElement() self.composition.readXML(myElement, myDocument) self.printToPdf(theOutputFilePath)
def printToPdf(self, theFilename): """Generate the printout for our final map. Args: theFilename: str - optional path on the file system to which the pdf should be saved. If None, a generated file name will be used. Returns: str: file name of the output file (equivalent to theFilename if provided). Raises: None """ LOGGER.debug('InaSAFE Map printToPdf called') if theFilename is None: myMapPdfPath = unique_filename(prefix='report', suffix='.pdf', dir=temp_dir('work')) else: # We need to cast to python string in case we receive a QString myMapPdfPath = str(theFilename) self.composeMap() self.printer = setupPrinter(myMapPdfPath) _, myImage, myRectangle = self.renderComposition() myPainter = QtGui.QPainter(self.printer) myPainter.drawImage(myRectangle, myImage, myRectangle) myPainter.end() return myMapPdfPath