DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
daq::QTUtils::Highlighter Class Reference

#include <highlighter.hpp>

Inheritance diagram for daq::QTUtils::Highlighter:
[legend]
Collaboration diagram for daq::QTUtils::Highlighter:
[legend]

Classes

struct  FormatCache
 
struct  HighlightingRule
 

Public Member Functions

 Highlighter (QTextDocument *parent=0)
 ctor
 
void setRule (QRegExp r, QTextCharFormat f)
 
void highlightAll (const QString &txt, bool caseSensitivity)
 

Protected Member Functions

void highlightBlock (const QString &text)
 

Private Attributes

QVector< HighlightingRulehighlightingRules
 highlighting rules
 
QRegExp highlightAllReg
 regexp containing text to highlight
 
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)
 

Detailed Description

This class provides custom highlighting of textedits. It is possible to define any number of patterns (using regular expressions) and a format to be applied to the given pattern. It also provides functionality to highlight all occocurences of a given text using the

highlightAll(const QString &txt, bool caseSensitive)
void highlightAll(const QString &txt, bool caseSensitivity)

method. For example in dvs_gui it is used to highlight the log outputs. Usage:

Highlighter* hl = new Highlighter(textedit->document());
QTextCharFormat format;
format.setForeGround(Qt::darkRed);
pattern = QRegExp("FAILED");
hl->setRule(pattern,format);
void setRule(QRegExp r, QTextCharFormat f)
Highlighter(QTextDocument *parent=0)
ctor

Definition at line 29 of file highlighter.hpp.

Constructor & Destructor Documentation

◆ Highlighter()

Highlighter::Highlighter ( QTextDocument * parent = 0)

ctor

Creates highlighter. Initializes the mutex.

Definition at line 10 of file highlighter.cpp.

11 : QSyntaxHighlighter(parent)
12{
13 mutex = new QMutex();
14}
QMutex * mutex
mutex to protect highlightBlock method

Member Function Documentation

◆ highlightAll()

void daq::QTUtils::Highlighter::highlightAll ( const QString & txt,
bool caseSensitivity )
inline

Updates the text to highlight and rechecks the entire text

Parameters
txtThe text to highlight
caseSensitivitywhether the highlighting is case sensitive or not

Definition at line 55 of file highlighter.hpp.

56 {
57 highlightAllReg.setPattern(txt);
58 if(caseSensitivity)
59 highlightAllReg.setCaseSensitivity(Qt::CaseSensitive);
60 else
61 highlightAllReg.setCaseSensitivity(Qt::CaseInsensitive);
62 rehighlight();
63 }
QRegExp highlightAllReg
regexp containing text to highlight

◆ highlightBlock()

void Highlighter::highlightBlock ( const QString & text)
protected

Highlights a block of text according to the given rules (regexp + QTextCharFormat) Uses a cache for performance reasons. This way we do not have to run regexps every time.

Parameters
texttext to highlight. Used to calculate the hash and hence cache value

Reapply the stored formattings

Apply formatting using regexp and store it in cache for later use

Note. we should not cache highlighting as it changes all the time

Definition at line 21 of file highlighter.cpp.

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
50 foreach (HighlightingRule rule, highlightingRules)
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}
QVector< HighlightingRule > highlightingRules
highlighting rules
QHash< uint, QList< FormatCache > > cache
stores result of formatting for a given QString text (using uint qHash(QString) method)

◆ setRule()

void daq::QTUtils::Highlighter::setRule ( QRegExp r,
QTextCharFormat f )
inline

Adds a highlighting rule to the list. We need to clear the cache in this case (though normally rules are added very rarely)

Parameters
rRegular expression to identify the text to highlight
fformat to apply to the text

Definition at line 41 of file highlighter.hpp.

42 {
43 HighlightingRule rule;
44 rule.pattern = r;
45 rule.format = f;
46 highlightingRules.append(rule);
47 cache.clear();
48
49 }

Member Data Documentation

◆ cache

QHash<uint, QList<FormatCache> > daq::QTUtils::Highlighter::cache
private

stores result of formatting for a given QString text (using uint qHash(QString) method)

Definition at line 88 of file highlighter.hpp.

◆ highlightAllReg

QRegExp daq::QTUtils::Highlighter::highlightAllReg
private

regexp containing text to highlight

Definition at line 86 of file highlighter.hpp.

◆ highlightingRules

QVector<HighlightingRule> daq::QTUtils::Highlighter::highlightingRules
private

highlighting rules

Definition at line 85 of file highlighter.hpp.

◆ mutex

QMutex* daq::QTUtils::Highlighter::mutex
private

mutex to protect highlightBlock method

Definition at line 87 of file highlighter.hpp.


The documentation for this class was generated from the following files: