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 m_config->setOption("height", m_screenWidget->height());
394 m_config->setOption("width", m_screenWidget->width());
395 m_config->setOption("fullscreen", isFullScreen());
396}
397
398void Window::closeEvent(QCloseEvent* event) {
399 emit shutdown();
400 m_config->setQtOption("windowPos", pos());
401 saveConfig();
402 QMainWindow::closeEvent(event);
403}
404
405void Window::focusOutEvent(QFocusEvent*) {
406 m_controller->setTurbo(false, false);
407 m_controller->stopRewinding();
408 m_controller->clearKeys();
409}
410
411void Window::dragEnterEvent(QDragEnterEvent* event) {
412 if (event->mimeData()->hasFormat("text/uri-list")) {
413 event->acceptProposedAction();
414 }
415}
416
417void Window::dropEvent(QDropEvent* event) {
418 QString uris = event->mimeData()->data("text/uri-list");
419 uris = uris.trimmed();
420 if (uris.contains("\n")) {
421 // Only one file please
422 return;
423 }
424 QUrl url(uris);
425 if (!url.isLocalFile()) {
426 // No remote loading
427 return;
428 }
429 event->accept();
430 m_controller->loadGame(url.path());
431}
432
433void Window::mouseDoubleClickEvent(QMouseEvent* event) {
434 if (event->button() != Qt::LeftButton) {
435 return;
436 }
437 toggleFullScreen();
438}
439
440void Window::enterFullScreen() {
441 if (isFullScreen()) {
442 return;
443 }
444 showFullScreen();
445 setCursor(Qt::BlankCursor);
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 unsetCursor();
458 showNormal();
459 menuBar()->show();
460}
461
462void Window::toggleFullScreen() {
463 if (isFullScreen()) {
464 exitFullScreen();
465 } else {
466 enterFullScreen();
467 }
468}
469
470void Window::gameStarted(GBAThread* context) {
471 char title[13] = { '\0' };
472 MutexLock(&context->stateMutex);
473 if (context->state < THREAD_EXITING) {
474 emit startDrawing(context);
475 GBAGetGameTitle(context->gba, title);
476 } else {
477 MutexUnlock(&context->stateMutex);
478 return;
479 }
480 MutexUnlock(&context->stateMutex);
481 foreach (QAction* action, m_gameActions) {
482 action->setDisabled(false);
483 }
484 if (context->fname) {
485 appendMRU(context->fname);
486 }
487 updateTitle();
488 attachWidget(m_display);
489
490#ifndef Q_OS_MAC
491 if(isFullScreen()) {
492 menuBar()->hide();
493 }
494#endif
495
496 m_hitUnimplementedBiosCall = false;
497 m_fpsTimer.start();
498}
499
500void Window::gameStopped() {
501 foreach (QAction* action, m_gameActions) {
502 action->setDisabled(true);
503 }
504 updateTitle();
505 detachWidget(m_display);
506 m_screenWidget->setLockAspectRatio(m_logo.width(), m_logo.height());
507 m_screenWidget->setPixmap(m_logo);
508
509 m_fpsTimer.stop();
510}
511
512void Window::gameCrashed(const QString& errorMessage) {
513 QMessageBox* crash = new QMessageBox(QMessageBox::Critical, tr("Crash"),
514 tr("The game has crashed with the following error:\n\n%1").arg(errorMessage),
515 QMessageBox::Ok, this, Qt::Sheet);
516 crash->setAttribute(Qt::WA_DeleteOnClose);
517 crash->show();
518}
519
520void Window::gameFailed() {
521 QMessageBox* fail = new QMessageBox(QMessageBox::Warning, tr("Couldn't Load"),
522 tr("Could not load game. Are you sure it's in the correct format?"),
523 QMessageBox::Ok, this, Qt::Sheet);
524 fail->setAttribute(Qt::WA_DeleteOnClose);
525 fail->show();
526}
527
528void Window::unimplementedBiosCall(int call) {
529 if (m_hitUnimplementedBiosCall) {
530 return;
531 }
532 m_hitUnimplementedBiosCall = true;
533
534 QMessageBox* fail = new QMessageBox(QMessageBox::Warning, tr("Unimplemented BIOS call"),
535 tr("This game uses a BIOS call that is not implemented. Please use the official BIOS for best experience."),
536 QMessageBox::Ok, this, Qt::Sheet);
537 fail->setAttribute(Qt::WA_DeleteOnClose);
538 fail->show();
539}
540
541void Window::recordFrame() {
542 m_frameList.append(QDateTime::currentDateTime());
543 while (m_frameList.count() > FRAME_LIST_SIZE) {
544 m_frameList.removeFirst();
545 }
546}
547
548void Window::showFPS() {
549 if (m_frameList.isEmpty()) {
550 updateTitle();
551 return;
552 }
553 qint64 interval = m_frameList.first().msecsTo(m_frameList.last());
554 float fps = (m_frameList.count() - 1) * 10000.f / interval;
555 fps = round(fps) / 10.f;
556 updateTitle(fps);
557}
558
559void Window::updateTitle(float fps) {
560 QString title;
561
562 m_controller->threadInterrupt();
563 if (m_controller->isLoaded()) {
564 char gameTitle[13] = { '\0' };
565 GBAGetGameTitle(m_controller->thread()->gba, gameTitle);
566
567 title = (gameTitle);
568 }
569 MultiplayerController* multiplayer = m_controller->multiplayerController();
570 if (multiplayer && multiplayer->attached() > 1) {
571 title += tr(" - Player %1 of %2").arg(m_playerId + 1).arg(multiplayer->attached());
572 }
573 m_controller->threadContinue();
574 if (title.isNull()) {
575 setWindowTitle(tr("%1 - %2").arg(projectName).arg(projectVersion));
576 } else if (isnan(fps)) {
577 setWindowTitle(tr("%1 - %2 - %3").arg(projectName).arg(title).arg(projectVersion));
578 } else {
579 setWindowTitle(tr("%1 - %2 (%3 fps) - %4").arg(projectName).arg(title).arg(fps).arg(projectVersion));
580 }
581}
582
583void Window::openStateWindow(LoadSave ls) {
584 if (m_stateWindow) {
585 return;
586 }
587 bool wasPaused = m_controller->isPaused();
588 m_stateWindow = new LoadSaveState(m_controller);
589 connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
590 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
591 connect(m_stateWindow, &LoadSaveState::closed, [this]() {
592 m_screenWidget->layout()->removeWidget(m_stateWindow);
593 m_stateWindow = nullptr;
594 QMetaObject::invokeMethod(this, "setFocus", Qt::QueuedConnection);
595 });
596 if (!wasPaused) {
597 m_controller->setPaused(true);
598 connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
599 }
600 m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
601 m_stateWindow->setMode(ls);
602 attachWidget(m_stateWindow);
603}
604
605void Window::setupMenu(QMenuBar* menubar) {
606 menubar->clear();
607 QMenu* fileMenu = menubar->addMenu(tr("&File"));
608 m_shortcutController->addMenu(fileMenu);
609 installEventFilter(m_shortcutController);
610 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open), "loadROM");
611 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS())), "loadBIOS");
612 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch())), "loadPatch");
613 addControlledAction(fileMenu, fileMenu->addAction(tr("Boot BIOS"), m_controller, SLOT(bootBIOS())), "bootBIOS");
614
615 m_mruMenu = fileMenu->addMenu(tr("Recent"));
616
617 fileMenu->addSeparator();
618
619 QAction* loadState = new QAction(tr("&Load state"), fileMenu);
620 loadState->setShortcut(tr("F10"));
621 connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
622 m_gameActions.append(loadState);
623 addControlledAction(fileMenu, loadState, "loadState");
624
625 QAction* saveState = new QAction(tr("&Save state"), fileMenu);
626 saveState->setShortcut(tr("Shift+F10"));
627 connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
628 m_gameActions.append(saveState);
629 addControlledAction(fileMenu, saveState, "saveState");
630
631 QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
632 QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
633 m_shortcutController->addMenu(quickLoadMenu);
634 m_shortcutController->addMenu(quickSaveMenu);
635
636 QAction* quickLoad = new QAction(tr("Load recent"), quickLoadMenu);
637 connect(quickLoad, SIGNAL(triggered()), m_controller, SLOT(loadState()));
638 m_gameActions.append(quickLoad);
639 addControlledAction(quickLoadMenu, quickLoad, "quickLoad");
640
641 QAction* quickSave = new QAction(tr("Save recent"), quickSaveMenu);
642 connect(quickSave, SIGNAL(triggered()), m_controller, SLOT(saveState()));
643 m_gameActions.append(quickSave);
644 addControlledAction(quickSaveMenu, quickSave, "quickSave");
645
646 quickLoadMenu->addSeparator();
647 quickSaveMenu->addSeparator();
648
649 int i;
650 for (i = 1; i < 10; ++i) {
651 quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
652 quickLoad->setShortcut(tr("F%1").arg(i));
653 connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
654 m_gameActions.append(quickLoad);
655 addControlledAction(quickLoadMenu, quickLoad, QString("quickLoad.%1").arg(i));
656
657 quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
658 quickSave->setShortcut(tr("Shift+F%1").arg(i));
659 connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
660 m_gameActions.append(quickSave);
661 addControlledAction(quickSaveMenu, quickSave, QString("quickSave.%1").arg(i));
662 }
663
664 fileMenu->addSeparator();
665 QAction* importShark = new QAction(tr("Import GameShark Save"), fileMenu);
666 connect(importShark, SIGNAL(triggered()), this, SLOT(importSharkport()));
667 m_gameActions.append(importShark);
668 addControlledAction(fileMenu, importShark, "importShark");
669
670 QAction* exportShark = new QAction(tr("Export GameShark Save"), fileMenu);
671 connect(exportShark, SIGNAL(triggered()), this, SLOT(exportSharkport()));
672 m_gameActions.append(exportShark);
673 addControlledAction(fileMenu, exportShark, "exportShark");
674
675 fileMenu->addSeparator();
676 QAction* multiWindow = new QAction(tr("New multiplayer window"), fileMenu);
677 connect(multiWindow, &QAction::triggered, [this]() {
678 GBAApp::app()->newWindow();
679 });
680 addControlledAction(fileMenu, multiWindow, "multiWindow");
681
682#ifndef Q_OS_MAC
683 addControlledAction(fileMenu, fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit), "quit");
684#endif
685
686 QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
687 m_shortcutController->addMenu(emulationMenu);
688 QAction* reset = new QAction(tr("&Reset"), emulationMenu);
689 reset->setShortcut(tr("Ctrl+R"));
690 connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
691 m_gameActions.append(reset);
692 addControlledAction(emulationMenu, reset, "reset");
693
694 QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
695 connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
696 m_gameActions.append(shutdown);
697 addControlledAction(emulationMenu, shutdown, "shutdown");
698 emulationMenu->addSeparator();
699
700 QAction* pause = new QAction(tr("&Pause"), emulationMenu);
701 pause->setChecked(false);
702 pause->setCheckable(true);
703 pause->setShortcut(tr("Ctrl+P"));
704 connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
705 connect(m_controller, &GameController::gamePaused, [this, pause]() {
706 pause->setChecked(true);
707
708 QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
709 QPixmap pixmap;
710 pixmap.convertFromImage(currentImage.rgbSwapped());
711 m_screenWidget->setPixmap(pixmap);
712 m_screenWidget->setLockAspectRatio(3, 2);
713 });
714 connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
715 m_gameActions.append(pause);
716 addControlledAction(emulationMenu, pause, "pause");
717
718 QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
719 frameAdvance->setShortcut(tr("Ctrl+N"));
720 connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
721 m_gameActions.append(frameAdvance);
722 addControlledAction(emulationMenu, frameAdvance, "frameAdvance");
723
724 emulationMenu->addSeparator();
725
726 m_shortcutController->addFunctions(emulationMenu, [this]() {
727 m_controller->setTurbo(true, false);
728 }, [this]() {
729 m_controller->setTurbo(false, false);
730 }, QKeySequence(Qt::Key_Tab), tr("Fast forward (held)"), "holdFastForward");
731
732 QAction* turbo = new QAction(tr("&Fast forward"), emulationMenu);
733 turbo->setCheckable(true);
734 turbo->setChecked(false);
735 turbo->setShortcut(tr("Shift+Tab"));
736 connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
737 addControlledAction(emulationMenu, turbo, "fastForward");
738
739 QMenu* ffspeedMenu = emulationMenu->addMenu(tr("Fast forward speed"));
740 ConfigOption* ffspeed = m_config->addOption("fastForwardRatio");
741 ffspeed->connect([this](const QVariant& value) {
742 m_controller->setTurboSpeed(value.toFloat());
743 }, this);
744 ffspeed->addValue(tr("Unbounded"), -1.0f, ffspeedMenu);
745 ffspeed->setValue(QVariant(-1.0f));
746 ffspeedMenu->addSeparator();
747 for (i = 2; i < 11; ++i) {
748 ffspeed->addValue(tr("%0x").arg(i), i, ffspeedMenu);
749 }
750 m_config->updateOption("fastForwardRatio");
751
752 m_shortcutController->addFunctions(emulationMenu, [this]() {
753 m_controller->startRewinding();
754 }, [this]() {
755 m_controller->stopRewinding();
756 }, QKeySequence("~"), tr("Rewind (held)"), "holdRewind");
757
758 QAction* rewind = new QAction(tr("Re&wind"), emulationMenu);
759 rewind->setShortcut(tr("`"));
760 connect(rewind, SIGNAL(triggered()), m_controller, SLOT(rewind()));
761 m_gameActions.append(rewind);
762 addControlledAction(emulationMenu, rewind, "rewind");
763
764 QAction* frameRewind = new QAction(tr("Step backwards"), emulationMenu);
765 frameRewind->setShortcut(tr("Ctrl+B"));
766 connect(frameRewind, &QAction::triggered, [this] () {
767 m_controller->rewind(1);
768 });
769 m_gameActions.append(frameRewind);
770 addControlledAction(emulationMenu, frameRewind, "frameRewind");
771
772 ConfigOption* videoSync = m_config->addOption("videoSync");
773 videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
774 videoSync->connect([this](const QVariant& value) {
775 m_controller->setVideoSync(value.toBool());
776 }, this);
777 m_config->updateOption("videoSync");
778
779 ConfigOption* audioSync = m_config->addOption("audioSync");
780 audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
781 audioSync->connect([this](const QVariant& value) {
782 m_controller->setAudioSync(value.toBool());
783 }, this);
784 m_config->updateOption("audioSync");
785
786 emulationMenu->addSeparator();
787
788 QMenu* solarMenu = emulationMenu->addMenu(tr("Solar sensor"));
789 m_shortcutController->addMenu(solarMenu);
790 QAction* solarIncrease = new QAction(tr("Increase solar level"), solarMenu);
791 connect(solarIncrease, SIGNAL(triggered()), m_controller, SLOT(increaseLuminanceLevel()));
792 addControlledAction(solarMenu, solarIncrease, "increaseLuminanceLevel");
793
794 QAction* solarDecrease = new QAction(tr("Decrease solar level"), solarMenu);
795 connect(solarDecrease, SIGNAL(triggered()), m_controller, SLOT(decreaseLuminanceLevel()));
796 addControlledAction(solarMenu, solarDecrease, "decreaseLuminanceLevel");
797
798 QAction* maxSolar = new QAction(tr("Brightest solar level"), solarMenu);
799 connect(maxSolar, &QAction::triggered, [this]() { m_controller->setLuminanceLevel(10); });
800 addControlledAction(solarMenu, maxSolar, "maxLuminanceLevel");
801
802 QAction* minSolar = new QAction(tr("Darkest solar level"), solarMenu);
803 connect(minSolar, &QAction::triggered, [this]() { m_controller->setLuminanceLevel(0); });
804 addControlledAction(solarMenu, minSolar, "minLuminanceLevel");
805
806 solarMenu->addSeparator();
807 for (int i = 0; i <= 10; ++i) {
808 QAction* setSolar = new QAction(tr("Brightness %1").arg(QString::number(i)), solarMenu);
809 connect(setSolar, &QAction::triggered, [this, i]() {
810 m_controller->setLuminanceLevel(i);
811 });
812 addControlledAction(solarMenu, setSolar, QString("luminanceLevel.%1").arg(QString::number(i)));
813 }
814
815 QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
816 m_shortcutController->addMenu(avMenu);
817 QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
818 m_shortcutController->addMenu(frameMenu, avMenu);
819 for (int i = 1; i <= 6; ++i) {
820 QAction* setSize = new QAction(tr("%1x").arg(QString::number(i)), avMenu);
821 connect(setSize, &QAction::triggered, [this, i]() {
822 showNormal();
823 resizeFrame(VIDEO_HORIZONTAL_PIXELS * i, VIDEO_VERTICAL_PIXELS * i);
824 });
825 addControlledAction(frameMenu, setSize, QString("frame%1x").arg(QString::number(i)));
826 }
827 addControlledAction(frameMenu, frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F")), "fullscreen");
828
829 ConfigOption* lockAspectRatio = m_config->addOption("lockAspectRatio");
830 lockAspectRatio->addBoolean(tr("Lock aspect ratio"), avMenu);
831 lockAspectRatio->connect([this](const QVariant& value) {
832 m_display->lockAspectRatio(value.toBool());
833 }, this);
834 m_config->updateOption("lockAspectRatio");
835
836 ConfigOption* resampleVideo = m_config->addOption("resampleVideo");
837 resampleVideo->addBoolean(tr("Resample video"), avMenu);
838 resampleVideo->connect([this](const QVariant& value) {
839 m_display->filter(value.toBool());
840 }, this);
841 m_config->updateOption("resampleVideo");
842
843 QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
844 ConfigOption* skip = m_config->addOption("frameskip");
845 skip->connect([this](const QVariant& value) {
846 m_controller->setFrameskip(value.toInt());
847 }, this);
848 for (int i = 0; i <= 10; ++i) {
849 skip->addValue(QString::number(i), i, skipMenu);
850 }
851 m_config->updateOption("frameskip");
852
853 avMenu->addSeparator();
854
855 QMenu* buffersMenu = avMenu->addMenu(tr("Audio buffer &size"));
856 ConfigOption* buffers = m_config->addOption("audioBuffers");
857 buffers->connect([this](const QVariant& value) {
858 emit audioBufferSamplesChanged(value.toInt());
859 }, this);
860 buffers->addValue(tr("512"), 512, buffersMenu);
861 buffers->addValue(tr("768"), 768, buffersMenu);
862 buffers->addValue(tr("1024"), 1024, buffersMenu);
863 buffers->addValue(tr("2048"), 2048, buffersMenu);
864 buffers->addValue(tr("4096"), 4096, buffersMenu);
865 m_config->updateOption("audioBuffers");
866
867 avMenu->addSeparator();
868
869 QMenu* target = avMenu->addMenu(tr("FPS target"));
870 ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
871 fpsTargetOption->connect([this](const QVariant& value) {
872 emit fpsTargetChanged(value.toFloat());
873 }, this);
874 fpsTargetOption->addValue(tr("15"), 15, target);
875 fpsTargetOption->addValue(tr("30"), 30, target);
876 fpsTargetOption->addValue(tr("45"), 45, target);
877 fpsTargetOption->addValue(tr("Native (59.7)"), float(GBA_ARM7TDMI_FREQUENCY) / float(VIDEO_TOTAL_LENGTH), target);
878 fpsTargetOption->addValue(tr("60"), 60, target);
879 fpsTargetOption->addValue(tr("90"), 90, target);
880 fpsTargetOption->addValue(tr("120"), 120, target);
881 fpsTargetOption->addValue(tr("240"), 240, target);
882 m_config->updateOption("fpsTarget");
883
884#if defined(USE_PNG) || defined(USE_FFMPEG) || defined(USE_MAGICK)
885 avMenu->addSeparator();
886#endif
887
888#ifdef USE_PNG
889 QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
890 screenshot->setShortcut(tr("F12"));
891 connect(screenshot, SIGNAL(triggered()), m_controller, SLOT(screenshot()));
892 m_gameActions.append(screenshot);
893 addControlledAction(avMenu, screenshot, "screenshot");
894#endif
895
896#ifdef USE_FFMPEG
897 QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
898 recordOutput->setShortcut(tr("F11"));
899 connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
900 addControlledAction(avMenu, recordOutput, "recordOutput");
901#endif
902
903#ifdef USE_MAGICK
904 QAction* recordGIF = new QAction(tr("Record GIF..."), avMenu);
905 recordGIF->setShortcut(tr("Shift+F11"));
906 connect(recordGIF, SIGNAL(triggered()), this, SLOT(openGIFWindow()));
907 addControlledAction(avMenu, recordGIF, "recordGIF");
908#endif
909
910 avMenu->addSeparator();
911 QMenu* videoLayers = avMenu->addMenu(tr("Video layers"));
912
913 for (int i = 0; i < 4; ++i) {
914 QAction* enableBg = new QAction(tr("Background %0").arg(i), videoLayers);
915 enableBg->setCheckable(true);
916 enableBg->setChecked(true);
917 connect(enableBg, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->video.renderer->disableBG[i] = !enable; });
918 m_gameActions.append(enableBg);
919 addControlledAction(videoLayers, enableBg, QString("enableBG%0").arg(i));
920 }
921
922 QAction* enableObj = new QAction(tr("OBJ (sprites)"), videoLayers);
923 enableObj->setCheckable(true);
924 enableObj->setChecked(true);
925 connect(enableObj, &QAction::triggered, [this](bool enable) { m_controller->thread()->gba->video.renderer->disableOBJ = !enable; });
926 m_gameActions.append(enableObj);
927 addControlledAction(videoLayers, enableObj, "enableOBJ");
928
929 QMenu* audioChannels = avMenu->addMenu(tr("Audio channels"));
930
931 for (int i = 0; i < 4; ++i) {
932 QAction* enableCh = new QAction(tr("Channel %0").arg(i + 1), audioChannels);
933 enableCh->setCheckable(true);
934 enableCh->setChecked(true);
935 connect(enableCh, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->audio.forceDisableCh[i] = !enable; });
936 m_gameActions.append(enableCh);
937 addControlledAction(audioChannels, enableCh, QString("enableCh%0").arg(i + 1));
938 }
939
940 QAction* enableChA = new QAction(tr("Channel A"), audioChannels);
941 enableChA->setCheckable(true);
942 enableChA->setChecked(true);
943 connect(enableChA, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->audio.forceDisableChA = !enable; });
944 m_gameActions.append(enableChA);
945 addControlledAction(audioChannels, enableChA, QString("enableChA"));
946
947 QAction* enableChB = new QAction(tr("Channel B"), audioChannels);
948 enableChB->setCheckable(true);
949 enableChB->setChecked(true);
950 connect(enableChB, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->audio.forceDisableChB = !enable; });
951 m_gameActions.append(enableChB);
952 addControlledAction(audioChannels, enableChB, QString("enableChB"));
953
954 QMenu* toolsMenu = menubar->addMenu(tr("&Tools"));
955 m_shortcutController->addMenu(toolsMenu);
956 QAction* viewLogs = new QAction(tr("View &logs..."), toolsMenu);
957 connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
958 addControlledAction(toolsMenu, viewLogs, "viewLogs");
959
960 QAction* overrides = new QAction(tr("Game &overrides..."), toolsMenu);
961 connect(overrides, SIGNAL(triggered()), this, SLOT(openOverrideWindow()));
962 addControlledAction(toolsMenu, overrides, "overrideWindow");
963
964 QAction* sensors = new QAction(tr("Game &Pak sensors..."), toolsMenu);
965 connect(sensors, SIGNAL(triggered()), this, SLOT(openSensorWindow()));
966 addControlledAction(toolsMenu, sensors, "sensorWindow");
967
968 QAction* cheats = new QAction(tr("&Cheats..."), toolsMenu);
969 connect(cheats, SIGNAL(triggered()), this, SLOT(openCheatsWindow()));
970 addControlledAction(toolsMenu, cheats, "cheatsWindow");
971
972#ifdef USE_GDB_STUB
973 QAction* gdbWindow = new QAction(tr("Start &GDB server..."), toolsMenu);
974 connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
975 addControlledAction(toolsMenu, gdbWindow, "gdbWindow");
976#endif
977
978 toolsMenu->addSeparator();
979 addControlledAction(toolsMenu, toolsMenu->addAction(tr("Settings..."), this, SLOT(openSettingsWindow())), "settings");
980 addControlledAction(toolsMenu, toolsMenu->addAction(tr("Edit shortcuts..."), this, SLOT(openShortcutWindow())), "shortcuts");
981
982 QAction* keymap = new QAction(tr("Remap keyboard..."), toolsMenu);
983 connect(keymap, SIGNAL(triggered()), this, SLOT(openKeymapWindow()));
984 addControlledAction(toolsMenu, keymap, "remapKeyboard");
985
986#ifdef BUILD_SDL
987 QAction* gamepad = new QAction(tr("Remap gamepad..."), toolsMenu);
988 connect(gamepad, SIGNAL(triggered()), this, SLOT(openGamepadWindow()));
989 addControlledAction(toolsMenu, gamepad, "remapGamepad");
990#endif
991
992 toolsMenu->addSeparator();
993
994 QAction* paletteView = new QAction(tr("View &palette..."), toolsMenu);
995 connect(paletteView, SIGNAL(triggered()), this, SLOT(openPaletteWindow()));
996 m_gameActions.append(paletteView);
997 addControlledAction(toolsMenu, paletteView, "paletteWindow");
998
999 QAction* memoryView = new QAction(tr("View memory..."), toolsMenu);
1000 connect(memoryView, SIGNAL(triggered()), this, SLOT(openMemoryWindow()));
1001 m_gameActions.append(memoryView);
1002 addControlledAction(toolsMenu, memoryView, "memoryView");
1003
1004 ConfigOption* skipBios = m_config->addOption("skipBios");
1005 skipBios->connect([this](const QVariant& value) {
1006 m_controller->setSkipBIOS(value.toBool());
1007 }, this);
1008
1009 ConfigOption* volume = m_config->addOption("volume");
1010 volume->connect([this](const QVariant& value) {
1011 m_controller->setVolume(value.toInt());
1012 }, this);
1013
1014 ConfigOption* mute = m_config->addOption("mute");
1015 mute->connect([this](const QVariant& value) {
1016 m_controller->setMute(value.toBool());
1017 }, this);
1018
1019 ConfigOption* rewindEnable = m_config->addOption("rewindEnable");
1020 rewindEnable->connect([this](const QVariant& value) {
1021 m_controller->setRewind(value.toBool(), m_config->getOption("rewindBufferCapacity").toInt(), m_config->getOption("rewindBufferInterval").toInt());
1022 }, this);
1023
1024 ConfigOption* rewindBufferCapacity = m_config->addOption("rewindBufferCapacity");
1025 rewindBufferCapacity->connect([this](const QVariant& value) {
1026 m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), value.toInt(), m_config->getOption("rewindBufferInterval").toInt());
1027 }, this);
1028
1029 ConfigOption* rewindBufferInterval = m_config->addOption("rewindBufferInterval");
1030 rewindBufferInterval->connect([this](const QVariant& value) {
1031 m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), m_config->getOption("rewindBufferCapacity").toInt(), value.toInt());
1032 }, this);
1033
1034 ConfigOption* allowOpposingDirections = m_config->addOption("allowOpposingDirections");
1035 allowOpposingDirections->connect([this](const QVariant& value) {
1036 m_inputController.setAllowOpposing(value.toBool());
1037 }, this);
1038
1039 QAction* exitFullScreen = new QAction(tr("Exit fullscreen"), frameMenu);
1040 connect(exitFullScreen, SIGNAL(triggered()), this, SLOT(exitFullScreen()));
1041 exitFullScreen->setShortcut(QKeySequence("Esc"));
1042 addHiddenAction(frameMenu, exitFullScreen, "exitFullScreen");
1043
1044 foreach (QAction* action, m_gameActions) {
1045 action->setDisabled(true);
1046 }
1047}
1048
1049void Window::attachWidget(QWidget* widget) {
1050 m_screenWidget->layout()->addWidget(widget);
1051 static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
1052}
1053
1054void Window::detachWidget(QWidget* widget) {
1055 m_screenWidget->layout()->removeWidget(widget);
1056}
1057
1058void Window::appendMRU(const QString& fname) {
1059 int index = m_mruFiles.indexOf(fname);
1060 if (index >= 0) {
1061 m_mruFiles.removeAt(index);
1062 }
1063 m_mruFiles.prepend(fname);
1064 while (m_mruFiles.size() > ConfigController::MRU_LIST_SIZE) {
1065 m_mruFiles.removeLast();
1066 }
1067 updateMRU();
1068}
1069
1070void Window::updateMRU() {
1071 if (!m_mruMenu) {
1072 return;
1073 }
1074 m_mruMenu->clear();
1075 int i = 0;
1076 for (const QString& file : m_mruFiles) {
1077 QAction* item = new QAction(file, m_mruMenu);
1078 item->setShortcut(QString("Ctrl+%1").arg(i));
1079 connect(item, &QAction::triggered, [this, file]() { m_controller->loadGame(file); });
1080 m_mruMenu->addAction(item);
1081 ++i;
1082 }
1083 m_config->setMRU(m_mruFiles);
1084 m_config->write();
1085 m_mruMenu->setEnabled(i > 0);
1086}
1087
1088QAction* Window::addControlledAction(QMenu* menu, QAction* action, const QString& name) {
1089 addHiddenAction(menu, action, name);
1090 menu->addAction(action);
1091 return action;
1092}
1093
1094QAction* Window::addHiddenAction(QMenu* menu, QAction* action, const QString& name) {
1095 m_shortcutController->addAction(menu, action, name);
1096 action->setShortcutContext(Qt::WidgetShortcut);
1097 addAction(action);
1098 return action;
1099}
1100
1101WindowBackground::WindowBackground(QWidget* parent)
1102 : QLabel(parent)
1103{
1104 setLayout(new QStackedLayout());
1105 layout()->setContentsMargins(0, 0, 0, 0);
1106 setAlignment(Qt::AlignCenter);
1107}
1108
1109void WindowBackground::setSizeHint(const QSize& hint) {
1110 m_sizeHint = hint;
1111}
1112
1113QSize WindowBackground::sizeHint() const {
1114 return m_sizeHint;
1115}
1116
1117void WindowBackground::setLockAspectRatio(int width, int height) {
1118 m_aspectWidth = width;
1119 m_aspectHeight = height;
1120}
1121
1122void WindowBackground::paintEvent(QPaintEvent*) {
1123 const QPixmap* logo = pixmap();
1124 if (!logo) {
1125 return;
1126 }
1127 QPainter painter(this);
1128 painter.setRenderHint(QPainter::SmoothPixmapTransform);
1129 painter.fillRect(QRect(QPoint(), size()), Qt::black);
1130 QSize s = size();
1131 QSize ds = s;
1132 if (s.width() * m_aspectHeight > s.height() * m_aspectWidth) {
1133 ds.setWidth(s.height() * m_aspectWidth / m_aspectHeight);
1134 } else if (s.width() * m_aspectHeight < s.height() * m_aspectWidth) {
1135 ds.setHeight(s.width() * m_aspectHeight / m_aspectWidth);
1136 }
1137 QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
1138 QRect full(origin, ds);
1139 painter.drawPixmap(full, *logo);
1140}