Example #1
0
void RSButtonOnText::appendToText(QTextEdit *textEdit)
{
	clear();
	_textEdit = textEdit;
	_textEditViewPort = textEdit->viewport();

	updateImage();

	_textCursor = new QTextCursor(textEdit->textCursor());
	_textCursor->movePosition(QTextCursor::End);
	_textEdit->insertPlainText(QString(QChar::Nbsp));//To get cursorForPosition, else it returns next char after middle
	int textCursorSavePos = _textCursor->position();
	_textCursor->insertHtml(htmlText());
	_textCursor->setPosition(textCursorSavePos);
	_textCursor->movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
	_lenght = _textCursor->position() - _textCursor->anchor();
	_textEdit->insertPlainText(QString(QChar::Nbsp));//To get cursorForPosition, else it returns next char after middle
	_textCursor->setPosition(textCursorSavePos);

	_textEditViewPort->installEventFilter(this);
}
Example #2
0
TextUrlData TextParser::extractUrlData(const QString& text, bool doUrlFixup)
{
    TextUrlData data;
    QString htmlText(text);
    s_urlPattern.setCaseSensitivity(Qt::CaseInsensitive);

    int pos = 0;
    int urlLen = 0;

    QString protocol;
    QString href;

    while ((pos = s_urlPattern.indexIn(htmlText, pos)) >= 0) {
        urlLen = s_urlPattern.matchedLength();
        href = htmlText.mid(pos, urlLen);

        data.urlRanges << QPair<int, int>(pos, href.length());
        pos += href.length();

        if (doUrlFixup) {
            protocol.clear();
            if (s_urlPattern.cap(2).isEmpty()) {
                QString urlPatternCap1(s_urlPattern.cap(1));
                if (urlPatternCap1.contains(QLatin1Char('@'))) {
                    protocol = QLatin1String("mailto:");
                } else if (urlPatternCap1.startsWith(QLatin1String("ftp."), Qt::CaseInsensitive)) {
                    protocol = QLatin1String("ftp://");
                } else {
                    protocol = QLatin1String("http://");
                }
            }

            href = protocol + href;
            data.fixedUrls.append(href);
        }
    }
    return data;
}