* color scheme

This commit is contained in:
royqh1979@gmail.com 2021-06-13 23:22:26 +08:00
parent bbe3b555a6
commit 154969f0b6
4 changed files with 286 additions and 8 deletions

View File

@ -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 \

215
RedPandaIDE/colorschema.cpp Normal file
View File

@ -0,0 +1,215 @@
#include "colorschema.h"
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#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<ColorSchemaItem>(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;
}

67
RedPandaIDE/colorschema.h Normal file
View File

@ -0,0 +1,67 @@
#ifndef COLORSCHEMA_H
#define COLORSCHEMA_H
#include <QColor>
#include <qsynedit/highlighter/base.h>
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<ColorSchemaItem>;
class ColorSchema
{
public:
ColorSchema();
QMap<QString,PColorSchemaItem> 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<QString,PColorSchemaItem> mItems;
QString mName;
};
#endif // COLORSCHEMA_H

View File

@ -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<Settings>(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();