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