def printReport(self, currentValuesDict, callback): """ Generate a html report file and print it :param currentValuesDict: dictionary of GUI values """ if not self.isCurrentReportSaved: # Save the report first self.saveReport(currentValuesDict) # Generate the snapshots of the images self.__printSnapshots__() self.callback = callback # Generate the html code html = self.__generateHtml__(currentValuesDict) # Write the html to a temp file p = os.path.join(self.getCurrentDataFolder(), "report.html") with open(p, "w+b") as f: f.write(html) self.webView = qt.QWebView() self.webView.settings().setAttribute( qt.QWebSettings.DeveloperExtrasEnabled, True) self.webView.connect('loadFinished(bool)', self.__webViewFormLoadedCallback__) self.webView.show() u = qt.QUrl(p) self.webView.setUrl(u)
def webViewFormTest(self): """Just as a demo, load a google search in a web view and use the qt api to fill in a search term""" self.webView = qt.QWebView() self.webView.settings().setAttribute( qt.QWebSettings.DeveloperExtrasEnabled, True) self.webView.connect('loadFinished(bool)', self.webViewFormLoadedCallback) self.webView.show() u = qt.QUrl('https://www.google.com') self.webView.setUrl(u)
def printPdf( self, htmlTemplatePath, values, callbackFunction, pdfOutputPath=None, imagesFileList=None, tempHtmlFolder=None, ): """ Print a pdf file with the html stored in htmlPath and the specified values :param htmlTemplatePath: path to the html file that contains the template :param values: dictionary of values that will be used to build the final html :param callbackFunction: function that will be called when the process has finished (it is an asynchronous process) :param pdfOutputPath: path to the pdf file that will be created (if none, it will be saved in a temp file) :param imagesFileList: list of full paths to images that may be needed to generate the report :param tempHtmlFolder: folder where all the intermediate files will be stored. If none, a temporary folder will be used """ if tempHtmlFolder is None: tempHtmlFolder = tempfile.mkdtemp() self.__pdfOutputPath__ = pdfOutputPath if pdfOutputPath is not None else os.path.join( tempHtmlFolder, "report.pdf") self.__callbackFunction__ = callbackFunction self.webView = qt.QWebView() self.webView.settings().setAttribute( qt.QWebSettings.DeveloperExtrasEnabled, True) self.webView.connect('loadFinished(bool)', self.__webViewFormLoadedCallback__) self.webView.show() # Generate the Html with open(htmlTemplatePath, "r+b") as f: html = f.read() for key, value in values.iteritems(): html = html.replace(key, value) # Save the file in the temporary folder htmlPath = os.path.join(tempHtmlFolder, "temp__.html") with open(htmlPath, "w") as f: f.write(html) logging.debug("Html generated in {}".format(htmlPath)) # If we need images, copy them to the temporary folder if imagesFileList: for im in imagesFileList: fileName = os.path.basename(im) shutil.copy(im, os.path.join(tempHtmlFolder, fileName)) # Assign the html file to the viewer u = qt.QUrl(htmlPath) self.webView.setUrl(u)
def __init__(self): self.catalogMainUrl = qt.QUrl('http://perk-software.cs.queensu.ca/plus/doc/nightly/modelcatalog/') self.catalogName = 'Plus toolkit 3D model catalog' self.browser = qt.QWebView() # Change QWebView such that link clicks are not automatically acted on # When links are clicked, run the handleClick() function self.browser.page().setLinkDelegationPolicy(qt.QWebPage.DelegateAllLinks) self.browser.linkClicked.connect(self.handleClick) self.downloadProgressBar = qt.QProgressDialog() self.downloadProgressBar.setLabelText('Downloading File')
def webViewTest(self): self.webView = qt.QWebView() html = """ <a href="reslicing">Run reslicing test</a> <p> <a href="chart">Run chart test</a> """ self.webView.setHtml(html) self.webView.settings().setAttribute( qt.QWebSettings.DeveloperExtrasEnabled, True) self.webView.page().setLinkDelegationPolicy( qt.QWebPage.DelegateAllLinks) self.webView.connect('linkClicked(QUrl)', self.webViewCallback) self.webView.show()
def printPdf( self, htmlTemplatePath, values, callbackFunction, pdfOutputPath=None, imagesFileList=None, tempHtmlFolder=None, ): """ Print a pdf file with the html stored in htmlPath and the specified values :param htmlTemplatePath: path to the html file that contains the template :param values: dictionary of values that will be used to build the final html :param callbackFunction: function that will be called when the process has finished (it is an asynchronous process) :param pdfOutputPath: path to the pdf file that will be created (if none, it will be saved in a temp file) :param imagesFileList: list of full paths to images that may be needed to generate the report :param tempHtmlFolder: folder where all the intermediate files will be stored. If none, a temporary folder will be used """ if tempHtmlFolder is None: tempHtmlFolder = tempfile.mkdtemp() self.__pdfOutputPath__ = pdfOutputPath if pdfOutputPath is not None else os.path.join( tempHtmlFolder, "report.pdf") self.__callbackFunction__ = callbackFunction # Generate the Html with open(htmlTemplatePath, "r") as f: html = f.read() for key, value in values.items(): html = html.replace(key, value) # If we need images, copy them to the temporary folder if imagesFileList: for im in imagesFileList: fileName = os.path.basename(im) shutil.copy(im, os.path.join(tempHtmlFolder, fileName)) if hasattr(qt, 'QWebView'): # Save the file in the temporary folder htmlPath = os.path.join(tempHtmlFolder, "temp__.html") with open(htmlPath, "w") as f: f.write(html) logging.debug("Html generated in {}".format(htmlPath)) # Create a web browser for rendering html self.webView = qt.QWebView() self.webView.settings().setAttribute( qt.QWebSettings.DeveloperExtrasEnabled, True) self.webView.connect('loadFinished(bool)', self.__webViewFormLoadedCallback__) u = qt.QUrl(htmlPath) self.webView.setUrl(u) self.webView.show() else: printer = qt.QPrinter(qt.QPrinter.PrinterResolution) printer.setOutputFormat(qt.QPrinter.PdfFormat) printer.setPaperSize(qt.QPrinter.A4) printer.setOutputFileName(self.__pdfOutputPath__) doc = qt.QTextDocument() doc.setHtml(html) doc.baseUrl = qt.QUrl.fromLocalFile(tempHtmlFolder + "/") doc.setPageSize(qt.QSizeF( printer.pageRect().size())) # hide the page number doc.print(printer) # Call the callback self.__callbackFunction__(self.__pdfOutputPath__) self.__callbackFunction__ = None