| 1 | // Copyright 2015 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/ToolBar.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include <QAction> |
| 10 | #include <QIcon> |
| 11 | |
| 12 | #include "Core/Core.h" |
| 13 | #include "Core/System.h" |
| 14 | #include "DolphinQt/Host.h" |
| 15 | #include "DolphinQt/Resources.h" |
| 16 | #include "DolphinQt/Settings.h" |
| 17 | |
| 18 | static QSize ICON_SIZE(32, 32); |
| 19 | |
| 20 | ToolBar::ToolBar(QWidget* parent) : QToolBar(parent) |
| 21 | { |
| 22 | setToolButtonStyle(Qt::ToolButtonTextUnderIcon); |
| 23 | setMovable(!Settings::Instance().AreWidgetsLocked()); |
| 24 | setFloatable(false); |
| 25 | setIconSize(ICON_SIZE); |
| 26 | setVisible(Settings::Instance().IsToolBarVisible()); |
| 27 | |
| 28 | setWindowTitle(tr("Toolbar")); |
| 29 | setObjectName(QStringLiteral("toolbar")); |
| 30 | |
| 31 | MakeActions(); |
| 32 | connect(&Settings::Instance(), &Settings::ThemeChanged, this, &ToolBar::UpdateIcons); |
| 33 | UpdateIcons(); |
| 34 | |
| 35 | connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, |
| 36 | [this](Core::State state) { OnEmulationStateChanged(state); }); |
| 37 | |
| 38 | connect(Host::GetInstance(), &Host::UpdateDisasmDialog, this, |
| 39 | [this] { OnEmulationStateChanged(Core::GetState(Core::System::GetInstance())); }); |
| 40 | |
| 41 | connect(&Settings::Instance(), &Settings::DebugModeToggled, this, &ToolBar::OnDebugModeToggled); |
| 42 | |
| 43 | connect(&Settings::Instance(), &Settings::ToolBarVisibilityChanged, this, &ToolBar::setVisible); |
| 44 | connect(this, &ToolBar::visibilityChanged, &Settings::Instance(), &Settings::SetToolBarVisible); |
| 45 | |
| 46 | connect(&Settings::Instance(), &Settings::WidgetLockChanged, this, |
| 47 | [this](bool locked) { setMovable(!locked); }); |
| 48 | |
| 49 | connect(&Settings::Instance(), &Settings::GameListRefreshRequested, this, |
| 50 | [this] { m_refresh_action->setEnabled(false); }); |
| 51 | connect(&Settings::Instance(), &Settings::GameListRefreshStarted, this, |
| 52 | [this] { m_refresh_action->setEnabled(true); }); |
| 53 | |
| 54 | OnEmulationStateChanged(Core::GetState(Core::System::GetInstance())); |
| 55 | OnDebugModeToggled(Settings::Instance().IsDebugModeEnabled()); |
| 56 | } |
| 57 | |
| 58 | void ToolBar::OnEmulationStateChanged(Core::State state) |
| 59 | { |
| 60 | bool running = state != Core::State::Uninitialized; |
| 61 | m_stop_action->setEnabled(running); |
| 62 | m_fullscreen_action->setEnabled(running); |
| 63 | m_screenshot_action->setEnabled(running); |
| 64 | |
| 65 | bool playing = running && state != Core::State::Paused; |
| 66 | UpdatePausePlayButtonState(playing); |
| 67 | |
| 68 | const bool paused = Core::GetState(Core::System::GetInstance()) == Core::State::Paused; |
| 69 | m_step_action->setEnabled(paused); |
| 70 | m_step_over_action->setEnabled(paused); |
| 71 | m_step_out_action->setEnabled(paused); |
| 72 | m_skip_action->setEnabled(paused); |
| 73 | m_set_pc_action->setEnabled(paused); |
| 74 | } |
| 75 | |
| 76 | void ToolBar::closeEvent(QCloseEvent*) |
| 77 | { |
| 78 | Settings::Instance().SetToolBarVisible(false); |
| 79 | } |
| 80 | |
| 81 | void ToolBar::OnDebugModeToggled(bool enabled) |
| 82 | { |
| 83 | m_step_action->setVisible(enabled); |
| 84 | m_step_over_action->setVisible(enabled); |
| 85 | m_step_out_action->setVisible(enabled); |
| 86 | m_skip_action->setVisible(enabled); |
| 87 | m_show_pc_action->setVisible(enabled); |
| 88 | m_set_pc_action->setVisible(enabled); |
| 89 | |
| 90 | const bool paused = Core::GetState(Core::System::GetInstance()) == Core::State::Paused; |
| 91 | m_step_action->setEnabled(paused); |
| 92 | m_step_over_action->setEnabled(paused); |
| 93 | m_step_out_action->setEnabled(paused); |
| 94 | m_skip_action->setEnabled(paused); |
| 95 | m_set_pc_action->setEnabled(paused); |
| 96 | } |
| 97 | |
| 98 | void ToolBar::MakeActions() |
| 99 | { |
| 100 | // i18n: Here, "Step" is a verb. This feature is used for |
| 101 | // going through code step by step. |
| 102 | m_step_action = addAction(tr("Step"), this, &ToolBar::StepPressed); |
| 103 | // i18n: Here, "Step" is a verb. This feature is used for |
| 104 | // going through code step by step. |
| 105 | m_step_over_action = addAction(tr("Step Over"), this, &ToolBar::StepOverPressed); |
| 106 | // i18n: Here, "Step" is a verb. This feature is used for |
| 107 | // going through code step by step. |
| 108 | m_step_out_action = addAction(tr("Step Out"), this, &ToolBar::StepOutPressed); |
| 109 | m_skip_action = addAction(tr("Skip"), this, &ToolBar::SkipPressed); |
| 110 | // i18n: Here, PC is an acronym for program counter, not personal computer. |
| 111 | m_show_pc_action = addAction(tr("Show PC"), this, &ToolBar::ShowPCPressed); |
| 112 | // i18n: Here, PC is an acronym for program counter, not personal computer. |
| 113 | m_set_pc_action = addAction(tr("Set PC"), this, &ToolBar::SetPCPressed); |
| 114 | |
| 115 | m_open_action = addAction(tr("Open"), this, &ToolBar::OpenPressed); |
| 116 | m_refresh_action = addAction(tr("Refresh"), [this] { emit RefreshPressed(); }); |
| 117 | m_refresh_action->setEnabled(false); |
| 118 | |
| 119 | addSeparator(); |
| 120 | |
| 121 | m_pause_play_action = addAction(tr("Play"), this, &ToolBar::PlayPressed); |
| 122 | |
| 123 | m_stop_action = addAction(tr("Stop"), this, &ToolBar::StopPressed); |
| 124 | m_fullscreen_action = addAction(tr("FullScr"), this, &ToolBar::FullScreenPressed); |
| 125 | m_screenshot_action = addAction(tr("ScrShot"), this, &ToolBar::ScreenShotPressed); |
| 126 | |
| 127 | addSeparator(); |
| 128 | |
| 129 | m_config_action = addAction(tr("Config"), this, &ToolBar::SettingsPressed); |
| 130 | m_graphics_action = addAction(tr("Graphics"), this, &ToolBar::GraphicsPressed); |
| 131 | m_controllers_action = addAction(tr("Controllers"), this, &ToolBar::ControllersPressed); |
| 132 | |
| 133 | // Ensure every button has about the same width |
| 134 | std::vector<QWidget*> items; |
| 135 | for (const auto& action : |
| 136 | {m_open_action, m_pause_play_action, m_stop_action, m_stop_action, m_fullscreen_action, |
| 137 | m_screenshot_action, m_config_action, m_graphics_action, m_controllers_action, |
| 138 | m_step_action, m_step_over_action, m_step_out_action, m_skip_action, m_show_pc_action, |
| 139 | m_set_pc_action}) |
| 140 | { |
| 141 | items.emplace_back(widgetForAction(action)); |
| 142 | } |
| 143 | |
| 144 | std::vector<int> widths; |
| 145 | std::ranges::transform(items, std::back_inserter(widths), |
| 146 | [](QWidget* item) { return item->sizeHint().width(); }); |
| 147 | |
| 148 | const int min_width = *std::ranges::max_element(widths) * 0.85; |
| 149 | for (QWidget* widget : items) |
| 150 | widget->setMinimumWidth(min_width); |
| 151 | } |
| 152 | |
| 153 | void ToolBar::UpdatePausePlayButtonState(const bool playing_state) |
| 154 | { |
| 155 | if (playing_state) |
| 156 | { |
| 157 | disconnect(m_pause_play_action, nullptr, nullptr, nullptr); |
| 158 | m_pause_play_action->setText(tr("Pause")); |
| 159 | m_pause_play_action->setIcon(Resources::GetThemeIcon("pause")); |
| 160 | connect(m_pause_play_action, &QAction::triggered, this, &ToolBar::PausePressed); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | disconnect(m_pause_play_action, nullptr, nullptr, nullptr); |
| 165 | m_pause_play_action->setText(tr("Play")); |
| 166 | m_pause_play_action->setIcon(Resources::GetThemeIcon("play")); |
| 167 | connect(m_pause_play_action, &QAction::triggered, this, &ToolBar::PlayPressed); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void ToolBar::UpdateIcons() |
| 172 | { |
| 173 | m_step_action->setIcon(Resources::GetThemeIcon("debugger_step_in")); |
| 174 | m_step_over_action->setIcon(Resources::GetThemeIcon("debugger_step_over")); |
| 175 | m_step_out_action->setIcon(Resources::GetThemeIcon("debugger_step_out")); |
| 176 | m_skip_action->setIcon(Resources::GetThemeIcon("debugger_skip")); |
| 177 | m_show_pc_action->setIcon(Resources::GetThemeIcon("debugger_show_pc")); |
| 178 | m_set_pc_action->setIcon(Resources::GetThemeIcon("debugger_set_pc")); |
| 179 | |
| 180 | m_open_action->setIcon(Resources::GetThemeIcon("open")); |
| 181 | m_refresh_action->setIcon(Resources::GetThemeIcon("refresh")); |
| 182 | |
| 183 | const Core::State state = Core::GetState(Core::System::GetInstance()); |
| 184 | const bool playing = state != Core::State::Uninitialized && state != Core::State::Paused; |
| 185 | if (!playing) |
| 186 | m_pause_play_action->setIcon(Resources::GetThemeIcon("play")); |
| 187 | else |
| 188 | m_pause_play_action->setIcon(Resources::GetThemeIcon("pause")); |
| 189 | |
| 190 | m_stop_action->setIcon(Resources::GetThemeIcon("stop")); |
| 191 | m_fullscreen_action->setIcon(Resources::GetThemeIcon("fullscreen")); |
| 192 | m_screenshot_action->setIcon(Resources::GetThemeIcon("screenshot")); |
| 193 | m_config_action->setIcon(Resources::GetThemeIcon("config")); |
| 194 | m_controllers_action->setIcon(Resources::GetThemeIcon("classic")); |
| 195 | m_graphics_action->setIcon(Resources::GetThemeIcon("graphics")); |
| 196 | } |