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