From 154969f0b69e6532e9298ec0e14c2c24e9272064 Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Sun, 13 Jun 2021 23:22:26 +0800 Subject: [PATCH] * color scheme --- RedPandaIDE/RedPandaIDE.pro | 2 + RedPandaIDE/colorschema.cpp | 215 ++++++++++++++++++++++++++++++++++++ RedPandaIDE/colorschema.h | 67 +++++++++++ RedPandaIDE/main.cpp | 10 +- 4 files changed, 286 insertions(+), 8 deletions(-) create mode 100644 RedPandaIDE/colorschema.cpp create mode 100644 RedPandaIDE/colorschema.h diff --git a/RedPandaIDE/RedPandaIDE.pro b/RedPandaIDE/RedPandaIDE.pro index 69a9b6ba..177d9f6d 100644 --- a/RedPandaIDE/RedPandaIDE.pro +++ b/RedPandaIDE/RedPandaIDE.pro @@ -10,6 +10,7 @@ CONFIG += c++17 SOURCES += \ HighlighterManager.cpp \ + colorschema.cpp \ compiler/compiler.cpp \ compiler/compilermanager.cpp \ compiler/executablerunner.cpp \ @@ -47,6 +48,7 @@ SOURCES += \ HEADERS += \ HighlighterManager.h \ + colorschema.h \ compiler/compiler.h \ compiler/compilermanager.h \ compiler/executablerunner.h \ diff --git a/RedPandaIDE/colorschema.cpp b/RedPandaIDE/colorschema.cpp new file mode 100644 index 00000000..e7c7bbfb --- /dev/null +++ b/RedPandaIDE/colorschema.cpp @@ -0,0 +1,215 @@ +#include "colorschema.h" +#include +#include +#include +#include +#include "utils.h" + +ColorSchema::ColorSchema() +{ + +} + +QString ColorSchema::name() const +{ + return mName; +} + +void ColorSchema::setName(const QString &name) +{ + mName = name; +} + +void ColorSchema::read(const QJsonObject &json) +{ + if (json.contains("name") && json["name"].isString()) { + setName(json["name"].toString()); + } else { + setName(""); + } + mItems.clear(); + if (json.contains("items") && json["items"].isObject()) { + QJsonObject itemsList = json["items"].toObject(); + for (QString key:itemsList.keys()) { + PColorSchemaItem item = std::make_shared(key); + item->read(itemsList[key].toObject()); + } + } +} + +void ColorSchema::write(QJsonObject &json) +{ + json["name"] = mName; + QJsonObject itemsList; + for (PColorSchemaItem item:mItems) { + if (!item->name().isEmpty()) { + QJsonObject itemObject; + item->write(itemObject); + itemsList[item->name()] = itemObject; + } + } + json["items"]=itemsList; +} + +void ColorSchema::load(const QString &filename) +{ + QFile file(filename); + if (!file.open(QFile::ReadOnly)) { + throw new FileError(QObject::tr("Can't open file '%1' for read").arg(file.fileName())); + } + QByteArray content = file.readAll(); + QJsonParseError error; + QJsonDocument doc = QJsonDocument::fromJson(content,&error); + if (error.error!=QJsonParseError::NoError) { + throw new FileError(QObject::tr("Can't parse json file '%1' at offset %2! Error Code: %3") + .arg(file.fileName()).arg(error.offset).arg(error.error)); + } + if (!doc.isObject()) { + throw new FileError(QObject::tr("Can't parse json file '%1' is not a color schema config file!") + .arg(file.fileName())); + } + read(doc.object()); +} + +void ColorSchema::save(const QString &filename) +{ + QFile file(filename); + if (!file.open(QFile::WriteOnly)) { + throw new FileError(QObject::tr("Can't open file '%1' for write").arg(file.fileName())); + } + QJsonObject json; + write(json); + QJsonDocument doc(json); + QByteArray content = doc.toJson(); + file.write(content); +} + +ColorSchemaItem::ColorSchemaItem(const QString& name): + mName(name), + mForeground(), + mBackground(), + mBold(false), + mItalic(false), + mUnderlined(false), + mStrikeout(false) +{ + +} + +QString ColorSchemaItem::name() const +{ + return mName; +} + +QColor ColorSchemaItem::foreground() const +{ + return mForeground; +} + +void ColorSchemaItem::setForeground(const QColor &foreground) +{ + mForeground = foreground; +} + +QColor ColorSchemaItem::background() const +{ + return mBackground; +} + +void ColorSchemaItem::setBackground(const QColor &background) +{ + mBackground = background; +} + +bool ColorSchemaItem::bold() const +{ + return mBold; +} + +void ColorSchemaItem::setBold(bool bold) +{ + mBold = bold; +} + +bool ColorSchemaItem::italic() const +{ + return mItalic; +} + +void ColorSchemaItem::setItalic(bool italic) +{ + mItalic = italic; +} + +bool ColorSchemaItem::underlined() const +{ + return mUnderlined; +} + +void ColorSchemaItem::setUnderlined(bool underlined) +{ + mUnderlined = underlined; +} + +bool ColorSchemaItem::strikeout() const +{ + return mStrikeout; +} + +void ColorSchemaItem::setStrikeout(bool strikeout) +{ + mStrikeout = strikeout; +} + +void ColorSchemaItem::read(const QJsonObject &json) +{ + if (json.contains("foreground") && json["foreground"].isString()) { + setForeground(json["foreground"].toString()); + } else { + setForeground(QColor()); + } + if (json.contains("background") && json["background"].isString()) { + setBackground(json["background"].toString()); + } else { + setBackground(QColor()); + } + if (json.contains("bold") && json["bold"].isBool()) { + setBold(json["bold"].toBool()); + } else { + setBold(false); + } + if (json.contains("italic") && json["italic"].isBool()) { + setBold(json["italic"].toBool()); + } else { + setItalic(false); + } + if (json.contains("underlined") && json["underlined"].isBool()) { + setBold(json["underlined"].toBool()); + } else { + setUnderlined(false); + } + if (json.contains("strikeout") && json["strikeout"].isBool()) { + setBold(json["strikeout"].toBool()); + } else { + setUnderlined(false); + } + +} + +void ColorSchemaItem::write(QJsonObject &json) +{ + if (mForeground.isValid()) { + json["foreground"] = mForeground.name(); + } else if (json.contains("foreground")){ + json.remove("foreground"); + } + if (mBackground.isValid()) { + json["background"] = mBackground.name(); + } else if (json.contains("background")){ + json.remove("background"); + } + json["bold"] = mBold; + json["italic"] = mItalic; + json["underlined"] = mUnderlined; + json["strikeout"] = mStrikeout; +} diff --git a/RedPandaIDE/colorschema.h b/RedPandaIDE/colorschema.h new file mode 100644 index 00000000..c0f28620 --- /dev/null +++ b/RedPandaIDE/colorschema.h @@ -0,0 +1,67 @@ +#ifndef COLORSCHEMA_H +#define COLORSCHEMA_H + +#include +#include + +class ColorSchemaItem { + +public: + explicit ColorSchemaItem(const QString& name); + QString name() const; + + QColor foreground() const; + void setForeground(const QColor &foreground); + + QColor background() const; + void setBackground(const QColor &background); + + bool bold() const; + void setBold(bool bold); + + bool italic() const; + void setItalic(bool italic); + + bool underlined() const; + void setUnderlined(bool underlined); + + bool strikeout() const; + void setStrikeout(bool strikeout); + + void read(const QJsonObject& json); + void write(QJsonObject& json); + + +private: + QString mName; + QColor mForeground; + QColor mBackground; + bool mBold; + bool mItalic; + bool mUnderlined; + bool mStrikeout; +}; + +using PColorSchemaItem = std::shared_ptr; + +class ColorSchema +{ +public: + ColorSchema(); + + QMap items(); + QString name() const; + void setName(const QString &name); + + void read(const QJsonObject& json); + void write(QJsonObject& json); + + void load(const QString& filename); + void save(const QString& filename); + +private: + QMap mItems; + QString mName; +}; + +#endif // COLORSCHEMA_H diff --git a/RedPandaIDE/main.cpp b/RedPandaIDE/main.cpp index 8f185af3..f6466fac 100644 --- a/RedPandaIDE/main.cpp +++ b/RedPandaIDE/main.cpp @@ -59,23 +59,17 @@ int main(int argc, char *argv[]) SystemConsts systemConsts; pSystemConsts = &systemConsts; -// Settings::CompilerSet testSet("e:/workspace/contributes/Dev-CPP/MinGW32_GCC92"); -// qDebug() << testSet.binDirs(); -// qDebug() << testSet.CIncludeDirs(); -// qDebug() << testSet.CppIncludeDirs(); -// qDebug() << testSet.LibDirs(); - pSettings = createAppSettings(); if (pSettings == nullptr) { return -1; } auto settings = std::unique_ptr(pSettings); + settings->compilerSets().loadSets(); + settings->editor().load(); //settings->compilerSets().addSets("e:/workspace/contributes/Dev-CPP/MinGW32_GCC92"); // settings->compilerSets().findSets(); // settings->compilerSets().saveSets(); - settings->compilerSets().loadSets(); - settings->editor().load(); // qDebug() << settings->compilerSets().defaultSet()->binDirs(); // settings->compilerSets().loadSets(); // qDebug() << settings->compilerSets().defaultSet()->defines();