SyntaxHighlighter.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025
00026 #include "SyntaxHighlighter.h"
00027 #include "TextEdit.h"
00028
00029 using namespace Gui;
00030
00031 namespace Gui {
00032 class SyntaxHighlighterP
00033 {
00034 public:
00035 SyntaxHighlighterP()
00036 {
00037 cNormalText.setRgb(0, 0, 0); cComment.setRgb(0, 170, 0);
00038 cBlockcomment.setRgb(160, 160, 164); cLiteral.setRgb(255, 0, 0);
00039 cNumber.setRgb(0, 0, 255); cOperator.setRgb(160, 160, 164);
00040 cKeyword.setRgb(0, 0, 255); cClassName.setRgb(255, 170, 0);
00041 cDefineName.setRgb(255, 170, 0); cOutput.setRgb(170, 170, 127);
00042 cError.setRgb(255, 0, 0);
00043 }
00044
00045 QColor cNormalText, cComment, cBlockcomment, cLiteral, cNumber,
00046 cOperator, cKeyword, cClassName, cDefineName, cOutput, cError;
00047 };
00048 }
00049
00053 SyntaxHighlighter::SyntaxHighlighter(QObject* parent)
00054 : QSyntaxHighlighter(parent)
00055 {
00056 d = new SyntaxHighlighterP;
00057 }
00058
00060 SyntaxHighlighter::~SyntaxHighlighter()
00061 {
00062 delete d;
00063 }
00064
00069 void SyntaxHighlighter::setColor(const QString& type, const QColor& col)
00070 {
00071
00072 QColor old = color(type);
00073 if (!old.isValid())
00074 return;
00075 if (old == col)
00076 return;
00077 if (type == QLatin1String("Text"))
00078 d->cNormalText = col;
00079 else if (type == QLatin1String("Comment"))
00080 d->cComment = col;
00081 else if (type == QLatin1String("Block comment"))
00082 d->cBlockcomment = col;
00083 else if (type == QLatin1String("Number"))
00084 d->cNumber = col;
00085 else if (type == QLatin1String("String"))
00086 d->cLiteral = col;
00087 else if (type == QLatin1String("Keyword"))
00088 d->cKeyword = col;
00089 else if (type == QLatin1String("Class name"))
00090 d->cClassName = col;
00091 else if (type == QLatin1String("Define name"))
00092 d->cDefineName = col;
00093 else if (type == QLatin1String("Operator"))
00094 d->cOperator = col;
00095 else if (type == QLatin1String("Python output"))
00096 d->cOutput = col;
00097 else if (type == QLatin1String("Python error"))
00098 d->cError = col;
00099 colorChanged(type, col);
00100 }
00101
00102 QColor SyntaxHighlighter::color(const QString& type)
00103 {
00104 if (type == QLatin1String("Text"))
00105 return d->cNormalText;
00106 else if (type == QLatin1String("Comment"))
00107 return d->cComment;
00108 else if (type == QLatin1String("Block comment"))
00109 return d->cBlockcomment;
00110 else if (type == QLatin1String("Number"))
00111 return d->cNumber;
00112 else if (type == QLatin1String("String"))
00113 return d->cLiteral;
00114 else if (type == QLatin1String("Keyword"))
00115 return d->cKeyword;
00116 else if (type == QLatin1String("Class name"))
00117 return d->cClassName;
00118 else if (type == QLatin1String("Define name"))
00119 return d->cDefineName;
00120 else if (type == QLatin1String("Operator"))
00121 return d->cOperator;
00122 else if (type == QLatin1String("Python output"))
00123 return d->cOutput;
00124 else if (type == QLatin1String("Python error"))
00125 return d->cError;
00126 else
00127 return QColor();
00128 }
00129
00130 QColor SyntaxHighlighter::colorByType(SyntaxHighlighter::TColor type)
00131 {
00132 if (type == SyntaxHighlighter::Text)
00133 return d->cNormalText;
00134 else if (type == SyntaxHighlighter::Comment)
00135 return d->cComment;
00136 else if (type == SyntaxHighlighter::BlockComment)
00137 return d->cBlockcomment;
00138 else if (type == SyntaxHighlighter::Number)
00139 return d->cNumber;
00140 else if (type == SyntaxHighlighter::String)
00141 return d->cLiteral;
00142 else if (type == SyntaxHighlighter::Keyword)
00143 return d->cKeyword;
00144 else if (type == SyntaxHighlighter::Classname)
00145 return d->cClassName;
00146 else if (type == SyntaxHighlighter::Defname)
00147 return d->cDefineName;
00148 else if (type == SyntaxHighlighter::Operator)
00149 return d->cOperator;
00150 else if (type == SyntaxHighlighter::Output)
00151 return d->cOutput;
00152 else if (type == SyntaxHighlighter::Error)
00153 return d->cError;
00154 else
00155 return QColor();
00156 }
00157
00158 void SyntaxHighlighter::colorChanged(const QString& type, const QColor& col)
00159 {
00160
00161 #if QT_VERSION >= 0x040200
00162 rehighlight();
00163 #else
00164 document()->setPlainText(document()->toPlainText());
00165 document()->setModified(false);
00166 #endif
00167 }
00168
00169 int SyntaxHighlighter::maximumUserState() const
00170 {
00171 return 8;
00172 }