File: ToolBar.cpp

package info (click to toggle)
dolphin-emu 5.0-17995-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 68,196 kB
  • sloc: cpp: 528,500; ansic: 74,478; sh: 2,477; javascript: 1,773; python: 1,116; makefile: 773; asm: 726; pascal: 257; perl: 97; objc: 75; xml: 4
file content (196 lines) | stat: -rw-r--r-- 7,716 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2015 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "DolphinQt/ToolBar.h"

#include <algorithm>
#include <vector>

#include <QAction>
#include <QIcon>

#include "Core/Core.h"
#include "Core/NetPlayProto.h"
#include "DolphinQt/Host.h"
#include "DolphinQt/Resources.h"
#include "DolphinQt/Settings.h"

static QSize ICON_SIZE(32, 32);

ToolBar::ToolBar(QWidget* parent) : QToolBar(parent)
{
  setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  setMovable(!Settings::Instance().AreWidgetsLocked());
  setFloatable(false);
  setIconSize(ICON_SIZE);
  setVisible(Settings::Instance().IsToolBarVisible());

  setWindowTitle(tr("Toolbar"));
  setObjectName(QStringLiteral("toolbar"));

  MakeActions();
  connect(&Settings::Instance(), &Settings::ThemeChanged, this, &ToolBar::UpdateIcons);
  UpdateIcons();

  connect(&Settings::Instance(), &Settings::EmulationStateChanged, this,
          [this](Core::State state) { OnEmulationStateChanged(state); });

  connect(Host::GetInstance(), &Host::UpdateDisasmDialog, this,
          [this] { OnEmulationStateChanged(Core::GetState()); });

  connect(&Settings::Instance(), &Settings::DebugModeToggled, this, &ToolBar::OnDebugModeToggled);

  connect(&Settings::Instance(), &Settings::ToolBarVisibilityChanged, this, &ToolBar::setVisible);
  connect(this, &ToolBar::visibilityChanged, &Settings::Instance(), &Settings::SetToolBarVisible);

  connect(&Settings::Instance(), &Settings::WidgetLockChanged, this,
          [this](bool locked) { setMovable(!locked); });

  connect(&Settings::Instance(), &Settings::GameListRefreshRequested, this,
          [this] { m_refresh_action->setEnabled(false); });
  connect(&Settings::Instance(), &Settings::GameListRefreshStarted, this,
          [this] { m_refresh_action->setEnabled(true); });

  OnEmulationStateChanged(Core::GetState());
  OnDebugModeToggled(Settings::Instance().IsDebugModeEnabled());
}

void ToolBar::OnEmulationStateChanged(Core::State state)
{
  bool running = state != Core::State::Uninitialized;
  m_stop_action->setEnabled(running);
  m_fullscreen_action->setEnabled(running);
  m_screenshot_action->setEnabled(running);

  bool playing = running && state != Core::State::Paused;
  UpdatePausePlayButtonState(playing);

  bool paused = Core::GetState() == Core::State::Paused;
  m_step_action->setEnabled(paused);
  m_step_over_action->setEnabled(paused);
  m_step_out_action->setEnabled(paused);
  m_skip_action->setEnabled(paused);
  m_set_pc_action->setEnabled(paused);
}

void ToolBar::closeEvent(QCloseEvent*)
{
  Settings::Instance().SetToolBarVisible(false);
}

void ToolBar::OnDebugModeToggled(bool enabled)
{
  m_step_action->setVisible(enabled);
  m_step_over_action->setVisible(enabled);
  m_step_out_action->setVisible(enabled);
  m_skip_action->setVisible(enabled);
  m_show_pc_action->setVisible(enabled);
  m_set_pc_action->setVisible(enabled);

  bool paused = Core::GetState() == Core::State::Paused;
  m_step_action->setEnabled(paused);
  m_step_over_action->setEnabled(paused);
  m_step_out_action->setEnabled(paused);
  m_skip_action->setEnabled(paused);
  m_set_pc_action->setEnabled(paused);
}

