/app/src/window/optionswindow.cpp

https://github.com/yznpku/LabCompass · C++ · 79 lines · 65 code · 14 blank · 0 comment · 4 complexity · 453f5c2ea0a05b9b9ec24093af4a44b8 MD5 · raw file

  1. #include "optionswindow.h"
  2. static const QStringList UI_SCALE_FACTORS {
  3. "0.5",
  4. "0.75",
  5. "1",
  6. "1.25",
  7. "1.5",
  8. "1.75",
  9. "2",
  10. "2.5"
  11. };
  12. OptionsWindow::OptionsWindow(QQmlEngine* engine, Settings* settings) : Window(engine, false, true)
  13. {
  14. this->settings = settings;
  15. setSource(QUrl("qrc:/ui/options/Options.qml"));
  16. QStringList uiScaleFactorModel;
  17. std::transform(UI_SCALE_FACTORS.constBegin(), UI_SCALE_FACTORS.constEnd(), std::back_inserter(uiScaleFactorModel),
  18. [](const QString& s) { return s + 'x'; });
  19. rootObject()->findChild<QObject*>("uiScaleFactorInput")->setProperty("model", uiScaleFactorModel);
  20. connect(global(), SIGNAL(optionsWindowOpenChanged()),
  21. this, SLOT(onWindowOpenChanged()));
  22. connect(rootObject(), SIGNAL(openUrl(QString)),
  23. this, SLOT(onOpenUrl(QString)));
  24. connect(rootObject(), SIGNAL(browseClientPath()),
  25. this, SLOT(onBrowseClientPath()));
  26. connect(rootObject(), SIGNAL(save()),
  27. this, SLOT(save()));
  28. }
  29. void OptionsWindow::onWindowOpenChanged()
  30. {
  31. bool open = global()->property("optionsWindowOpen").toBool();
  32. if (open)
  33. load();
  34. setVisible(open);
  35. }
  36. void OptionsWindow::onBrowseClientPath()
  37. {
  38. const auto& file = QFileDialog::getOpenFileName(this, "Find Game Client", "", "Path of Exile Client (*.exe)");
  39. if (!file.isEmpty())
  40. rootObject()->setProperty("poeClientPath", QFileInfo(file).dir().absolutePath());
  41. }
  42. void OptionsWindow::onOpenUrl(const QString& url)
  43. {
  44. global()->setProperty("optionsWindowOpen", false);
  45. QDesktopServices::openUrl(QUrl(url));
  46. }
  47. void OptionsWindow::load()
  48. {
  49. foreach (const auto& name, settingNames) {
  50. const auto& s = name.toLocal8Bit().constData();
  51. rootObject()->setProperty(s, settings->property(s));
  52. }
  53. int uiScaleFactorIndex = UI_SCALE_FACTORS.indexOf(settings->get_scaleFactor());
  54. if (uiScaleFactorIndex == -1)
  55. uiScaleFactorIndex = UI_SCALE_FACTORS.indexOf("1");
  56. rootObject()->setProperty("uiScaleFactorIndex", uiScaleFactorIndex);
  57. }
  58. void OptionsWindow::save()
  59. {
  60. foreach (const auto& name, settingNames) {
  61. settings->setProperty(name.toLocal8Bit().constData(), rootObject()->property(name.toLocal8Bit().constData()));
  62. }
  63. int uiScaleFactorIndex = rootObject()->property("uiScaleFactorIndex").toInt();
  64. settings->setProperty("scaleFactor", UI_SCALE_FACTORS[uiScaleFactorIndex]);
  65. }