File: datahighlighter.cpp

package info (click to toggle)
cutecom 0.51.0-1
  • links: PTS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,272 kB
  • sloc: cpp: 4,228; python: 240; makefile: 54; sh: 9
file content (100 lines) | stat: -rw-r--r-- 3,298 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (c) 2016 Meinhard Ritscher <cyc1ingsir@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * For more information on the GPL, please go to:
 * http://www.gnu.org/copyleft/gpl.html
 */

#include "datahighlighter.h"

DataHighlighter::DataHighlighter(QTextDocument *parent)
    : QSyntaxHighlighter(parent)
{
    m_format_time.setForeground(Qt::darkGreen);
    m_pattern_time = new QRegExp("\\d{2,2}:\\d{2,2}:\\d{2,2}:\\d{3,3} ");
    m_format_bytes.setForeground(QColor(120, 180, 200));
    QFont font;
    font.setFamily(font.defaultFamily());
    font.setPointSize(10);
    m_format_bytes.setFont(font);
    m_pattern_bytes = new QRegExp("^\\d{8} ");
    m_format_ctrl.setForeground(Qt::darkRed);
    m_format_ctrl.setFontWeight(QFont::Bold);
    m_pattern_ctrl = new QRegExp("[\\x240A\\x240D\\x21E5]");
    font = QFont("Monospace");
    font.setStyleHint(QFont::Courier);
    font.setPointSize(10);
    m_format_hex.setFont(font);
    m_format_hex.setForeground(Qt::darkMagenta);
    m_pattern_hex = new QRegExp("<0x[\\da-f]{2}>");
    m_format_search.setBackground(QColor(230, 230, 180));
    m_format_search.setForeground(QColor(50, 50, 180));
}

void DataHighlighter::setSearchString(const QString &search)
{
    m_searchString = search;
    rehighlight();
}

void DataHighlighter::setCharFormat(QTextCharFormat *format, DataHighlighter::Formats type)
{
    switch (type) {
    case Formats::HEX:
        // m_format_hex = format;
        break;
    default:
        break;
    }
}

void DataHighlighter::highlightBlock(const QString &text)
{
    if (text.isEmpty())
        return;
    int index = m_pattern_time->indexIn(text);
    if (index >= 0)
        setFormat(index, m_pattern_time->matchedLength(), m_format_time);

    index = m_pattern_bytes->indexIn(text);
    if (index >= 0)
        setFormat(index, m_pattern_bytes->matchedLength(), m_format_bytes);

    index = m_pattern_ctrl->indexIn(text, 0);
    while (index >= 0) {
        setFormat(index, 1, m_format_ctrl);
        index = m_pattern_ctrl->indexIn(text, index + 1);
    }

    index = m_pattern_hex->indexIn(text, 0);
    int l = 0;
    while (index >= 0) {
        l = m_pattern_hex->matchedLength();
        setFormat(index, l, m_format_hex);
        index = m_pattern_hex->indexIn(text, index + l);
    }

    if (m_searchString.isEmpty())
        return;

    const int length = m_searchString.length();
    index = text.indexOf(m_searchString, 0, Qt::CaseInsensitive);
    while (index >= 0) {
        setFormat(index, length, m_format_search);
        index = text.indexOf(m_searchString, index + length, Qt::CaseInsensitive);
    }
}