RedPanda-CPP/RedPandaIDE/colorscheme.cpp

814 lines
24 KiB
C++
Raw Normal View History

2021-12-26 23:18:28 +08:00
/*
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-06-17 10:39:46 +08:00
#include "colorscheme.h"
2021-06-18 08:09:32 +08:00
#include <QDir>
2021-06-17 10:39:46 +08:00
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include "utils.h"
2021-06-18 08:09:32 +08:00
#include "settings.h"
2023-01-11 16:22:26 +08:00
#include "qsynedit/constants.h"
2021-06-17 10:39:46 +08:00
2021-06-18 21:48:40 +08:00
ColorManager * pColorManager;
2021-06-17 10:39:46 +08:00
ColorScheme::ColorScheme()
{
}
2021-06-18 22:57:19 +08:00
PColorScheme ColorScheme::fromJson(const QJsonObject &json)
2021-06-17 10:39:46 +08:00
{
2021-06-18 22:57:19 +08:00
PColorScheme scheme = std::make_shared<ColorScheme>();
scheme->mItems.clear();
for (QString key:json.keys()) {
if (json[key].isObject()) {
scheme->mItems[key]=ColorSchemeItem::fromJson(json[key].toObject());
2021-06-17 10:39:46 +08:00
}
}
//backward compatibility (for config files version < 2.5)
if (!scheme->mItems.contains(SYNS_AttrReserveWord_Type)
&& scheme->mItems.contains(SYNS_AttrIdentifier)) {
scheme->mItems.insert(SYNS_AttrReserveWord_Type,
ColorSchemeItem::fromJson(json[SYNS_AttrReservedWord].toObject()));
}
2021-06-18 22:57:19 +08:00
return scheme;
2021-06-17 10:39:46 +08:00
}
2021-06-18 22:57:19 +08:00
void ColorScheme::toJson(QJsonObject &json)
2021-06-17 10:39:46 +08:00
{
2021-06-18 22:57:19 +08:00
for (QString key:mItems.keys()) {
PColorSchemeItem item = mItems[key];
if (item) {
2021-06-17 10:39:46 +08:00
QJsonObject itemObject;
2021-06-18 22:57:19 +08:00
item->toJson(itemObject);
json[key] = itemObject;
2021-06-17 10:39:46 +08:00
}
}
}
2021-06-18 08:09:32 +08:00
PColorScheme ColorScheme::load(const QString &filename)
2021-06-17 10:39:46 +08:00
{
QFile file(filename);
if (!file.open(QFile::ReadOnly)) {
2021-06-20 09:27:37 +08:00
qDebug()<<QObject::tr("Can't open file '%1' for read").arg(file.fileName());
return PColorScheme();
2021-06-17 10:39:46 +08:00
}
2024-04-01 19:33:28 +08:00
QByteArray content = file.readAll().trimmed();
if (content.isEmpty())
return PColorScheme();
2021-06-17 10:39:46 +08:00
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(content,&error);
if (error.error!=QJsonParseError::NoError) {
2021-06-20 09:27:37 +08:00
qDebug()<<QObject::tr("Can't parse json file '%1' at offset %2! Error Code: %3")
.arg(file.fileName()).arg(error.offset).arg(error.error);
2021-06-17 10:39:46 +08:00
}
if (!doc.isObject()) {
2021-06-20 14:30:47 +08:00
qDebug()<<QObject::tr("Can't parse json file '%1' is not a color scheme config file!")
2021-06-20 09:27:37 +08:00
.arg(file.fileName());
2021-06-17 10:39:46 +08:00
}
2021-06-18 22:57:19 +08:00
return ColorScheme::fromJson(doc.object());
2021-06-17 10:39:46 +08:00
}
void ColorScheme::addItem(const QString& name)
{
if (mItems.contains(name))
return;
PColorSchemeItem item = std::make_shared<ColorSchemeItem>();
mItems[name]=item;
}
QMap<QString, PColorSchemeItem> ColorScheme::items()
{
return mItems;
}
2021-06-17 10:39:46 +08:00
void ColorScheme::save(const QString &filename)
{
QFile file(filename);
2021-06-20 14:30:47 +08:00
QFileInfo info(filename);
info.dir().mkpath(info.dir().absolutePath());
2021-06-17 10:39:46 +08:00
if (!file.open(QFile::WriteOnly)) {
2021-06-20 14:30:47 +08:00
throw FileError(QObject::tr("Can't open file '%1' for write").arg(file.fileName()));
2021-06-17 10:39:46 +08:00
}
QJsonObject json;
2021-06-18 22:57:19 +08:00
toJson(json);
2021-06-17 10:39:46 +08:00
QJsonDocument doc(json);
QByteArray content = doc.toJson();
file.write(content);
}
bool ColorScheme::bundled() const
{
return mBundled;
}
void ColorScheme::setBundled(bool bundled)
{
mBundled = bundled;
}
2021-06-18 08:09:32 +08:00
bool ColorScheme::customed() const
2021-06-17 10:39:46 +08:00
{
2021-06-18 08:09:32 +08:00
return mCustomed;
2021-06-17 10:39:46 +08:00
}
2021-06-18 08:09:32 +08:00
void ColorScheme::setCustomed(bool customed)
2021-06-17 10:39:46 +08:00
{
2021-06-18 08:09:32 +08:00
mCustomed = customed;
2021-06-17 10:39:46 +08:00
}
QString ColorScheme::preferThemeType() const
{
return mPreferThemeType;
}
void ColorScheme::setPreferThemeType(const QString &preferThemeType)
{
mPreferThemeType = preferThemeType;
}
2021-06-18 22:57:19 +08:00
ColorSchemeItem::ColorSchemeItem():
2021-06-17 10:39:46 +08:00
mForeground(),
mBackground(),
mBold(false),
mItalic(false),
mUnderlined(false),
mStrikeout(false)
{
}
QColor ColorSchemeItem::foreground() const
{
return mForeground;
}
void ColorSchemeItem::setForeground(const QColor &foreground)
{
mForeground = foreground;
}
QColor ColorSchemeItem::background() const
{
return mBackground;
}
void ColorSchemeItem::setBackground(const QColor &background)
{
mBackground = background;
}
bool ColorSchemeItem::bold() const
{
return mBold;
}
void ColorSchemeItem::setBold(bool bold)
{
mBold = bold;
}
bool ColorSchemeItem::italic() const
{
return mItalic;
}
void ColorSchemeItem::setItalic(bool italic)
{
mItalic = italic;
}
bool ColorSchemeItem::underlined() const
{
return mUnderlined;
}
void ColorSchemeItem::setUnderlined(bool underlined)
{
mUnderlined = underlined;
}
bool ColorSchemeItem::strikeout() const
{
return mStrikeout;
}
void ColorSchemeItem::setStrikeout(bool strikeout)
{
mStrikeout = strikeout;
}
2021-06-18 22:57:19 +08:00
PColorSchemeItem ColorSchemeItem::fromJson(const QJsonObject &json)
2021-06-17 10:39:46 +08:00
{
2021-06-18 22:57:19 +08:00
PColorSchemeItem item = std::make_shared<ColorSchemeItem>();
2021-06-17 10:39:46 +08:00
if (json.contains("foreground") && json["foreground"].isString()) {
2021-06-18 22:57:19 +08:00
item->setForeground(json["foreground"].toString());
2021-06-17 10:39:46 +08:00
} else {
2021-06-18 22:57:19 +08:00
item->setForeground(QColor());
2021-06-17 10:39:46 +08:00
}
if (json.contains("background") && json["background"].isString()) {
2021-06-18 22:57:19 +08:00
item->setBackground(json["background"].toString());
2021-06-17 10:39:46 +08:00
} else {
2021-06-18 22:57:19 +08:00
item->setBackground(QColor());
2021-06-17 10:39:46 +08:00
}
if (json.contains("bold") && json["bold"].isBool()) {
2021-06-18 22:57:19 +08:00
item->setBold(json["bold"].toBool());
2021-06-17 10:39:46 +08:00
} else {
2021-06-18 22:57:19 +08:00
item->setBold(false);
2021-06-17 10:39:46 +08:00
}
if (json.contains("italic") && json["italic"].isBool()) {
item->setItalic(json["italic"].toBool());
2021-06-17 10:39:46 +08:00
} else {
2021-06-18 22:57:19 +08:00
item->setItalic(false);
2021-06-17 10:39:46 +08:00
}
if (json.contains("underlined") && json["underlined"].isBool()) {
item->setUnderlined(json["underlined"].toBool());
2021-06-17 10:39:46 +08:00
} else {
2021-06-18 22:57:19 +08:00
item->setUnderlined(false);
2021-06-17 10:39:46 +08:00
}
if (json.contains("strikeout") && json["strikeout"].isBool()) {
item->setStrikeout(json["strikeout"].toBool());
2021-06-17 10:39:46 +08:00
} else {
item->setStrikeout(false);
2021-06-17 10:39:46 +08:00
}
2021-06-18 22:57:19 +08:00
return item;
2021-06-17 10:39:46 +08:00
}
2021-06-18 22:57:19 +08:00
void ColorSchemeItem::toJson(QJsonObject &json)
2021-06-17 10:39:46 +08:00
{
if (mForeground.isValid()) {
json["foreground"] = mForeground.name(QColor::HexArgb);
2021-06-17 10:39:46 +08:00
} else if (json.contains("foreground")){
json.remove("foreground");
}
if (mBackground.isValid()) {
json["background"] = mBackground.name(QColor::HexArgb);
2021-06-17 10:39:46 +08:00
} else if (json.contains("background")){
json.remove("background");
}
json["bold"] = mBold;
json["italic"] = mItalic;
json["underlined"] = mUnderlined;
json["strikeout"] = mStrikeout;
}
2021-06-18 08:09:32 +08:00
ColorManager::ColorManager()
{
mDefaultSchemeItemDefine = std::make_shared<ColorSchemeItemDefine>();
initItemDefines();
2021-06-18 21:48:40 +08:00
init();
2021-06-18 08:09:32 +08:00
}
2021-06-17 10:39:46 +08:00
void ColorManager::init()
{
reload();
}
2021-06-18 08:09:32 +08:00
void ColorManager::reload()
{
mSchemes.clear();
//bundled schemes ( the lowest priority)
2022-01-28 16:13:20 +08:00
loadSchemesInDir(pSettings->dirs().data(Settings::Dirs::DataType::ColorScheme),true,false);
2021-06-18 08:09:32 +08:00
//config schemes ( higher priority)
2022-01-28 16:13:20 +08:00
loadSchemesInDir(pSettings->dirs().config(Settings::Dirs::DataType::ColorScheme),false,false);
2021-06-18 08:09:32 +08:00
//customed schemes ( highest priority)
2022-01-28 16:13:20 +08:00
loadSchemesInDir(pSettings->dirs().config(Settings::Dirs::DataType::ColorScheme),false,true);
2021-06-18 08:09:32 +08:00
}
QStringList ColorManager::getSchemes(const QString &themeType)
{
if (themeType.isEmpty()) {
return mSchemes.keys();
}
QStringList lst;
for (QString name:mSchemes.keys()) {
PColorScheme scheme = mSchemes[name];
if (scheme && scheme->preferThemeType() == themeType) {
lst.append(name);
}
}
return lst;
}
QStringList ColorManager::getDefines()
{
return mSchemeItemDefines.keys();
}
2021-06-18 08:09:32 +08:00
bool ColorManager::exists(const QString name)
{
return mSchemes.contains(name);
}
2021-06-20 14:49:38 +08:00
QString ColorManager::copy(const QString &sourceName)
2021-06-17 10:39:46 +08:00
{
if (!mSchemes.contains(sourceName))
2021-06-20 14:49:38 +08:00
return QString();
2021-06-17 10:39:46 +08:00
PColorScheme sourceScheme = mSchemes[sourceName];
2021-06-18 08:09:32 +08:00
QString newName = sourceName+" Copy";
if (mSchemes.contains(newName))
2021-06-20 14:49:38 +08:00
return QString();
2021-06-18 08:09:32 +08:00
// save source with the new name
QString newFilepath = generateFullPathname(newName,false,false);
sourceScheme->save(newFilepath);
2021-06-17 10:39:46 +08:00
// then load it to the copied
2021-06-18 08:09:32 +08:00
PColorScheme newScheme = ColorScheme::load(newFilepath);
newScheme->setBundled(false);
newScheme->setCustomed(false);
mSchemes[newName]=newScheme;
2021-06-20 14:49:38 +08:00
return newName;
2021-06-18 08:09:32 +08:00
}
2021-06-17 10:39:46 +08:00
2021-06-20 22:54:16 +08:00
bool ColorManager::restoreToDefault(const QString &name)
{
PColorScheme scheme = get(name);
if (!scheme)
return false;
if (!scheme->customed())
return false;
QString fullPath = generateFullPathname(name,scheme->bundled(),false);
PColorScheme oldScheme = ColorScheme::load(fullPath);
if (!oldScheme)
throw FileError(QObject::tr("Can't Find the color scheme file %1!").arg(fullPath));
fullPath = generateFullPathname(name,scheme->bundled(),true);
QFile file(fullPath);
if (file.exists() && !file.remove())
throw FileError(QObject::tr("Can't remove the color scheme file %1!").arg(fullPath));
oldScheme->setBundled(scheme->bundled());
oldScheme->setCustomed(false);
mSchemes[name]=oldScheme;
return true;
}
bool ColorManager::remove(const QString &name)
{
PColorScheme scheme = get(name);
if (!scheme)
return false;
if (scheme->bundled())
return false;
if (scheme->customed()) {
QString fullPath = generateFullPathname(name,false,true);
QFile file(fullPath);
if (!file.remove())
throw FileError(QObject::tr("Can't remove the color scheme file %1!").arg(fullPath));
}
QString fullPath = generateFullPathname(name,false,false);
QFile file(fullPath);
if (!file.remove())
throw FileError(QObject::tr("Can't remove the color scheme file %1!").arg(fullPath));
mSchemes.remove(name);
return true;
}
2021-06-18 08:09:32 +08:00
QString ColorManager::generateFilename(const QString &name, bool isCustomed)
{
QString newName = name;
newName.replace(' ','_');
if (isCustomed)
newName += EXT_PREFIX_CUSTOM;
return newName += EXT_COLOR_SCHEME;
}
2021-06-20 14:30:47 +08:00
void ColorManager::loadSchemesInDir(const QString &dirName, bool isBundled, bool isCustomed)
2021-06-18 08:09:32 +08:00
{
QDir dir(dirName);
dir.setFilter(QDir::Files);
QFileInfoList list = dir.entryInfoList();
QString suffix;
2021-06-20 14:30:47 +08:00
QString customSuffix = EXT_PREFIX_CUSTOM;
customSuffix += EXT_COLOR_SCHEME;
2021-06-18 08:09:32 +08:00
if (isCustomed) {
2021-06-20 14:30:47 +08:00
suffix = customSuffix;
2021-06-18 08:09:32 +08:00
} else {
suffix = EXT_COLOR_SCHEME;
}
for (int i=0;i<list.size();i++) {
QFileInfo fileInfo = list[i];
QString name = fileInfo.fileName();
if (name.toLower().endsWith(suffix)) {
2021-06-20 22:54:16 +08:00
// if (!isCustomed && name.toLower().endsWith(customSuffix))
// continue;
2021-06-18 22:57:19 +08:00
name.remove(name.length()-suffix.length(),suffix.length());
name.replace('_',' ');
2021-06-20 22:54:16 +08:00
if (!isValidName(name))
continue;
PColorScheme scheme = ColorScheme::load(fileInfo.absoluteFilePath());
2021-06-20 14:30:47 +08:00
if (!isCustomed) {
scheme->setBundled(isBundled);
scheme->setCustomed(false);
} else {
scheme->setBundled(false);
if (mSchemes.contains(name)) {
PColorScheme oldScheme = mSchemes[name];
if (oldScheme) {
scheme->setBundled(oldScheme->bundled());
}
mSchemes.remove(name);
}
scheme->setCustomed(true);
}
2021-06-18 22:57:19 +08:00
mSchemes[name]=scheme;
2021-06-18 08:09:32 +08:00
}
}
}
void ColorManager::initItemDefines()
{
2022-12-10 21:23:49 +08:00
//Token highlight colors
// addDefine(SYNS_AttrAssembler,
// QObject::tr("Assembler"),
// QObject::tr("Syntax"),
// true,true,true);
addDefine(SYNS_AttrCharacter,
QObject::tr("Character"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrComment,
QObject::tr("Comment"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrClass,
QObject::tr("Class"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrFloat,
QObject::tr("Float"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrFunction,
QObject::tr("Function"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrGlobalVariable,
QObject::tr("Gloabal Variable"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrHexadecimal,
QObject::tr("Hexadecimal Integer"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrIdentifier,
QObject::tr("Identifier"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrIllegalChar,
QObject::tr("Illegal Char"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrLocalVariable,
QObject::tr("Local Variable"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrNumber,
QObject::tr("Integer"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrOctal,
QObject::tr("Octal Integer"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrPreprocessor,
QObject::tr("Preprocessor"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrReservedWord,
QObject::tr("Reserve Word"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrReserveWord_Type,
QObject::tr("Reserve Word for Types"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrSpace,
QObject::tr("Space"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrString,
QObject::tr("String"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrStringEscapeSequences,
QObject::tr("Escape Sequences"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrSymbol,
QObject::tr("Symbol"),
QObject::tr("Syntax"),
true,true,true);
addDefine(SYNS_AttrVariable,
QObject::tr("Variable"),
QObject::tr("Syntax"),
true,true,true);
//Brace/Bracket/Parenthesis Level 1 2 3 4
addDefine(COLOR_SCHEME_BRACE_1,
QObject::tr("Brace/Bracket/Parenthesis Level 1"),
QObject::tr("Syntax"),
true,false,false);
addDefine(COLOR_SCHEME_BRACE_2,
QObject::tr("Brace/Bracket/Parenthesis Level 2"),
QObject::tr("Syntax"),
true,false,false);
addDefine(COLOR_SCHEME_BRACE_3,
QObject::tr("Brace/Bracket/Parenthesis Level 3"),
QObject::tr("Syntax"),
true,false,false);
addDefine(COLOR_SCHEME_BRACE_4,
QObject::tr("Brace/Bracket/Parenthesis Level 4"),
QObject::tr("Syntax"),
true,false,false);
2021-06-18 08:09:32 +08:00
//Gutter colors
addDefine(COLOR_SCHEME_GUTTER,
QObject::tr("Gutter"),
QObject::tr("Editor"),
true,false,false);
addDefine(COLOR_SCHEME_GUTTER_ACTIVE_LINE,
QObject::tr("Gutter Active Line"),
QObject::tr("Editor"),
true,false,false);
2021-06-18 08:09:32 +08:00
//Active Line
addDefine(COLOR_SCHEME_ACTIVE_LINE,
QObject::tr("Active Line"),
QObject::tr("Editor"),
false,true,false);
2021-06-18 08:09:32 +08:00
//Breakpoint Line
addDefine(COLOR_SCHEME_BREAKPOINT,
QObject::tr("Breakpoint"),
QObject::tr("Editor"),
true,true,false);
2021-06-18 08:09:32 +08:00
//Current Debug Line
addDefine(COLOR_SCHEME_ACTIVE_BREAKPOINT,
QObject::tr("Active Breakpoint"),
QObject::tr("Editor"),
true,true,false);
2021-06-18 08:09:32 +08:00
//Fold line
addDefine(COLOR_SCHEME_FOLD_LINE,
QObject::tr("Fold Line"),
QObject::tr("Editor"),
true,false,false);
addDefine(COLOR_SCHEME_SELECTION,
QObject::tr("Selection"),
QObject::tr("Editor"),
true,true,false);
addDefine(COLOR_SCHEME_TEXT,
QObject::tr("Editor Text"),
QObject::tr("Editor"),
true,true,false);
addDefine(COLOR_SCHEME_CURRENT_HIGHLIGHTED_WORD,
QObject::tr("Current Highlighted Word"),
QObject::tr("Editor"),
true,true,false);
2021-06-18 08:09:32 +08:00
//Syntax Error
addDefine(COLOR_SCHEME_ERROR,
QObject::tr("Error"),
QObject::tr("Syntax Check"),
true,false,false);
addDefine(COLOR_SCHEME_WARNING,
QObject::tr("Warning"),
QObject::tr("Syntax Check"),
true,false,false);
2021-06-17 10:39:46 +08:00
}
bool ColorManager::rename(const QString &oldName, const QString &newName)
{
if (mSchemes.contains(newName))
return false;
if (!isValidName(newName))
return false;
PColorScheme scheme = get(oldName);
if (!scheme)
return false;
2021-06-20 22:54:16 +08:00
if (scheme->bundled())
return false;
if (scheme->customed()) {
QString oldfullPath = generateFullPathname(oldName,false,true);
QString fullpath = generateFullPathname(newName,false,true);
QFile oldFile(oldfullPath);
if (oldFile.exists() && !oldFile.rename(fullpath))
throw FileError(QObject::tr("Rename file '%1' to '%2' failed!").arg(oldfullPath).arg(fullpath));
}
QString oldfullPath = generateFullPathname(oldName,false,false);
QString fullpath = generateFullPathname(newName,false,false);
QFile oldFile(oldfullPath);
if (oldFile.exists() && !oldFile.rename(fullpath))
throw FileError(QObject::tr("Rename file '%1' to '%2' failed!").arg(oldfullPath).arg(fullpath));
mSchemes.remove(oldName);
2021-06-17 10:39:46 +08:00
mSchemes[newName] = scheme;
2021-06-20 22:54:16 +08:00
return true;
2021-06-17 10:39:46 +08:00
}
2021-06-20 22:54:16 +08:00
bool ColorManager::add(const QString &name, PColorScheme scheme)
2021-06-17 10:39:46 +08:00
{
2021-06-20 22:54:16 +08:00
if (mSchemes.contains(name))
throw FileError(QObject::tr("Scheme '%1' already exists!").arg(name));
scheme->setBundled(false);
scheme->setCustomed(false);
mSchemes[name] = scheme;
saveScheme(name);
return true;
2021-06-17 10:39:46 +08:00
}
PColorScheme ColorManager::get(const QString &name)
{
if (mSchemes.contains(name))
return mSchemes[name];
return PColorScheme();
}
PColorSchemeItem ColorManager::getItem(const QString &schemeName, const QString &itemName)
{
PColorScheme scheme = get(schemeName);
if (!scheme)
return PColorSchemeItem();
if (!scheme->items().contains(itemName))
return PColorSchemeItem();
return scheme->items()[itemName];
}
2021-06-17 10:39:46 +08:00
bool ColorManager::isValidName(const QString &name)
{
for (QChar ch:name) {
2021-08-16 23:17:48 +08:00
if (!((ch == ' ') || (ch>='a' && ch<='z') || (ch>='A' && ch <= 'Z')
|| (ch>='0' && ch<='9') || (ch == '-') ))
2021-06-17 10:39:46 +08:00
return false;
}
return true;
}
void ColorManager::addDefine(const QString &name, const QString &displayName, const QString &group, bool hasForeground, bool hasBackground, bool hasFontStyle)
2021-06-17 10:39:46 +08:00
{
PColorSchemeItemDefine define = std::make_shared<ColorSchemeItemDefine>();
define->setDisplayName(displayName);
define->setGroup(group);
2021-06-17 10:39:46 +08:00
define->setHasForeground(hasForeground);
define->setHasBackground(hasBackground);
define->setHasFontStyle(hasFontStyle);
mSchemeItemDefines[name]=define;
2021-06-17 10:39:46 +08:00
}
bool ColorManager::removeDefine(const QString &name)
{
return mSchemeItemDefines.remove(name)==1;
2021-06-17 10:39:46 +08:00
}
PColorSchemeItemDefine ColorManager::getDefine(const QString &name)
{
if (mSchemeItemDefines.contains(name))
return mSchemeItemDefines[name];
2021-06-17 10:39:46 +08:00
return PColorSchemeItemDefine();
}
2021-06-20 14:30:47 +08:00
bool ColorManager::saveScheme(const QString &name)
{
PColorScheme scheme = get(name);
if (!scheme)
return false;
QString newFilepath = generateFullPathname(name,scheme->bundled(),scheme->customed());
scheme->save(newFilepath);
return true;
2021-06-20 14:30:47 +08:00
}
void ColorManager::updateStatementColors(std::shared_ptr<QHash<StatementKind, std::shared_ptr<ColorSchemeItem> > > statementColors, const QString &schemeName)
{
PColorSchemeItem item;
item = getItem(schemeName, SYNS_AttrFunction);
if (item) {
2024-04-03 10:37:29 +08:00
statementColors->insert(StatementKind::Function,item);
statementColors->insert(StatementKind::Constructor,item);
statementColors->insert(StatementKind::Destructor,item);
}
item = getItem(schemeName, SYNS_AttrClass);
if (item) {
2024-04-03 10:37:29 +08:00
statementColors->insert(StatementKind::Class,item);
statementColors->insert(StatementKind::Typedef,item);
statementColors->insert(StatementKind::Alias,item);
}
item = getItem(schemeName, SYNS_AttrIdentifier);
if (item) {
2024-04-03 10:37:29 +08:00
statementColors->insert(StatementKind::EnumType,item);
statementColors->insert(StatementKind::EnumClassType,item);
}
item = getItem(schemeName, SYNS_AttrVariable);
if (item) {
2024-04-03 10:37:29 +08:00
statementColors->insert(StatementKind::Variable,item);
}
item = getItem(schemeName, SYNS_AttrLocalVariable);
if (item) {
2024-04-03 10:37:29 +08:00
statementColors->insert(StatementKind::LocalVariable,item);
statementColors->insert(StatementKind::Parameter,item);
}
item = getItem(schemeName, SYNS_AttrGlobalVariable);
if (item) {
2024-04-03 10:37:29 +08:00
statementColors->insert(StatementKind::GlobalVariable,item);
}
item = getItem(schemeName, SYNS_AttrPreprocessor);
if (item) {
2024-04-03 10:37:29 +08:00
statementColors->insert(StatementKind::Preprocessor,item);
statementColors->insert(StatementKind::Enum,item);
}
item = getItem(schemeName, SYNS_AttrReservedWord);
if (item) {
2024-04-03 10:37:29 +08:00
statementColors->insert(StatementKind::Keyword,item);
statementColors->insert(StatementKind::UserCodeSnippet,item);
statementColors->insert(StatementKind::KeywordType,item);
}
item = getItem(schemeName, SYNS_AttrReserveWord_Type);
if (item) {
2024-04-03 10:37:29 +08:00
statementColors->insert(StatementKind::KeywordType,item);
}
item = getItem(schemeName, SYNS_AttrString);
if (item) {
2024-04-03 10:37:29 +08:00
statementColors->insert(StatementKind::Namespace,item);
statementColors->insert(StatementKind::NamespaceAlias,item);
}
}
2021-06-18 08:09:32 +08:00
QString ColorManager::generateFullPathname(const QString &name, bool isBundled, bool isCustomed)
{
QString filename = generateFilename(name,isCustomed);
2021-06-20 14:30:47 +08:00
if (isBundled && !isCustomed) {
2022-01-28 16:13:20 +08:00
return includeTrailingPathDelimiter(pSettings->dirs().data(Settings::Dirs::DataType::ColorScheme))+filename;
2021-06-18 08:09:32 +08:00
} else {
2022-01-28 16:13:20 +08:00
return includeTrailingPathDelimiter(pSettings->dirs().config(Settings::Dirs::DataType::ColorScheme))+filename;
2021-06-18 08:09:32 +08:00
}
}
ColorSchemeItemDefine::ColorSchemeItemDefine()
{
mHasBackground = true;
mHasForeground = true;
mHasFontStyle = true;
mGroup = QObject::tr("default");
2021-06-18 08:09:32 +08:00
}
2021-06-17 10:39:46 +08:00
bool ColorSchemeItemDefine::hasBackground() const
{
return mHasBackground;
}
void ColorSchemeItemDefine::setHasBackground(bool hasBackground)
{
mHasBackground = hasBackground;
}
bool ColorSchemeItemDefine::hasForeground() const
{
return mHasForeground;
}
void ColorSchemeItemDefine::setHasForeground(bool hasForeground)
{
mHasForeground = hasForeground;
}
2021-06-18 08:09:32 +08:00
bool ColorSchemeItemDefine::hasFontStyle() const
2021-06-17 10:39:46 +08:00
{
2021-06-18 08:09:32 +08:00
return mHasFontStyle;
2021-06-17 10:39:46 +08:00
}
void ColorSchemeItemDefine::setHasFontStyle(bool value)
{
2021-06-18 08:09:32 +08:00
mHasFontStyle = value;
2021-06-17 10:39:46 +08:00
}
QString ColorSchemeItemDefine::group() const
{
return mGroup;
}
void ColorSchemeItemDefine::setGroup(const QString &group)
{
mGroup = group;
}
QString ColorSchemeItemDefine::displayName() const
{
return mDisplayName;
}
void ColorSchemeItemDefine::setDisplayName(const QString &displayName)
{
mDisplayName = displayName;
}