Esempio n. 1
0
 def logMessage(self, message):
     # Show message in status bar
     doc = qt.QTextDocument()
     doc.setHtml(message)
     slicer.util.showStatusMessage(doc.toPlainText(), 3000)
     # Show message in log window at the bottom of the module widget
     self.log.insertHtml(message)
     self.log.insertPlainText('\n')
     self.log.ensureCursorVisible()
     self.log.repaint()
     slicer.app.processEvents(qt.QEventLoop.ExcludeUserInputEvents)
Esempio n. 2
0
 def logMessage(self, message, logLevel=logging.INFO):
   # Set text color based on log level
   if logLevel >= logging.ERROR:
     message = '<font color="red">' + message + '</font>'
   elif logLevel >= logging.WARNING:
     message = '<font color="orange">' + message + '</font>'
   # Show message in status bar
   doc = qt.QTextDocument()
   doc.setHtml(message)
   slicer.util.showStatusMessage(doc.toPlainText(),3000)
   # Show message in log window at the bottom of the module widget
   self.log.insertHtml(message)
   self.log.insertPlainText('\n')
   self.log.ensureCursorVisible()
   self.log.repaint()
   logging.log(logLevel, message)
   slicer.app.processEvents(qt.QEventLoop.ExcludeUserInputEvents)
Esempio n. 3
0
    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