Пример #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.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, c.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)
Пример #2
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:
                 name = name or url
                 fmt = QTextCharFormat()
                 fmt.setAnchor(True)
                 fmt.setAnchorHref(url)
                 fmt.setFontUnderline(True)
                 fmt.setForeground(QBrush(QColor('blue')))
                 prev_fmt = c.charFormat()
                 c.mergeCharFormat(fmt)
                 c.insertText(url)
                 c.setCharFormat(prev_fmt)
     else:
         error_dialog(self, _('Invalid URL'),
                      _('The url %r is invalid') % link, show=True)