| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <QApplication> |
| 25 |
#include <QPalette> |
| 26 |
|
| 27 |
#include "SyntaxHighlighter.h" |
| 28 |
|
| 29 |
|
| 30 |
using namespace Gui; |
| 31 |
|
| 32 |
namespace Gui |
| 33 |
{ |
| 34 |
class SyntaxHighlighterP |
| 35 |
{ |
| 36 |
public: |
| 37 |
SyntaxHighlighterP() |
| 38 |
{ |
| 39 |
cNormalText = qApp->palette().windowText().color(); |
| 40 |
cComment.setRgb(0, 170, 0); |
| 41 |
cBlockcomment.setRgb(160, 160, 164); |
| 42 |
cLiteral.setRgb(255, 0, 0); |
| 43 |
cNumber.setRgb(0, 0, 255); |
| 44 |
cOperator.setRgb(160, 160, 164); |
| 45 |
cKeyword.setRgb(0, 0, 255); |
| 46 |
cClassName.setRgb(255, 170, 0); |
| 47 |
cDefineName.setRgb(255, 170, 0); |
| 48 |
cOutput.setRgb(170, 170, 127); |
| 49 |
cError.setRgb(255, 0, 0); |
| 50 |
} |
| 51 |
|
| 52 |
QColor cNormalText, cComment, cBlockcomment, cLiteral, cNumber, cOperator, cKeyword, cClassName, |
| 53 |
cDefineName, cOutput, cError; |
| 54 |
}; |
| 55 |
} // namespace Gui |
| 56 |
|
| 57 |
/** |
| 58 |
* Constructs a syntax highlighter. |
| 59 |
*/ |
| 60 |
SyntaxHighlighter::SyntaxHighlighter(QObject* parent) |
| 61 |
: QSyntaxHighlighter(parent) |
| 62 |
{ |
| 63 |
d = new SyntaxHighlighterP; |
| 64 |
} |
| 65 |
|
| 66 |
/** Destroys this object. */ |
| 67 |
SyntaxHighlighter::~SyntaxHighlighter() |
| 68 |
{ |
| 69 |
delete d; |
| 70 |
} |
| 71 |
|
| 72 |
/** Sets the color \a col to the paragraph type \a type. |
| 73 |
* This method is provided for convenience to specify the paragraph type |
| 74 |
* by its name. |
| 75 |
*/ |
| 76 |
void SyntaxHighlighter::setColor(const QString& type, const QColor& col) |
| 77 |
{ |
| 78 |
// Rehighlighting is very expensive, thus avoid it if this color is already set |
| 79 |
QColor old = color(type); |
| 80 |
if (!old.isValid()) { |
| 81 |
return; // no such type |
| 82 |
} |
| 83 |
if (old == col) { |
| 84 |
return; |
| 85 |
} |
| 86 |
if (type == QLatin1String("Text")) { |
| 87 |
d->cNormalText = col; |
| 88 |
} |
| 89 |
else if (type == QLatin1String("Comment")) { |
| 90 |
d->cComment = col; |
| 91 |
} |
| 92 |
else if (type == QLatin1String("Block comment")) { |
| 93 |
d->cBlockcomment = col; |
| 94 |
} |
| 95 |
else if (type == QLatin1String("Number")) { |
| 96 |
d->cNumber = col; |
| 97 |
} |
| 98 |
else if (type == QLatin1String("String")) { |
| 99 |
d->cLiteral = col; |
| 100 |
} |
| 101 |
else if (type == QLatin1String("Keyword")) { |
| 102 |
d->cKeyword = col; |
| 103 |
} |
| 104 |
else if (type == QLatin1String("Class name")) { |
| 105 |
d->cClassName = col; |
| 106 |
} |
| 107 |
else if (type == QLatin1String("Define name")) { |
| 108 |
d->cDefineName = col; |
| 109 |
} |
| 110 |
else if (type == QLatin1String("Operator")) { |
| 111 |
d->cOperator = col; |
| 112 |
} |
| 113 |
else if (type == QLatin1String("Python output")) { |
| 114 |
d->cOutput = col; |
| 115 |
} |
| 116 |
else if (type == QLatin1String("Python error")) { |
| 117 |
d->cError = col; |
| 118 |
} |
| 119 |
colorChanged(type, col); |
| 120 |
} |
| 121 |
|
| 122 |
QColor SyntaxHighlighter::color(const QString& type) |
| 123 |
{ |
| 124 |
if (type == QLatin1String("Text")) { |
| 125 |
return d->cNormalText; |
| 126 |
} |
| 127 |
else if (type == QLatin1String("Comment")) { |
| 128 |
return d->cComment; |
| 129 |
} |
| 130 |
else if (type == QLatin1String("Block comment")) { |
| 131 |
return d->cBlockcomment; |
| 132 |
} |
| 133 |
else if (type == QLatin1String("Number")) { |
| 134 |
return d->cNumber; |
| 135 |
} |
| 136 |
else if (type == QLatin1String("String")) { |
| 137 |
return d->cLiteral; |
| 138 |
} |
| 139 |
else if (type == QLatin1String("Keyword")) { |
| 140 |
return d->cKeyword; |
| 141 |
} |
| 142 |
else if (type == QLatin1String("Class name")) { |
| 143 |
return d->cClassName; |
| 144 |
} |
| 145 |
else if (type == QLatin1String("Define name")) { |
| 146 |
return d->cDefineName; |
| 147 |
} |
| 148 |
else if (type == QLatin1String("Operator")) { |
| 149 |
return d->cOperator; |
| 150 |
} |
| 151 |
else if (type == QLatin1String("Python output")) { |
| 152 |
return d->cOutput; |
| 153 |
} |
| 154 |
else if (type == QLatin1String("Python error")) { |
| 155 |
return d->cError; |
| 156 |
} |
| 157 |
else { |
| 158 |
return {}; // not found |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
QColor SyntaxHighlighter::colorByType(SyntaxHighlighter::TColor type) |
| 163 |
{ |
| 164 |
if (type == SyntaxHighlighter::Text) { |
| 165 |
return d->cNormalText; |
| 166 |
} |
| 167 |
else if (type == SyntaxHighlighter::Comment) { |
| 168 |
return d->cComment; |
| 169 |
} |
| 170 |
else if (type == SyntaxHighlighter::BlockComment) { |
| 171 |
return d->cBlockcomment; |
| 172 |
} |
| 173 |
else if (type == SyntaxHighlighter::Number) { |
| 174 |
return d->cNumber; |
| 175 |
} |
| 176 |
else if (type == SyntaxHighlighter::String) { |
| 177 |
return d->cLiteral; |
| 178 |
} |
| 179 |
else if (type == SyntaxHighlighter::Keyword) { |
| 180 |
return d->cKeyword; |
| 181 |
} |
| 182 |
else if (type == SyntaxHighlighter::Classname) { |
| 183 |
return d->cClassName; |
| 184 |
} |
| 185 |
else if (type == SyntaxHighlighter::Defname) { |
| 186 |
return d->cDefineName; |
| 187 |
} |
| 188 |
else if (type == SyntaxHighlighter::Operator) { |
| 189 |
return d->cOperator; |
| 190 |
} |
| 191 |
else if (type == SyntaxHighlighter::Output) { |
| 192 |
return d->cOutput; |
| 193 |
} |
| 194 |
else if (type == SyntaxHighlighter::Error) { |
| 195 |
return d->cError; |
| 196 |
} |
| 197 |
else { |
| 198 |
return {}; // not found |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
void SyntaxHighlighter::colorChanged(const QString& type, const QColor& col) |
| 203 |
{ |
| 204 |
Q_UNUSED(type); |
| 205 |
Q_UNUSED(col); |
| 206 |
rehighlight(); |
| 207 |
} |
| 208 |
|
| 209 |
int SyntaxHighlighter::maximumUserState() const |
| 210 |
{ |
| 211 |
return 8; |
| 212 |
} |