| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/SettingsWindow.h" |
| 5 | |
| 6 | #include <utility> |
| 7 | |
| 8 | #include <QApplication> |
| 9 | #include <QColor> |
| 10 | #include <QDialogButtonBox> |
| 11 | #include <QEvent> |
| 12 | #include <QHBoxLayout> |
| 13 | #include <QListWidget> |
| 14 | #include <QPalette> |
| 15 | #include <QStackedWidget> |
| 16 | #include <QTabWidget> |
| 17 | #include <QVBoxLayout> |
| 18 | |
| 19 | #include "DolphinQt/Config/ControllersPane.h" |
| 20 | #include "DolphinQt/Config/Graphics/GraphicsPane.h" |
| 21 | #include "DolphinQt/MainWindow.h" |
| 22 | #include "DolphinQt/QtUtils/QtUtils.h" |
| 23 | #include "DolphinQt/QtUtils/WrapInScrollArea.h" |
| 24 | #include "DolphinQt/Settings.h" |
| 25 | #include "DolphinQt/Settings/AdvancedPane.h" |
| 26 | #include "DolphinQt/Settings/AudioPane.h" |
| 27 | #include "DolphinQt/Settings/GameCubePane.h" |
| 28 | #include "DolphinQt/Settings/GeneralPane.h" |
| 29 | #include "DolphinQt/Settings/InterfacePane.h" |
| 30 | #include "DolphinQt/Settings/OnScreenDisplayPane.h" |
| 31 | #include "DolphinQt/Settings/PathPane.h" |
| 32 | #include "DolphinQt/Settings/TriforcePane.h" |
| 33 | #include "DolphinQt/Settings/WiiPane.h" |
| 34 | |
| 35 | StackedSettingsWindow::StackedSettingsWindow(QWidget* parent) : QDialog{parent} |
| 36 | { |
| 37 | // This eliminates the ugly line between the title bar and window contents with KDE Plasma. |
| 38 | setStyleSheet(QStringLiteral("QDialog { border: none; }")); |
| 39 | |
| 40 | auto* const layout = new QHBoxLayout{this}; |
| 41 | |
| 42 | // Calculated value for the padding in our list items. |
| 43 | const int list_item_padding = layout->contentsMargins().left() / 2; |
| 44 | |
| 45 | // Eliminate padding around layouts. |
| 46 | layout->setContentsMargins(QMargins{}); |
| 47 | layout->setSpacing(0); |
| 48 | |
| 49 | m_navigation_list = new QListWidget; |
| 50 | |
| 51 | // Ensure list doesn't grow horizontally and is not resized smaller than its contents. |
| 52 | m_navigation_list->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum); |
| 53 | m_navigation_list->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents); |
| 54 | |
| 55 | // FYI: "base" is the window color on Windows and "alternate-base" is very high contrast on macOS. |
| 56 | const auto* const list_background = |
| 57 | #if !defined(__APPLE__) |
| 58 | "palette(alternate-base)"; |
| 59 | #else |
| 60 | "palette(base)"; |
| 61 | #endif |
| 62 | |
| 63 | m_navigation_list->setStyleSheet( |
| 64 | QString::fromUtf8( |
| 65 | // Remove border around entire widget and adjust background color. |
| 66 | "QListWidget { border: 0; background: %1; } " |
| 67 | // Note: padding-left is broken unless border is set, which then breaks colors. |
| 68 | // see: https://bugreports.qt.io/browse/QTBUG-122698 |
| 69 | "QListWidget::item { padding-top: %2px; padding-bottom: %2px; } " |
| 70 | // Maintain selected item color when unfocused. |
| 71 | "QListWidget::item:selected { background: palette(highlight); " |
| 72 | // Prevent text color change on focus loss. |
| 73 | "color: palette(highlighted-text); " |
| 74 | "} " |
| 75 | // Remove ugly dotted outline on selected row (Windows and GNOME). |
| 76 | "* { outline: none; } ") |
| 77 | .arg(QString::fromUtf8(list_background)) |
| 78 | .arg(list_item_padding)); |
| 79 | |
| 80 | UpdateNavigationListStyle(); |
| 81 | |
| 82 | layout->addWidget(m_navigation_list); |
| 83 | |
| 84 | auto* const right_side = new QVBoxLayout; |
| 85 | layout->addLayout(right_side); |
| 86 | |
| 87 | m_stacked_panes = new QStackedWidget; |
| 88 | |
| 89 | right_side->addWidget(m_stacked_panes); |
| 90 | |
| 91 | // The QFrame gives us some padding around the button. |
| 92 | auto* const button_frame = new QFrame; |
| 93 | auto* const button_layout = new QGridLayout{button_frame}; |
| 94 | auto* const button_box = new QDialogButtonBox(QDialogButtonBox::Close); |
| 95 | right_side->addWidget(button_frame); |
| 96 | button_layout->addWidget(button_box); |
| 97 | |
| 98 | connect(button_box, &QDialogButtonBox::rejected, this, &QDialog::reject); |
| 99 | |
| 100 | connect(m_navigation_list, &QListWidget::currentRowChanged, m_stacked_panes, |
| 101 | &QStackedWidget::setCurrentIndex); |
| 102 | } |
| 103 | |
| 104 | void StackedSettingsWindow::OnDoneCreatingPanes() |
| 105 | { |
| 106 | // Make sure the first item is actually selected by default. |
| 107 | ActivatePane(0); |
| 108 | // Take on the preferred size. |
| 109 | QtUtils::AdjustSizeWithinScreen(this); |
| 110 | } |
| 111 | |
| 112 | void StackedSettingsWindow::changeEvent(QEvent* event) |
| 113 | { |
| 114 | QDialog::changeEvent(event); |
| 115 | |
| 116 | const auto type = event->type(); |
| 117 | |
| 118 | const bool palette_changed = type == QEvent::PaletteChange; |
| 119 | const bool application_palette_changed = type == QEvent::ApplicationPaletteChange; |
| 120 | const bool style_changed = type == QEvent::StyleChange; |
| 121 | const bool theme_event = type == QEvent::ThemeChange; |
| 122 | |
| 123 | const bool theme_changed = application_palette_changed || theme_event; |
| 124 | |
| 125 | if (theme_changed && !m_handling_theme_change) |
| 126 | { |
| 127 | m_handling_theme_change = true; |
| 128 | Settings::Instance().ApplyStyle(); |
| 129 | // Ensure the dialog and its children adopt the new system palette. |
| 130 | setPalette(qApp->palette()); |
| 131 | Settings::Instance().TriggerThemeChanged(); |
| 132 | m_handling_theme_change = false; |
| 133 | } |
| 134 | |
| 135 | if (palette_changed || application_palette_changed || style_changed || theme_event) |
| 136 | UpdateNavigationListStyle(); |
| 137 | } |
| 138 | |
| 139 | void StackedSettingsWindow::UpdateNavigationListStyle() |
| 140 | { |
| 141 | if (!m_navigation_list) |
| 142 | return; |
| 143 | |
| 144 | QPalette list_palette = m_navigation_list->palette(); |
| 145 | const QPalette app_palette = qApp->palette(); |
| 146 | |
| 147 | QColor highlight_color = app_palette.color(QPalette::Active, QPalette::Highlight); |
| 148 | QColor highlighted_text = app_palette.color(QPalette::Active, QPalette::HighlightedText); |
| 149 | |
| 150 | #if defined(__APPLE__) |
| 151 | const bool is_dark_theme = Settings::Instance().IsThemeDark(); |
| 152 | // The default macOS accent is quite light in our list; darken it for readability in light mode. |
| 153 | if (!is_dark_theme) |
| 154 | { |
| 155 | highlight_color = highlight_color.darker(130); |
| 156 | highlighted_text = QColor(Qt::white); |
| 157 | } |
| 158 | #endif |
| 159 | |
| 160 | for (const QPalette::ColorGroup group : {QPalette::Active, QPalette::Inactive}) |
| 161 | { |
| 162 | list_palette.setColor(group, QPalette::Base, app_palette.color(group, QPalette::Base)); |
| 163 | list_palette.setColor(group, QPalette::AlternateBase, |
| 164 | app_palette.color(group, QPalette::AlternateBase)); |
| 165 | list_palette.setColor(group, QPalette::Highlight, highlight_color); |
| 166 | list_palette.setColor(group, QPalette::HighlightedText, highlighted_text); |
| 167 | } |
| 168 | |
| 169 | m_navigation_list->setPalette(list_palette); |
| 170 | } |
| 171 | |
| 172 | void StackedSettingsWindow::AddPane(QWidget* widget, const QString& name) |
| 173 | { |
| 174 | m_stacked_panes->addWidget(widget); |
| 175 | // Pad the left and right of each item. |
| 176 | m_navigation_list->addItem(QStringLiteral(" %1 ").arg(name)); |
| 177 | } |
| 178 | |
| 179 | void StackedSettingsWindow::AddWrappedPane(QWidget* widget, const QString& name) |
| 180 | { |
| 181 | AddPane(GetWrappedWidget(widget), name); |
| 182 | } |
| 183 | |
| 184 | void StackedSettingsWindow::ActivatePane(int index) |
| 185 | { |
| 186 | m_navigation_list->setCurrentRow(index); |
| 187 | } |
| 188 | |
| 189 | SettingsWindow::SettingsWindow(MainWindow* parent) : StackedSettingsWindow{parent} |
| 190 | { |
| 191 | setWindowTitle(tr("Settings")); |
| 192 | |
| 193 | // If you change the order, don't forget to update the SettingsWindowPaneIndex enum. |
| 194 | AddWrappedPane(new GeneralPane, tr("General")); |
| 195 | AddPane(new GraphicsPane{parent, nullptr}, tr("Graphics")); |
| 196 | AddWrappedPane(new ControllersPane, tr("Controllers")); |
| 197 | AddWrappedPane(new InterfacePane, tr("Interface")); |
| 198 | AddWrappedPane(new OnScreenDisplayPane, tr("On-Screen Display")); |
| 199 | AddWrappedPane(new AudioPane, tr("Audio")); |
| 200 | AddWrappedPane(new PathPane, tr("Paths")); |
| 201 | AddWrappedPane(new GameCubePane{parent}, tr("GameCube")); |
| 202 | AddWrappedPane(new WiiPane, tr("Wii")); |
| 203 | AddWrappedPane(new TriforcePane, tr("Triforce")); |
| 204 | AddWrappedPane(new AdvancedPane, tr("Advanced")); |
| 205 | |
| 206 | OnDoneCreatingPanes(); |
| 207 | } |
| 208 | |
| 209 | void SettingsWindow::SelectPane(SettingsWindowPaneIndex index) |
| 210 | { |
| 211 | ActivatePane(std::to_underlying(index)); |
| 212 | } |
| 213 | |
| 214 | void SettingsWindow::closeEvent(QCloseEvent*) |
| 215 | { |
| 216 | Config::Save(); |
| 217 | } |