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