def getRenderer(rendertype): """Factory to create a renderer with the paper color adjusted to the prefs. Currently, the rendertype can be "pdf", "svg", "image" or "diff". For the PDF type, the render backends are also read from the prefs. For image types, the paper color is not used. For diff types, the paper- color is not set. """ if rendertype == "pdf": if popplerqt5: import qpageview.poppler r = qpageview.poppler.PopplerRenderer() r.renderBackend, r.printRenderBackend = getPopplerBackends() else: return None elif rendertype == "svg": import qpageview.svg r = qpageview.svg.SvgRenderer() elif rendertype == "image": import qpageview.image r = qpageview.image.ImageRenderer() elif rendertype == "diff": import qpageview.diff return qpageview.diff.DiffRenderer() else: raise ValueError("unknown render type") r.paperColor = textformats.formatData('editor').baseColors['paper'] return r
def html(cursor, scheme='editor', inline=False, number_lines=False, full_html=True, wrap_tag="pre", wrap_attrib="id", wrap_attrib_name="document"): """Return a HTML document with the syntax-highlighted region. The tokens are marked with <span> tags. The cursor is a ly.document.Cursor instance. The specified text formats scheme is used (by default 'editor'). If inline is True, the span tags have inline style attributes. If inline is False, the span tags have class attributes and a stylesheet is included. Set number_lines to True to add line numbers. """ data = textformats.formatData(scheme) # the current highlighting scheme w = ly.colorize.HtmlWriter() w.set_wrapper_tag(wrap_tag) w.set_wrapper_attribute(wrap_attrib) w.document_id = wrap_attrib_name w.inline_style = inline w.number_lines = number_lines w.full_html = full_html w.fgcolor = data.baseColors['text'].name() w.bgcolor = data.baseColors['background'].name() w.css_scheme = data.css_scheme() return w.html(cursor)
def show(cursor, pos=None, num_lines=6): """Displays a tooltip showing part of the cursor's Document. If the cursor has a selection, those blocks are displayed. Otherwise, num_lines lines are displayed. If pos is not given, the global mouse position is used. """ block = cursor.document().findBlock(cursor.selectionStart()) c2 = QTextCursor(block) if cursor.hasSelection(): c2.setPosition(cursor.selectionEnd(), QTextCursor.KeepAnchor) c2.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor) else: c2.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor, num_lines) data = textformats.formatData('editor') doc = QTextDocument() font = QFont(data.font) font.setPointSizeF(font.pointSizeF() * .8) doc.setDefaultFont(font) doc.setPlainText(c2.selection().toPlainText()) if metainfo.info(cursor.document()).highlighting: highlighter.highlight(doc, state=tokeniter.state(block)) size = doc.size().toSize() + QSize(8, -4) pix = QPixmap(size) pix.fill(data.baseColors['background']) doc.drawContents(QPainter(pix)) label = QLabel() label.setPixmap(pix) label.setStyleSheet("QLabel { border: 1px solid #777; }") label.resize(size) widgets.customtooltip.show(label, pos)
def readSettings(self): data = textformats.formatData('editor') self.searchEntry.setFont(data.font) self.replaceEntry.setFont(data.font) p = data.palette() self.searchEntry.setPalette(p) self.replaceEntry.setPalette(p)
def pixmap(cursor, num_lines=6, scale=0.8): """Return a QPixmap displaying the selected lines of the document. If the cursor has no selection, num_lines are drawn. By default the text is drawn 0.8 * the normal font size. You can change that by supplying the scale parameter. """ block = cursor.document().findBlock(cursor.selectionStart()) c2 = QTextCursor(block) if cursor.hasSelection(): c2.setPosition(cursor.selectionEnd(), QTextCursor.KeepAnchor) c2.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor) else: c2.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor, num_lines) data = textformats.formatData('editor') doc = QTextDocument() font = QFont(data.font) font.setPointSizeF(font.pointSizeF() * scale) doc.setDefaultFont(font) doc.setPlainText(c2.selection().toPlainText()) if metainfo.info(cursor.document()).highlighting: highlighter.highlight(doc, state=tokeniter.state(block)) size = doc.size().toSize() + QSize(8, -4) pix = QPixmap(size) pix.fill(data.baseColors['background']) doc.drawContents(QPainter(pix)) return pix
def html(cursor, scheme='editor', inline=False, number_lines=False, full_html=True): """Return a HTML document with the syntax-highlighted region. The tokens are marked with <span> tags. The cursor is a ly.document.Cursor instance. The specified text formats scheme is used (by default 'editor'). If inline is True, the span tags have inline style attributes. If inline is False, the span tags have class attributes and a stylesheet is included. Set number_lines to True to add line numbers. """ data = textformats.formatData(scheme) # the current highlighting scheme w = ly.colorize.HtmlWriter() w.inline_style = inline w.number_lines = number_lines w.full_html = full_html w.fgcolor = data.baseColors['text'].name() w.bgcolor = data.baseColors['background'].name() w.css_scheme = data.css_scheme() return w.html(cursor)
def highlight_mapping(): """Return the global Mapping instance that maps token class to QTextCharFormat.""" global _highlight_mapping try: return _highlight_mapping except NameError: _highlight_mapping = mapping(textformats.formatData('editor')) return _highlight_mapping
def highlightFormats(): """Return the global HighlightFormats instance.""" global _highlightFormats try: return _highlightFormats except NameError: _highlightFormats = HighlightFormats(textformats.formatData('editor')) return _highlightFormats
def readSettings(self): """Reads the settings from the user's preferences.""" # background and highlight colors of music view colors = textformats.formatData('editor').baseColors self._highlightMusicFormat.setColor(colors['musichighlight']) color = colors['selectionbackground'] color.setAlpha(128) self._highlightFormat.setBackground(color)
def highlight_mapping(): """Return the global Mapping instance that maps token class to QTextCharFormat.""" global _highlight_mapping try: return _highlight_mapping except NameError: _highlight_mapping = mapping(textformats.formatData("editor")) return _highlight_mapping
def __init__(self, data=None, inline_style=False): """Initialize the HtmlHighlighter with a TextFormatData instance. If none is given, the textformats.textFormat('editor') is used. """ self.inline_style = inline_style self.setFormatData(data or textformats.formatData('editor'))
def htmlCopy(document, type='editor'): """Returns a new QTextDocument with highlighting set as HTML textcharformats.""" data = textformats.formatData(type) doc = QTextDocument() doc.setDefaultFont(data.font) doc.setPlainText(document.toPlainText()) highlight(doc, HighlightFormats(data), ly.lex.state(documentinfo.mode(document))) return doc
def readSettings(self): # shadow/margin shadow = QSettings().value("musicview/shadow", True, bool) oldshadow, self.dropShadowEnabled = self.dropShadowEnabled, shadow self.pageLayout().setMargins( QMargins(6, 6, 6, 6) if shadow else QMargins(1, 1, 1, 1)) changed = shadow != oldshadow # kinetic scrolling kinetic = QSettings().value("musicview/kinetic_scrolling", True, bool) self.kineticPagingEnabled = kinetic self.kineticScrollingEnabled = kinetic # scrollbar visibility scrollbars = QSettings().value("musicview/show_scrollbars", True, bool) policy = Qt.ScrollBarAsNeeded if scrollbars else Qt.ScrollBarAlwaysOff oldpolicy = self.horizontalScrollBarPolicy() self.setHorizontalScrollBarPolicy(policy) self.setVerticalScrollBarPolicy(policy) if policy != oldpolicy: changed = True if not self.pageCount(): return # if margin or scrollbar visibility was changed, relayout if changed: if self._viewMode: self.fitPageLayout() self.updatePageLayout() # set certain preferences to the existing renderers renderers = set(page.renderer for page in self.pageLayout() if page.renderer) pages = set(page for page in self.pageLayout() if not page.renderer) changed = False # paper color; change the existing renderer paperColor = textformats.formatData('editor').baseColors['paper'] # if a page has no renderer, adjust the page itself :-) for rp in itertools.chain(renderers, pages): if rp.paperColor != paperColor: changed = True rp.paperColor = paperColor # render backend preference if renderers and popplerqt5: import qpageview.poppler renderBackend, printRenderBackend = getPopplerBackends() for r in renderers: if isinstance(r, qpageview.poppler.PopplerRenderer): r.printRenderBackend = printRenderBackend if r.renderBackend != renderBackend: changed = True r.renderBackend = renderBackend if changed: self.rerender()
def readSettings(self): s = QSettings() s.beginGroup("documentation") ws = self.webview.page().settings() family = s.value("fontfamily", self.font().family(), str) size = s.value("fontsize", 16, int) ws.setFontFamily(QWebSettings.StandardFont, family) ws.setFontSize(QWebSettings.DefaultFontSize, size) fixed = textformats.formatData('editor').font ws.setFontFamily(QWebSettings.FixedFont, fixed.family()) ws.setFontSize(QWebSettings.DefaultFixedFontSize, fixed.pointSizeF() * 96 / 72)
def readSettings(self): """Read preferences and adjust to those. Called on init and when app.settingsChanged fires. Sets colors, font and tab width from the preferences. """ data = textformats.formatData('editor') self.setFont(data.font) self.setPalette(data.palette()) self.setTabWidth()
def readSettings(self): s = QSettings() s.beginGroup("documentation") ws = self.webview.page().settings() family = s.value("fontfamily", self.font().family(), type("")) size = s.value("fontsize", 16, int) ws.setFontFamily(QWebSettings.StandardFont, family) ws.setFontSize(QWebSettings.DefaultFontSize, size) fixed = textformats.formatData('editor').font ws.setFontFamily(QWebSettings.FixedFont, fixed.family()) ws.setFontSize(QWebSettings.DefaultFixedFontSize, fixed.pointSizeF() * 96 / 72)
def readSettings(self): """Reads the settings from the user's preferences.""" # background and highlight colors of music view colors = textformats.formatData('editor').baseColors self._highlightMusicFormat.color = colors['musichighlight'] color = colors['selectionbackground'] color.setAlpha(128) self._highlightFormat.setBackground(color) if QSettings().value("musicview/document_properties", True, bool): self.view.documentPropertyStore.mask = None else: self.view.documentPropertyStore.mask = ('position')
def readSettings(self): s = QSettings() s.beginGroup("documentation") ws = self.webview.page().settings() family = s.value("fontfamily", self.font().family(), str) size = s.value("fontsize", 16, int) ws.setFontFamily(QWebEngineSettings.StandardFont, family) ws.setFontSize(QWebEngineSettings.DefaultFontSize, size) fixed = textformats.formatData('editor').font ws.setFontFamily(QWebEngineSettings.FixedFont, fixed.family()) ws.setFontSize(QWebEngineSettings.DefaultFixedFontSize, int(fixed.pointSizeF() * 96 / 72)) self.webview.page().profile().setHttpAcceptLanguage(','.join( lilydoc.network.langs()))
def colorize(text, state=None): """Converts the text to HTML using the specified or guessed state.""" if state is None: state = ly.lex.guessState(text) data = textformats.formatData('editor') h = highlight2html.HtmlHighlighter(data, inline_style=True) result = [ '<pre style="color: {0}; background: {1}; font-family: {2}">'.format( data.baseColors['text'].name(), data.baseColors['background'].name(), data.font.family())] result.extend(map(h.html_for_token, state.tokens(text))) result.append('</pre>') return ''.join(result)
def html_copy(cursor, scheme='editor', number_lines=False): """Return a new QTextDocument with highlighting set as HTML textcharformats. The cursor is a cursor of a document.Document instance. If the cursor has a selection, only the selection is put in the new document. If number_lines is True, line numbers are added. """ data = textformats.formatData(scheme) doc = QTextDocument() doc.setDefaultFont(data.font) doc.setPlainText(cursor.document().toPlainText()) if metainfo.info(cursor.document()).highlighting: highlight(doc, mapping(data), ly.lex.state(documentinfo.mode(cursor.document()))) if cursor.hasSelection(): # cut out not selected text start, end = cursor.selectionStart(), cursor.selectionEnd() cur1 = QTextCursor(doc) cur1.setPosition(start, QTextCursor.KeepAnchor) cur2 = QTextCursor(doc) cur2.setPosition(end) cur2.movePosition(QTextCursor.End, QTextCursor.KeepAnchor) cur2.removeSelectedText() cur1.removeSelectedText() if number_lines: c = QTextCursor(doc) f = QTextCharFormat() f.setBackground(QColor('#eeeeee')) if cursor.hasSelection(): num = cursor.document().findBlock( cursor.selectionStart()).blockNumber() + 1 last = cursor.document().findBlock(cursor.selectionEnd()) else: num = 1 last = cursor.document().lastBlock() lastnum = last.blockNumber() + 1 padding = len(format(lastnum)) block = doc.firstBlock() while block.isValid(): c.setPosition(block.position()) c.setCharFormat(f) c.insertText('{0:>{1}d} '.format(num, padding)) block = block.next() num += 1 return doc
def html_copy(cursor, scheme="editor", number_lines=False): """Return a new QTextDocument with highlighting set as HTML textcharformats. The cursor is a cursor of a document.Document instance. If the cursor has a selection, only the selection is put in the new document. If number_lines is True, line numbers are added. """ data = textformats.formatData(scheme) doc = QTextDocument() doc.setDefaultFont(data.font) doc.setPlainText(cursor.document().toPlainText()) if metainfo.info(cursor.document()).highlighting: highlight(doc, mapping(data), ly.lex.state(documentinfo.mode(cursor.document()))) if cursor.hasSelection(): # cut out not selected text start, end = cursor.selectionStart(), cursor.selectionEnd() cur1 = QTextCursor(doc) cur1.setPosition(start, QTextCursor.KeepAnchor) cur2 = QTextCursor(doc) cur2.setPosition(end) cur2.movePosition(QTextCursor.End, QTextCursor.KeepAnchor) cur2.removeSelectedText() cur1.removeSelectedText() if number_lines: c = QTextCursor(doc) f = QTextCharFormat() f.setBackground(QColor("#eeeeee")) if cursor.hasSelection(): num = cursor.document().findBlock(cursor.selectionStart()).blockNumber() + 1 last = cursor.document().findBlock(cursor.selectionEnd()) else: num = 1 last = cursor.document().lastBlock() lastnum = last.blockNumber() + 1 padding = len(format(lastnum)) block = doc.firstBlock() while block.isValid(): c.setPosition(block.position()) c.setCharFormat(f) c.insertText("{0:>{1}d} ".format(num, padding)) block = block.next() num += 1 return doc
def colorize(text, state=None): """Converts the text to HTML using the specified or guessed state.""" if state is None: state = ly.lex.guessState(text) result = [] h = highlighter.highlightFormats() d = textformats.formatData('editor') result.append('<pre style="color: {0}; background: {1}; ' 'font-family: "{2}"; font-size: {3}pt;">'.format( d.baseColors['text'].name(), d.baseColors['background'].name(), d.font.family(), d.font.pointSizeF())) for t in state.tokens(text): f = h.format(t) if f: s = style(f) if s: result.append('<span style="{0}">{1}</span>'.format(s, escape(t))) continue result.append(escape(t)) result.append('</pre>') return ''.join(result)
def _setbackground(): colors = textformats.formatData('editor').baseColors qpopplerview.cache.options().setPaperColor(colors['paper']) qpopplerview.cache.clear()
def readSettings(self): self.format = QTextCharFormat() self.format.setBackground( textformats.formatData('editor').baseColors['match'])
def readSettings(self): self._styles = textformats.formatData('editor').defaultStyles
def readSettings(self): self._baseColors = textformats.formatData('editor').baseColors self.reload()
def readSettings(self): data = textformats.formatData('editor') self.text.setFont(data.font) self.text.setPalette(data.palette())
def readSettings(self): data = textformats.formatData('editor') self.textbrowser.setPalette(data.palette()) self.textbrowser.setFont(data.font) highlighter.highlight(self.textbrowser.document())
def readSettings(self): font = textformats.formatData('editor').font self.diff.setFont(font) diffFont = QFont("Monospace") diffFont.setStyleHint(QFont.TypeWriter) self.uni_diff.setFont(diffFont)
def readSettings(self): self.popup().setFont(textformats.formatData('editor').font) self.popup().setPalette(textformats.formatData('editor').palette())
def readSettings(self): data = textformats.formatData('editor') self._baseColors = data.baseColors self.updateCursor() self.reload()
def highlightFormats(): global _highlightFormats if _highlightFormats is None: _highlightFormats = HighlightFormats(textformats.formatData('editor')) return _highlightFormats
def readSettings(self): data = textformats.formatData('editor') self.textView.setFont(data.font) self.textView.setPalette(data.palette())
def readSettings(self): self.format = QTextCharFormat() self.format.setBackground(textformats.formatData('editor').baseColors['match'])
def readSettings(self): p = self.htmlView.palette() p.setColor(QPalette.Base, textformats.formatData('editor').baseColors['paper']) self.htmlView.setPalette(p)
def readSettings(self): font = textformats.formatData('editor').font self.diff.setFont(font)
def readSettings(self): """Reads the settings from the user's preferences.""" color = textformats.formatData("editor").baseColors["selectionbackground"] color.setAlpha(128) self._highlightFormat.setBackground(color)
def readSettings(self): """Reads the settings from the user's preferences.""" color = textformats.formatData( 'editor').baseColors['selectionbackground'] color.setAlpha(128) self._highlightFormat.setBackground(color)