QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 = QRegExp( pattern, Qt::CaseInsensitive );
33  rule.format = keywordFormat;
34  highlightingRules.append( rule );
35  }
36 
37  quotationFormat.setForeground( Qt::darkGreen );
38  rule.pattern = QRegExp( "\'[^\'\r\n]*\'" );
39  rule.format = quotationFormat;
40  highlightingRules.append( rule );
41 
42  columnNameFormat.setForeground( Qt::darkRed );
43  rule.pattern = QRegExp( "\"[^\"\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 = QRegExp( "\\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  QRegExp expression( rule.pattern );
69  int index = expression.indexIn( text );
70  while ( index >= 0 )
71  {
72  int length = expression.matchedLength();
73  if ( length == 0 )
74  break; // avoid infinite loops
75  setFormat( index, length, rule.format );
76  index = expression.indexIn( text, index + length );
77  }
78  }
79 }
80 
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:456
qgsexpressionhighlighter.h
QgsExpressionHighlighter::QgsExpressionHighlighter
QgsExpressionHighlighter(QTextDocument *parent=nullptr)
Definition: qgsexpressionhighlighter.cpp:18