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