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