Qt 5.10 Book Examples
SyntaxHighlighter.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // SyntaxHighlighter.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtGui>
12 #include "SyntaxHighlighter.h"
13 
14 // ----------------------------------------------------------------------
15 SyntaxHighlighter::SyntaxHighlighter(QTextDocument* parent/*= 0*/)
16  : QSyntaxHighlighter(parent)
17 {
18  m_lstKeywords
19  << "foreach" << "bool" << "int" << "void" << "double"
20  << "float" << "char" << "delete" << "class" << "const"
21  << "virtual" << "mutable" << "this" << "struct" << "union"
22  << "throw" << "for" << "if" << "else" << "false"
23  << "namespace" << "switch" << "case" << "public" << "private"
24  << "protected" << "new" << "return" << "using" << "true"
25  << "->" << ">>" << "<<" << ">" << "<"
26  << "(" << ")" << "{" << "}" << "["
27  << "]" << "+" << "-" << "*" << "/"
28  << "=" << "!" << "." << "," << ";"
29  << ":" << "&" << "emit" << "connect" << "SIGNAL"
30  << "|" << "SLOT" << "slots" << "signals";
31 }
32 
33 // ----------------------------------------------------------------------
34 /*virtual*/ void SyntaxHighlighter::highlightBlock(const QString& str)
35 {
36  int nState = previousBlockState();
37  int nStart = 0;
38  for (int i = 0; i < str.length(); ++i) {
39  if (nState == InsideCStyleComment) {
40  if (str.mid(i, 2) == "*/") {
41  nState = NormalState;
42  setFormat(nStart, i - nStart + 2, Qt::darkGray);
43  i++;
44  }
45  }
46  else if (nState == InsideCString) {
47  if (str.mid(i, 1) == "\"" || str.mid(i, 1) == "\'") {
48  if (str.mid(i - 1, 2) != "\\\""
49  && str.mid(i - 1, 2) != "\\\'"
50  ) {
51  nState = NormalState;
52  setFormat(nStart, i - nStart + 1, Qt::cyan);
53  }
54  }
55  }
56  else {
57  if (str.mid(i, 2) == "//") {
58  setFormat(i, str.length() - i, Qt::darkGray);
59  break;
60  }
61  else if (str.mid(i, 1) == "#") {
62  setFormat(i, str.length() - i, Qt::green);
63  break;
64  }
65  else if (str.at(i).isNumber()) {
66  setFormat(i, 1, Qt::cyan);
67  }
68  else if (str.mid(i, 2) == "/*") {
69  nStart = i;
70  nState = InsideCStyleComment;
71  }
72  else if (str.mid(i, 1) == "\"" || str.mid(i, 1) == "\'") {
73  nStart = i;
74  nState = InsideCString;
75  }
76  else {
77  QString strKeyword = getKeyword(i, str);
78  if (!strKeyword.isEmpty()) {
79  setFormat(i, strKeyword.length(), Qt::white);
80  i += strKeyword.length() - 1;
81  }
82  }
83  }
84  }
85  if (nState == InsideCStyleComment) {
86  setFormat(nStart, str.length() - nStart, Qt::darkGray);
87  }
88  if (nState == InsideCString) {
89  setFormat(nStart, str.length() - nStart, Qt::cyan);
90  }
91 
92  setCurrentBlockState(nState);
93 }
94 
95 // ----------------------------------------------------------------------
96 QString SyntaxHighlighter::getKeyword(int nPos, const QString& str) const
97 {
98  QString strTemp = "";
99 
100  foreach (QString strKeyword, m_lstKeywords) {
101  if (str.mid(nPos, strKeyword.length()) == strKeyword) {
102  strTemp = strKeyword;
103  break;
104  }
105  }
106 
107  return strTemp;
108 }
109 
QString getKeyword(int i, const QString &str) const
SyntaxHighlighter(QTextDocument *parent=0)
virtual void highlightBlock(const QString &)