/
MainWindow.cpp
353 lines (295 loc) · 9.9 KB
/
MainWindow.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
#include <QActionGroup>
#include <QDesktopServices>
#include <QFileDialog>
#include <QMessageBox>
#include <QUrl>
#include "ui_MainWindow.h"
#include "Common/StdMakeUnique.h"
#include "Core/BootManager.h"
#include "Core/ConfigManager.h"
#include "Core/HW/ProcessorInterface.h"
#include "DolphinQt/AboutDialog.h"
#include "DolphinQt/MainWindow.h"
#include "DolphinQt/SystemInfo.h"
#include "DolphinQt/Utils/Resources.h"
#include "DolphinQt/Utils/Utils.h"
#include "VideoCommon/VideoConfig.h"
// The "g_main_window" object as defined in MainWindow.h
DMainWindow* g_main_window = nullptr;
DMainWindow::DMainWindow(QWidget* parent_widget)
: QMainWindow(parent_widget)
{
m_ui = std::make_unique<Ui::DMainWindow>();
m_ui->setupUi(this);
Resources::Init();
UpdateIcons();
setWindowIcon(Resources::GetIcon(Resources::DOLPHIN_LOGO));
// Create the GameList
m_game_tracker = new DGameTracker(this);
m_ui->centralWidget->addWidget(m_game_tracker);
m_game_tracker->ScanForGames();
m_game_tracker->SelectLastBootedGame();
// Setup the GameList style switching actions
QActionGroup* gamelistGroup = new QActionGroup(this);
gamelistGroup->addAction(m_ui->actionListView);
gamelistGroup->addAction(m_ui->actionTreeView);
gamelistGroup->addAction(m_ui->actionGridView);
gamelistGroup->addAction(m_ui->actionIconView);
// TODO: save/load this from user prefs!
m_ui->actionListView->setChecked(true);
OnGameListStyleChanged();
// Connect all the signals/slots
connect(this, SIGNAL(CoreStateChanged(Core::EState)), this, SLOT(OnCoreStateChanged(Core::EState)));
connect(m_ui->actionOpen, SIGNAL(triggered()), this, SLOT(OnOpen()));
connect(m_ui->actionBrowse, SIGNAL(triggered()), this, SLOT(OnBrowse()));
connect(m_ui->actionExit, SIGNAL(triggered()), this, SLOT(OnExit()));
connect(m_ui->actionListView, SIGNAL(triggered()), this, SLOT(OnGameListStyleChanged()));
connect(m_ui->actionTreeView, SIGNAL(triggered()), this, SLOT(OnGameListStyleChanged()));
connect(m_ui->actionGridView, SIGNAL(triggered()), this, SLOT(OnGameListStyleChanged()));
connect(m_ui->actionIconView, SIGNAL(triggered()), this, SLOT(OnGameListStyleChanged()));
connect(m_ui->actionPlay, SIGNAL(triggered()), this, SLOT(OnPlay()));
connect(m_ui->actionPlay_mnu, SIGNAL(triggered()), this, SLOT(OnPlay()));
connect(m_game_tracker, SIGNAL(StartGame()), this, SLOT(OnPlay()));
connect(m_ui->actionStop, SIGNAL(triggered()), this, SLOT(OnStop()));
connect(m_ui->actionStop_mnu, SIGNAL(triggered()), this, SLOT(OnStop()));
connect(m_ui->actionReset, SIGNAL(triggered()), this, SLOT(OnReset()));
connect(m_ui->actionWebsite, SIGNAL(triggered()), this, SLOT(OnOpenWebsite()));
connect(m_ui->actionOnlineDocs, SIGNAL(triggered()), this, SLOT(OnOpenDocs()));
connect(m_ui->actionGitHub, SIGNAL(triggered()), this, SLOT(OnOpenGitHub()));
connect(m_ui->actionSystemInfo, SIGNAL(triggered()), this, SLOT(OnOpenSystemInfo()));
connect(m_ui->actionAbout, SIGNAL(triggered()), this, SLOT(OnOpenAbout()));
// Update GUI items
emit CoreStateChanged(Core::CORE_UNINITIALIZED);
// Platform-specific stuff
#ifdef Q_OS_MACX
m_ui->toolbar->setMovable(false);
#endif
}
DMainWindow::~DMainWindow()
{
}
// Emulation
void DMainWindow::StartGame(const QString filename)
{
m_render_widget = std::make_unique<DRenderWidget>();
m_render_widget->setWindowTitle(tr("Dolphin")); // TODO
m_render_widget->setWindowIcon(windowIcon());
if (SConfig::GetInstance().m_LocalCoreStartupParameter.bFullscreen)
{
m_render_widget->setWindowFlags(m_render_widget->windowFlags() | Qt::BypassWindowManagerHint);
g_Config.bFullscreen = !g_Config.bBorderlessFullscreen;
m_render_widget->showFullScreen();
}
else
{
m_ui->centralWidget->addWidget(m_render_widget.get());
m_ui->centralWidget->setCurrentWidget(m_render_widget.get());
// TODO: When rendering to main, this won't resize the parent window...
m_render_widget->resize(SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowWidth,
SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowHeight);
}
if (!BootManager::BootCore(filename.toStdString()))
{
QMessageBox::critical(this, tr("Fatal error"), tr("Failed to init Core"), QMessageBox::Ok);
if (SConfig::GetInstance().m_LocalCoreStartupParameter.bFullscreen)
m_render_widget->close();
else
m_ui->centralWidget->removeWidget(m_render_widget.get());
m_render_widget.reset();
}
else
{
// TODO: Disable screensaver!
emit CoreStateChanged(Core::CORE_RUN);
}
}
QString DMainWindow::RequestBootFilename()
{
// If a game is already selected, just return the filename
if (m_game_tracker->SelectedGame() != nullptr)
return m_game_tracker->SelectedGame()->GetFileName();
return ShowFileDialog();
}
QString DMainWindow::ShowFileDialog()
{
return QFileDialog::getOpenFileName(this, tr("Open File"), QString(),
tr("All supported ROMs (%1);;All files (*)")
.arg(SL("*.gcm *.iso *.ciso *.gcz *.wbfs *.elf *.dol *.dff *.tmd *.wad")));
}
QString DMainWindow::ShowFolderDialog()
{
return QFileDialog::getExistingDirectory(this, tr("Browse for a directory to add"),
QDir::homePath(),
QFileDialog::ShowDirsOnly);
}
void DMainWindow::DoStartPause()
{
if (Core::GetState() == Core::CORE_RUN)
{
Core::SetState(Core::CORE_PAUSE);
emit CoreStateChanged(Core::CORE_PAUSE);
}
else
{
Core::SetState(Core::CORE_RUN);
emit CoreStateChanged(Core::CORE_RUN);
}
if (SConfig::GetInstance().m_LocalCoreStartupParameter.bHideCursor)
m_render_widget->setCursor(Qt::BlankCursor);
}
void DMainWindow::OnOpen()
{
QString filename = ShowFileDialog();
if (!filename.isNull())
StartGame(filename);
}
void DMainWindow::OnBrowse()
{
std::string path = ShowFolderDialog().toStdString();
std::vector<std::string>& iso_folder = SConfig::GetInstance().m_ISOFolder;
if (!path.empty())
{
auto itResult = std::find(iso_folder.begin(), iso_folder.end(), path);
if (itResult == iso_folder.end())
{
iso_folder.push_back(path);
SConfig::GetInstance().SaveSettings();
}
}
m_game_tracker->ScanForGames();
}
void DMainWindow::OnExit()
{
close();
if (Core::GetState() == Core::CORE_UNINITIALIZED || m_isStopping)
return;
Stop();
}
void DMainWindow::OnPlay()
{
if (Core::GetState() != Core::CORE_UNINITIALIZED)
{
DoStartPause();
}
else
{
// initialize Core and boot the game
QString filename = RequestBootFilename();
if (!filename.isNull())
StartGame(filename);
}
}
bool DMainWindow::OnStop()
{
if (Core::GetState() == Core::CORE_UNINITIALIZED || m_isStopping)
return true;
// Ask for confirmation in case the user accidentally clicked Stop / Escape
if (SConfig::GetInstance().m_LocalCoreStartupParameter.bConfirmStop)
{
// Pause emulation
Core::SetState(Core::CORE_PAUSE);
emit CoreStateChanged(Core::CORE_PAUSE);
QMessageBox::StandardButton ret = QMessageBox::question(m_render_widget.get(), tr("Please confirm..."),
tr("Do you want to stop the current emulation?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
if (ret == QMessageBox::No)
{
DoStartPause();
return false;
}
}
return Stop();
}
bool DMainWindow::Stop()
{
m_isStopping = true;
// TODO: Movie stuff
// TODO: Show the author/description dialog here
BootManager::Stop();
// TODO: Allow screensaver again
// TODO: Restore original window title
// TODO:
// If batch mode was specified on the command-line, exit now.
//if (m_bBatchMode)
// Close(true);
if (SConfig::GetInstance().m_LocalCoreStartupParameter.bFullscreen)
m_render_widget->close();
else
m_ui->centralWidget->removeWidget(m_render_widget.get());
m_render_widget.reset();
emit CoreStateChanged(Core::CORE_UNINITIALIZED);
m_isStopping = false;
return true;
}
void DMainWindow::OnReset()
{
// TODO: Movie needs to be reset here
ProcessorInterface::ResetButton_Tap();
}
void DMainWindow::OnGameListStyleChanged()
{
if (m_ui->actionListView->isChecked())
m_game_tracker->SetViewStyle(STYLE_LIST);
else if (m_ui->actionTreeView->isChecked())
m_game_tracker->SetViewStyle(STYLE_TREE);
else if (m_ui->actionGridView->isChecked())
m_game_tracker->SetViewStyle(STYLE_GRID);
else if (m_ui->actionIconView->isChecked())
m_game_tracker->SetViewStyle(STYLE_ICON);
}
void DMainWindow::OnCoreStateChanged(Core::EState state)
{
bool is_not_initialized = (state == Core::CORE_UNINITIALIZED);
bool is_running = (state == Core::CORE_RUN);
bool is_paused = (state == Core::CORE_PAUSE);
// Update the toolbar
m_ui->actionPlay->setEnabled(is_not_initialized || is_running || is_paused);
if (is_running)
{
m_ui->actionPlay->setIcon(Resources::GetIcon(Resources::TOOLBAR_PAUSE));
m_ui->actionPlay->setText(tr("Pause"));
m_ui->actionPlay_mnu->setText(tr("Pause"));
}
else if (is_paused || is_not_initialized)
{
m_ui->actionPlay->setIcon(Resources::GetIcon(Resources::TOOLBAR_PLAY));
m_ui->actionPlay->setText(tr("Play"));
m_ui->actionPlay_mnu->setText(tr("Play"));
}
m_ui->actionStop->setEnabled(!is_not_initialized);
m_ui->actionOpen->setEnabled(is_not_initialized);
m_game_tracker->setEnabled(is_not_initialized);
}
// Update all the icons used in DMainWindow with fresh ones from
// "Resources". Call this function after changing the icon theme.
void DMainWindow::UpdateIcons()
{
// Play/Pause is handled in OnCoreStateChanged().
m_ui->actionStop->setIcon(Resources::GetIcon(Resources::TOOLBAR_STOP));
}
// Help menu
void DMainWindow::OnOpenWebsite()
{
QDesktopServices::openUrl(QUrl(SL("https://dolphin-emu.org/")));
}
void DMainWindow::OnOpenDocs()
{
QDesktopServices::openUrl(QUrl(SL("https://dolphin-emu.org/docs/guides/")));
}
void DMainWindow::OnOpenGitHub()
{
QDesktopServices::openUrl(QUrl(SL("https://github.com/dolphin-emu/dolphin")));
}
void DMainWindow::OnOpenSystemInfo()
{
DSystemInfo* dlg = new DSystemInfo(this);
dlg->open();
}
void DMainWindow::OnOpenAbout()
{
DAboutDialog* dlg = new DAboutDialog(this);
dlg->open();
}