Quantum GIS API Documentation  1.8
src/gui/qgsexpressionhighlighter.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgsexpressionhighlighter.cpp - A syntax highlighter for a qgsexpression
00003      --------------------------------------
00004     Date                 :  28-Dec-2011
00005     Copyright            : (C) 2011 by Nathan Woodrow
00006     Email                : woodrow.nathan at gmail dot com
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00016 #include "qgsexpressionhighlighter.h"
00017 
00018 QgsExpressionHighlighter::QgsExpressionHighlighter( QTextDocument *parent )
00019     : QSyntaxHighlighter( parent )
00020 {
00021   HighlightingRule rule;
00022 
00023   keywordFormat.setForeground( Qt::darkBlue );
00024   keywordFormat.setFontWeight( QFont::Bold );
00025   QStringList keywordPatterns;
00026   keywordPatterns << "\\bCASE\\b" << "\\bWHEN\\b" << "\\bTHEN\\b"
00027   << "\\bELSE\\b" << "\\bEND\\b";
00028 
00029   foreach( const QString &pattern, keywordPatterns )
00030   {
00031     rule.pattern = QRegExp( pattern, Qt::CaseInsensitive );
00032     rule.format = keywordFormat;
00033     highlightingRules.append( rule );
00034   }
00035 
00036   quotationFormat.setForeground( Qt::darkGreen );
00037   rule.pattern = QRegExp( "\'[^\'\r\n]*\'" );
00038   rule.format = quotationFormat;
00039   highlightingRules.append( rule );
00040 
00041   columnNameFormat.setForeground( Qt::darkRed );
00042   rule.pattern = QRegExp( "\"[^\"\r\n]*\"" );
00043   rule.format = columnNameFormat;
00044   highlightingRules.append( rule );
00045 }
00046 
00047 void QgsExpressionHighlighter::addFields( QStringList fieldList )
00048 {
00049   columnNameFormat.setForeground( Qt::darkRed );
00050   HighlightingRule rule;
00051   foreach( const QString field, fieldList )
00052   {
00053     if ( field.isEmpty() ) // this really happened :)
00054       continue;
00055     rule.pattern = QRegExp( "\\b" + field + "\\b" );
00056     rule.format = columnNameFormat;
00057     highlightingRules.append( rule );
00058   }
00059 }
00060 
00061 void QgsExpressionHighlighter::highlightBlock( const QString &text )
00062 {
00063   foreach( const HighlightingRule &rule, highlightingRules )
00064   {
00065     QRegExp expression( rule.pattern );
00066     int index = expression.indexIn( text );
00067     while ( index >= 0 )
00068     {
00069       int length = expression.matchedLength();
00070       if ( length == 0 )
00071         break; // avoid infinite loops
00072       setFormat( index, length, rule.format );
00073       index = expression.indexIn( text, index + length );
00074     }
00075   }
00076 }
00077 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines