File indexing completed on 2024-05-12 08:02:31
0001 /* 0002 SPDX-FileCopyrightText: 2015 Jakob Gruber <jakob.gruber@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "settings.h" 0008 0009 Settings::Settings() { 0010 0011 m_keys << QStringLiteral("game/width") 0012 << QStringLiteral("game/height") 0013 << QStringLiteral("game/box_ratio") 0014 << QStringLiteral("game/prevent_mistakes") 0015 << QStringLiteral("game/level") 0016 << QStringLiteral("game/custom_bg_enabled") 0017 << QStringLiteral("game/custom_bg_path") 0018 << QStringLiteral("game/font_color_solved") 0019 << QStringLiteral("game/font_color_unsolved"); 0020 0021 /* We explicitly pass "picmi" as the organization name in order to 0022 * access the correct settings. 0023 * This has historical reason in that we used to set the organization 0024 * name globally, which however caused problems in 0025 * QStandardPaths::locate(). 0026 */ 0027 m_qsettings = QSharedPointer<QSettings>(new QSettings(QStringLiteral("picmi"), QStringLiteral("picmi"))); 0028 restore(); 0029 } 0030 0031 int Settings::height() const { 0032 return m_height; 0033 } 0034 0035 int Settings::width() const { 0036 return m_width; 0037 } 0038 0039 double Settings::boxDensity() const { 0040 return m_box_density; 0041 } 0042 0043 bool Settings::preventMistakes() const { 0044 return m_prevent_mistakes; 0045 } 0046 0047 KGameDifficultyLevel::StandardLevel Settings::level() const { 0048 return m_level; 0049 } 0050 0051 bool Settings::customBgEnabled() const 0052 { 0053 return m_custom_bg_enabled; 0054 } 0055 0056 QString Settings::customBgPath() const 0057 { 0058 return m_custom_bg_path; 0059 } 0060 0061 QString Settings::fontColorSolved() const 0062 { 0063 return m_font_color_solved; 0064 } 0065 0066 QString Settings::fontColorUnsolved() const 0067 { 0068 return m_font_color_unsolved; 0069 } 0070 0071 void Settings::setWidth(int width) { 0072 m_width = width; 0073 setValue(Width, width); 0074 } 0075 0076 void Settings::setHeight(int height) { 0077 m_height = height; 0078 setValue(Height, height); 0079 } 0080 0081 void Settings::setBoxDensity(double box_density) { 0082 m_box_density = box_density; 0083 setValue(BoxDensity, box_density); 0084 } 0085 0086 void Settings::setPreventMistakes(bool prevent_mistakes) { 0087 m_prevent_mistakes = prevent_mistakes; 0088 setValue(PreventMistakes, prevent_mistakes); 0089 } 0090 0091 void Settings::setLevel(KGameDifficultyLevel::StandardLevel level) { 0092 m_level = level; 0093 setValue(Level, level); 0094 } 0095 0096 void Settings::setCustomBgEnabled(bool enabled) 0097 { 0098 m_custom_bg_enabled = enabled; 0099 setValue(CustomBgEnabled, enabled); 0100 } 0101 0102 void Settings::setCustomBgPath(const QString &path) 0103 { 0104 m_custom_bg_path = path; 0105 setValue(CustomBgPath, path); 0106 } 0107 0108 void Settings::setFontColorSolved(const QString &color) 0109 { 0110 m_font_color_solved = color; 0111 setValue(FontColorSolved, color); 0112 } 0113 0114 void Settings::setFontColorUnsolved(const QString &color) 0115 { 0116 m_font_color_unsolved = color; 0117 setValue(FontColorUnsolved, color); 0118 } 0119 0120 void Settings::restore() { 0121 m_width = m_qsettings->value(m_keys[Width], 15).toInt(); 0122 m_height = m_qsettings->value(m_keys[Height], 10).toInt(); 0123 m_box_density = m_qsettings->value(m_keys[BoxDensity], 0.55).toDouble(); 0124 m_prevent_mistakes = m_qsettings->value(m_keys[PreventMistakes], false).toBool(); 0125 m_level = (KGameDifficultyLevel::StandardLevel)m_qsettings->value(m_keys[Level], 0126 KGameDifficultyLevel::Medium).toInt(); 0127 m_custom_bg_enabled = m_qsettings->value(m_keys[CustomBgEnabled], false).toBool(); 0128 m_custom_bg_path = m_qsettings->value(m_keys[CustomBgPath], QString()).toString(); 0129 m_font_color_solved = m_qsettings->value(m_keys[FontColorSolved], QStringLiteral("#5500ff")).toString(); 0130 m_font_color_unsolved = m_qsettings->value(m_keys[FontColorUnsolved], QStringLiteral("#000000")).toString(); 0131 } 0132 0133 void Settings::setValue(SettingsType type, const QVariant &value) 0134 { 0135 m_qsettings->setValue(m_keys[type], value); 0136 Q_EMIT settingChanged(type); 0137 } 0138 0139 void Settings::sync() 0140 { 0141 m_qsettings->sync(); 0142 } 0143 0144 Settings *Settings::instance() 0145 { 0146 static Settings settings; 0147 return &settings; 0148 } 0149 0150 #include "moc_settings.cpp"