Example #1
0
    def stdHtml(
        self,
        body: str,
        css: Optional[list[str]] = None,
        js: Optional[list[str]] = None,
        head: str = "",
        context: Optional[Any] = None,
        default_css: bool = True,
    ) -> None:
        css = (["css/webview.css"]
               if default_css else []) + ([] if css is None else css)
        web_content = WebContent(
            body=body,
            head=head,
            js=["js/webview.js"] +
            (["js/vendor/jquery.min.js"] if js is None else js),
            css=css,
        )

        gui_hooks.webview_will_set_content(web_content, context)

        csstxt = ""
        if "css/webview.css" in css:
            # we want our dynamic styling to override the defaults in
            # css/webview.css, but come before user-provided stylesheets so that
            # they can override us if necessary
            web_content.css.remove("css/webview.css")
            csstxt = self.bundledCSS("css/webview.css")
            csstxt += f"<style>{self.standard_css()}</style>"

        csstxt += "\n".join(
            self.bundledCSS(fname) for fname in web_content.css)
        jstxt = "\n".join(
            self.bundledScript(fname) for fname in web_content.js)

        from aqt import mw

        head = mw.baseHTML() + csstxt + jstxt + web_content.head
        body_class = theme_manager.body_class()

        if theme_manager.night_mode:
            doc_class = "night-mode"
        else:
            doc_class = ""

        html = f"""
<!doctype html>
<html class="{doc_class}">
<head>
    <title>{self.title}</title>
{head}
</head>

<body class="{body_class}">{web_content.body}</body>
</html>"""
        # print(html)
        self.setHtml(html)
Example #2
0
    def stdHtml(
        self,
        body: str,
        css: Optional[List[str]] = None,
        js: Optional[List[str]] = None,
        head: str = "",
        context: Optional[Any] = None,
    ):

        web_content = WebContent(
            body=body,
            head=head,
            js=["webview.js"] + (["jquery.js"] if js is None else js),
            css=["webview.css"] + ([] if css is None else css),
        )

        gui_hooks.webview_will_set_content(web_content, context)

        palette = self.style().standardPalette()
        color_hl = palette.color(QPalette.Highlight).name()

        if isWin:
            # T: include a font for your language on Windows, eg: "Segoe UI", "MS Mincho"
            family = _('"Segoe UI"')
            widgetspec = "button { font-family:%s; }" % family
            widgetspec += "\n:focus { outline: 1px solid %s; }" % color_hl
            fontspec = "font-size:12px;font-family:%s;" % family
        elif isMac:
            family = "Helvetica"
            fontspec = 'font-size:15px;font-family:"%s";' % family
            widgetspec = """
button { -webkit-appearance: none; background: #fff; border: 1px solid #ccc;
border-radius:5px; font-family: Helvetica }"""
        else:
            family = self.font().family()
            color_hl_txt = palette.color(QPalette.HighlightedText).name()
            color_btn = palette.color(QPalette.Button).name()
            fontspec = 'font-size:14px;font-family:"%s";' % family
            widgetspec = """
/* Buttons */
button{ 
        background-color: %(color_btn)s;
        font-family:"%(family)s"; }
button:focus{ border-color: %(color_hl)s }
button:active, button:active:hover { background-color: %(color_hl)s; color: %(color_hl_txt)s;}
/* Input field focus outline */
textarea:focus, input:focus, input[type]:focus, .uneditable-input:focus,
div[contenteditable="true"]:focus {   
    outline: 0 none;
    border-color: %(color_hl)s;
}""" % {
                "family": family,
                "color_btn": color_btn,
                "color_hl": color_hl,
                "color_hl_txt": color_hl_txt,
            }

        csstxt = "\n".join(self.bundledCSS(fname) for fname in web_content.css)
        jstxt = "\n".join(
            self.bundledScript(fname) for fname in web_content.js)

        from aqt import mw

        head = mw.baseHTML() + csstxt + jstxt + web_content.head

        body_class = theme_manager.body_class()

        html = """
<!doctype html>
<html><head>
<title>{}</title>

<style>
body {{ zoom: {}; background: {}; {} }}
{}
</style>
  
{}
</head>

<body class="{}">{}</body>
</html>""".format(
            self.title,
            self.zoomFactor(),
            self._getWindowColor().name(),
            fontspec,
            widgetspec,
            head,
            body_class,
            web_content.body,
        )
        # print(html)
        self.setHtml(html)