• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kmail

  • sources
  • kde-4.12
  • kdepim
  • kmail
  • searchdialog
  • debug
sparqlsyntaxhighlighter.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 Vishesh Handa <handa.vish@gmail.com>
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 
19 #include "sparqlsyntaxhighlighter.h"
20 #include <KDebug>
21 
22 Nepomuk2::SparqlSyntaxHighlighter::SparqlSyntaxHighlighter(QTextDocument* parent)
23  : QSyntaxHighlighter(parent)
24 {
25  init();
26 }
27 
28 void Nepomuk2::SparqlSyntaxHighlighter::init()
29 {
30  // Keywords
31  QTextCharFormat keywordFormat;
32  keywordFormat.setForeground( Qt::darkMagenta );
33  keywordFormat.setFontWeight( QFont::Bold );
34  QStringList keywords;
35 
36  //FIXME: Separate special inbuilt keywords
37  keywords << QLatin1String( "\\bprefix\\b" ) << QLatin1String( "\\bselect\\b" ) << QLatin1String( "\\bdistinct\\b" ) << QLatin1String( "\\breduced\\b" )
38  << QLatin1String( "\\bconstruct\\b" ) << QLatin1String( "\\bdescribe\\b" ) << QLatin1String( "\\bask\\b" ) << QLatin1String( "\\bfrom\\b" )
39  << QLatin1String( "\\bnamed\\b" ) << QLatin1String( "\\bwhere\\b" ) << QLatin1String( "\\border\\b" ) << QLatin1String( "\\bby\\b" ) << QLatin1String( "\\basc\\b" )
40  << QLatin1String( "\\bdesc\\b" ) << QLatin1String( "\\blimit\\b" ) << QLatin1String( "\\boffset\\b" ) << QLatin1String( "\\boptional\\b" )
41  << QLatin1String( "\\bgraph\\b" ) << QLatin1String( "\\bunion\\b" ) << QLatin1String( "\\bfilter\\b" ) << QLatin1String( "\\bstr\\b" )
42  << QLatin1String( "\\blang\\b" ) << QLatin1String( "\\blangmatches\\b" ) << QLatin1String( "\\bdatatype\\b" ) << QLatin1String( "\\bbound\\b" )
43  << QLatin1String( "\\bsameTerm\\b" ) << QLatin1String( "\\bisIRI\\b" ) << QLatin1String( "\\bisURI\\b" ) << QLatin1String( "\\bisLiteral\\b" )
44  << QLatin1String( "\\bisBlank\\b" ) << QLatin1String( "\\bregex\\b" ) << QLatin1String( "\\btrue\\b" ) << QLatin1String( "\\bfalse\\b" ) << QLatin1String( "\\ba\\b" );
45 
46  foreach( const QString & s, keywords ) {
47  QRegExp regex( s, Qt::CaseInsensitive );
48  m_rules.append( Rule( regex, keywordFormat ) );
49  }
50 
51  // Variables
52  QTextCharFormat varFormat;
53  varFormat.setForeground( Qt::blue );
54  QRegExp varRegex( QLatin1String( "\\?\\w+" ) );
55  m_rules.append( Rule( varRegex, varFormat ) );
56 
57  // URI
58  QTextCharFormat uriFormat;
59  uriFormat.setForeground( Qt::darkGreen );
60  QRegExp uriRegex( QLatin1String( "<[^ >]*>" ) );
61  m_rules.append( Rule( uriRegex, uriFormat ) );
62 
63  // Abbreviated uris --> uri:word
64  //TODO: Highlight uri and word with different colours
65  QTextCharFormat abrUriFormat;
66  abrUriFormat.setForeground( Qt::darkGray );
67  QRegExp abrUriRegex( QLatin1String( "\\b\\w+:\\w*\\b" ) );
68  m_rules.append( Rule( abrUriRegex, abrUriFormat ) );
69 
70  // Literals
71  QTextCharFormat literalFormat;
72  literalFormat.setForeground( Qt::red );
73  QRegExp literalRegex( QLatin1String( "(\"[^\"]*\")|(\'[^\']*\')" ) );
74  m_rules.append( Rule( literalRegex, literalFormat ) );
75 
76  // Comments
77  QTextCharFormat commentFormat;
78  commentFormat.setForeground( Qt::darkYellow );
79  QRegExp commentRegex( QLatin1String( "^#.*$" ) );
80  m_rules.append( Rule( commentRegex, commentFormat ) );
81 }
82 
83 void Nepomuk2::SparqlSyntaxHighlighter::highlightBlock(const QString& text)
84 {
85  foreach (const Rule &rule, m_rules) {
86  QRegExp expression(rule.pattern);
87  int index = expression.indexIn(text);
88  int length = 0;
89  while (index >= 0 && ( length = expression.matchedLength() ) > 0 ) {
90  setFormat(index, length, rule.format);
91  index = expression.indexIn(text, index + length);
92  }
93  }
94 }
95 
96 #include "sparqlsyntaxhighlighter.moc"
text
virtual QByteArray text(quint32 serialNumber) const =0
sparqlsyntaxhighlighter.h
Nepomuk2::SparqlSyntaxHighlighter::highlightBlock
void highlightBlock(const QString &text)
Definition: sparqlsyntaxhighlighter.cpp:83
QSyntaxHighlighter
Nepomuk2::SparqlSyntaxHighlighter::SparqlSyntaxHighlighter
SparqlSyntaxHighlighter(QTextDocument *parent)
Definition: sparqlsyntaxhighlighter.cpp:22
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:52 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kmail

Skip menu "kmail"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal