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