QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 << "\\bCASE\\b" << "\\bWHEN\\b" << "\\bTHEN\\b"
27  << "\\bELSE\\b" << "\\bEND\\b";
28 
29  Q_FOREACH ( const QString &pattern, keywordPatterns )
30  {
31  rule.pattern = QRegExp( pattern, Qt::CaseInsensitive );
32  rule.format = keywordFormat;
33  highlightingRules.append( rule );
34  }
35 
36  quotationFormat.setForeground( Qt::darkGreen );
37  rule.pattern = QRegExp( "\'[^\'\r\n]*\'" );
38  rule.format = quotationFormat;
39  highlightingRules.append( rule );
40 
41  columnNameFormat.setForeground( Qt::darkRed );
42  rule.pattern = QRegExp( "\"[^\"\r\n]*\"" );
43  rule.format = columnNameFormat;
44  highlightingRules.append( rule );
45 }
46 
48 {
49  columnNameFormat.setForeground( Qt::darkRed );
50  HighlightingRule rule;
51  Q_FOREACH ( const QString& field, fieldList )
52  {
53  if ( field.isEmpty() ) // this really happened :)
54  continue;
55  rule.pattern = QRegExp( "\\b" + field + "\\b" );
56  rule.format = columnNameFormat;
57  highlightingRules.append( rule );
58  }
59 }
60 
62 {
63  Q_FOREACH ( const HighlightingRule &rule, highlightingRules )
64  {
65  QRegExp expression( rule.pattern );
66  int index = expression.indexIn( text );
67  while ( index >= 0 )
68  {
69  int length = expression.matchedLength();
70  if ( length == 0 )
71  break; // avoid infinite loops
72  setFormat( index, length, rule.format );
73  index = expression.indexIn( text, index + length );
74  }
75  }
76 }
77 
static unsigned index
void append(const T &value)
void setFormat(int start, int count, const QTextCharFormat &format)
void highlightBlock(const QString &text) override
int matchedLength() const
void setForeground(const QBrush &brush)
int indexIn(const QString &str, int offset, CaretMode caretMode) const
QgsExpressionHighlighter(QTextDocument *parent=nullptr)
bool isEmpty() const
void setFontWeight(int weight)
void addFields(const QStringList &fieldList)