* settings infrastrcture ok

This commit is contained in:
royqh1979@gmail.com 2021-04-13 11:23:37 +08:00
parent 48eaa4cbb8
commit e37053f2dd
3 changed files with 133 additions and 14 deletions

View File

@ -63,7 +63,7 @@ Editor::Editor(QWidget *parent, const QString& filename,
lexer->setHighlightEscapeSequences(true);
lexer->setFoldComments(true);
this->setLexer(lexer);
this->setAutoIndent(pSettings->value(EDITOR_AUTO_INDENT).toBool());
this->setAutoIndent(pSettings->editor().autoIndent());
this->setFolding(FoldStyle::BoxedTreeFoldStyle,3);
//行号显示区域
@ -154,7 +154,7 @@ void Editor::saveFile(const QString &filename) {
}
if (mEncodingOption == ENCODING_AUTO_DETECT && mFileEncoding == ENCODING_ASCII) {
if (!isTextAllAscii(this->text())) {
mFileEncoding = pSettings->value(EDITOR_DEFAULT_ENCODING).toByteArray();
mFileEncoding = pSettings->editor().defaultEncoding();
}
pMainWindow->updateStatusBarForEncoding();
//todo: update status bar, and set fileencoding using configurations

View File

@ -1,28 +1,114 @@
#include "settings.h"
#include <QApplication>
#include <QTextCodec>
#include "utils.h"
Settings* pSettings;
Settings::Settings():
mSettings(QSettings::IniFormat,QSettings::UserScope,"Red Panda C++")
mSettings(QSettings::IniFormat,QSettings::UserScope,"Red Panda C++"),
mDirs(this),
mEditor(this)
{
// default values for editors
setDefault(EDITOR_DEFAULT_ENCODING, QTextCodec::codecForLocale()->name());
setDefault(EDITOR_AUTO_INDENT,true);
mEditor.setDefault(SETTING_EDITOR_DEFAULT_ENCODING, QTextCodec::codecForLocale()->name());
mEditor.setDefault(SETTING_EDITOR_AUTO_INDENT,true);
}
void Settings::setDefault(const QString &key, const QVariant &value) {
void Settings::setDefault(const QString&group,const QString &key, const QVariant &value) {
mSettings.beginGroup(group);
auto act = finally([this] {
this->mSettings.endGroup();
});
if (!mSettings.contains(key)) {
mSettings.setValue(key,value);
}
}
void Settings::setValue(const QString &key, const QVariant &value) {
void Settings::setValue(const QString& group, const QString &key, const QVariant &value) {
mSettings.beginGroup(group);
auto act = finally([this] {
this->mSettings.endGroup();
});
mSettings.setValue(key,value);
}
QVariant Settings::value(const QString &key) const {
QVariant Settings::value(const QString& group, const QString &key) {
mSettings.beginGroup(group);
auto act = finally([this] {
this->mSettings.endGroup();
});
return mSettings.value(key);
}
Settings::Dirs &Settings::dirs()
{
return mDirs;
}
Settings::Editor &Settings::editor()
{
return mEditor;
}
Settings::Dirs::Dirs(Settings *settings):
_Base(settings, SETTING_DIRS)
{
}
const QString Settings::Dirs::app() const
{
QApplication::instance()->applicationDirPath();
}
Settings::_Base::_Base(Settings *settings, const QString &groupName):
mSettings(settings),
mGroup(groupName)
{
}
void Settings::_Base::setDefault(const QString &key, const QVariant &value)
{
mSettings->setDefault(mGroup,key,value);
}
void Settings::_Base::setValue(const QString &key, const QVariant &value)
{
mSettings->setValue(mGroup,key,value);
}
QVariant Settings::_Base::value(const QString &key)
{
return mSettings->value(mGroup,key);
}
Settings::Editor::Editor(Settings *settings): _Base(settings, SETTING_EDITOR)
{
}
QByteArray Settings::Editor::defaultEncoding()
{
return value(SETTING_EDITOR_DEFAULT_ENCODING).toByteArray();
}
void Settings::Editor::setDefaultEncoding(const QByteArray &encoding)
{
setValue(SETTING_EDITOR_DEFAULT_ENCODING,encoding);
}
bool Settings::Editor::autoIndent()
{
return value(SETTING_EDITOR_AUTO_INDENT).toBool();
}
void Settings::Editor::setAutoIndent(bool indent)
{
setValue(SETTING_EDITOR_AUTO_INDENT,indent);
}

View File

@ -3,25 +3,58 @@
#include <QSettings>
#define EDITOR_DEFAULT_ENCODING "editor/default_encoding"
#define EDITOR_AUTO_INDENT "editor/default_auto_indent"
#define SETTING_DIRS "dirs"
#define SETTING_EDITOR "editor"
#define SETTING_EDITOR_DEFAULT_ENCODING "default_encoding"
#define SETTING_EDITOR_AUTO_INDENT "default_auto_indent"
class Settings
{
private:
class _Base {
public:
explicit _Base(Settings* settings, const QString& groupName);
void setDefault(const QString &key, const QVariant &value);
void setValue(const QString &key, const QVariant &value);
QVariant value(const QString &key);
protected:
Settings* mSettings;
QString mGroup;
};
public:
class Dirs: public _Base {
public:
explicit Dirs(Settings * settings);
const QString app() const;
};
class Editor: public _Base {
public:
explicit Editor(Settings * settings);
QByteArray defaultEncoding();
void setDefaultEncoding(const QByteArray& encoding);
bool autoIndent();
void setAutoIndent(bool indent);
};
public:
Settings();
void setDefault(const QString &key, const QVariant &value);
void setValue(const QString &key, const QVariant &value);
QVariant value(const QString &key) const;
void setDefault(const QString& group, const QString &key, const QVariant &value);
void setValue(const QString& group, const QString &key, const QVariant &value);
QVariant value(const QString& group, const QString &key);
Dirs& dirs();
Editor& editor();
private:
QSettings mSettings;
Dirs mDirs;
Editor mEditor;
};
extern Settings* pSettings;
#endif // SETTINGS_H