* work save for color scheme

This commit is contained in:
royqh1979@gmail.com 2021-06-17 10:39:46 +08:00
parent f9938c29e1
commit d7c65fe801
11 changed files with 531 additions and 291 deletions

View File

@ -10,7 +10,7 @@ CONFIG += c++17
SOURCES += \
HighlighterManager.cpp \
colorschema.cpp \
colorscheme.cpp \
compiler/compiler.cpp \
compiler/compilermanager.cpp \
compiler/executablerunner.cpp \
@ -49,7 +49,7 @@ SOURCES += \
HEADERS += \
HighlighterManager.h \
colorschema.h \
colorscheme.h \
compiler/compiler.h \
compiler/compilermanager.h \
compiler/executablerunner.h \

View File

@ -1,215 +0,0 @@
#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;
}

View File

@ -1,67 +0,0 @@
#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

349
RedPandaIDE/colorscheme.cpp Normal file
View File

@ -0,0 +1,349 @@
#include "colorscheme.h"
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include "utils.h"
ColorScheme::ColorScheme()
{
}
QString ColorScheme::name() const
{
return mName;
}
void ColorScheme::setName(const QString &name)
{
mName = name;
}
void ColorScheme::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()) {
PColorSchemeItem item = std::make_shared<ColorSchemeItem>(key);
item->read(itemsList[key].toObject());
}
}
}
void ColorScheme::write(QJsonObject &json)
{
json["name"] = mName;
QJsonObject itemsList;
for (PColorSchemeItem item:mItems) {
if (!item->name().isEmpty()) {
QJsonObject itemObject;
item->write(itemObject);
itemsList[item->name()] = itemObject;
}
}
json["items"]=itemsList;
}
void ColorScheme::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 ColorScheme::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);
}
bool ColorScheme::bundled() const
{
return mBundled;
}
void ColorScheme::setBundled(bool bundled)
{
mBundled = bundled;
}
bool ColorScheme::modified() const
{
return mModified;
}
void ColorScheme::setModified(bool modified)
{
mModified = modified;
}
QString ColorScheme::preferThemeType() const
{
return mPreferThemeType;
}
void ColorScheme::setPreferThemeType(const QString &preferThemeType)
{
mPreferThemeType = preferThemeType;
}
ColorSchemeItem::ColorSchemeItem(const QString& name):
mName(name),
mForeground(),
mBackground(),
mBold(false),
mItalic(false),
mUnderlined(false),
mStrikeout(false)
{
}
QString ColorSchemeItem::name() const
{
return mName;
}
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;
}
void ColorSchemeItem::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 ColorSchemeItem::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;
}
void ColorManager::init()
{
reload();
}
PColorScheme ColorManager::copy(const QString &sourceName)
{
if (!mSchemes.contains(sourceName))
return PColorScheme();
PColorScheme sourceScheme = mSchemes[sourceName];
// todo:save source with the new name
// then load it to the copied
}
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;
mSchemes[newName] = scheme;
}
PColorScheme ColorManager::remove(const QString &name)
{
PColorScheme scheme=get(name);
if (scheme)
mSchemes.remove(name);
return scheme;
}
PColorScheme ColorManager::get(const QString &name)
{
if (mSchemes.contains(name))
return mSchemes[name];
return PColorScheme();
}
bool ColorManager::isValidName(const QString &name)
{
for (QChar ch:name) {
if (!((ch == ' ') or (ch>='a' && ch<='z') or (ch>='A' && ch <= 'Z')
or (ch>='0' && ch<='9') or (ch == '-') ))
return false;
}
return true;
}
void ColorManager::addDefine(const QString &name, bool hasForeground, bool hasBackground, bool hasFontStyle)
{
PColorSchemeItemDefine define = std::make_shared<ColorSchemeItemDefine>();
define->setHasForeground(hasForeground);
define->setHasBackground(hasBackground);
define->setHasFontStyle(hasFontStyle);
mSchemeDefine[name]=define;
}
bool ColorManager::removeDefine(const QString &name)
{
return mSchemeDefine.remove(name)==1;
}
PColorSchemeItemDefine ColorManager::getDefine(const QString &name)
{
if (mSchemeDefine.contains(name))
return mSchemeDefine[name];
return PColorSchemeItemDefine();
}
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;
}
bool ColorSchemeItemDefine::getHasFontStyle() const
{
return hasFontStyle;
}
void ColorSchemeItemDefine::setHasFontStyle(bool value)
{
hasFontStyle = value;
}

