QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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#include "moc_qgsexpressionhighlighter.cpp"
18
20 : QSyntaxHighlighter( parent )
21{
22 HighlightingRule rule;
23
24 keywordFormat.setForeground( Qt::darkBlue );
25 keywordFormat.setFontWeight( QFont::Bold );
26 QStringList keywordPatterns;
27 keywordPatterns << QStringLiteral( "\\bCASE\\b" ) << QStringLiteral( "\\bWHEN\\b" ) << QStringLiteral( "\\bTHEN\\b" )
28 << QStringLiteral( "\\bELSE\\b" ) << QStringLiteral( "\\bEND\\b" );
29
30 const auto constKeywordPatterns = keywordPatterns;
31 for ( const QString &pattern : constKeywordPatterns )
32 {
33 rule.pattern = QRegularExpression( pattern, QRegularExpression::CaseInsensitiveOption );
34 rule.format = keywordFormat;
35 highlightingRules.append( rule );
36 }
37
38 quotationFormat.setForeground( Qt::darkGreen );
39 rule.pattern = QRegularExpression( "\'[^\'\r\n]*\'" );
40 rule.format = quotationFormat;
41 highlightingRules.append( rule );
42
43 columnNameFormat.setForeground( Qt::darkRed );
44 rule.pattern = QRegularExpression( "\"[^\"\r\n]*\"" );
45 rule.format = columnNameFormat;
46 highlightingRules.append( rule );
47}
48
49void QgsExpressionHighlighter::addFields( const QStringList &fieldList )
50{
51 columnNameFormat.setForeground( Qt::darkRed );
52 HighlightingRule rule;
53 const auto constFieldList = fieldList;
54 for ( const QString &field : constFieldList )
55 {
56 if ( field.isEmpty() ) // this really happened :)
57 continue;
58 rule.pattern = QRegularExpression( "\\b" + field + "\\b" );
59 rule.format = columnNameFormat;
60 highlightingRules.append( rule );
61 }
62}
63
65{
66 const auto constHighlightingRules = highlightingRules;
67 for ( const HighlightingRule &rule : constHighlightingRules )
68 {
69 const QRegularExpression expression( rule.pattern );
70 QRegularExpressionMatch match = expression.match( text );
71 while ( match.hasMatch() )
72 {
73 const int index = match.capturedStart();
74 const int length = match.capturedLength();
75 if ( length == 0 )
76 break; // avoid infinite loops
77 setFormat( index, length, rule.format );
78 match = expression.match( text, index + length );
79 }
80 }
81}
void highlightBlock(const QString &text) override
QgsExpressionHighlighter(QTextDocument *parent=nullptr)
void addFields(const QStringList &fieldList)