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