diff options
Diffstat (limited to 'src/field-styles')
-rw-r--r-- | src/field-styles/FieldStyle.cpp | 38 | ||||
-rw-r--r-- | src/field-styles/FieldStyle.h | 28 | ||||
-rw-r--r-- | src/field-styles/FieldStyleFactory.cpp | 145 | ||||
-rw-r--r-- | src/field-styles/FieldStyleFactory.h | 49 |
4 files changed, 260 insertions, 0 deletions
diff --git a/src/field-styles/FieldStyle.cpp b/src/field-styles/FieldStyle.cpp new file mode 100644 index 0000000..4e710f8 --- /dev/null +++ b/src/field-styles/FieldStyle.cpp @@ -0,0 +1,38 @@ +#include "FieldStyle.h" + +FieldStyle::FieldStyle(): + FieldStyle("", 0) {} + +FieldStyle::FieldStyle(const QString& family, int size, + bool bold, bool italic, const QString& colorName, + const QString& prefix, const QString& suffix, + bool hasKeyword, const QString& keywordColorName): + color(Qt::black), + prefix(prefix), suffix(suffix), + hasKeyword(hasKeyword), + keywordBold(false), + keywordItalic(false) +{ + font.setFamily("Times New Roman"); + font.setPointSize(18); + if(!family.isEmpty()) + font.setFamily(family); + if(size > 0) + font.setPointSize(size); + font.setBold(bold); + font.setItalic(italic); + if(!colorName.isEmpty()) + color.setNamedColor(colorName); + keywordColor.setNamedColor(keywordColorName); +} + +FieldStyle FieldStyle::getKeywordStyle() const +{ + if(!hasKeyword) + return *this; + FieldStyle keywordStyle = *this; + keywordStyle.font.setBold(keywordBold); + keywordStyle.font.setItalic(keywordItalic); + keywordStyle.color = keywordColor; + return keywordStyle; +} diff --git a/src/field-styles/FieldStyle.h b/src/field-styles/FieldStyle.h new file mode 100644 index 0000000..20da858 --- /dev/null +++ b/src/field-styles/FieldStyle.h @@ -0,0 +1,28 @@ +#ifndef FIELDSTYLE_H +#define FIELDSTYLE_H + +#include <QFont> +#include <QColor> + +class FieldStyle +{ +public: + FieldStyle(); + FieldStyle(const QString& family, int size, + bool bold = true, bool italic = false, const QString& colorName = "", + const QString& prefix = "", const QString& suffix = "", + bool hasKeyword = false, const QString& keywordColorName = "#000000"); + FieldStyle getKeywordStyle() const; + +public: + QFont font; + QColor color; + QString prefix; + QString suffix; + bool hasKeyword; + bool keywordBold; + bool keywordItalic; + QColor keywordColor; +}; + +#endif 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()); +} diff --git a/src/field-styles/FieldStyleFactory.h b/src/field-styles/FieldStyleFactory.h new file mode 100644 index 0000000..072bdd5 --- /dev/null +++ b/src/field-styles/FieldStyleFactory.h @@ -0,0 +1,49 @@ +#ifndef FIELDSTYLEFACTORY_H +#define FIELDSTYLEFACTORY_H + +#include "FieldStyle.h" + +#include <QtCore> + +class FieldStyleFactory +{ +public: + static FieldStyleFactory* inst(); + +public: + FieldStyleFactory(); + const QStringList getStyleNames() const + { return stylesNames; } + const FieldStyle getStyle(const QString& aStyleName) const + { return styles[aStyleName]; } + FieldStyle* getStylePtr( const QString& aStyleName ) + { return &( styles[aStyleName] ); } + void setStyle(const QString& aStyleName, FieldStyle aStyle ); + void load(); + void save(); + +private: + void initStyles(); + void loadStyle(QSettings& settings, const QString& styleName); + void loadStyleMainProperties(const QSettings& settings, FieldStyle& curStyle, + const FieldStyle& defaultStyle); + void loadKeywordStyle(const QSettings& settings, FieldStyle& curStyle, + const FieldStyle& defaultStyle); + void saveStyle(QSettings& settings, const QString& styleName); + void saveStyleMainProperties(QSettings& settings, const FieldStyle& curStyle, + const FieldStyle& defaultStyle); + void saveKeywordStyle(QSettings& settings, const FieldStyle& curStyle, + const FieldStyle& defaultStyle); + +public: + static const QString DefaultStyle; + +public: + QColor cardBgColor; + +private: + QHash<QString, FieldStyle> styles; + QStringList stylesNames; // Keeps order of styles +}; + +#endif |