src/platform/qt/Window.cpp (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "Window.h"
7
8#include <QKeyEvent>
9#include <QKeySequence>
10#include <QMenuBar>
11#include <QMessageBox>
12#include <QMimeData>
13#include <QPainter>
14#include <QStackedLayout>
15
16#include "CheatsView.h"
17#include "ConfigController.h"
18#include "Display.h"
19#include "GameController.h"
20#include "GBAApp.h"
21#include "GBAKeyEditor.h"
22#include "GDBController.h"
23#include "GDBWindow.h"
24#include "GIFView.h"
25#include "LoadSaveState.h"
26#include "LogView.h"
27#include "MultiplayerController.h"
28#include "MemoryView.h"
29#include "OverrideView.h"
30#include "PaletteView.h"
31#include "SensorView.h"
32#include "SettingsView.h"
33#include "ShortcutController.h"
34#include "ShortcutView.h"
35#include "VideoView.h"
36
37extern "C" {
38#include "platform/commandline.h"
39}
40
41using namespace QGBA;
42
43#ifdef __WIN32
44// This is a macro everywhere except MinGW, it seems
45using std::isnan;
46#endif
47
48Window::Window(ConfigController* config, int playerId, QWidget* parent)
49 : QMainWindow(parent)
50 , m_logView(new LogView())
51 , m_stateWindow(nullptr)
52 , m_screenWidget(new WindowBackground())
53 , m_logo(":/res/mgba-1024.png")
54 , m_config(config)
55 , m_inputController(playerId)
56#ifdef USE_FFMPEG
57 , m_videoView(nullptr)
58#endif
59#ifdef USE_MAGICK
60 , m_gifView(nullptr)
61#endif
62#ifdef USE_GDB_STUB
63 , m_gdbController(nullptr)
64#endif
65 , m_mruMenu(nullptr)
66 , m_shortcutController(new ShortcutController(this))
67 , m_playerId(playerId)
68{
69 setFocusPolicy(Qt::StrongFocus);
70 setAcceptDrops(true);
71 setAttribute(Qt::WA_DeleteOnClose);
72 m_controller = new GameController(this);
73 m_controller->setInputController(&m_inputController);
74 m_controller->setOverrides(m_config->overrides());
75 updateTitle();
76
77 m_display = Display::create(this);
78
79 m_logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
80 m_logo = m_logo; // Free memory left over in old pixmap
81
82 m_screenWidget->setMinimumSize(m_display->minimumSize());
83 m_screenWidget->setSizePolicy(m_display->sizePolicy());
84 m_screenWidget->setSizeHint(m_display->minimumSize() * 2);
85 m_screenWidget->setPixmap(m_logo);
86 m_screenWidget->setLockAspectRatio(m_logo.width(), m_logo.height());
87 setCentralWidget(m_screenWidget);
88
89 QVariant windowPos = m_config->getQtOption("windowPos");
90 if (!windowPos.isNull()) {
91 move(windowPos.toPoint());
92 }
93
94 connect(m_controller, SIGNAL(gameStarted(GBAThread*)), this, SLOT(gameStarted(GBAThread*)));
95 connect(m_controller, SIGNAL(gameStarted(GBAThread*)), &m_inputController, SLOT(suspendScreensaver()));
96 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_display, SLOT(stopDrawing()));
97 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(gameStopped()));
98 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), &m_inputController, SLOT(resumeScreensaver()));
99 connect(m_controller, SIGNAL(stateLoaded(GBAThread*)), m_display, SLOT(forceDraw()));
100 connect(m_controller, SIGNAL(rewound(GBAThread*)), m_display, SLOT(forceDraw()));
101 connect(m_controller, SIGNAL(gamePaused(GBAThread*)), m_display, SLOT(pauseDrawing()));
102#ifndef Q_OS_MAC
103 connect(m_controller, SIGNAL(gamePaused(GBAThread*)), menuBar(), SLOT(show()));
104 connect(m_controller, &GameController::gameUnpaused, [this]() {
105 if(isFullScreen()) {
106 menuBar()->hide();
107 }
108 });
109#endif
110 connect(m_controller, SIGNAL(gamePaused(GBAThread*)), &m_inputController, SLOT(resumeScreensaver()));
111 connect(m_controller, SIGNAL(gameUnpaused(GBAThread*)), m_display, SLOT(unpauseDrawing()));
112 connect(m_controller, SIGNAL(gameUnpaused(GBAThread*)), &m_inputController, SLOT(suspendScreensaver()));
113 connect(m_controller, SIGNAL(postLog(int, const QString&)), m_logView, SLOT(postLog(int, const QString&)));
114 connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(recordFrame()));
115 connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), m_display, SLOT(framePosted(const uint32_t*)));
116 connect(m_controller, SIGNAL(gameCrashed(const QString&)), this, SLOT(gameCrashed(const QString&)));
117 connect(m_controller, SIGNAL(gameFailed()), this, SLOT(gameFailed()));
118 connect(m_controller, SIGNAL(unimplementedBiosCall(int)), this, SLOT(unimplementedBiosCall(int)));
119 connect(m_controller, SIGNAL(statusPosted(const QString&)), m_display, SLOT(showMessage(const QString&)));
120 connect(m_logView, SIGNAL(levelsSet(int)), m_controller, SLOT(setLogLevel(int)));
121 connect(m_logView, SIGNAL(levelsEnabled(int)), m_controller, SLOT(enableLogLevel(int)));
122 connect(m_logView, SIGNAL(levelsDisabled(int)), m_controller, SLOT(disableLogLevel(int)));
123 connect(this, SIGNAL(startDrawing(GBAThread*)), m_display, SLOT(startDrawing(GBAThread*)), Qt::QueuedConnection);
124 connect(this, SIGNAL(shutdown()), m_display, SLOT(stopDrawing()));
125 connect(this, SIGNAL(shutdown()), m_controller, SLOT(closeGame()));
126 connect(this, SIGNAL(shutdown()), m_logView, SLOT(hide()));
127 connect(this, SIGNAL(audioBufferSamplesChanged(int)), m_controller, SLOT(setAudioBufferSamples(int)));
128 connect(this, SIGNAL(fpsTargetChanged(float)), m_controller, SLOT(setFPSTarget(float)));
129 connect(&m_fpsTimer, SIGNAL(timeout()), this, SLOT(showFPS()));
130
131 m_logView->setLevels(GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL | GBA_LOG_STATUS);
132 m_fpsTimer.setInterval(FPS_TIMER_INTERVAL);
133
134 m_shortcutController->setConfigController(m_config);
135 setupMenu(menuBar());
136}
137
138Window::~Window() {
139 delete m_logView;
140
141#ifdef USE_FFMPEG
142 delete m_videoView;
143#endif
144
145#ifdef USE_MAGICK
146 delete m_gifView;
147#endif
148}
149
150void Window::argumentsPassed(GBAArguments* args) {
151 loadConfig();
152
153 if (args->patch) {
154 m_controller->loadPatch(args->patch);
155 }
156
157 if (args->fname) {
158 m_controller->loadGame(args->fname, args->dirmode);
159 }
160}
161
162void Window::resizeFrame(int width, int height) {
163 QSize newSize(width, height);
164 newSize -= m_screenWidget->size();
165 newSize += size();
166 resize(newSize);
167}
168
169void Window::setConfig(ConfigController* config) {
170 m_config = config;
171}
172
173void Window::loadConfig() {
174 const GBAOptions* opts = m_config->options();
175
176 m_logView->setLevels(opts->logLevel);
177
178 m_controller->setOptions(opts);
179 m_display->lockAspectRatio(opts->lockAspectRatio);
180 m_display->filter(opts->resampleVideo);
181
182 if (opts->bios) {
183 m_controller->loadBIOS(opts->bios);
184 }
185
186 if (opts->fpsTarget) {
187 emit fpsTargetChanged(opts->fpsTarget);
188 }
189
190 if (opts->audioBuffers) {
191 emit audioBufferSamplesChanged(opts->audioBuffers);
192 }
193
194 if (opts->width && opts->height) {
195 resizeFrame(opts->width, opts->height);
196 }
197
198 if (opts->fullscreen) {
199 enterFullScreen();
200 }
201
202 m_inputController.setScreensaverSuspendable(opts->suspendScreensaver);
203
204 m_mruFiles = m_config->getMRU();
205 updateMRU();
206
207 m_inputController.setConfiguration(m_config);
208}
209
210void Window::saveConfig() {
211 m_inputController.saveConfiguration();
212 m_config->write();
213}
214
215void Window::selectROM() {
216 QStringList formats{
217 "*.gba",
218#ifdef USE_LIBZIP
219 "*.zip",
220#endif
221#ifdef USE_LZMA
222 "*.7z",
223#endif
224 "*.rom",
225 "*.bin"};
226 QString filter = tr("Game Boy Advance ROMs (%1)").arg(formats.join(QChar(' ')));
227 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select ROM"), filter);
228 if (!filename.isEmpty()) {
229 m_controller->loadGame(filename);
230 }
231}
232
233void Window::selectBIOS() {
234 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select BIOS"));
235 if (!filename.isEmpty()) {
236 m_config->setOption("bios", filename);
237 m_config->updateOption("bios");
238 m_config->setOption("useBios", true);
239 m_config->updateOption("useBios");
240 m_controller->loadBIOS(filename);
241 }
242}
243
244void Window::selectPatch() {
245 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select patch"), tr("Patches (*.ips *.ups *.bps)"));
246 if (!filename.isEmpty()) {
247 m_controller->loadPatch(filename);
248 }
249}
250
251void Window::openView(QWidget* widget) {
252 connect(this, SIGNAL(shutdown()), widget, SLOT(close()));
253 widget->setAttribute(Qt::WA_DeleteOnClose);
254 widget->show();
255}
256
257void Window::importSharkport() {
258 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select save"), tr("GameShark saves (*.sps *.xps)"));
259 if (!filename.isEmpty()) {
260 m_controller->importSharkport(filename);
261 }
262}
263
264void Window::exportSharkport() {
265 QString filename = GBAApp::app()->getSaveFileName(this, tr("Select save"), tr("GameShark saves (*.sps *.xps)"));
266 if (!filename.isEmpty()) {
267 m_controller->exportSharkport(filename);
268 }
269}
270
271void Window::openKeymapWindow() {
272 GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, InputController::KEYBOARD);
273 openView(keyEditor);
274}
275
276void Window::openSettingsWindow() {
277 SettingsView* settingsWindow = new SettingsView(m_config);
278 connect(settingsWindow, SIGNAL(biosLoaded(const QString&)), m_controller, SLOT(loadBIOS(const QString&)));
279 connect(settingsWindow, SIGNAL(audioDriverChanged()), m_controller, SLOT(reloadAudioDriver()));
280 openView(settingsWindow);
281}
282
283void Window::openShortcutWindow() {
284#ifdef BUILD_SDL
285 m_inputController.recalibrateAxes();
286#endif
287 ShortcutView* shortcutView = new ShortcutView();
288 shortcutView->setController(m_shortcutController);
289 openView(shortcutView);
290}
291
292void Window::openOverrideWindow() {
293 OverrideView* overrideWindow = new OverrideView(m_controller, m_config);
294 openView(overrideWindow);
295}
296
297void Window::openSensorWindow() {
298 SensorView* sensorWindow = new SensorView(m_controller, &m_inputController);
299 openView(sensorWindow);
300}
301
302void Window::openCheatsWindow() {
303 CheatsView* cheatsWindow = new CheatsView(m_controller);
304 openView(cheatsWindow);
305}
306
307void Window::openPaletteWindow() {
308 PaletteView* paletteWindow = new PaletteView(m_controller);
309 openView(paletteWindow);
310}
311
312void Window::openMemoryWindow() {
313 MemoryView* memoryWindow = new MemoryView(m_controller);
314 openView(memoryWindow);
315}
316
317#ifdef BUILD_SDL
318void Window::openGamepadWindow() {
319 const char* profile = m_inputController.profileForType(SDL_BINDING_BUTTON);
320 GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, SDL_BINDING_BUTTON, profile);
321 openView(keyEditor);
322}
323#endif
324
325#ifdef USE_FFMPEG
326void Window::openVideoWindow() {
327 if (!m_videoView) {
328 m_videoView = new VideoView();
329 connect(m_videoView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
330 connect(m_videoView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
331 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(stopRecording()));
332 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(close()));
333 connect(this, SIGNAL(shutdown()), m_videoView, SLOT(close()));
334 }
335 m_videoView->show();
336}
337#endif
338
339#ifdef USE_MAGICK
340void Window::openGIFWindow() {
341 if (!m_gifView) {
342 m_gifView = new GIFView();
343 connect(m_gifView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
344 connect(m_gifView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
345 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(stopRecording()));
346 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(close()));
347 connect(this, SIGNAL(shutdown()), m_gifView, SLOT(close()));
348 }
349 m_gifView->show();
350}
351#endif
352
353#ifdef USE_GDB_STUB
354void Window::gdbOpen() {
355 if (!m_gdbController) {
356 m_gdbController = new GDBController(m_controller, this);
357 }
358 GDBWindow* window = new GDBWindow(m_gdbController);
359 connect(this, SIGNAL(shutdown()), window, SLOT(close()));
360 window->setAttribute(Qt::WA_DeleteOnClose);
361 window->show();
362}
363#endif
364
365void Window::keyPressEvent(QKeyEvent* event) {
366 if (event->isAutoRepeat()) {
367 QWidget::keyPressEvent(event);
368 return;
369 }
370 GBAKey key = m_inputController.mapKeyboard(event->key());
371 if (key == GBA_KEY_NONE) {
372 QWidget::keyPressEvent(event);
373 return;
374 }
375 m_controller->keyPressed(key);
376 event->accept();
377}
378
379void Window::keyReleaseEvent(QKeyEvent* event) {
380 if (event->isAutoRepeat()) {
381 QWidget::keyReleaseEvent(event);
382 return;
383 }
384 GBAKey key = m_inputController.mapKeyboard(event->key());
385 if (key == GBA_KEY_NONE) {
386 QWidget::keyPressEvent(event);
387 return;
388 }
389 m_controller->keyReleased(key);
390 event->accept();
391}
392
393void Window::resizeEvent(QResizeEvent*) {
394 if (!isFullScreen()) {
395 m_config->setOption("height", m_screenWidget->height());
396 m_config->setOption("width", m_screenWidget->width());
397 }
398 m_config->setOption("fullscreen", isFullScreen());
399}
400
401void Window::closeEvent(QCloseEvent* event) {
402 emit shutdown();
403 m_config->setQtOption("windowPos", pos());
404 saveConfig();
405 QMainWindow::closeEvent(event);
406}
407
408void Window::focusOutEvent(QFocusEvent*) {
409 m_controller->setTurbo(false, false);
410 m_controller->stopRewinding();
411 m_controller->clearKeys();
412}
413
414void Window::dragEnterEvent(QDragEnterEvent* event) {
415 if (event->mimeData()->hasFormat("text/uri-list")) {
416 event->acceptProposedAction();
417 }
418}
419
420void Window::dropEvent(QDropEvent* event) {
421 QString uris = event->mimeData()->data("text/uri-list");
422 uris = uris.trimmed();
423 if (uris.contains("\n")) {
424 // Only one file please
425 return;
426 }
427 QUrl url(uris);
428 if (!url.isLocalFile()) {
429 // No remote loading
430 return;
431 }
432 event->accept();
433 m_controller->loadGame(url.path());
434}
435
436void Window::mouseDoubleClickEvent(QMouseEvent* event) {
437 if (event->button() != Qt::LeftButton) {
438 return;
439 }
440 toggleFullScreen();
441}
442
443void Window::enterFullScreen() {
444 if (isFullScreen()) {
445 return;
446 }
447 showFullScreen();
448 setCursor(Qt::BlankCursor);
449#ifndef Q_OS_MAC
450 if (m_controller->isLoaded() && !m_controller->isPaused()) {
451 menuBar()->hide();
452 }
453#endif
454}
455
456void Window::exitFullScreen() {
457 if (!isFullScreen()) {
458 return;
459 }
460 unsetCursor();
461 showNormal();
462 menuBar()->show();
463}
464
465void Window::toggleFullScreen() {
466 if (isFullScreen()) {
467 exitFullScreen();
468 } else {
469 enterFullScreen();
470 }
471}
472
473void Window::gameStarted(GBAThread* context) {
474 char title[13] = { '\0' };
475 MutexLock(&context->stateMutex);
476 if (context->state < THREAD_EXITING) {
477 emit startDrawing(context);
478 GBAGetGameTitle(context->gba, title);
479 } else {
480 MutexUnlock(&context->stateMutex);
481 return;
482 }
483 MutexUnlock(&context->stateMutex);
484 foreach (QAction* action, m_gameActions) {
485 action->setDisabled(false);
486 }
487 if (context->fname) {
488 appendMRU(context->fname);
489 }
490 updateTitle();
491 attachWidget(m_display);
492
493#ifndef Q_OS_MAC
494 if(isFullScreen()) {
495 menuBar()->hide();
496 }
497#endif
498
499 m_hitUnimplementedBiosCall = false;
500 m_fpsTimer.start();
501}
502
503void Window::gameStopped() {
504 foreach (QAction* action, m_gameActions) {
505 action->setDisabled(true);
506 }
507 updateTitle();
508 detachWidget(m_display);
509 m_screenWidget->setLockAspectRatio(m_logo.width(), m_logo.height());
510 m_screenWidget->setPixmap(m_logo);
511
512 m_fpsTimer.stop();
513}
514
515void Window::gameCrashed(const QString& errorMessage) {
516 QMessageBox* crash = new QMessageBox(QMessageBox::Critical, tr("Crash"),
517 tr("The game has crashed with the following error:\n\n%1").arg(errorMessage),
518 QMessageBox::Ok, this, Qt::Sheet);
519 crash->setAttribute(Qt::WA_DeleteOnClose);
520 crash->show();
521}
522
523void Window::gameFailed() {
524 QMessageBox* fail = new QMessageBox(QMessageBox::Warning, tr("Couldn't Load"),
525 tr("Could not load game. Are you sure it's in the correct format?"),
526 QMessageBox::Ok, this, Qt::Sheet);
527 fail->setAttribute(Qt::WA_DeleteOnClose);
528 fail->show();
529}
530
531void Window::unimplementedBiosCall(int call) {
532 if (m_hitUnimplementedBiosCall) {
533 return;
534 }
535 m_hitUnimplementedBiosCall = true;
536
537 QMessageBox* fail = new QMessageBox(QMessageBox::Warning, tr("Unimplemented BIOS call"),
538 tr("This game uses a BIOS call that is not implemented. Please use the official BIOS for best experience."),
539 QMessageBox::Ok, this, Qt::Sheet);
540 fail->setAttribute(Qt::WA_DeleteOnClose);
541 fail->show();
542}
543
544void Window::recordFrame() {
545 m_frameList.append(QDateTime::currentDateTime());
546 while (m_frameList.count() > FRAME_LIST_SIZE) {
547 m_frameList.removeFirst();
548 }
549}
550
551void Window::showFPS() {
552 if (m_frameList.isEmpty()) {
553 updateTitle();
554 return;
555 }
556 qint64 interval = m_frameList.first().msecsTo(m_frameList.last());
557 float fps = (m_frameList.count() - 1) * 10000.f / interval;
558 fps = round(fps) / 10.f;
559 updateTitle(fps);
560}
561
562void Window::updateTitle(float fps) {
563 QString title;
564
565 m_controller->threadInterrupt();
566 if (m_controller->isLoaded()) {
567 char gameTitle[13] = { '\0' };
568 GBAGetGameTitle(m_controller->thread()->gba, gameTitle);
569
570 title = (gameTitle);
571 }
572 MultiplayerController* multiplayer = m_controller->multiplayerController();
573 if (multiplayer && multiplayer->attached() > 1) {
574 title += tr(" - Player %1 of %2").arg(m_playerId + 1).arg(multiplayer->attached());
575 }
576 m_controller->threadContinue();
577 if (title.isNull()) {
578 setWindowTitle(tr("%1 - %2").arg(projectName).arg(projectVersion));
579 } else if (isnan(fps)) {
580 setWindowTitle(tr("%1 - %2 - %3").arg(projectName).arg(title).arg(projectVersion));
581 } else {
582 setWindowTitle(tr("%1 - %2 (%3 fps) - %4").arg(projectName).arg(title).arg(fps).arg(projectVersion));
583 }
584}
585
586void Window::openStateWindow(LoadSave ls) {
587 if (m_stateWindow) {
588 return;
589 }
590 bool wasPaused = m_controller->isPaused();
591 m_stateWindow = new LoadSaveState(m_controller);
592 connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
593 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
594 connect(m_stateWindow, &LoadSaveState::closed, [this]() {
595 m_screenWidget->layout()->removeWidget(m_stateWindow);
596 m_stateWindow = nullptr;
597 QMetaObject::invokeMethod(this, "setFocus", Qt::QueuedConnection);
598 });
599 if (!wasPaused) {
600 m_controller->setPaused(true);
601 connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
602 }
603 m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
604 m_stateWindow->setMode(ls);
605 attachWidget(m_stateWindow);
606}
607
608void Window::setupMenu(QMenuBar* menubar) {
609 menubar->clear();
610 QMenu* fileMenu = menubar->addMenu(tr("&File"));
611 m_shortcutController->addMenu(fileMenu);
612 installEventFilter(m_shortcutController);
613 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open), "loadROM");
614 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS())), "loadBIOS");
615 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch())), "loadPatch");
616 addControlledAction(fileMenu, fileMenu->addAction(tr("Boot BIOS"), m_controller, SLOT(bootBIOS())), "bootBIOS");
617
618 m_mruMenu = fileMenu->addMenu(tr("Recent"));
619
620 fileMenu->addSeparator();
621
622 QAction* loadState = new QAction(tr("&Load state"), fileMenu);
623 loadState->setShortcut(tr("F10"));
624 connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
625 m_gameActions.append(loadState);
626 addControlledAction(fileMenu, loadState, "loadState");
627
628 QAction* saveState = new QAction(tr("&Save state"), fileMenu);
629 saveState->setShortcut(tr("Shift+F10"));
630 connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
631 m_gameActions.append(saveState);
632 addControlledAction(fileMenu, saveState, "saveState");
633
634 QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
635 QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
636 m_shortcutController->addMenu(quickLoadMenu);
637 m_shortcutController->addMenu(quickSaveMenu);
638
639 QAction* quickLoad = new QAction(tr("Load recent"), quickLoadMenu);
640 connect(quickLoad, SIGNAL(triggered()), m_controller, SLOT(loadState()));
641 m_gameActions.append(quickLoad);
642 addControlledAction(quickLoadMenu, quickLoad, "quickLoad");
643
644 QAction* quickSave = new QAction(tr("Save recent"), quickSaveMenu);
645 connect(quickSave, SIGNAL(triggered()), m_controller, SLOT(saveState()));
646 m_gameActions.append(quickSave);
647 addControlledAction(quickSaveMenu, quickSave, "quickSave");
648
649 quickLoadMenu->addSeparator();
650 quickSaveMenu->addSeparator();
651
652 int i;
653 for (i = 1; i < 10; ++i) {
654 quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
655 quickLoad->setShortcut(tr("F%1").arg(i));
656 connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
657 m_gameActions.append(quickLoad);
658 addControlledAction(quickLoadMenu, quickLoad, QString("quickLoad.%1").arg(i));
659
660 quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
661 quickSave->setShortcut(tr("Shift+F%1").arg(i));
662 connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
663 m_gameActions.append(quickSave);
664 addControlledAction(quickSaveMenu, quickSave, QString("quickSave.%1").arg(i));
665 }
666
667 fileMenu->addSeparator();
668 QAction* importShark = new QAction(tr("Import GameShark Save"), fileMenu);
669 connect(importShark, SIGNAL(triggered()), this, SLOT(importSharkport()));
670 m_gameActions.append(importShark);
671 addControlledAction(fileMenu, importShark, "importShark");
672
673 QAction* exportShark = new QAction(tr("Export GameShark Save"), fileMenu);
674 connect(exportShark, SIGNAL(triggered()), this, SLOT(exportSharkport()));
675 m_gameActions.append(exportShark);
676 addControlledAction(fileMenu, exportShark, "exportShark");
677
678 fileMenu->addSeparator();
679 QAction* multiWindow = new QAction(tr("New multiplayer window"), fileMenu);
680 connect(multiWindow, &QAction::triggered, [this]() {
681 GBAApp::app()->newWindow();
682 });
683 addControlledAction(fileMenu, multiWindow, "multiWindow");
684
685#ifndef Q_OS_MAC
686 addControlledAction(fileMenu, fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit), "quit");
687#endif
688
689 QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
690 m_shortcutController->addMenu(emulationMenu);
691 QAction* reset = new QAction(tr("&Reset"), emulationMenu);
692 reset->setShortcut(tr("Ctrl+R"));
693 connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
694 m_gameActions.append(reset);
695 addControlledAction(emulationMenu, reset, "reset");
696
697 QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
698 connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
699 m_gameActions.append(shutdown);
700 addControlledAction(emulationMenu, shutdown, "shutdown");
701
702 QAction* yank = new QAction(tr("Yank game pak"), emulationMenu);
703 connect(yank, SIGNAL(triggered()), m_controller, SLOT(yankPak()));
704 m_gameActions.append(yank);
705 addControlledAction(emulationMenu, yank, "yank");
706 emulationMenu->addSeparator();
707
708 QAction* pause = new QAction(tr("&Pause"), emulationMenu);
709 pause->setChecked(false);
710 pause->setCheckable(true);
711 pause->setShortcut(tr("Ctrl+P"));
712 connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
713 connect(m_controller, &GameController::gamePaused, [this, pause]() {
714 pause->setChecked(true);
715
716 QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
717 QPixmap pixmap;
718 pixmap.convertFromImage(currentImage.rgbSwapped());
719 m_screenWidget->setPixmap(pixmap);
720 m_screenWidget->setLockAspectRatio(3, 2);
721 });
722 connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
723 m_gameActions.append(pause);
724 addControlledAction(emulationMenu, pause, "pause");
725
726 QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
727 frameAdvance->setShortcut(tr("Ctrl+N"));
728 connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
729 m_gameActions.append(frameAdvance);
730 addControlledAction(emulationMenu, frameAdvance, "frameAdvance");
731
732 emulationMenu->addSeparator();
733
734 m_shortcutController->addFunctions(emulationMenu, [this]() {
735 m_controller->setTurbo(true, false);
736 }, [this]() {
737 m_controller->setTurbo(false, false);
738 }, QKeySequence(Qt::Key_Tab), tr("Fast forward (held)"), "holdFastForward");
739
740 QAction* turbo = new QAction(tr("&Fast forward"), emulationMenu);
741 turbo->setCheckable(true);
742 turbo->setChecked(false);
743 turbo->setShortcut(tr("Shift+Tab"));
744 connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
745 addControlledAction(emulationMenu, turbo, "fastForward");
746
747 QMenu* ffspeedMenu = emulationMenu->addMenu(tr("Fast forward speed"));
748 ConfigOption* ffspeed = m_config->addOption("fastForwardRatio");
749 ffspeed->connect([this](const QVariant& value) {
750 m_controller->setTurboSpeed(value.toFloat());
751 }, this);
752 ffspeed->addValue(tr("Unbounded"), -1.0f, ffspeedMenu);
753 ffspeed->setValue(QVariant(-1.0f));
754 ffspeedMenu->addSeparator();
755 for (i = 2; i < 11; ++i) {
756 ffspeed->addValue(tr("%0x").arg(i), i, ffspeedMenu);
757 }
758 m_config->updateOption("fastForwardRatio");
759
760 m_shortcutController->addFunctions(emulationMenu, [this]() {
761 m_controller->startRewinding();
762 }, [this]() {
763 m_controller->stopRewinding();
764 }, QKeySequence("~"), tr("Rewind (held)"), "holdRewind");
765
766 QAction* rewind = new QAction(tr("Re&wind"), emulationMenu);
767 rewind->setShortcut(tr("`"));
768 connect(rewind, SIGNAL(triggered()), m_controller, SLOT(rewind()));
769 m_gameActions.append(rewind);
770 addControlledAction(emulationMenu, rewind, "rewind");
771
772 QAction* frameRewind = new QAction(tr("Step backwards"), emulationMenu);
773 frameRewind->setShortcut(tr("Ctrl+B"));
774 connect(frameRewind, &QAction::triggered, [this] () {
775 m_controller->rewind(1);
776 });
777 m_gameActions.append(frameRewind);
778 addControlledAction(emulationMenu, frameRewind, "frameRewind");
779
780 ConfigOption* videoSync = m_config->addOption("videoSync");
781 videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
782 videoSync->connect([this](const QVariant& value) {
783 m_controller->setVideoSync(value.toBool());
784 }, this);
785 m_config->updateOption("videoSync");
786
787 ConfigOption* audioSync = m_config->addOption("audioSync");
788 audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
789 audioSync->connect([this](const QVariant& value) {
790 m_controller->setAudioSync(value.toBool());
791 }, this);
792 m_config->updateOption("audioSync");
793
794 emulationMenu->addSeparator();
795
796 QMenu* solarMenu = emulationMenu->addMenu(tr("Solar sensor"));
797 m_shortcutController->addMenu(solarMenu);
798 QAction* solarIncrease = new QAction(tr("Increase solar level"), solarMenu);
799 connect(solarIncrease, SIGNAL(triggered()), m_controller, SLOT(increaseLuminanceLevel()));
800 addControlledAction(solarMenu, solarIncrease, "increaseLuminanceLevel");
801
802 QAction* solarDecrease = new QAction(tr("Decrease solar level"), solarMenu);
803 connect(solarDecrease, SIGNAL(triggered()), m_controller, SLOT(decreaseLuminanceLevel()));
804 addControlledAction(solarMenu, solarDecrease, "decreaseLuminanceLevel");
805
806 QAction* maxSolar = new QAction(tr("Brightest solar level"), solarMenu);
807 connect(maxSolar, &QAction::triggered, [this]() { m_controller->setLuminanceLevel(10); });
808 addControlledAction(solarMenu, maxSolar, "maxLuminanceLevel");
809
810 QAction* minSolar = new QAction(tr("Darkest solar level"), solarMenu);
811 connect(minSolar, &QAction::triggered, [this]() { m_controller->setLuminanceLevel(0); });
812 addControlledAction(solarMenu, minSolar, "minLuminanceLevel");
813
814 solarMenu->addSeparator();
815 for (int i = 0; i <= 10; ++i) {
816 QAction* setSolar = new QAction(tr("Brightness %1").arg(QString::number(i)), solarMenu);
817 connect(setSolar, &QAction::triggered, [this, i]() {
818 m_controller->setLuminanceLevel(i);
819 });
820 addControlledAction(solarMenu, setSolar, QString("luminanceLevel.%1").arg(QString::number(i)));
821 }
822
823 QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
824 m_shortcutController->addMenu(avMenu);
825 QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
826 m_shortcutController->addMenu(frameMenu, avMenu);
827 for (int i = 1; i <= 6; ++i) {
828 QAction* setSize = new QAction(tr("%1x").arg(QString::number(i)), avMenu);
829 connect(setSize, &QAction::triggered, [this, i]() {
830 showNormal();
831 resizeFrame(VIDEO_HORIZONTAL_PIXELS * i, VIDEO_VERTICAL_PIXELS * i);
832 });
833 addControlledAction(frameMenu, setSize, QString("frame%1x").arg(QString::number(i)));
834 }
835 QKeySequence fullscreenKeys;
836#ifdef Q_OS_WIN
837 fullscreenKeys = QKeySequence("Alt+Enter");
838#else
839 fullscreenKeys = QKeySequence("Ctrl+F");
840#endif
841 addControlledAction(frameMenu, frameMenu->addAction(tr("Toggle fullscreen"), this, SLOT(toggleFullScreen()), fullscreenKeys), "fullscreen");
842
843 ConfigOption* lockAspectRatio = m_config->addOption("lockAspectRatio");
844 lockAspectRatio->addBoolean(tr("Lock aspect ratio"), avMenu);
845 lockAspectRatio->connect([this](const QVariant& value) {
846 m_display->lockAspectRatio(value.toBool());
847 }, this);
848 m_config->updateOption("lockAspectRatio");
849
850 ConfigOption* resampleVideo = m_config->addOption("resampleVideo");
851 resampleVideo->addBoolean(tr("Resample video"), avMenu);
852 resampleVideo->connect([this](const QVariant& value) {
853 m_display->filter(value.toBool());
854 }, this);
855 m_config->updateOption("resampleVideo");
856
857 QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
858 ConfigOption* skip = m_config->addOption("frameskip");
859 skip->connect([this](const QVariant& value) {
860 m_controller->setFrameskip(value.toInt());
861 }, this);
862 for (int i = 0; i <= 10; ++i) {
863 skip->addValue(QString::number(i), i, skipMenu);
864 }
865 m_config->updateOption("frameskip");
866
867 avMenu->addSeparator();
868
869 QMenu* buffersMenu = avMenu->addMenu(tr("Audio buffer &size"));
870 ConfigOption* buffers = m_config->addOption("audioBuffers");
871 buffers->connect([this](const QVariant& value) {
872 emit audioBufferSamplesChanged(value.toInt());
873 }, this);
874 buffers->addValue(tr("512"), 512, buffersMenu);
875 buffers->addValue(tr("768"), 768, buffersMenu);
876 buffers->addValue(tr("1024"), 1024, buffersMenu);
877 buffers->addValue(tr("2048"), 2048, buffersMenu);
878 buffers->addValue(tr("4096"), 4096, buffersMenu);
879 m_config->updateOption("audioBuffers");
880
881 avMenu->addSeparator();
882
883 QMenu* target = avMenu->addMenu(tr("FPS target"));
884 ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
885 fpsTargetOption->connect([this](const QVariant& value) {
886 emit fpsTargetChanged(value.toFloat());
887 }, this);
888 fpsTargetOption->addValue(tr("15"), 15, target);
889 fpsTargetOption->addValue(tr("30"), 30, target);
890 fpsTargetOption->addValue(tr("45"), 45, target);
891 fpsTargetOption->addValue(tr("Native (59.7)"), float(GBA_ARM7TDMI_FREQUENCY) / float(VIDEO_TOTAL_LENGTH), target);
892 fpsTargetOption->addValue(tr("60"), 60, target);
893 fpsTargetOption->addValue(tr("90"), 90, target);
894 fpsTargetOption->addValue(tr("120"), 120, target);
895 fpsTargetOption->addValue(tr("240"), 240, target);
896 m_config->updateOption("fpsTarget");
897
898#if defined(USE_PNG) || defined(USE_FFMPEG) || defined(USE_MAGICK)
899 avMenu->addSeparator();
900#endif
901
902#ifdef USE_PNG
903 QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
904 screenshot->setShortcut(tr("F12"));
905 connect(screenshot, SIGNAL(triggered()), m_controller, SLOT(screenshot()));
906 m_gameActions.append(screenshot);
907 addControlledAction(avMenu, screenshot, "screenshot");
908#endif
909
910#ifdef USE_FFMPEG
911 QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
912 recordOutput->setShortcut(tr("F11"));
913 connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
914 addControlledAction(avMenu, recordOutput, "recordOutput");
915#endif
916
917#ifdef USE_MAGICK
918 QAction* recordGIF = new QAction(tr("Record GIF..."), avMenu);
919 recordGIF->setShortcut(tr("Shift+F11"));
920 connect(recordGIF, SIGNAL(triggered()), this, SLOT(openGIFWindow()));
921 addControlledAction(avMenu, recordGIF, "recordGIF");
922#endif
923
924 avMenu->addSeparator();
925 QMenu* videoLayers = avMenu->addMenu(tr("Video layers"));
926
927 for (int i = 0; i < 4; ++i) {
928 QAction* enableBg = new QAction(tr("Background %0").arg(i), videoLayers);
929 enableBg->setCheckable(true);
930 enableBg->setChecked(true);
931 connect(enableBg, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->video.renderer->disableBG[i] = !enable; });
932 m_gameActions.append(enableBg);
933 addControlledAction(videoLayers, enableBg, QString("enableBG%0").arg(i));
934 }
935
936 QAction* enableObj = new QAction(tr("OBJ (sprites)"), videoLayers);
937 enableObj->setCheckable(true);
938 enableObj->setChecked(true);
939 connect(enableObj, &QAction::triggered, [this](bool enable) { m_controller->thread()->gba->video.renderer->disableOBJ = !enable; });
940 m_gameActions.append(enableObj);
941 addControlledAction(videoLayers, enableObj, "enableOBJ");
942
943 QMenu* audioChannels = avMenu->addMenu(tr("Audio channels"));
944
945 for (int i = 0; i < 4; ++i) {
946 QAction* enableCh = new QAction(tr("Channel %0").arg(i + 1), audioChannels);
947 enableCh->setCheckable(true);
948 enableCh->setChecked(true);
949 connect(enableCh, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->audio.forceDisableCh[i] = !enable; });
950 m_gameActions.append(enableCh);
951 addControlledAction(audioChannels, enableCh, QString("enableCh%0").arg(i + 1));
952 }
953
954 QAction* enableChA = new QAction(tr("Channel A"), audioChannels);
955 enableChA->setCheckable(true);
956 enableChA->setChecked(true);
957 connect(enableChA, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->audio.forceDisableChA = !enable; });
958 m_gameActions.append(enableChA);
959 addControlledAction(audioChannels, enableChA, QString("enableChA"));
960
961 QAction* enableChB = new QAction(tr("Channel B"), audioChannels);
962 enableChB->setCheckable(true);
963 enableChB->setChecked(true);
964 connect(enableChB, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->audio.forceDisableChB = !enable; });
965 m_gameActions.append(enableChB);
966 addControlledAction(audioChannels, enableChB, QString("enableChB"));
967
968 QMenu* toolsMenu = menubar->addMenu(tr("&Tools"));
969 m_shortcutController->addMenu(toolsMenu);
970 QAction* viewLogs = new QAction(tr("View &logs..."), toolsMenu);
971 connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
972 addControlledAction(toolsMenu, viewLogs, "viewLogs");
973
974 QAction* overrides = new QAction(tr("Game &overrides..."), toolsMenu);
975 connect(overrides, SIGNAL(triggered()), this, SLOT(openOverrideWindow()));
976 addControlledAction(toolsMenu, overrides, "overrideWindow");
977
978 QAction* sensors = new QAction(tr("Game &Pak sensors..."), toolsMenu);
979 connect(sensors, SIGNAL(triggered()), this, SLOT(openSensorWindow()));
980 addControlledAction(toolsMenu, sensors, "sensorWindow");
981
982 QAction* cheats = new QAction(tr("&Cheats..."), toolsMenu);
983 connect(cheats, SIGNAL(triggered()), this, SLOT(openCheatsWindow()));
984 addControlledAction(toolsMenu, cheats, "cheatsWindow");
985
986#ifdef USE_GDB_STUB
987 QAction* gdbWindow = new QAction(tr("Start &GDB server..."), toolsMenu);
988 connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
989 addControlledAction(toolsMenu, gdbWindow, "gdbWindow");
990#endif
991
992 toolsMenu->addSeparator();
993 addControlledAction(toolsMenu, toolsMenu->addAction(tr("Settings..."), this, SLOT(openSettingsWindow())), "settings");
994 addControlledAction(toolsMenu, toolsMenu->addAction(tr("Edit shortcuts..."), this, SLOT(openShortcutWindow())), "shortcuts");
995
996 QAction* keymap = new QAction(tr("Remap keyboard..."), toolsMenu);
997 connect(keymap, SIGNAL(triggered()), this, SLOT(openKeymapWindow()));
998 addControlledAction(toolsMenu, keymap, "remapKeyboard");
999
1000#ifdef BUILD_SDL
1001 QAction* gamepad = new QAction(tr("Remap gamepad..."), toolsMenu);
1002 connect(gamepad, SIGNAL(triggered()), this, SLOT(openGamepadWindow()));
1003 addControlledAction(toolsMenu, gamepad, "remapGamepad");
1004#endif
1005
1006 toolsMenu->addSeparator();
1007
1008 QAction* paletteView = new QAction(tr("View &palette..."), toolsMenu);
1009 connect(paletteView, SIGNAL(triggered()), this, SLOT(openPaletteWindow()));
1010 m_gameActions.append(paletteView);
1011 addControlledAction(toolsMenu, paletteView, "paletteWindow");
1012
1013 QAction* memoryView = new QAction(tr("View memory..."), toolsMenu);
1014 connect(memoryView, SIGNAL(triggered()), this, SLOT(openMemoryWindow()));
1015 m_gameActions.append(memoryView);
1016 addControlledAction(toolsMenu, memoryView, "memoryView");
1017
1018 ConfigOption* skipBios = m_config->addOption("skipBios");
1019 skipBios->connect([this](const QVariant& value) {
1020 m_controller->setSkipBIOS(value.toBool());
1021 }, this);
1022
1023 ConfigOption* volume = m_config->addOption("volume");
1024 volume->connect([this](const QVariant& value) {
1025 m_controller->setVolume(value.toInt());
1026 }, this);
1027
1028 ConfigOption* mute = m_config->addOption("mute");
1029 mute->connect([this](const QVariant& value) {
1030 m_controller->setMute(value.toBool());
1031 }, this);
1032
1033 ConfigOption* rewindEnable = m_config->addOption("rewindEnable");
1034 rewindEnable->connect([this](const QVariant& value) {
1035 m_controller->setRewind(value.toBool(), m_config->getOption("rewindBufferCapacity").toInt(), m_config->getOption("rewindBufferInterval").toInt());
1036 }, this);
1037
1038 ConfigOption* rewindBufferCapacity = m_config->addOption("rewindBufferCapacity");
1039 rewindBufferCapacity->connect([this](const QVariant& value) {
1040 m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), value.toInt(), m_config->getOption("rewindBufferInterval").toInt());
1041 }, this);
1042
1043 ConfigOption* rewindBufferInterval = m_config->addOption("rewindBufferInterval");
1044 rewindBufferInterval->connect([this](const QVariant& value) {
1045 m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), m_config->getOption("rewindBufferCapacity").toInt(), value.toInt());
1046 }, this);
1047
1048 ConfigOption* allowOpposingDirections = m_config->addOption("allowOpposingDirections");
1049 allowOpposingDirections->connect([this](const QVariant& value) {
1050 m_inputController.setAllowOpposing(value.toBool());
1051 }, this);
1052
1053 QAction* exitFullScreen = new QAction(tr("Exit fullscreen"), frameMenu);
1054 connect(exitFullScreen, SIGNAL(triggered()), this, SLOT(exitFullScreen()));
1055 exitFullScreen->setShortcut(QKeySequence("Esc"));
1056 addHiddenAction(frameMenu, exitFullScreen, "exitFullScreen");
1057
1058 foreach (QAction* action, m_gameActions) {
1059 action->setDisabled(true);
1060 }
1061}
1062
1063void Window::attachWidget(QWidget* widget) {
1064 m_screenWidget->layout()->addWidget(widget);
1065 static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
1066}
1067
1068void Window::detachWidget(QWidget* widget) {
1069 m_screenWidget->layout()->removeWidget(widget);
1070}
1071
1072void Window::appendMRU(const QString& fname) {
1073 int index = m_mruFiles.indexOf(fname);
1074 if (index >= 0) {
1075 m_mruFiles.removeAt(index);
1076 }
1077 m_mruFiles.prepend(fname);
1078 while (m_mruFiles.size() > ConfigController::MRU_LIST_SIZE) {
1079 m_mruFiles.removeLast();
1080 }
1081 updateMRU();
1082}
1083
1084void Window::updateMRU() {
1085 if (!m_mruMenu) {
1086 return;
1087 }
1088 m_mruMenu->clear();
1089 int i = 0;
1090 for (const QString& file : m_mruFiles) {
1091 QAction* item = new QAction(file, m_mruMenu);
1092 item->setShortcut(QString("Ctrl+%1").arg(i));
1093 connect(item, &QAction::triggered, [this, file]() { m_controller->loadGame(file); });
1094 m_mruMenu->addAction(item);
1095 ++i;
1096 }
1097 m_config->setMRU(m_mruFiles);
1098 m_config->write();
1099 m_mruMenu->setEnabled(i > 0);
1100}
1101
1102QAction* Window::addControlledAction(QMenu* menu, QAction* action, const QString& name) {
1103 addHiddenAction(menu, action, name);
1104 menu->addAction(action);
1105 return action;
1106}
1107
1108QAction* Window::addHiddenAction(QMenu* menu, QAction* action, const QString& name) {
1109 m_shortcutController->addAction(menu, action, name);
1110 action->setShortcutContext(Qt::WidgetShortcut);
1111 addAction(action);
1112 return action;
1113}
1114
1115WindowBackground::WindowBackground(QWidget* parent)
1116 : QLabel(parent)
1117{
1118 setLayout(new QStackedLayout());
1119 layout()->setContentsMargins(0, 0, 0, 0);
1120 setAlignment(Qt::AlignCenter);
1121}
1122
1123void WindowBackground::setSizeHint(const QSize& hint) {
1124 m_sizeHint = hint;
1125}
1126
1127QSize WindowBackground::sizeHint() const {
1128 return m_sizeHint;
1129}
1130
1131void WindowBackground::setLockAspectRatio(int width, int height) {
1132 m_aspectWidth = width;
1133 m_aspectHeight = height;
1134}
1135
1136void WindowBackground::paintEvent(QPaintEvent*) {
1137 const QPixmap* logo = pixmap();
1138 if (!logo) {
1139 return;
1140 }
1141 QPainter painter(this);
1142 painter.setRenderHint(QPainter::SmoothPixmapTransform);
1143 painter.fillRect(QRect(QPoint(), size()), Qt::black);
1144 QSize s = size();
1145 QSize ds = s;
1146 if (s.width() * m_aspectHeight > s.height() * m_aspectWidth) {
1147 ds.setWidth(s.height() * m_aspectWidth / m_aspectHeight);
1148 } else if (s.width() * m_aspectHeight < s.height() * m_aspectWidth) {
1149 ds.setHeight(s.width() * m_aspectHeight / m_aspectWidth);
1150 }
1151 QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
1152 QRect full(origin, ds);
1153 painter.drawPixmap(full, *logo);
1154}