can add html content to the QgsLayoutItem

This commit is contained in:
nobohan 2021-07-13 16:02:32 +02:00
parent cb28240d74
commit 37f0f6f2cb

View File

@ -1,3 +1,8 @@
from qgis.PyQt.QtCore import (
QUrl,
QSize
)
from qgis.core import (
QgsLayoutItem,
QgsLayoutItemRegistry,
@ -6,41 +11,52 @@ from qgis.core import (
QgsMessageLog,
QgsGeometry
)
from qgis.PyQt.QtWebKitWidgets import QWebPage
ITEM_TYPE = QgsLayoutItemRegistry.PluginItem + 1338
class LoggingWebPage(QWebPage):
def __init__(self, parent=None):
super().__init__(parent)
def javaScriptConsoleMessage(self, message, lineNumber, source):
QgsMessageLog.logMessage('{}:{} {}'.format(source, lineNumber, message), 'plot-layout')
class PlotLayoutItem(QgsLayoutItem):
def __init__(self, layout):
super().__init__(layout)
self.web_page = LoggingWebPage(self)
self.web_page.mainFrame().setZoomFactor(30.0)
self.html_units_to_layout_units = self.calculate_html_units_to_layout_units()
def calculate_html_units_to_layout_units(self):
if not self.layout():
return 1
return 72
def draw(self, context):
#pass
#if not self.html_loaded:
base_url = QUrl.fromLocalFile(self.layout().project().absoluteFilePath())
self.web_page.setViewportSize(QSize(self.rect().width() * self.html_units_to_layout_units,
self.rect().height() * self.html_units_to_layout_units))
self.web_page.mainFrame().setHtml('<b>Hello</b>', base_url)
# almost a direct copy from QgsLayoutItemLabel!
painter = context.renderContext().painter()
painter.save()
def load_content(self):
return 'vjdnvjndjn'
# if not self.layout().renderContext().isPreviewRender():
# # this is NOT safe to do when rendering in the gui (i.e. a preview render), but for exports we have
# # to loop around until the HTML has fully loaded
# while not self.html_loaded:
# QCoreApplication.processEvents()
# # almost a direct copy from QgsLayoutItemLabel!
# painter = context.renderContext().painter()
# painter.save()
# # painter is scaled to dots, so scale back to layout units
# painter.scale(context.renderContext().scaleFactor() / self.html_units_to_layout_units,
# context.renderContext().scaleFactor() / self.html_units_to_layout_units)
# self.web_page.mainFrame().render(painter)
# painter.restore()
# painter is scaled to dots, so scale back to layout units
painter.scale(context.renderContext().scaleFactor() / self.html_units_to_layout_units,
context.renderContext().scaleFactor() / self.html_units_to_layout_units)
self.web_page.mainFrame().render(painter)
def type(self):
return ITEM_TYPE