diff options
Diffstat (limited to 'src/field-styles/FieldStyleFactory.cpp')
-rw-r--r-- | src/field-styles/FieldStyleFactory.cpp | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/src/field-styles/FieldStyleFactory.cpp b/src/field-styles/FieldStyleFactory.cpp new file mode 100644 index 0000000..ab789e3 --- /dev/null +++ b/src/field-styles/FieldStyleFactory.cpp @@ -0,0 +1,145 @@ +#include "FieldStyleFactory.h" + +const QString FieldStyleFactory::DefaultStyle = "Normal"; + +FieldStyleFactory::FieldStyleFactory() + { + cardBgColor.setNamedColor("white"); + initStyles(); + } + +void FieldStyleFactory::initStyles() +{ + setStyle(DefaultStyle, FieldStyle()); + setStyle("Example", {"", 14, false, false, "", "", "", true, "blue"}); + setStyle("Transcription", {"Arial", 0, false, false, "", "/", "/"}); + setStyle("Big", {"Arial", 26}); + setStyle("Color1", {"", 0, true, false, "red"}); + setStyle("Color2", {"", 0, true, false, "blue"}); +} + +FieldStyleFactory* FieldStyleFactory::inst() + { + static FieldStyleFactory factory; + return &factory; + } + +void FieldStyleFactory::setStyle(const QString& aStyleName, FieldStyle aStyle ) + { + styles[aStyleName] = aStyle; + if(!stylesNames.contains( aStyleName )) + stylesNames << aStyleName; + } + +void FieldStyleFactory::load() +{ + QSettings settings; + settings.beginGroup("Styles"); + cardBgColor.setNamedColor( + settings.value("bg-color", cardBgColor.name()).toString()); + foreach(QString styleName, + settings.value("list", getStyleNames()).toStringList()) + loadStyle(settings, styleName); + settings.endGroup(); +} + +void FieldStyleFactory::loadStyle(QSettings& settings, const QString& styleName) +{ + FieldStyle curStyle; + settings.beginGroup(styleName); + FieldStyle defaultStyle = getStyle(styleName); + loadStyleMainProperties(settings, curStyle, defaultStyle); + loadKeywordStyle(settings, curStyle, defaultStyle); + settings.endGroup(); + setStyle(styleName, curStyle); +} + +void FieldStyleFactory::loadStyleMainProperties(const QSettings& settings, + FieldStyle& curStyle, const FieldStyle& defaultStyle) +{ + curStyle.font.setFamily( + settings.value("font-family", defaultStyle.font.family()).toString()); + curStyle.font.setPointSize( + settings.value("font-size", defaultStyle.font.pointSize()).toInt()); + curStyle.font.setBold( + settings.value("font-bold", defaultStyle.font.bold()).toBool()); + curStyle.font.setItalic( + settings.value("font-italic", defaultStyle.font.italic()).toBool()); + curStyle.color.setNamedColor( + settings.value("color", defaultStyle.color.name()).toString()); + curStyle.prefix = settings.value("prefix", defaultStyle.prefix).toString(); + curStyle.suffix = settings.value("suffix", defaultStyle.suffix).toString(); + curStyle.hasKeyword = settings.value("keyword", defaultStyle.hasKeyword).toBool(); +} + +void FieldStyleFactory::loadKeywordStyle(const QSettings& settings, FieldStyle& curStyle, + const FieldStyle& defaultStyle) +{ + if(!curStyle.hasKeyword) + return; + curStyle.keywordBold = + settings.value("keyword/font-bold", defaultStyle.keywordBold).toBool(); + curStyle.keywordItalic = + settings.value("keyword/font-italic", defaultStyle.keywordItalic).toBool(); + curStyle.keywordColor.setNamedColor( + settings.value("keyword/color", defaultStyle.keywordColor.name()).toString()); +} + +void FieldStyleFactory::save() + { + QSettings settings; + settings.beginGroup("Styles"); + settings.remove(""); // Remove old user settings + if(cardBgColor != FieldStyleFactory().cardBgColor) + settings.setValue("bg-color", cardBgColor.name()); + foreach(QString styleName, getStyleNames()) + saveStyle(settings, styleName); + settings.endGroup(); + } + +void FieldStyleFactory::saveStyle(QSettings& settings, const QString& styleName) +{ + const FieldStyle curStyle = getStyle(styleName); + const FieldStyle defaultStyle = FieldStyleFactory().getStyle(styleName); + settings.beginGroup( styleName ); + saveStyleMainProperties(settings, curStyle, defaultStyle); + saveKeywordStyle(settings, curStyle, defaultStyle); + settings.endGroup(); +} + +void FieldStyleFactory::saveStyleMainProperties(QSettings& settings, const FieldStyle& curStyle, + const FieldStyle& defaultStyle) +{ + if(curStyle.font.family() != defaultStyle.font.family()) + settings.setValue("font-family", curStyle.font.family()); + if(curStyle.font.pointSize() != defaultStyle.font.pointSize()) + settings.setValue("font-size", curStyle.font.pointSize()); + if(curStyle.font.bold() != defaultStyle.font.bold()) + settings.setValue("font-bold", curStyle.font.bold()); + if(curStyle.font.italic() != defaultStyle.font.italic()) + settings.setValue("font-italic", curStyle.font.italic()); + if(curStyle.color != defaultStyle.color) + settings.setValue("color", curStyle.color.name()); + if(curStyle.prefix != defaultStyle.prefix) + settings.setValue("prefix", curStyle.prefix); + if(curStyle.suffix != defaultStyle.suffix) + settings.setValue("suffix", curStyle.suffix); + if(curStyle.hasKeyword != defaultStyle.hasKeyword) + settings.setValue("keyword", curStyle.hasKeyword); +} + +void FieldStyleFactory::saveKeywordStyle(QSettings& settings, const FieldStyle& curStyle, + const FieldStyle& defaultStyle) +{ + if(!curStyle.hasKeyword) + return; + if(!defaultStyle.hasKeyword || + curStyle.keywordBold != defaultStyle.keywordBold) + settings.setValue("keyword/font-bold", curStyle.keywordBold); + if(!defaultStyle.hasKeyword || + curStyle.keywordItalic != defaultStyle.keywordItalic) + settings.setValue("keyword/font-italic", curStyle.keywordItalic); + if(!defaultStyle.hasKeyword || + curStyle.keywordColor != defaultStyle.keywordColor) + settings.setValue("keyword/color", curStyle.keywordColor.name()); +} |