ToolBar.cpp 7.6 KB

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