DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
highlighter.cpp
Go to the documentation of this file.
1#include "dbe/highlighter.hpp"
2#include <QMutexLocker>
3#include <QtGui>
4
5// using namespace daq::QTUtils;
10Highlighter::Highlighter(QTextDocument* parent)
11 : QSyntaxHighlighter(parent)
12{
13 mutex = new QMutex();
14}
15
21void
22Highlighter::highlightBlock(const QString& text)
23{
24 QMutexLocker locker(mutex);
25
26 /*
27 * check if we have formatted this text before
28 * If so there is no need to run regexp again and we can simply reapply the cached formatting.
29 * Remember to clear the cache if new rules are added after it has been filled (not likely)
30 */
31 uint hash = qHash(text);
32 if (cache.contains(hash)) {
36 FormatCache form;
37 foreach (form, cache.value(hash)) {
38 for (int i = 0; i < form.indexes.size(); i++) {
39 setFormat(form.indexes.at(i), form.lengths.at(i), form.rule.format);
40 }
41 }
42 } else {
46 QList<FormatCache> forms; // list of all formatting being done
47 foreach (HighlightingRule rule, highlightingRules) {
48 FormatCache form;
49 form.rule = rule; // store the rule
50 QRegExp expression(rule.pattern);
51 int index = text.indexOf(expression);
52 int length;
53 while (index >= 0) {
54
55 length = expression.matchedLength();
56 form.indexes.append(index); // store index
57 form.lengths.append(length); // store length
58 setFormat(index, length, rule.format);
59 index = text.indexOf(expression, index + length);
60 }
61 forms.append(form); // append this formatting (rule, indexes and lengths) to the list
62 }
63 cache.insert(hash, forms); // store list in cache
64 }
68 if (!highlightAllReg.isEmpty()) {
69 int index = text.indexOf(highlightAllReg);
70 while (index >= 0) {
71 int length = highlightAllReg.matchedLength();
72 QTextCharFormat format;
73 format.setBackground(Qt::yellow);
74 setFormat(index, length, format);
75 index = text.indexOf(highlightAllReg, index + length);
76 }
77 }
78}
QRegExp highlightAllReg
regexp containing text to highlight
QVector< HighlightingRule > highlightingRules
highlighting rules
QMutex * mutex
mutex to protect highlightBlock method
QHash< uint, QList< FormatCache > > cache
stores result of formatting for a given QString text (using uint qHash(QString) method)
void highlightBlock(const QString &text)
Highlighter(QTextDocument *parent=0)
ctor