SettingsDialog.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
  3. * Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibURL/URL.h>
  8. #include <LibWebView/Application.h>
  9. #include <LibWebView/SearchEngine.h>
  10. #include <UI/Qt/Settings.h>
  11. #include <UI/Qt/SettingsDialog.h>
  12. #include <UI/Qt/StringUtils.h>
  13. #include <QLabel>
  14. #include <QMenu>
  15. namespace Ladybird {
  16. SettingsDialog::SettingsDialog(QMainWindow* window)
  17. : QDialog(window)
  18. , m_window(window)
  19. {
  20. m_layout = new QFormLayout(this);
  21. m_enable_search = new QCheckBox(this);
  22. m_enable_search->setChecked(Settings::the()->enable_search());
  23. m_search_engine_dropdown = new QPushButton(this);
  24. m_search_engine_dropdown->setText(qstring_from_ak_string(Settings::the()->search_engine().name));
  25. m_search_engine_dropdown->setMaximumWidth(200);
  26. m_preferred_languages = new QLineEdit(this);
  27. m_preferred_languages->setText(Settings::the()->preferred_languages().join(","));
  28. QObject::connect(m_preferred_languages, &QLineEdit::editingFinished, this, [this] {
  29. Settings::the()->set_preferred_languages(m_preferred_languages->text().split(","));
  30. });
  31. QObject::connect(m_preferred_languages, &QLineEdit::returnPressed, this, [this] {
  32. close();
  33. });
  34. m_enable_autocomplete = new QCheckBox(this);
  35. m_enable_autocomplete->setChecked(Settings::the()->enable_autocomplete());
  36. m_autocomplete_engine_dropdown = new QPushButton(this);
  37. m_autocomplete_engine_dropdown->setText(Settings::the()->autocomplete_engine().name);
  38. m_autocomplete_engine_dropdown->setMaximumWidth(200);
  39. m_new_tab_page = new QLineEdit(this);
  40. m_new_tab_page->setText(Settings::the()->new_tab_page());
  41. QObject::connect(m_new_tab_page, &QLineEdit::textChanged, this, [this] {
  42. auto url_string = ak_string_from_qstring(m_new_tab_page->text());
  43. m_new_tab_page->setStyleSheet(URL::URL(url_string).is_valid() ? "" : "border: 1px solid red;");
  44. });
  45. QObject::connect(m_new_tab_page, &QLineEdit::editingFinished, this, [this] {
  46. auto url_string = ak_string_from_qstring(m_new_tab_page->text());
  47. if (URL::URL(url_string).is_valid())
  48. Settings::the()->set_new_tab_page(m_new_tab_page->text());
  49. });
  50. QObject::connect(m_new_tab_page, &QLineEdit::returnPressed, this, [this] {
  51. close();
  52. });
  53. m_enable_do_not_track = new QCheckBox(this);
  54. m_enable_do_not_track->setChecked(Settings::the()->enable_do_not_track());
  55. #if (QT_VERSION > QT_VERSION_CHECK(6, 7, 0))
  56. QObject::connect(m_enable_do_not_track, &QCheckBox::checkStateChanged, this, [&](int state) {
  57. #else
  58. QObject::connect(m_enable_do_not_track, &QCheckBox::stateChanged, this, [&](int state) {
  59. #endif
  60. Settings::the()->set_enable_do_not_track(state == Qt::Checked);
  61. });
  62. m_enable_autoplay = new QCheckBox(this);
  63. if (WebView::Application::web_content_options().enable_autoplay == WebView::EnableAutoplay::Yes) {
  64. m_enable_autoplay->setChecked(true);
  65. } else {
  66. m_enable_autoplay->setChecked(Settings::the()->enable_autoplay());
  67. }
  68. #if (QT_VERSION > QT_VERSION_CHECK(6, 7, 0))
  69. QObject::connect(m_enable_autoplay, &QCheckBox::checkStateChanged, this, [&](int state) {
  70. #else
  71. QObject::connect(m_enable_autoplay, &QCheckBox::stateChanged, this, [&](int state) {
  72. #endif
  73. Settings::the()->set_enable_autoplay(state == Qt::Checked);
  74. });
  75. m_scrolling_speed = new QSlider( Qt::Horizontal, this);
  76. m_scrolling_speed->setRange(0, 300);
  77. m_scrolling_speed->setValue(Settings::the()->scrolling_speed());
  78. m_reset_scrolling_speed = new QPushButton("Reset", this);
  79. m_reset_scrolling_speed->setEnabled(m_scrolling_speed->value() != 100);
  80. QObject::connect(m_reset_scrolling_speed, &QPushButton::pressed, this, [&]() {
  81. m_scrolling_speed->setValue(100);
  82. });
  83. QHBoxLayout* scroll_speed_layout = new QHBoxLayout();
  84. scroll_speed_layout->addWidget(m_scrolling_speed);
  85. scroll_speed_layout->addWidget(m_reset_scrolling_speed);
  86. m_scrolling_speed_label = new QLabel(QString("Scrolling Speed (%1%)").arg(m_scrolling_speed->value()), this);
  87. QObject::connect(m_scrolling_speed, &QSlider::valueChanged, this, [&](int value) {
  88. Settings::the()->set_scrolling_speed(value);
  89. m_scrolling_speed_label->setText(QString("Scrolling Speed (%1%)").arg(value));
  90. m_reset_scrolling_speed->setEnabled(value != 100);
  91. });
  92. m_invert_vertical_scrolling = new QCheckBox(this);
  93. m_invert_vertical_scrolling->setChecked(Settings::the()->invert_vertical_scrolling());
  94. #if (QT_VERSION > QT_VERSION_CHECK(6, 7, 0))
  95. QObject::connect(m_invert_vertical_scrolling, &QCheckBox::checkStateChanged, this, [&](int state) {
  96. #else
  97. QObject::connect(m_invert_vertical_scrolling, &QCheckBox::stateChanged, this, [&](int state) {
  98. #endif
  99. Settings::the()->set_invert_vertical_scrolling(state == Qt::Checked);
  100. });
  101. m_invert_horizontal_scrolling = new QCheckBox(this);
  102. m_invert_horizontal_scrolling->setChecked(Settings::the()->invert_horizontal_scrolling());
  103. #if (QT_VERSION > QT_VERSION_CHECK(6, 7, 0))
  104. QObject::connect(m_invert_horizontal_scrolling, &QCheckBox::checkStateChanged, this, [&](int state) {
  105. #else
  106. QObject::connect(m_invert_horizontal_scrolling, &QCheckBox::stateChanged, this, [&](int state) {
  107. #endif
  108. Settings::the()->set_invert_horizontal_scrolling(state == Qt::Checked);
  109. });
  110. setup_search_engines();
  111. m_layout->addRow(new QLabel("Page on New Tab", this), m_new_tab_page);
  112. m_layout->addRow(new QLabel("Enable Search", this), m_enable_search);
  113. m_layout->addRow(new QLabel("Search Engine", this), m_search_engine_dropdown);
  114. m_layout->addRow(new QLabel("Preferred Language(s)", this), m_preferred_languages);
  115. m_layout->addRow(new QLabel("Enable Autocomplete", this), m_enable_autocomplete);
  116. m_layout->addRow(new QLabel("Autocomplete Engine", this), m_autocomplete_engine_dropdown);
  117. m_layout->addRow(new QLabel("Send web sites a \"Do Not Track\" request", this), m_enable_do_not_track);
  118. m_layout->addRow(new QLabel("Enable autoplay on all websites", this), m_enable_autoplay);
  119. m_layout->addRow(m_scrolling_speed_label, scroll_speed_layout);
  120. m_layout->addRow(new QLabel("Invert Vertical Scrolling", this), m_invert_vertical_scrolling);
  121. m_layout->addRow(new QLabel("Invert Horizontal Scrolling", this), m_invert_horizontal_scrolling);
  122. setWindowTitle("Settings");
  123. setLayout(m_layout);
  124. resize(600, 250);
  125. }
  126. void SettingsDialog::setup_search_engines()
  127. {
  128. // FIXME: These should be centralized in LibWebView.
  129. Vector<Settings::EngineProvider> autocomplete_engines = {
  130. { "DuckDuckGo", "https://duckduckgo.com/ac/?q={}" },
  131. { "Google", "https://www.google.com/complete/search?client=chrome&q={}" },
  132. { "Yahoo", "https://search.yahoo.com/sugg/gossip/gossip-us-ura/?output=sd1&command={}" },
  133. };
  134. QMenu* search_engine_menu = new QMenu(this);
  135. for (auto const& search_engine : WebView::search_engines()) {
  136. auto search_engine_name = qstring_from_ak_string(search_engine.name);
  137. QAction* action = new QAction(search_engine_name, this);
  138. connect(action, &QAction::triggered, this, [&, search_engine_name = std::move(search_engine_name)]() {
  139. Settings::the()->set_search_engine(search_engine);
  140. m_search_engine_dropdown->setText(search_engine_name);
  141. });
  142. search_engine_menu->addAction(action);
  143. }
  144. m_search_engine_dropdown->setMenu(search_engine_menu);
  145. m_search_engine_dropdown->setEnabled(Settings::the()->enable_search());
  146. QMenu* autocomplete_engine_menu = new QMenu(this);
  147. for (auto& autocomplete_engine : autocomplete_engines) {
  148. QAction* action = new QAction(autocomplete_engine.name, this);
  149. connect(action, &QAction::triggered, this, [&, autocomplete_engine] {
  150. Settings::the()->set_autocomplete_engine(autocomplete_engine);
  151. m_autocomplete_engine_dropdown->setText(autocomplete_engine.name);
  152. });
  153. autocomplete_engine_menu->addAction(action);
  154. }
  155. m_autocomplete_engine_dropdown->setMenu(autocomplete_engine_menu);
  156. m_autocomplete_engine_dropdown->setEnabled(Settings::the()->enable_autocomplete());
  157. #if (QT_VERSION > QT_VERSION_CHECK(6, 7, 0))
  158. connect(m_enable_search, &QCheckBox::checkStateChanged, this, [&](int state) {
  159. #else
  160. connect(m_enable_search, &QCheckBox::stateChanged, this, [&](int state) {
  161. #endif
  162. Settings::the()->set_enable_search(state == Qt::Checked);
  163. m_search_engine_dropdown->setEnabled(state == Qt::Checked);
  164. });
  165. #if (QT_VERSION > QT_VERSION_CHECK(6, 7, 0))
  166. connect(m_enable_autocomplete, &QCheckBox::checkStateChanged, this, [&](int state) {
  167. #else
  168. connect(m_enable_autocomplete, &QCheckBox::stateChanged, this, [&](int state) {
  169. #endif
  170. Settings::the()->set_enable_autocomplete(state == Qt::Checked);
  171. m_autocomplete_engine_dropdown->setEnabled(state == Qt::Checked);
  172. });
  173. }
  174. }