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