File indexing completed on 2024-05-05 05:40:25
0001 /*************************************************************************** 0002 * Copyright (C) 2019 by Renaud Guezennec * 0003 * http://www.rolisteam.org/contact * 0004 * * 0005 * This software is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 #include "controller/preferencescontroller.h" 0021 0022 #include <QFile> 0023 #include <QJsonDocument> 0024 #include <QStyleFactory> 0025 0026 #include "controller/gamecontroller.h" 0027 #include "data/rolisteamtheme.h" 0028 #include "model/characterstatemodel.h" 0029 #include "model/dicealiasmodel.h" 0030 #include "model/languagemodel.h" 0031 #include "model/palettemodel.h" 0032 #include "preferences/preferencesmanager.h" 0033 #include "worker/iohelper.h" 0034 #include "worker/messagehelper.h" 0035 0036 #define GRAY_SCALE 191 0037 0038 void initializeThemeModel(ThemeModel* model) 0039 { 0040 // normal 0041 model->addTheme(new RolisteamTheme(QPalette(), ThemeModel::tr("default"), "", QStyleFactory::create("fusion"), 0042 ":/resources/rolistheme/workspacebackground.jpg", 0, 0043 QColor(GRAY_SCALE, GRAY_SCALE, GRAY_SCALE), false)); 0044 0045 // DarkOrange 0046 QFile styleFile(":/stylesheet/resources/stylesheet/darkorange.qss"); 0047 styleFile.open(QFile::ReadOnly); 0048 QByteArray bytes= styleFile.readAll(); 0049 QString css(bytes); 0050 model->addTheme(new RolisteamTheme(QPalette(), ThemeModel::tr("darkorange"), css, QStyleFactory::create("fusion"), 0051 ":/resources/rolistheme/workspacebackground.jpg", 0, 0052 QColor(GRAY_SCALE, GRAY_SCALE, GRAY_SCALE), false)); 0053 0054 // DarkFusion 0055 QPalette palette; 0056 palette.setColor(QPalette::Window, QColor(53, 53, 53)); 0057 palette.setColor(QPalette::WindowText, Qt::white); 0058 palette.setColor(QPalette::Base, QColor(15, 15, 15)); 0059 palette.setColor(QPalette::AlternateBase, QColor(53, 53, 53)); 0060 palette.setColor(QPalette::ToolTipBase, Qt::white); 0061 palette.setColor(QPalette::ToolTipText, Qt::black); 0062 palette.setColor(QPalette::Text, Qt::white); 0063 palette.setColor(QPalette::Button, QColor(53, 53, 53)); 0064 palette.setColor(QPalette::ButtonText, Qt::white); 0065 palette.setColor(QPalette::BrightText, Qt::red); 0066 0067 palette.setColor(QPalette::Highlight, QColor(142, 45, 197).lighter()); 0068 palette.setColor(QPalette::HighlightedText, Qt::black); 0069 palette.setColor(QPalette::Disabled, QPalette::Text, Qt::darkGray); 0070 palette.setColor(QPalette::Disabled, QPalette::ButtonText, Qt::darkGray); 0071 0072 model->addTheme(new RolisteamTheme(palette, ThemeModel::tr("darkfusion"), "", QStyleFactory::create("fusion"), 0073 ":/resources/rolistheme/workspacebackground.jpg", 0, 0074 QColor(GRAY_SCALE, GRAY_SCALE, GRAY_SCALE), false)); 0075 } 0076 0077 /*appendAlias(new DiceAlias("l5r", "D10k")); 0078 model->addAlias(new DiceAlias("l5R", "D10K")); 0079 model->addAlias(new DiceAlias("DF", "D[-1-1]")); 0080 model->addAlias(new DiceAlias("nwod", "D10e10c[>7]")); 0081 model->addAlias(new DiceAlias("(.*)wod(.*)", "\\1d10e[=10]c[>=\\2]-@c[=1]", false));*/ 0082 0083 // healthy 0084 /*CharacterState* state= new CharacterState(); 0085 state->setId("0"); 0086 state->setColor(Qt::black); 0087 state->setLabel(CharacterStateModel::tr("Healthy")); 0088 model->addState(state); 0089 0090 state= new CharacterState(); 0091 state->setId("1"); 0092 state->setColor(QColor(255, 100, 100)); 0093 state->setLabel(CharacterStateModel::tr("Lightly Wounded")); 0094 model->addState(state); 0095 0096 state= new CharacterState(); 0097 state->setId("2"); 0098 state->setColor(QColor(255, 0, 0)); 0099 state->setLabel(CharacterStateModel::tr("Seriously injured")); 0100 model->addState(state); 0101 0102 state= new CharacterState(); 0103 state->setId("4"); 0104 state->setColor(Qt::gray); 0105 state->setLabel(CharacterStateModel::tr("Dead")); 0106 model->addState(state); 0107 0108 state= new CharacterState(); 0109 state->setId("5"); 0110 state->setColor(QColor(80, 80, 255)); 0111 state->setLabel(CharacterStateModel::tr("Sleeping")); 0112 model->addState(state); 0113 0114 state= new CharacterState(); 0115 state->setId("6"); 0116 state->setColor(QColor(0, 200, 0)); 0117 state->setLabel(CharacterStateModel::tr("Bewitched")); 0118 model->addState(state);*/ 0119 0120 PreferencesController::PreferencesController(QObject* parent) 0121 : AbstractControllerInterface(parent), m_themeModel(new ThemeModel), m_languageModel(new LanguageModel) 0122 { 0123 } 0124 0125 PreferencesController::~PreferencesController()= default; 0126 0127 void PreferencesController::setGameController(GameController* game) 0128 { 0129 m_preferences= game->preferencesManager(); 0130 loadPreferences(); 0131 0132 if(m_themeModel->rowCount() == 0) 0133 initializeThemeModel(m_themeModel.get()); 0134 } 0135 0136 QAbstractItemModel* PreferencesController::languageModel() const 0137 { 0138 return m_languageModel.get(); 0139 } 0140 ThemeModel* PreferencesController::themeModel() const 0141 { 0142 return m_themeModel.get(); 0143 } 0144 0145 void PreferencesController::savePreferences() 0146 { // paths 0147 m_preferences->registerValue("ThemeNumber", m_themeModel->rowCount(QModelIndex())); 0148 0149 // i18n 0150 m_preferences->registerValue("i18n_system", systemLang()); 0151 m_preferences->registerValue("i18n_index", currentLangIndex()); 0152 m_preferences->registerValue("i18n_path", currentLangPath()); 0153 m_preferences->registerValue("i18n_hasCustomfile", hasCustomFile()); 0154 m_preferences->registerValue("i18n_customfile", customFilePath()); 0155 0156 int i= 0; 0157 for(const auto& tmp : m_themeModel->themes()) 0158 { 0159 m_preferences->registerValue(QStringLiteral("Theme_%1_name").arg(i), tmp->getName()); 0160 QVariant var; 0161 var.setValue<QPalette>(tmp->getPalette()); 0162 m_preferences->registerValue(QStringLiteral("Theme_%1_palette").arg(i), var); 0163 m_preferences->registerValue(QStringLiteral("Theme_%1_stylename").arg(i), tmp->getStyleName()); 0164 m_preferences->registerValue(QStringLiteral("Theme_%1_bgColor").arg(i), tmp->getBackgroundColor()); 0165 m_preferences->registerValue(QStringLiteral("Theme_%1_bgPath").arg(i), tmp->getBackgroundImage()); 0166 m_preferences->registerValue(QStringLiteral("Theme_%1_bgPosition").arg(i), tmp->getBackgroundPosition()); 0167 m_preferences->registerValue(QStringLiteral("Theme_%1_css").arg(i), tmp->getCss()); 0168 m_preferences->registerValue(QStringLiteral("Theme_%1_removable").arg(i), tmp->isRemovable()); 0169 m_preferences->registerValue(QStringLiteral("Theme_%1_highlightDice").arg(i), tmp->getDiceHighlightColor()); 0170 ++i; 0171 } 0172 } 0173 0174 void PreferencesController::loadPreferences() 0175 { 0176 auto preferences= m_preferences; 0177 // Lang 0178 setCurrentLangIndex(m_preferences->value("i18n_index", -1).toInt()); 0179 setSystemLang(m_preferences->value("i18n_system", true).toBool()); 0180 setHasCustomFile(m_preferences->value("i18n_hasCustomfile", false).toBool()); 0181 setCustomFile(m_preferences->value("i18n_customfile", "").toString()); 0182 0183 // theme 0184 int size= preferences->value("ThemeNumber", 0).toInt(); 0185 m_currentThemeIndex= static_cast<std::size_t>(size); 0186 m_themeModel->clear(); 0187 for(int i= 0; i < size; ++i) 0188 { 0189 QString name= preferences->value(QStringLiteral("Theme_%1_name").arg(i), QString()).toString(); 0190 QPalette pal= preferences->value(QStringLiteral("Theme_%1_palette").arg(i), QPalette()).value<QPalette>(); 0191 QString style= preferences->value(QStringLiteral("Theme_%1_stylename").arg(i), "fusion").toString(); 0192 QColor color 0193 = preferences->value(QStringLiteral("Theme_%1_bgColor").arg(i), QColor(GRAY_SCALE, GRAY_SCALE, GRAY_SCALE)) 0194 .value<QColor>(); 0195 QString path= preferences->value(QStringLiteral("Theme_%1_bgPath").arg(i), "").toString(); 0196 QColor hlDice 0197 = preferences->value(QStringLiteral("Theme_%1_highlightDice").arg(i), QColor(Qt::red)).value<QColor>(); 0198 int pos= preferences->value(QStringLiteral("Theme_%1_bgPosition").arg(i), 0).toInt(); 0199 QString css= preferences->value(QStringLiteral("Theme_%1_css").arg(i), "").toString(); 0200 bool isRemovable= preferences->value(QStringLiteral("Theme_%1_removable").arg(i), false).toBool(); 0201 RolisteamTheme* tmp 0202 = new RolisteamTheme(pal, name, css, QStyleFactory::create(style), path, pos, color, isRemovable); 0203 tmp->setDiceHighlightColor(hlDice); 0204 m_themeModel->addTheme(tmp); 0205 // m_preferences->registerValue(QString("Theme_%1_css").arg(i),tmp->getName()); 0206 } 0207 0208 setCurrentThemeIndex(static_cast<std::size_t>(preferences->value("currentThemeIndex", 0).toInt())); 0209 } 0210 0211 void PreferencesController::importData(const QString& filename) 0212 { 0213 if(filename.isEmpty()) 0214 return; 0215 0216 QFile file(filename); 0217 if(file.exists() && file.open(QIODevice::ReadOnly)) 0218 { 0219 QJsonDocument doc= QJsonDocument::fromJson(file.readAll()); 0220 auto obj= doc.object(); 0221 } 0222 } 0223 0224 void PreferencesController::exportData(const QString& filename) 0225 { 0226 if(filename.isEmpty()) 0227 return; 0228 0229 QJsonObject obj; 0230 QFile file(filename); 0231 if(file.open(QIODevice::WriteOnly)) 0232 { 0233 QJsonDocument doc; 0234 doc.setObject(obj); 0235 file.write(doc.toJson()); 0236 } 0237 } 0238 0239 void PreferencesController::setSystemLang(bool b) 0240 { 0241 if(m_systemLang == b) 0242 return; 0243 m_systemLang= b; 0244 emit systemLangChanged(); 0245 } 0246 0247 void PreferencesController::setHasCustomFile(bool b) 0248 { 0249 if(m_customFile == b) 0250 return; 0251 m_customFile= b; 0252 emit hasCustomFileChanged(); 0253 } 0254 0255 void PreferencesController::setCustomFile(const QString& string) 0256 { 0257 if(m_customFilePath == string) 0258 return; 0259 m_customFilePath= string; 0260 emit customFileChanged(); 0261 } 0262 0263 void PreferencesController::setCurrentLangIndex(int index) 0264 { 0265 if(m_currentLangIndex == index) 0266 return; 0267 m_currentLangIndex= index; 0268 emit currentLangIndexChanged(); 0269 } 0270 0271 QString PreferencesController::themeName(int i) const 0272 { 0273 return m_themeModel->name(i); 0274 } 0275 0276 RolisteamTheme* PreferencesController::currentTheme() const 0277 { 0278 return m_themeModel->theme(static_cast<int>(m_currentThemeIndex)); 0279 } 0280 0281 void PreferencesController::exportTheme(const QString& filename, int pos) 0282 { 0283 if(filename.isEmpty()) 0284 return; 0285 0286 auto theme= m_themeModel->theme(pos); 0287 0288 if(nullptr == theme) 0289 return; 0290 0291 QFile exportFile(filename); 0292 if(!exportFile.open(QIODevice::WriteOnly)) 0293 { 0294 return; 0295 } 0296 // QJsonObject skinObject; 0297 // theme->writeTo(skinObject); 0298 auto skinObject= IOHelper::themeToObject(theme); 0299 // m_paletteModel->writeTo(skinObject); 0300 QJsonDocument saveDoc(skinObject); 0301 exportFile.write(saveDoc.toJson()); 0302 } 0303 0304 void PreferencesController::importTheme(const QString& filename) 0305 { 0306 if(filename.isEmpty()) 0307 return; 0308 0309 QFile importFile(filename); 0310 if(!importFile.open(QIODevice::ReadOnly)) 0311 { 0312 return; 0313 } 0314 QByteArray saveData= importFile.readAll(); 0315 QJsonDocument loadDoc(QJsonDocument::fromJson(saveData)); 0316 auto theme= IOHelper::jsonToTheme(loadDoc.object()); 0317 0318 m_themeModel->addTheme(theme); 0319 } 0320 void PreferencesController::removeTheme(int i) 0321 { 0322 m_themeModel->removeTheme(i); 0323 } 0324 void PreferencesController::dupplicateTheme(int pos, bool selectNew) 0325 { 0326 auto theme= m_themeModel->theme(pos); 0327 if(theme == nullptr) 0328 return; 0329 0330 QString str= theme->getName(); 0331 str.append(tr(" (copy)")); 0332 RolisteamTheme* newTheme 0333 = new RolisteamTheme(theme->getPalette(), str, theme->getCss(), theme->getStyle(), theme->getBackgroundImage(), 0334 theme->getBackgroundPosition(), theme->getBackgroundColor(), true); 0335 0336 m_themeModel->addTheme(newTheme); 0337 if(selectNew) 0338 setCurrentThemeIndex(static_cast<std::size_t>(m_themeModel->rowCount() - 1)); 0339 } 0340 0341 void PreferencesController::setCurrentThemeIndex(std::size_t pos) 0342 { 0343 if(pos == m_currentThemeIndex) 0344 return; 0345 m_currentThemeIndex= pos; 0346 emit currentThemeIndexChanged(); 0347 0348 auto theme= currentTheme(); 0349 if(nullptr == theme) 0350 return; 0351 // m_paletteModel->setPalette(theme->getPalette()); 0352 0353 m_preferences->registerValue("PathOfBackgroundImage", theme->getBackgroundImage()); 0354 m_preferences->registerValue("BackGroundColor", theme->getBackgroundColor()); 0355 m_preferences->registerValue("BackGroundPositioning", theme->getBackgroundPosition()); 0356 } 0357 0358 std::size_t PreferencesController::currentThemeIndex() const 0359 { 0360 return m_currentThemeIndex; 0361 } 0362 0363 int PreferencesController::currentLangIndex() const 0364 { 0365 return m_currentLangIndex; 0366 } 0367 0368 bool PreferencesController::systemLang() const 0369 { 0370 return m_systemLang; 0371 } 0372 0373 bool PreferencesController::hasCustomFile() const 0374 { 0375 return m_customFile; 0376 } 0377 0378 QString PreferencesController::customFilePath() const 0379 { 0380 return m_customFilePath; 0381 } 0382 0383 QStringList PreferencesController::currentLangPath() const 0384 { 0385 QStringList list; 0386 list= m_languageModel->pathFromIndex(m_languageModel->index(m_currentLangIndex, 0)); 0387 return list; 0388 } 0389 0390 RolisteamTheme* PreferencesController::currentEditableTheme() 0391 { 0392 auto theme= currentTheme(); 0393 0394 if(!theme->isRemovable()) 0395 { 0396 dupplicateTheme(static_cast<int>(m_currentThemeIndex), true); 0397 theme= currentTheme(); 0398 } 0399 0400 return theme; 0401 } 0402 0403 void PreferencesController::setCurrentThemeStyle(QStyle* style) 0404 { 0405 auto theme= currentEditableTheme(); 0406 if(nullptr == theme) 0407 return; 0408 0409 theme->setStyle(style); 0410 } 0411 0412 void PreferencesController::setCurrentThemeBackground(const QString& path, int pos, const QColor& color) 0413 { 0414 auto theme= currentEditableTheme(); 0415 if(nullptr == theme) 0416 return; 0417 auto preferences= m_preferences; 0418 0419 theme->setBackgroundImage(path); 0420 theme->setBackgroundColor(color); 0421 theme->setBackgroundPosition(pos); 0422 0423 preferences->registerValue("PathOfBackgroundImage", path); 0424 preferences->registerValue("BackGroundColor", color); 0425 preferences->registerValue("BackGroundPositioning", pos); 0426 } 0427 0428 void PreferencesController::setColorCurrentTheme(const QColor& color, int palettePos) 0429 { 0430 auto theme= currentEditableTheme(); 0431 if(nullptr == theme) 0432 return; 0433 auto model= theme->paletteModel(); 0434 0435 model->setColor(palettePos, color); 0436 theme->setPalette(model->getPalette()); 0437 } 0438 0439 void PreferencesController::setDiceHighLightColor(const QColor& color) 0440 { 0441 auto theme= currentEditableTheme(); 0442 theme->setDiceHighlightColor(color); 0443 m_preferences->registerValue("DiceHighlightColor", color); 0444 } 0445 0446 void PreferencesController::setCurrentThemeCss(const QString& css) 0447 { 0448 auto theme= currentEditableTheme(); 0449 if(nullptr == theme) 0450 return; 0451 0452 theme->setCss(css); 0453 } 0454 0455 void PreferencesController::setCurrentThemeTitle(const QString& title) 0456 { 0457 auto theme= currentEditableTheme(); 0458 if(nullptr == theme) 0459 return; 0460 0461 theme->setName(title); 0462 }