Esempio n. 1
0
def main():
    # Query system colours.
    p = QPalette()
    textcolour = p.color(QPalette.Text)
    basecolour = p.color(QPalette.Base)

    # Inject background colour into the browser.
    def tree_colour_hook(self):
        p = self.form.tree.palette()
        p.setColor(QPalette.Base, basecolour)
        self.form.tree.setPalette(p)
    browser.Browser.setupTree = wrap(browser.Browser.setupTree, tree_colour_hook)

    # Change suspend and mark colours.
    coloursuspended = QColor()
    coloursuspended.setNamedColor(browser.COLOUR_SUSPENDED)
    colourmarked = QColor()
    colourmarked.setNamedColor(browser.COLOUR_MARKED)
    lightness_blacklist = [textcolour.lightness()]
    hue_blacklist = [basecolour.hue(), textcolour.hue()]
    lightness_preference = max(basecolour.lightness(), 40)
    for colour in [coloursuspended, colourmarked]:
        (h, s, l, a) = colour.getHsl()
        new_lightness = get_new_lightness(lightness_blacklist, lightness_preference)
        # print("Considering {0} with preference {2} choose lightness {1}\n".format(
        #     lightness_blacklist, new_lightness, lightness_preference))
        new_hue = get_new_hue(hue_blacklist, h)
        # print("Considering {0} with preference {2} choose hue {1}\n".format(
        #     hue_blacklist, new_hue, h))
        hue_blacklist.append(new_hue)
        colour.setHsl(new_hue, s, new_lightness, a)
    browser.COLOUR_SUSPENDED = coloursuspended.name()
    browser.COLOUR_MARKED = colourmarked.name()

    # Inject colouring into the web view.
    editor._html = re.sub(
        "(\\.fname\s*\\{)",
        "\\1 color: {0};".format(textcolour.name()),
        editor._html)
    # Fix the default text colour for type answer edit elements.
    reviewer.Reviewer._css = re.sub(
        "(#typeans\s*\\{)",
        "\\1 color: {0};".format(textcolour.name()),
        reviewer.Reviewer._css)
Esempio n. 2
0
def tooltip(msg, period=3000, parent=None):
    global _tooltipTimer, _tooltipLabel
    class CustomLabel(QLabel):
        silentlyClose = True
        def mousePressEvent(self, evt):
            evt.accept()
            self.hide()
    closeTooltip()
    aw = parent or aqt.mw.app.activeWindow() or aqt.mw
    lab = CustomLabel("""\
<table cellpadding=10>
<tr>
<td>%s</td>
</tr>
</table>""" % msg, aw)
    lab.setFrameStyle(QFrame.Panel)
    lab.setLineWidth(2)
    lab.setWindowFlags(Qt.ToolTip)
    p = QPalette()
    p.setColor(QPalette.Window, QColor("#feffc4"))
    p.setColor(QPalette.WindowText, QColor("#000000"))
    lab.setPalette(p)
    lab.move(
        aw.mapToGlobal(QPoint(0, -100 + aw.height())))
    lab.show()
    _tooltipTimer = aqt.mw.progress.timer(
        period, closeTooltip, False, requiresCollection=False)
    _tooltipLabel = lab
    def __init__(
        self,
        text: str,
        settings: NotificationSettings = NotificationSettings(),
        parent: Optional[QWidget] = None,
        **kwargs,
    ):
        super().__init__(text, parent=parent, **kwargs)
        self._settings = settings

        self.setFrameStyle(QFrame.Shape.Panel)
        self.setLineWidth(2)
        self.setWindowFlags(Qt.WindowType.ToolTip)
        self.setContentsMargins(10, 10, 10, 10)

        palette = QPalette()
        palette.setColor(QPalette.ColorRole.Window,
                         QColor(self._settings.bg_color))
        palette.setColor(QPalette.ColorRole.WindowText,
                         QColor(self._settings.fg_color))
        self.setPalette(palette)

        if parent and self._settings.focus_behavior != FocusBehavior.always_on_top:
            app: "AnkiApp" = QApplication.instance(
            )  # type: ignore[assignment]
            app.focusChanged.connect(self._on_app_focus_changed)
Esempio n. 4
0
    def _apply_palette(self, app: QApplication) -> None:
        if not self.night_mode:
            return

        if not self.macos_dark_mode():
            app.setStyle(QStyleFactory.create("fusion"))  # type: ignore

        palette = QPalette()

        text_fg = self.qcolor(colors.TEXT_FG)
        palette.setColor(QPalette.WindowText, text_fg)
        palette.setColor(QPalette.ToolTipText, text_fg)
        palette.setColor(QPalette.Text, text_fg)
        palette.setColor(QPalette.ButtonText, text_fg)

        hlbg = self.qcolor(colors.HIGHLIGHT_BG)
        hlbg.setAlpha(64)
        palette.setColor(QPalette.HighlightedText,
                         self.qcolor(colors.HIGHLIGHT_FG))
        palette.setColor(QPalette.Highlight, hlbg)

        window_bg = self.qcolor(colors.WINDOW_BG)
        palette.setColor(QPalette.Window, window_bg)
        palette.setColor(QPalette.AlternateBase, window_bg)

        palette.setColor(QPalette.Button, QColor("#454545"))

        frame_bg = self.qcolor(colors.FRAME_BG)
        palette.setColor(QPalette.Base, frame_bg)
        palette.setColor(QPalette.ToolTipBase, frame_bg)

        disabled_color = self.qcolor(colors.DISABLED)
        palette.setColor(QPalette.Disabled, QPalette.Text, disabled_color)
        palette.setColor(QPalette.Disabled, QPalette.ButtonText,
                         disabled_color)
        palette.setColor(QPalette.Disabled, QPalette.HighlightedText,
                         disabled_color)

        palette.setColor(QPalette.Link, self.qcolor(colors.LINK))

        palette.setColor(QPalette.BrightText, Qt.red)

        app.setPalette(palette)
Esempio n. 5
0
    def _apply_palette(self, app: QApplication) -> None:
        if not self.night_mode:
            return

        if not self.macos_dark_mode():
            app.setStyle(QStyleFactory.create("fusion"))  # type: ignore

        palette = QPalette()

        text_fg = self.qcolor("text-fg")
        palette.setColor(QPalette.WindowText, text_fg)
        palette.setColor(QPalette.ToolTipText, text_fg)
        palette.setColor(QPalette.Text, text_fg)
        palette.setColor(QPalette.ButtonText, text_fg)

        hlbg = self.qcolor("highlight-bg")
        hlbg.setAlpha(64)
        palette.setColor(QPalette.HighlightedText, self.qcolor("highlight-fg"))
        palette.setColor(QPalette.Highlight, hlbg)

        window_bg = self.qcolor("window-bg")
        palette.setColor(QPalette.Window, window_bg)
        palette.setColor(QPalette.AlternateBase, window_bg)

        palette.setColor(QPalette.Button, QColor(colors.get("fusion-button-base-bg")))

        frame_bg = self.qcolor("frame-bg")
        palette.setColor(QPalette.Base, frame_bg)
        palette.setColor(QPalette.ToolTipBase, frame_bg)

        disabled_color = self.qcolor("disabled")
        palette.setColor(QPalette.Disabled, QPalette.Text, disabled_color)
        palette.setColor(QPalette.Disabled, QPalette.ButtonText, disabled_color)
        palette.setColor(QPalette.Disabled, QPalette.HighlightedText, disabled_color)

        palette.setColor(QPalette.Link, self.qcolor("link"))

        palette.setColor(QPalette.BrightText, Qt.red)

        app.setPalette(palette)