| 1 | /* |
| 2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@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 <https://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #include "HighlighterManager.h" |
| 18 | #include <QFileInfo> |
| 19 | #include <QObject> |
| 20 | #include "qsynedit/highlighter/cpp.h" |
| 21 | #include "qsynedit/highlighter/asm.h" |
| 22 | #include "qsynedit/highlighter/glsl.h" |
| 23 | #include "qsynedit/Constants.h" |
| 24 | #include "colorscheme.h" |
| 25 | |
| 26 | HighlighterManager highlighterManager; |
| 27 | |
| 28 | HighlighterManager::HighlighterManager() |
| 29 | { |
| 30 | |
| 31 | } |
| 32 | |
| 33 | QSynedit::PHighlighter HighlighterManager::getHighlighter(const QString &filename) |
| 34 | { |
| 35 | QFileInfo info(filename); |
| 36 | QString suffix = info.suffix(); |
| 37 | if (suffix.isEmpty() || suffix == "c" || suffix == "cpp" || suffix == "cxx" |
| 38 | || suffix == "cc" || suffix == "h" || suffix == "hpp" |
| 39 | || suffix == "hxx" || suffix == "hh" || suffix == "C" |
| 40 | || suffix == "CPP" || suffix =="H" || suffix == "c++" |
| 41 | || suffix == "h++" ) { |
| 42 | return getCppHighlighter(); |
| 43 | } else if (suffix == "vs" || suffix == "fs" || suffix == "frag" ) { |
| 44 | return getGLSLHighlighter(); |
| 45 | } |
| 46 | return QSynedit::PHighlighter(); |
| 47 | } |
| 48 | |
| 49 | QSynedit::PHighlighter HighlighterManager::copyHighlighter(QSynedit::PHighlighter highlighter) |
| 50 | { |
| 51 | if (!highlighter) |
| 52 | return QSynedit::PHighlighter(); |
| 53 | if (highlighter->getName() == SYN_HIGHLIGHTER_CPP) |
| 54 | return getCppHighlighter(); |
| 55 | else if (highlighter->getName() == SYN_HIGHLIGHTER_ASM) |
| 56 | return getAsmHighlighter(); |
| 57 | else if (highlighter->getName() == SYN_HIGHLIGHTER_GLSL) |
| 58 | return getGLSLHighlighter(); |
| 59 | //todo |
| 60 | return QSynedit::PHighlighter(); |
| 61 | } |
| 62 | |
| 63 | QSynedit::PHighlighter HighlighterManager::getCppHighlighter() |
| 64 | { |
| 65 | QSynedit::CppHighlighter* highlighter = new QSynedit::CppHighlighter(); |
| 66 | highlighter->asmAttribute()->setForeground(Qt::blue); |
| 67 | highlighter->charAttribute()->setForeground(Qt::black); |
| 68 | highlighter->commentAttribute()->setForeground(0x8C8C8C); |
| 69 | highlighter->commentAttribute()->setStyles(QSynedit::FontStyle::fsItalic); |
| 70 | highlighter->classAttribute()->setForeground(0x008080); |
| 71 | highlighter->floatAttribute()->setForeground(Qt::darkMagenta); |
| 72 | highlighter->functionAttribute()->setForeground(0x00627A); |
| 73 | highlighter->globalVarAttribute()->setForeground(0x660E7A); |
| 74 | highlighter->hexAttribute()->setForeground(Qt::darkMagenta); |
| 75 | highlighter->identifierAttribute()->setForeground(0x080808); |
| 76 | highlighter->invalidAttribute()->setForeground(Qt::red); |
| 77 | highlighter->localVarAttribute()->setForeground(Qt::black); |
| 78 | highlighter->numberAttribute()->setForeground(0x1750EB); |
| 79 | highlighter->octAttribute()->setForeground(Qt::darkMagenta); |
| 80 | highlighter->preprocessorAttribute()->setForeground(0x1f542e); |
| 81 | highlighter->keywordAttribute()->setForeground(0x0033b3); |
| 82 | highlighter->whitespaceAttribute()->setForeground(Qt::lightGray); |
| 83 | highlighter->stringAttribute()->setForeground(0x007d17); |
| 84 | highlighter->stringEscapeSequenceAttribute()->setForeground(Qt::red); |
| 85 | highlighter->symbolAttribute()->setForeground(0xc10000); |
| 86 | highlighter->variableAttribute()->setForeground(0x400080); |
| 87 | QSynedit::PHighlighter pHighlighter=std::make_shared<QSynedit::CppHighlighter>(); |
| 88 | return pHighlighter; |
| 89 | } |
| 90 | |
| 91 | QSynedit::PHighlighter HighlighterManager::getAsmHighlighter() |
| 92 | { |
| 93 | QSynedit::ASMHighlighter* highlighter = new QSynedit::ASMHighlighter(); |
| 94 | QSynedit::PHighlighter pHighlighter(highlighter); |
| 95 | highlighter->commentAttribute()->setForeground(0x8C8C8C); |
| 96 | highlighter->commentAttribute()->setStyles(QSynedit::FontStyle::fsItalic); |
| 97 | highlighter->identifierAttribute()->setForeground(0x080808); |
| 98 | highlighter->keywordAttribute()->setForeground(0x0033b3); |
| 99 | highlighter->numberAttribute()->setForeground(0x1750EB); |
| 100 | highlighter->whitespaceAttribute()->setForeground(Qt::lightGray); |
| 101 | highlighter->stringAttribute()->setForeground(0x007d17); |
| 102 | highlighter->symbolAttribute()->setForeground(0xc10000); |
| 103 | return pHighlighter; |
| 104 | } |
| 105 | |
| 106 | QSynedit::PHighlighter HighlighterManager::getGLSLHighlighter() |
| 107 | { |
| 108 | QSynedit::GLSLHighlighter* highlighter = new QSynedit::GLSLHighlighter(); |
| 109 | QSynedit::PHighlighter pHighlighter(highlighter); |
| 110 | highlighter->asmAttribute()->setForeground(Qt::blue); |
| 111 | highlighter->charAttribute()->setForeground(Qt::black); |
| 112 | highlighter->commentAttribute()->setForeground(0x8C8C8C); |
| 113 | highlighter->commentAttribute()->setStyles(QSynedit::FontStyle::fsItalic); |
| 114 | highlighter->classAttribute()->setForeground(0x008080); |
| 115 | highlighter->floatAttribute()->setForeground(Qt::darkMagenta); |
| 116 | highlighter->functionAttribute()->setForeground(0x00627A); |
| 117 | highlighter->globalVarAttribute()->setForeground(0x660E7A); |
| 118 | highlighter->hexAttribute()->setForeground(Qt::darkMagenta); |
| 119 | highlighter->identifierAttribute()->setForeground(0x080808); |
| 120 | highlighter->invalidAttribute()->setForeground(Qt::red); |
| 121 | highlighter->localVarAttribute()->setForeground(Qt::black); |
| 122 | highlighter->numberAttribute()->setForeground(0x1750EB); |
| 123 | highlighter->octAttribute()->setForeground(Qt::darkMagenta); |
| 124 | highlighter->preprocessorAttribute()->setForeground(0x1f542e); |
| 125 | highlighter->keywordAttribute()->setForeground(0x0033b3); |
| 126 | highlighter->whitespaceAttribute()->setForeground(Qt::lightGray); |
| 127 | highlighter->stringAttribute()->setForeground(0x007d17); |
| 128 | highlighter->stringEscapeSequenceAttribute()->setForeground(Qt::red); |
| 129 | highlighter->symbolAttribute()->setForeground(0xc10000); |
| 130 | highlighter->variableAttribute()->setForeground(0x400080); |
| 131 | return pHighlighter; |
| 132 | } |
| 133 | |
| 134 | void HighlighterManager::applyColorScheme(QSynedit::PHighlighter highlighter, const QString &schemeName) |
| 135 | { |
| 136 | if (!highlighter) |
| 137 | return; |
| 138 | if ( (highlighter->getName() == SYN_HIGHLIGHTER_CPP) |
| 139 | || (highlighter->getName() == SYN_HIGHLIGHTER_ASM) |
| 140 | ) { |
| 141 | for (QString name: highlighter->attributes().keys()) { |
| 142 | PColorSchemeItem item = pColorManager->getItem(schemeName,name); |
| 143 | if (item) { |
| 144 | QSynedit::PHighlighterAttribute attr = highlighter->attributes()[name]; |
| 145 | attr->setBackground(item->background()); |
| 146 | attr->setForeground(item->foreground()); |
| 147 | QSynedit::FontStyles styles = QSynedit::FontStyle::fsNone; |
| 148 | styles.setFlag(QSynedit::FontStyle::fsBold, item->bold()); |
| 149 | styles.setFlag(QSynedit::FontStyle::fsItalic, item->italic()); |
| 150 | styles.setFlag(QSynedit::FontStyle::fsUnderline, item->underlined()); |
| 151 | styles.setFlag(QSynedit::FontStyle::fsStrikeOut, item->strikeout()); |
| 152 | attr->setStyles(styles); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |