File: web_widget.py

package info (click to toggle)
linuxcnc 1%3A2.9.4-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 282,780 kB
  • sloc: python: 201,110; ansic: 106,370; cpp: 99,219; tcl: 16,054; xml: 10,617; sh: 10,258; makefile: 1,251; javascript: 138; sql: 72; asm: 15
file content (47 lines) | stat: -rw-r--r-- 1,488 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os

from PyQt5.QtCore import QUrl, QFile, QUrl
from PyQt5.QtWidgets import (QWidget,QVBoxLayout,QLabel)
from qtvcp.core import Path

# Set up logging
from qtvcp import logger
LOG = logger.getLogger(__name__)
PATH = Path()

good = True
try:
    from PyQt5.QtWebEngineWidgets import QWebEngineView as WebBase
except:
    try:
        from PyQt5.QtWebKitWidgets import QWebView as WebBase
    except:
        LOG.warning('WebWidget - Is python3-pyqt-QtWebEngine installed?')
        # fail safe - mostly for designer
        # PyQt5.QtWebEngineWidgets must be loaded before QApplication
        # which doesn't happen in designer. Also screen won't immediately 
        # crash if both libraries are not missing
        WebBase = QWidget
        good = False

class WebWidget(WebBase):
    def __init__(self, parent=None):
        super(WebWidget, self).__init__(parent)

        # bad imports - give a clue
        if not good:
            vbox = QVBoxLayout(self)
            vbox.addStretch(1)
            mess = QLabel('WebWidget Import failed')
            vbox.addWidget(mess)

    # load a HTML file, but try to fix the image path
    def loadFile(self, path):
        file = QFile(path)
        file.open(QFile.ReadOnly)
        txt = file.readAll()
        txt = str(txt, encoding='utf8')
        # fix sample html image path
        if 'IMAGEDIR/' in txt:
            txt = txt.replace('IMAGEDIR/','')
        super().setHtml(txt,QUrl.fromLocalFile(str(PATH.IMAGEDIR+'/')))