示例#1
0
def css_for_highlight_style(style):
    is_dark = is_dark_theme()
    kind = style.get('kind')
    ans = ''
    if kind == 'color':
        key = 'dark' if is_dark else 'light'
        val = style.get(key)
        if val is None:
            which = style.get('which')
            val = (builtin_colors_dark
                   if is_dark else builtin_colors_light).get(which)
        if val is None:
            val = style.get('background-color')
        if val is not None:
            ans = f'background-color: {val}'
    elif 'background-color' in style:
        ans = 'background-color: ' + style['background-color']
        if 'color' in style:
            ans += '; color: ' + style["color"]
    elif kind == 'decoration':
        which = style.get('which')
        if which is not None:
            q = builtin_decorations.get(which)
            if q is not None:
                ans = q
        else:
            ans = '; '.join(f'{k}: {v}' for k, v in style.items())
    return ans
示例#2
0
 def __init__(self, prefs):
     self.log = default_log
     self.current_frag = None
     self.com_id = str(uuid4())
     QWebEnginePage.__init__(self)
     secure_webengine(self.settings(), for_viewer=True)
     self.titleChanged.connect(self.title_changed)
     self.loadFinished.connect(self.show_frag)
     s = QWebEngineScript()
     s.setName('toc.js')
     s.setInjectionPoint(QWebEngineScript.InjectionPoint.DocumentCreation)
     s.setRunsOnSubFrames(True)
     s.setWorldId(QWebEngineScript.ScriptWorldId.ApplicationWorld)
     js = P('toc.js', allow_user_override=False,
            data=True).decode('utf-8').replace('COM_ID', self.com_id, 1)
     if 'preview_background' in prefs.defaults and 'preview_foreground' in prefs.defaults:
         from calibre.gui2.tweak_book.preview import get_editor_settings
         settings = get_editor_settings(prefs)
     else:
         if is_dark_theme():
             settings = {
                 'is_dark_theme': True,
                 'bg': dark_color.name(),
                 'fg': dark_text_color.name(),
                 'link': dark_link_color.name(),
             }
         else:
             settings = {}
     js = js.replace('SETTINGS', json.dumps(settings), 1)
     dark_mode_css = P('dark_mode.css',
                       data=True,
                       allow_user_override=False).decode('utf-8')
     js = js.replace('CSS', json.dumps(dark_mode_css), 1)
     s.setSourceCode(js)
     self.scripts().insert(s)
示例#3
0
 def load(self, highlights):
     s = self.style()
     icon_size = s.pixelMetric(s.PM_SmallIconSize, None, self)
     dpr = self.devicePixelRatioF()
     is_dark = is_dark_theme()
     self.clear()
     self.uuid_map = {}
     highlights = (h for h in highlights
                   if not h.get('removed') and h.get('highlighted_text'))
     section_map = defaultdict(list)
     section_tt_map = {}
     for h in self.sorted_highlights(highlights):
         tfam = h.get('toc_family_titles') or ()
         if tfam:
             tsec = tfam[0]
             lsec = tfam[-1]
         else:
             tsec = h.get('top_level_section_title')
             lsec = h.get('lowest_level_section_title')
         sec = lsec or tsec or _('Unknown')
         if len(tfam) > 1:
             lines = []
             for i, node in enumerate(tfam):
                 lines.append('\xa0\xa0' * i + '➤ ' + node)
             tt = ngettext('Table of Contents section:',
                           'Table of Contents sections:', len(lines))
             tt += '\n' + '\n'.join(lines)
             section_tt_map[sec] = tt
         section_map[sec].append(h)
     for secnum, (sec, items) in enumerate(section_map.items()):
         section = QTreeWidgetItem([sec], 1)
         section.setFlags(Qt.ItemIsEnabled)
         section.setFont(0, self.section_font)
         tt = section_tt_map.get(sec)
         if tt:
             section.setToolTip(0, tt)
         self.addTopLevelItem(section)
         section.setExpanded(True)
         for itemnum, h in enumerate(items):
             txt = h.get('highlighted_text')
             txt = txt.replace('\n', ' ')
             if len(txt) > 100:
                 txt = txt[:100] + '…'
             item = QTreeWidgetItem(section, [txt], 2)
             item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled
                           | Qt.ItemNeverHasChildren)
             item.setData(0, Qt.UserRole, h)
             try:
                 dec = decoration_for_style(self.palette(),
                                            h.get('style') or {}, icon_size,
                                            dpr, is_dark)
             except Exception:
                 import traceback
                 traceback.print_exc()
                 dec = None
             if dec is None:
                 dec = self.default_decoration
             item.setData(0, Qt.DecorationRole, dec)
             self.uuid_map[h['uuid']] = secnum, itemnum
             self.num_of_items += 1
示例#4
0
 def __init__(self, doc):
     QSyntaxHighlighter.__init__(self, doc)
     self.colors = {}
     self.colors['doctype']        = QColor(192, 192, 192)
     self.colors['entity']         = QColor(128, 128, 128)
     self.colors['comment']        = QColor(35, 110,  37)
     if is_dark_theme():
         from calibre.gui2.palette import dark_link_color
         self.colors['tag']            = QColor(186,  78, 188)
         self.colors['attrname']       = QColor(193,  119, 60)
         self.colors['attrval']        = dark_link_color
     else:
         self.colors['tag']            = QColor(136,  18, 128)
         self.colors['attrname']       = QColor(153,  69,   0)
         self.colors['attrval']        = QColor(36,  36, 170)
示例#5
0
def get_editor_settings(tprefs):
    dark = is_dark_theme()

    def get_color(name, dark_val):
        ans = tprefs[name]
        if ans == 'auto' and dark:
            ans = dark_val.name()
        if ans in ('auto', 'unset'):
            return None
        return ans

    return {
        'is_dark_theme': dark,
        'bg': get_color('preview_background', dark_color),
        'fg': get_color('preview_foreground', dark_text_color),
        'link': get_color('preview_link_color', dark_link_color),
    }
示例#6
0
    def update_settings(self):
        dark = is_dark_theme()

        def get_color(name, dark_val):
            ans = tprefs[name]
            if ans == 'auto' and dark:
                ans = dark_val.name()
            if ans in ('auto', 'unset'):
                return None
            return ans

        settings = {
            'is_dark_theme': dark,
            'bg': get_color('preview_background', dark_color),
            'fg': get_color('preview_foreground', dark_text_color),
            'link': get_color('preview_link_color', dark_link_color),
        }
        p = self._page.profile()
        ua = p.httpUserAgent().split('|')[0] + '|' + json.dumps(settings)
        p.setHttpUserAgent(ua)
示例#7
0
def themed_css(reset=False):
    ans = css(reset)
    if is_dark_theme():
        ans = 'a { color: #6CB4EE }\n\n' + ans
    return ans