Pith - wedge_linux
wedge_linux/WedgeHighlighter.cpp [3.0 kb]
Modified: 22:03:59 118 026 (15 Jul 026)
10 Days Ago
#include "WedgeHighlighter.h"
#include "WedgeHeader.h"
#include <QDateTime>
#include <QRegularExpression>

void WedgeHighlighter::highlightBlock(const QString &text) {

    // next time high light

    int state = previousBlockState();
    if (state == -1) {
        state = 0;
    }

    QDateTime now = QDateTime::currentDateTime();
    QRegularExpression headerRegex = WedgeHeader::dateRegex(m_headerFormat, now.date());
    QString trimmedText = text.trimmed();

    if (headerRegex.match(text).hasMatch()) {
        state = 1;
    } else if (trimmedText.isEmpty()) {
        state = 0;
    } else if (state == 1 || state == 2) {
        static QRegularExpression timeRegex("^\\s*(\\d{2}):(\\d{2})");
        QRegularExpressionMatch timeMatch = timeRegex.match(text);

        if (timeMatch.hasMatch()) {
            int h = timeMatch.captured(1).toInt();
            int m = timeMatch.captured(2).toInt();
            int currentMinutes = now.time().hour() * 60 + now.time().minute();

            if ((h * 60 + m) > currentMinutes) {
                QTextCharFormat highlightFormat;
                highlightFormat.setBackground(m_highlightColor);
                setFormat(0, text.length(), highlightFormat);

                state = 3;
            } else {
                state = 2;
            }
        }
    } else if (state == 3) {
        state = 3;
    }

    // bold regex

    static QRegularExpression boldRegex("^.*!$");
    if (boldRegex.match(text).hasMatch()) {
        QTextCharFormat format;
        format.setFontWeight(QFont::Bold);
        format.setForeground(m_focusColor);
        setFormat(0, text.length(), format);
    }

    // italic regex

    static QRegularExpression italicRegex("^.*\\?$");
    if (italicRegex.match(text).hasMatch()) {
        QTextCharFormat format;
        format.setFontItalic(true);
        setFormat(0, text.length(), format);
    }

    // under line regex

    static QRegularExpression underlineRegex("^\\s*([^\\s].*\\*)$");
    QRegularExpressionMatch uMatch = underlineRegex.match(text);
    if (uMatch.hasMatch()) {
        QTextCharFormat uFormat;
        uFormat.setFontUnderline(true);
        setFormat(uMatch.capturedStart(1), uMatch.capturedLength(1), uFormat);
    }

    // strike thru regex

    static QRegularExpression strikethruRegex("^\\s*([^\\s].*~)$");
    QRegularExpressionMatch sMatch = strikethruRegex.match(text);
    if (sMatch.hasMatch()) {
        QTextCharFormat sFormat;
        sFormat.setFontStrikeOut(true);
        setFormat(sMatch.capturedStart(1), sMatch.capturedLength(1), sFormat);
    }

    // comment fade regex

    static QRegularExpression commentRegex("//.*$");
    QRegularExpressionMatchIterator ci = commentRegex.globalMatch(text);
    while (ci.hasNext()) {
        QRegularExpressionMatch cm = ci.next();
        QColor faded = m_textColor;
        faded.setAlphaF(0.6);
        QTextCharFormat cFormat;
        cFormat.setForeground(faded);
        setFormat(cm.capturedStart(), cm.capturedLength(), cFormat);
    }

    setCurrentBlockState(state);
}
Updates
Wedge - Android 126.026
Wedge - Linux 124.026
Shim - Android 117.026
Miter - 114.026
Kerf - Android 112.026
Dev
TVShow (227) 'CSA'
TVShow (228) 'APT'
TVProgram (83) 'BXT'
Miter Update(s)
Peen (Messaging)

Menu
Calendar
Project Tin (024/029)
Miter
RSS Feed
User Avatar
@vgmlr
=SUM(parts)