Пример #1
0
HtmlHighlighter::HtmlHighlighter(QTextEdit *textEdit)
    : QSyntaxHighlighter(textEdit->document())
{
    QTextCharFormat entityFormat;
    entityFormat.setForeground(Qt::red);
    setFormatFor(Entity, entityFormat);

    QTextCharFormat tagFormat;
    tagFormat.setForeground(Qt::darkMagenta);
    tagFormat.setFontWeight(QFont::Bold);
    setFormatFor(Tag, tagFormat);

    QTextCharFormat commentFormat;
    commentFormat.setForeground(Qt::gray);
    commentFormat.setFontItalic(true);
    setFormatFor(Comment, commentFormat);

    QTextCharFormat attributeFormat;
    attributeFormat.setForeground(Qt::black);
    attributeFormat.setFontWeight(QFont::Bold);
    setFormatFor(Attribute, attributeFormat);

    QTextCharFormat valueFormat;
    valueFormat.setForeground(Qt::blue);
    setFormatFor(Value, valueFormat);
}
Пример #2
0
tdHtmlHighlighter::tdHtmlHighlighter(QTextDocument *document)
    : QSyntaxHighlighter(document)
{
    QTextCharFormat entityFormat;
    entityFormat.setForeground(QColor(0, 168, 0));
    entityFormat.setFontWeight(QFont::Bold);
    setFormatFor(Entity, entityFormat);

    QTextCharFormat tagFormat;
    tagFormat.setForeground(QColor(192, 16, 112));
    tagFormat.setFontWeight(QFont::Bold);
    setFormatFor(Tag, tagFormat);

    QTextCharFormat commentFormat;
    commentFormat.setForeground(QColor(128, 10, 74));
    commentFormat.setFontItalic(true);
    setFormatFor(Comment, commentFormat);

    QTextCharFormat tagAttrFormat;
    tagAttrFormat.setForeground(QColor(255, 96, 15));
    tagAttrFormat.setFontWeight(QFont::Bold);
    setFormatFor(TagAttr, tagAttrFormat);

    QTextCharFormat quoteFormat;
    quoteFormat.setForeground(QColor(0, 168, 0));
    setFormatFor(Quote, quoteFormat);

    QColor linkBlue(0, 10, 255);
    QTextCharFormat quoteUriFormat;
    quoteUriFormat.setForeground(linkBlue);
    quoteUriFormat.setUnderlineStyle(QTextCharFormat::SingleUnderline);
    quoteUriFormat.setUnderlineColor(linkBlue);
    setFormatFor(QuoteUri, quoteUriFormat);
}
Highlighter::Highlighter(QTextDocument *document) //Constructor for HTML Syntax Highlighter
    : QSyntaxHighlighter(document)
{
    QPalette palette;
    QColor foreground = palette.text().color();
    QColor background = palette.base().color();

    QTextCharFormat entityFormat;
    QColor entityColor;
    entityColor.setRed((foreground.red() + background.red()) / 2);
    entityColor.setGreen(
            (foreground.green() + background.green()) / 2);
    entityColor.setBlue(
            (foreground.blue() + background.blue()) / 2);
    if (abs(entityColor.red() - background.red()) > 80)
        entityColor.setRed(foreground.red());
    else if (abs(entityColor.green()-background.green()) > 80)
        entityColor.setGreen(foreground.green());
    else entityColor.setBlue(foreground.blue());
    entityFormat.setForeground(entityColor);
    entityFormat.setFontWeight(QFont::Normal);
    setFormatFor(Entity, entityFormat);

    QTextCharFormat tagFormat;
    QColor tagColor;
    tagColor.setRed(foreground.red() + (background.red() - foreground.red()) / 4);
    tagColor.setGreen(
            foreground.green() + (background.green() - foreground.green()) / 4);
    tagColor.setBlue(
            foreground.blue() + (background.blue() - foreground.blue()) / 4);
    tagFormat.setForeground(tagColor);
    tagFormat.setFontWeight(QFont::Bold);
    setFormatFor(Tag, tagFormat);

    QTextCharFormat commentFormat;
    QColor commentColor;
    commentColor.setRed(background.red() + (foreground.red() - background.red()) / 3);
    commentColor.setGreen(
            background.green() + (foreground.green() - background.green()) / 3);
    commentColor.setBlue(
            background.blue() + (foreground.blue() - background.blue()) / 3);
    commentFormat.setForeground(commentColor);
    commentFormat.setFontWeight(QFont::Normal);
    setFormatFor(Comment, commentFormat);

    QTextCharFormat attributeFormat;
    QColor attributeColor;
    attributeColor.setRed((foreground.red() + background.red()) / 2);
    attributeColor.setGreen(
            (foreground.green() + background.green()) / 2);
    attributeColor.setBlue(
            (foreground.blue() + background.blue()) / 2);
    if (abs(attributeColor.red() - background.red()) > 80)
        attributeColor.setRed(background.red());
    else if (abs(attributeColor.green() - background.green()) > 80)
        attributeColor.setGreen(background.green());
    else attributeColor.setBlue(background.blue());
    attributeFormat.setForeground(attributeColor);
    attributeFormat.setFontWeight(QFont::Normal);
    setFormatFor(Attribute, attributeFormat);
}