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