121
RedPandaIDE/colorscheme.h Normal file
View File

@ -0,0 +1,121 @@
#ifndef COLORSCHEME_H
#define COLORSCHEME_H
#include <QColor>
#include <qsynedit/highlighter/base.h>
class ColorSchemeItem {
public:
explicit ColorSchemeItem(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 PColorSchemeItem = std::shared_ptr<ColorSchemeItem>;
class ColorScheme;
using PColorScheme = std::shared_ptr<ColorScheme>;
class ColorScheme
{
public:
explicit ColorScheme();
QMap<QString,PColorSchemeItem> 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);
bool bundled() const;
void setBundled(bool bundled);
bool modified() const;
void setModified(bool modified);
QString preferThemeType() const;
void setPreferThemeType(const QString &preferThemeType);
private:
QMap<QString,PColorSchemeItem> mItems;
QString mName;
QString mPreferThemeType;
bool mBundled;
bool mModified;
};
class ColorSchemeItemDefine {
public:
bool hasBackground() const;
void setHasBackground(bool hasBackground);
bool hasForeground() const;
void setHasForeground(bool hasForeground);
bool getHasFontStyle() const;
void setHasFontStyle(bool value);
private:
bool mHasBackground;
bool mHasForeground;
bool hasFontStyle;
};
using PColorSchemeItemDefine = std::shared_ptr<ColorSchemeItemDefine>;
class ColorManager {
public:
void init();
void reload();
QMap<QString,PColorScheme> getSchemesForTheme(QString themeName);
QMap<QString,PColorScheme> getSchemes();
bool exists(const QString name);
PColorScheme create(const QString& name);
PColorScheme copy(const QString& source);
bool rename(const QString& oldName, const QString& newName);
PColorScheme remove(const QString& name);
PColorScheme get(const QString& name);
bool isValidName(const QString& name);
void addDefine(const QString& name, bool hasForeground, bool hasBackground, bool hasFontStyle);
bool removeDefine(const QString &name);
PColorSchemeItemDefine getDefine(const QString& name);
private:
QMap<QString,PColorSchemeItemDefine> mSchemeDefine;
QMap<QString,PColorScheme> mSchemes;
};
#endif // COLORSCHEME_H

View File

@ -78,6 +78,11 @@ Settings::CompilerSets &Settings::compilerSets()
return mCompilerSets;
}
QString Settings::filename() const
{
return mFilename;
}
Settings::Dirs::Dirs(Settings *settings):
_Base(settings, SETTING_DIRS)
{
@ -88,6 +93,32 @@ QString Settings::Dirs::app() const
return QApplication::instance()->applicationDirPath();
}
QString Settings::Dirs::data(Settings::Dirs::DataType dataType) const
{
using DataType = Settings::Dirs::DataType;
QString dataDir = includeTrailingPathDelimiter(app())+"data";
switch (dataType) {
case DataType::None:
return dataDir;
case DataType::ColorSheme:
return includeTrailingPathDelimiter(dataDir)+"scheme";
}
}
QString Settings::Dirs::config(Settings::Dirs::DataType dataType) const
{
using DataType = Settings::Dirs::DataType;
QFileInfo configFile(pSettings->filename());
QString configDir = configFile.path();
switch (dataType) {
case DataType::None:
return configDir;
case DataType::ColorSheme:
return includeTrailingPathDelimiter(configDir)+"scheme";
}
}
void Settings::Dirs::doSave()
{
@ -1595,13 +1626,13 @@ void Settings::CompilerSets::saveDefaultIndex()
void Settings::CompilerSets::deleteSet(int index)
{
// Erase all sections at and above from disk
for (int i=index;i<mList.size();i++) {
for (size_t i=index;i<mList.size();i++) {
mSettings->mSettings.beginGroup(QString(SETTING_COMPILTER_SET).arg(i));
mSettings->mSettings.remove("");
mSettings->mSettings.endGroup();
}
mList.erase(std::begin(mList)+index);
for (int i=index;i<mList.size();i++) {
for (size_t i=index;i<mList.size();i++) {
saveSet(i);
}
if (mDefaultIndex>=mList.size()) {

View File

@ -69,8 +69,14 @@ private:
public:
class Dirs: public _Base {
public:
enum class DataType {
None,
ColorSheme
};
explicit Dirs(Settings * settings);
QString app() const;
QString data(DataType dataType = DataType::None) const;
QString config(DataType dataType = DataType::None) const;
// _Base interface
protected:
@ -441,7 +447,10 @@ public:
Dirs& dirs();
Editor& editor();
CompilerSets& compilerSets();
QString filename() const;
private:
QString mFilename;
QSettings mSettings;
Dirs mDirs;
Editor mEditor;

View File

@ -1,8 +1,8 @@
#include "editorcolorschemewidget.h"
#include "ui_editorcolorschemewidget.h"
EditorColorSchemeWidget::EditorColorSchemeWidget(QWidget *parent) :
QWidget(parent),
EditorColorSchemeWidget::EditorColorSchemeWidget(const QString& name, const QString& group, QWidget *parent) :
SettingsWidget(name,group,parent),
ui(new Ui::EditorColorSchemeWidget)
{
ui->setupUi(this);

View File

@ -1,22 +1,27 @@
#ifndef EDITORCOLORSCHEMEWIDGET_H
#define EDITORCOLORSCHEMEWIDGET_H
#include <QWidget>
#include "settingswidget.h"
namespace Ui {
class EditorColorSchemeWidget;
}
class EditorColorSchemeWidget : public QWidget
class EditorColorSchemeWidget : public SettingsWidget
{
Q_OBJECT
public:
explicit EditorColorSchemeWidget(QWidget *parent = nullptr);
explicit EditorColorSchemeWidget(const QString& name, const QString& group, QWidget *parent = nullptr);
~EditorColorSchemeWidget();
private:
Ui::EditorColorSchemeWidget *ui;
// SettingsWidget interface
protected:
void doLoad() override;
void doSave() override;
};
#endif // EDITORCOLORSCHEMEWIDGET_H

View File

@ -5,6 +5,7 @@
#include "editorgeneralwidget.h"
#include "editorfontwidget.h"
#include "editorclipboardwidget.h"
#include "editorcolorschemewidget.h"
#include <QDebug>
#include <QMessageBox>
@ -35,6 +36,10 @@ SettingsDialog::SettingsDialog(QWidget *parent) :
pEditorClipboardWidget = new EditorClipboardWidget(tr("Copy & Export"),tr("Editor"));
pEditorClipboardWidget->init();
addWidget(pEditorClipboardWidget);
pEditorColorSchemeWidget = new EditorColorSchemeWidget(tr("Color"),tr("Editor"));
pEditorColorSchemeWidget->init();
addWidget(pEditorColorSchemeWidget);
}
SettingsDialog::~SettingsDialog()

View File

@ -15,6 +15,7 @@ class EditorGeneralWidget;
class EditorFontWidget;
class EditorClipboardWidget;
class PCompilerSet;
class EditorColorSchemeWidget;
class SettingsWidget;
class SettingsDialog : public QDialog
{
@ -48,6 +49,7 @@ private:
EditorGeneralWidget* pEditorGeneralWidget;
EditorFontWidget* pEditorFontWidget;
EditorClipboardWidget *pEditorClipboardWidget;
EditorColorSchemeWidget *pEditorColorSchemeWidget;
};
#endif // SETTINGSDIALOG_H