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 <QtGui>
3#include <QMutexLocker>
4
5//using namespace daq::QTUtils;
10Highlighter::Highlighter(QTextDocument *parent)
11 : QSyntaxHighlighter(parent)
12{
13 mutex = new QMutex();
14}
15
21void Highlighter::highlightBlock(const QString &text)
22{
23 QMutexLocker locker(mutex);
24
25 /*
26 * check if we have formatted this text before
27 * If so there is no need to run regexp again and we can simply reapply the cached formatting.
28 * Remember to clear the cache if new rules are added after it has been filled (not likely)
29 */
30 uint hash = qHash(text);
31 if(cache.contains(hash))
32 {
36 FormatCache form;
37 foreach(form,cache.value(hash)){
38 for(int i =0; i < form.indexes.size();i++)
39 {
40 setFormat(form.indexes.at(i), form.lengths.at(i), form.rule.format);
41 }
42 }
43 }
44 else
45 {
49 QList<FormatCache> forms; //list of all formatting being done
51 {
52 FormatCache form;
53 form.rule = rule; //store the rule
54 QRegExp expression(rule.pattern);
55 int index = text.indexOf(expression);
56 int length;
57 while (index >= 0)
58 {
59
60 length = expression.matchedLength();
61 form.indexes.append(index); //store index
62 form.lengths.append(length);//store length
63 setFormat(index, length, rule.format);
64 index = text.indexOf(expression, index + length);
65 }
66 forms.append(form); //append this formatting (rule, indexes and lengths) to the list
67 }
68 cache.insert(hash,forms);//store list in cache
69
70 }
74 if(!highlightAllReg.isEmpty())
75 {
76 int index = text.indexOf(highlightAllReg);
77 while (index >= 0)
78 {
79 int length = highlightAllReg.matchedLength();
80 QTextCharFormat format;
81 format.setBackground(Qt::yellow);
82 setFormat(index, length, format);
83 index = text.indexOf(highlightAllReg, index + length);
84 }
85 }
86
87}
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)