QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsexpressionhighlighter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsexpressionhighlighter.cpp - A syntax highlighter for a qgsexpression
3  --------------------------------------
4  Date : 28-Dec-2011
5  Copyright : (C) 2011 by Nathan Woodrow
6  Email : woodrow.nathan at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
19  : QSyntaxHighlighter( parent )
20 {
21  HighlightingRule rule;
22 
23  keywordFormat.setForeground( Qt::darkBlue );
24  keywordFormat.setFontWeight( QFont::Bold );
25  QStringList keywordPatterns;
26  keywordPatterns << QStringLiteral( "\\bCASE\\b" ) << QStringLiteral( "\\bWHEN\\b" ) << QStringLiteral( "\\bTHEN\\b" )
27  << QStringLiteral( "\\bELSE\\b" ) << QStringLiteral( "\\bEND\\b" );
28 
29  const auto constKeywordPatterns = keywordPatterns;
30  for ( const QString &pattern : constKeywordPatterns )
31  {
32  rule.pattern = QRegularExpression( pattern, QRegularExpression::CaseInsensitiveOption );
33  rule.format = keywordFormat;
34  highlightingRules.append( rule );
35  }
36 
37  quotationFormat.setForeground( Qt::darkGreen );
38  rule.pattern = QRegularExpression( "\'[^\'\r\n]*\'" );
39  rule.format = quotationFormat;
40  highlightingRules.append( rule );
41 
42  columnNameFormat.setForeground( Qt::darkRed );
43  rule.pattern = QRegularExpression( "\"[^\"\r\n]*\"" );
44  rule.format = columnNameFormat;
45  highlightingRules.append( rule );
46 }
47 
48 void QgsExpressionHighlighter::addFields( const QStringList &fieldList )
49 {
50  columnNameFormat.setForeground( Qt::darkRed );
51  HighlightingRule rule;
52  const auto constFieldList = fieldList;
53  for ( const QString &field : constFieldList )
54  {
55  if ( field.isEmpty() ) // this really happened :)
56  continue;
57  rule.pattern = QRegularExpression( "\\b" + field + "\\b" );
58  rule.format = columnNameFormat;
59  highlightingRules.append( rule );
60  }
61 }
62 
63 void QgsExpressionHighlighter::highlightBlock( const QString &text )
64 {
65  const auto constHighlightingRules = highlightingRules;
66  for ( const HighlightingRule &rule : constHighlightingRules )
67  {
68  const QRegularExpression expression( rule.pattern );
69  QRegularExpressionMatch match = expression.match( text );
70  while ( match.hasMatch() )
71  {
72  const int index = match.capturedStart();
73  const int length = match.capturedLength();
74  if ( length == 0 )
75  break; // avoid infinite loops
76  setFormat( index, length, rule.format );
77  match = expression.match( text, index + length );
78  }
79  }
80 }
81 
QgsExpressionHighlighter::highlightBlock
void highlightBlock(const QString &text) override
Definition: qgsexpressionhighlighter.cpp:63
QgsExpressionHighlighter::addFields
void addFields(const QStringList &fieldList)
Definition: qgsexpressionhighlighter.cpp:48
field
const QgsField & field
Definition: qgsfield.h:463
qgsexpressionhighlighter.h
QgsExpressionHighlighter::QgsExpressionHighlighter
QgsExpressionHighlighter(QTextDocument *parent=nullptr)
Definition: qgsexpressionhighlighter.cpp:18