Пример #1
0
    def do_insert_link(self, *args):
        link, name, is_image = self.ask_link()
        if not link:
            return
        url = self.parse_link(link)
        if url.isValid():
            url = unicode_type(url.toString(NO_URL_FORMATTING))
            self.focus_self()
            with self.editing_cursor() as c:
                if is_image:
                    c.insertImage(url)
                else:
                    oldfmt = QTextCharFormat(c.charFormat())
                    fmt = QTextCharFormat()
                    fmt.setAnchor(True)
                    fmt.setAnchorHref(url)
                    fmt.setForeground(QBrush(self.palette().color(QPalette.ColorRole.Link)))
                    if name or not c.hasSelection():
                        c.mergeCharFormat(fmt)
                        c.insertText(name or url)
                    else:
                        pos, anchor = c.position(), c.anchor()
                        start, end = min(pos, anchor), max(pos, anchor)
                        for i in range(start, end):
                            cur = self.textCursor()
                            cur.setPosition(i), cur.setPosition(i + 1, QTextCursor.MoveMode.KeepAnchor)
                            cur.mergeCharFormat(fmt)
                    c.setPosition(c.position())
                    c.setCharFormat(oldfmt)

        else:
            error_dialog(self, _('Invalid URL'),
                         _('The url %r is invalid') % link, show=True)
    def initializeFormats(self):
        Config = self.Config
        Config["fontfamily"] = "monospace"
        pal = QApplication.instance().palette()
        for name, color, bold, italic in (
            ("normal", None, False, False),
            ("keyword", pal.color(QPalette.ColorRole.Link).name(), True,
             False), ("builtin", pal.color(QPalette.ColorRole.Link).name(),
                      False, False), ("comment", "#007F00", False, True),
            ("string", "#808000", False, False), ("number", "#924900", False,
                                                  False),
            ("lparen", None, True, True), ("rparen", None, True, True)):
            Config["%sfontcolor" % name] = color
            Config["%sfontbold" % name] = bold
            Config["%sfontitalic" % name] = italic
        baseFormat = QTextCharFormat()
        baseFormat.setFontFamily(Config["fontfamily"])
        Config["fontsize"] = gprefs['gpm_template_editor_font_size']
        baseFormat.setFontPointSize(Config["fontsize"])

        for name in ("normal", "keyword", "builtin", "comment", "string",
                     "number", "lparen", "rparen"):
            format = QTextCharFormat(baseFormat)
            col = Config["%sfontcolor" % name]
            if col:
                format.setForeground(QColor(col))
            if Config["%sfontbold" % name]:
                format.setFontWeight(QFont.Weight.Bold)
            format.setFontItalic(Config["%sfontitalic" % name])
            self.Formats[name] = format
Пример #3
0
 def do_color(self):
     col = QColorDialog.getColor(Qt.GlobalColor.black, self,
             _('Choose foreground color'), QColorDialog.ColorDialogOption.ShowAlphaChannel)
     if col.isValid():
         fmt = QTextCharFormat()
         fmt.setForeground(QBrush(col))
         with self.editing_cursor() as c:
             c.mergeCharFormat(fmt)
    def initialize_formats(self):
        font_name = gprefs.get('gpm_template_editor_font', None)
        size = gprefs['gpm_template_editor_font_size']
        if font_name is None:
            font = QFont()
            font.setFixedPitch(True)
            font.setPointSize(size)
            font_name = font.family()
        config = self.Config = {}
        config["fontfamily"] = font_name
        app_palette = QApplication.instance().palette()
        for name, color, bold, italic in (
            ("normal", None, False, False),
            ("keyword", app_palette.color(QPalette.ColorRole.Link).name(),
             True, False), ("builtin",
                            app_palette.color(QPalette.ColorRole.Link).name(),
                            False, False), ("identifier", None, False, True),
            ("comment", "#007F00", False, True), ("string", "#808000", False,
                                                  False), ("number", "#924900",
                                                           False, False),
            ("lparen", None, True, True), ("rparen", None, True, True)):
            config["%sfontcolor" % name] = color
            config["%sfontbold" % name] = bold
            config["%sfontitalic" % name] = italic
        base_format = QTextCharFormat()
        base_format.setFontFamily(config["fontfamily"])
        config["fontsize"] = size
        base_format.setFontPointSize(config["fontsize"])

        self.Formats = {}
        for name in ("normal", "keyword", "builtin", "comment", "identifier",
                     "string", "number", "lparen", "rparen"):
            format_ = QTextCharFormat(base_format)
            color = config["%sfontcolor" % name]
            if color:
                format_.setForeground(QColor(color))
            if config["%sfontbold" % name]:
                format_.setFontWeight(QFont.Weight.Bold)
            format_.setFontItalic(config["%sfontitalic" % name])
            self.Formats[name] = format_
Пример #5
0
    def initializeFormats(cls):
        baseFormat = QTextCharFormat()
        baseFormat.setFontFamily('monospace')
        p = QApplication.instance().palette()
        for name, color, bold, italic in (
            ("normal", None, False, False),
            ("keyword", p.color(QPalette.ColorRole.Link).name(), True, False),
            ("builtin", p.color(QPalette.ColorRole.Link).name(), False,
             False), ("constant", p.color(QPalette.ColorRole.Link).name(),
                      False, False), ("decorator", "#0000E0", False, False),
            ("comment", "#007F00", False, True), ("string", "#808000", False,
                                                  False), ("number", "#924900",
                                                           False, False),
            ("error", "#FF0000", False, False), ("pyqt", "#50621A", False,
                                                 False)):

            fmt = QTextCharFormat(baseFormat)
            if color is not None:
                fmt.setForeground(QColor(color))
            if bold:
                fmt.setFontWeight(QFont.Weight.Bold)
            if italic:
                fmt.setFontItalic(italic)
            cls.Formats[name] = fmt