| 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 | #ifndef COLORSCHEME_H |
| 18 | #define COLORSCHEME_H |
| 19 | |
| 20 | #include <QColor> |
| 21 | #include "qsynedit/highlighter/base.h" |
| 22 | #include "parser/statementmodel.h" |
| 23 | |
| 24 | #define EXT_COLOR_SCHEME ".scheme" |
| 25 | #define EXT_PREFIX_CUSTOM ".custom" |
| 26 | |
| 27 | #define COLOR_SCHEME_BREAKPOINT "Breakpoint" |
| 28 | #define COLOR_SCHEME_ERROR "Error" |
| 29 | #define COLOR_SCHEME_ACTIVE_BREAKPOINT "Active Breakpoint" |
| 30 | #define COLOR_SCHEME_GUTTER "Gutter" |
| 31 | #define COLOR_SCHEME_GUTTER_ACTIVE_LINE "Gutter Active Line" |
| 32 | #define COLOR_SCHEME_SELECTION "Selected text" |
| 33 | #define COLOR_SCHEME_TEXT "Editor Text" |
| 34 | #define COLOR_SCHEME_CURRENT_HIGHLIGHTED_WORD "Current Highlighted Word" |
| 35 | #define COLOR_SCHEME_FOLD_LINE "Fold Line" |
| 36 | #define COLOR_SCHEME_ACTIVE_LINE "Active Line" |
| 37 | #define COLOR_SCHEME_WARNING "Warning" |
| 38 | #define COLOR_SCHEME_INDENT_GUIDE_LINE "Indent Guide Line" |
| 39 | #define COLOR_SCHEME_BRACE_1 "brace/parenthesis/bracket level 1" |
| 40 | #define COLOR_SCHEME_BRACE_2 "brace/parenthesis/bracket level 2" |
| 41 | #define COLOR_SCHEME_BRACE_3 "brace/parenthesis/bracket level 3" |
| 42 | #define COLOR_SCHEME_BRACE_4 "brace/parenthesis/bracket level 4" |
| 43 | |
| 44 | |
| 45 | class ColorSchemeItem; |
| 46 | using PColorSchemeItem = std::shared_ptr<ColorSchemeItem>; |
| 47 | class ColorSchemeItem { |
| 48 | |
| 49 | public: |
| 50 | explicit ColorSchemeItem(); |
| 51 | |
| 52 | QColor foreground() const; |
| 53 | void setForeground(const QColor &foreground); |
| 54 | |
| 55 | QColor background() const; |
| 56 | void setBackground(const QColor &background); |
| 57 | |
| 58 | bool bold() const; |
| 59 | void setBold(bool bold); |
| 60 | |
| 61 | bool italic() const; |
| 62 | void setItalic(bool italic); |
| 63 | |
| 64 | bool underlined() const; |
| 65 | void setUnderlined(bool underlined); |
| 66 | |
| 67 | bool strikeout() const; |
| 68 | void setStrikeout(bool strikeout); |
| 69 | |
| 70 | static PColorSchemeItem fromJson(const QJsonObject& json); |
| 71 | void toJson(QJsonObject& json); |
| 72 | |
| 73 | private: |
| 74 | QColor mForeground; |
| 75 | QColor mBackground; |
| 76 | bool mBold; |
| 77 | bool mItalic; |
| 78 | bool mUnderlined; |
| 79 | bool mStrikeout; |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | class ColorScheme; |
| 84 | using PColorScheme = std::shared_ptr<ColorScheme>; |
| 85 | class ColorScheme |
| 86 | { |
| 87 | public: |
| 88 | explicit ColorScheme(); |
| 89 | |
| 90 | static PColorScheme load(const QString& filename); |
| 91 | |
| 92 | void addItem(const QString& name); |
| 93 | |
| 94 | QMap<QString,PColorSchemeItem> items(); |
| 95 | |
| 96 | static PColorScheme fromJson(const QJsonObject& json); |
| 97 | void toJson(QJsonObject& json); |
| 98 | |
| 99 | //void load(); |
| 100 | void save(const QString& filename); |
| 101 | |
| 102 | bool bundled() const; |
| 103 | void setBundled(bool bundled); |
| 104 | |
| 105 | bool customed() const; |
| 106 | void setCustomed(bool customed); |
| 107 | |
| 108 | QString preferThemeType() const; |
| 109 | void setPreferThemeType(const QString &preferThemeType); |
| 110 | private: |
| 111 | QMap<QString,PColorSchemeItem> mItems; |
| 112 | QString mPreferThemeType; |
| 113 | bool mBundled; |
| 114 | bool mCustomed; |
| 115 | }; |
| 116 | |
| 117 | class ColorSchemeItemDefine { |
| 118 | public: |
| 119 | explicit ColorSchemeItemDefine(); |
| 120 | bool hasBackground() const; |
| 121 | void setHasBackground(bool hasBackground); |
| 122 | |
| 123 | bool hasForeground() const; |
| 124 | void setHasForeground(bool hasForeground); |
| 125 | |
| 126 | bool hasFontStyle() const; |
| 127 | void setHasFontStyle(bool value); |
| 128 | |
| 129 | QString group() const; |
| 130 | void setGroup(const QString &group); |
| 131 | |
| 132 | QString displayName() const; |
| 133 | void setDisplayName(const QString &displayName); |
| 134 | |
| 135 | private: |
| 136 | bool mHasBackground; |
| 137 | bool mHasForeground; |
| 138 | bool mHasFontStyle; |
| 139 | QString mGroup; |
| 140 | QString mDisplayName; |
| 141 | }; |
| 142 | |
| 143 | using PColorSchemeItemDefine = std::shared_ptr<ColorSchemeItemDefine>; |
| 144 | |
| 145 | class ColorManager { |
| 146 | public: |
| 147 | explicit ColorManager(); |
| 148 | void init(); |
| 149 | void reload(); |
| 150 | QStringList getSchemes(const QString& themeType = QString()); |
| 151 | QStringList getDefines(); |
| 152 | |
| 153 | bool exists(const QString name); |
| 154 | QString copy(const QString& source); |
| 155 | bool restoreToDefault(const QString& name); |
| 156 | bool remove(const QString& name); |
| 157 | bool rename(const QString& oldName, const QString& newName); |
| 158 | bool add(const QString& name, PColorScheme scheme); |
| 159 | PColorScheme get(const QString& name); |
| 160 | PColorSchemeItem getItem(const QString& schemeName, const QString& itemName); |
| 161 | bool isValidName(const QString& name); |
| 162 | void addDefine(const QString& name, const QString& displayName, const QString& group, bool hasForeground, bool hasBackground, bool hasFontStyle); |
| 163 | bool removeDefine(const QString &name); |
| 164 | PColorSchemeItemDefine getDefine(const QString& name); |
| 165 | bool saveScheme(const QString &name); |
| 166 | void updateStatementColors( |
| 167 | std::shared_ptr<QHash<StatementKind, std::shared_ptr<ColorSchemeItem> > > statementColors, |
| 168 | const QString& schemeName); |
| 169 | private: |
| 170 | QString generateFullPathname(const QString& name, bool isBundled, bool isCustomed); |
| 171 | QString generateFilename(const QString& name, bool isCustomed); |
| 172 | void loadSchemesInDir(const QString& dirName, bool isBundled, bool isCustomed); |
| 173 | void initItemDefines(); |
| 174 | private: |
| 175 | QMap<QString,PColorSchemeItemDefine> mSchemeItemDefines; |
| 176 | QMap<QString,PColorScheme> mSchemes; |
| 177 | PColorSchemeItemDefine mDefaultSchemeItemDefine; |
| 178 | }; |
| 179 | |
| 180 | extern ColorManager * pColorManager; |
| 181 | |
| 182 | #endif // COLORSCHEME_H |
| 183 | |