void ToolBar::MakeActions()
{
  // i18n: Here, "Step" is a verb. This feature is used for
  // going through code step by step.
  m_step_action = addAction(tr("Step"), this, &ToolBar::StepPressed);
  // i18n: Here, "Step" is a verb. This feature is used for
  // going through code step by step.
  m_step_over_action = addAction(tr("Step Over"), this, &ToolBar::StepOverPressed);
  // i18n: Here, "Step" is a verb. This feature is used for
  // going through code step by step.
  m_step_out_action = addAction(tr("Step Out"), this, &ToolBar::StepOutPressed);
  m_skip_action = addAction(tr("Skip"), this, &ToolBar::SkipPressed);
  // i18n: Here, PC is an acronym for program counter, not personal computer.
  m_show_pc_action = addAction(tr("Show PC"), this, &ToolBar::ShowPCPressed);
  // i18n: Here, PC is an acronym for program counter, not personal computer.
  m_set_pc_action = addAction(tr("Set PC"), this, &ToolBar::SetPCPressed);

  m_open_action = addAction(tr("Open"), this, &ToolBar::OpenPressed);
  m_refresh_action = addAction(tr("Refresh"), [this] { emit RefreshPressed(); });
  m_refresh_action->setEnabled(false);

  addSeparator();

  m_pause_play_action = addAction(tr("Play"), this, &ToolBar::PlayPressed);

  m_stop_action = addAction(tr("Stop"), this, &ToolBar::StopPressed);
  m_fullscreen_action = addAction(tr("FullScr"), this, &ToolBar::FullScreenPressed);
  m_screenshot_action = addAction(tr("ScrShot"), this, &ToolBar::ScreenShotPressed);

  addSeparator();

  m_config_action = addAction(tr("Config"), this, &ToolBar::SettingsPressed);
  m_graphics_action = addAction(tr("Graphics"), this, &ToolBar::GraphicsPressed);
  m_controllers_action = addAction(tr("Controllers"), this, &ToolBar::ControllersPressed);

  // Ensure every button has about the same width
  std::vector<QWidget*> items;
  for (const auto& action :
       {m_open_action, m_pause_play_action, m_stop_action, m_stop_action, m_fullscreen_action,
        m_screenshot_action, m_config_action, m_graphics_action, m_controllers_action,
        m_step_action, m_step_over_action, m_step_out_action, m_skip_action, m_show_pc_action,
        m_set_pc_action})
  {
    items.emplace_back(widgetForAction(action));
  }

  std::vector<int> widths;
  std::transform(items.begin(), items.end(), std::back_inserter(widths),
                 [](QWidget* item) { return item->sizeHint().width(); });

  const int min_width = *std::max_element(widths.begin(), widths.end()) * 0.85;
  for (QWidget* widget : items)
    widget->setMinimumWidth(min_width);
}

void ToolBar::UpdatePausePlayButtonState(const bool playing_state)
{
  if (playing_state)
  {
    disconnect(m_pause_play_action, 0, 0, 0);
    m_pause_play_action->setText(tr("Pause"));
    m_pause_play_action->setIcon(Resources::GetScaledThemeIcon("pause"));
    connect(m_pause_play_action, &QAction::triggered, this, &ToolBar::PausePressed);
  }
  else
  {
    disconnect(m_pause_play_action, 0, 0, 0);
    m_pause_play_action->setText(tr("Play"));
    m_pause_play_action->setIcon(Resources::GetScaledThemeIcon("play"));
    connect(m_pause_play_action, &QAction::triggered, this, &ToolBar::PlayPressed);
  }
}

void ToolBar::UpdateIcons()
{
  m_step_action->setIcon(Resources::GetScaledThemeIcon("debugger_step_in"));
  m_step_over_action->setIcon(Resources::GetScaledThemeIcon("debugger_step_over"));
  m_step_out_action->setIcon(Resources::GetScaledThemeIcon("debugger_step_out"));
  m_skip_action->setIcon(Resources::GetScaledThemeIcon("debugger_skip"));
  m_show_pc_action->setIcon(Resources::GetScaledThemeIcon("debugger_show_pc"));
  m_set_pc_action->setIcon(Resources::GetScaledThemeIcon("debugger_set_pc"));

  m_open_action->setIcon(Resources::GetScaledThemeIcon("open"));
  m_refresh_action->setIcon(Resources::GetScaledThemeIcon("refresh"));

  const Core::State state = Core::GetState();
  const bool playing = state != Core::State::Uninitialized && state != Core::State::Paused;
  if (!playing)
    m_pause_play_action->setIcon(Resources::GetScaledThemeIcon("play"));
  else
    m_pause_play_action->setIcon(Resources::GetScaledThemeIcon("pause"));

  m_stop_action->setIcon(Resources::GetScaledThemeIcon("stop"));
  m_fullscreen_action->setIcon(Resources::GetScaledThemeIcon("fullscreen"));
  m_screenshot_action->setIcon(Resources::GetScaledThemeIcon("screenshot"));
  m_config_action->setIcon(Resources::GetScaledThemeIcon("config"));
  m_controllers_action->setIcon(Resources::GetScaledThemeIcon("classic"));
  m_graphics_action->setIcon(Resources::GetScaledThemeIcon("graphics"));
}