can add html content to the QgsLayoutItem
This commit is contained in:
parent
cb28240d74
commit
37f0f6f2cb
@ -1,3 +1,8 @@
|
|||||||
|
from qgis.PyQt.QtCore import (
|
||||||
|
QUrl,
|
||||||
|
QSize
|
||||||
|
)
|
||||||
|
|
||||||
from qgis.core import (
|
from qgis.core import (
|
||||||
QgsLayoutItem,
|
QgsLayoutItem,
|
||||||
QgsLayoutItemRegistry,
|
QgsLayoutItemRegistry,
|
||||||
@ -6,42 +11,53 @@ from qgis.core import (
|
|||||||
QgsMessageLog,
|
QgsMessageLog,
|
||||||
QgsGeometry
|
QgsGeometry
|
||||||
)
|
)
|
||||||
|
from qgis.PyQt.QtWebKitWidgets import QWebPage
|
||||||
|
|
||||||
ITEM_TYPE = QgsLayoutItemRegistry.PluginItem + 1338
|
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):
|
class PlotLayoutItem(QgsLayoutItem):
|
||||||
|
|
||||||
def __init__(self, layout):
|
def __init__(self, layout):
|
||||||
super().__init__(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):
|
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 = context.renderContext().painter()
|
||||||
painter.save()
|
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)
|
||||||
|
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
def type(self):
|
def type(self):
|
||||||
return ITEM_TYPE
|
return ITEM_TYPE
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user