SyntaxHighlighter.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
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 } // namespace Gui
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     // Rehighlighting is very expensive, thus avoid it if this color is already set
00072     QColor old = color(type);
00073     if (!old.isValid())
00074         return; // no such type
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(); // not found
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(); // not found
00156 }
00157 
00158 void SyntaxHighlighter::colorChanged(const QString& type, const QColor& col)
00159 {
00160   // rehighlight
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 }

Generated on Wed Nov 23 19:00:42 2011 for FreeCAD by  doxygen 1.6.1