src/platform/qt/Window.h (view raw)
1/* Copyright (c) 2013-2017 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#pragma once
7
8#include <QAction>
9#include <QDateTime>
10#include <QElapsedTimer>
11#include <QList>
12#include <QMainWindow>
13#include <QTimer>
14
15#include <functional>
16#include <memory>
17
18#include <mgba/core/core.h>
19#include <mgba/core/thread.h>
20
21#include "ActionMapper.h"
22#include "InputController.h"
23#include "LoadSaveState.h"
24#include "LogController.h"
25struct mArguments;
26
27namespace QGBA {
28
29class AudioProcessor;
30class ConfigController;
31class CoreController;
32class CoreManager;
33class DebuggerConsoleController;
34class Display;
35class GDBController;
36class GIFView;
37class LibraryController;
38class LogView;
39class OverrideView;
40class SensorView;
41class ShaderSelector;
42class ShortcutController;
43class VideoView;
44class WindowBackground;
45
46class Window : public QMainWindow {
47Q_OBJECT
48
49public:
50 Window(CoreManager* manager, ConfigController* config, int playerId = 0, QWidget* parent = nullptr);
51 virtual ~Window();
52
53 std::shared_ptr<CoreController> controller() { return m_controller; }
54
55 void setConfig(ConfigController*);
56 void argumentsPassed(mArguments*);
57
58 void resizeFrame(const QSize& size);
59
60 void updateMultiplayerStatus(bool canOpenAnother) { m_multiWindow->setEnabled(canOpenAnother); }
61
62signals:
63 void startDrawing();
64 void shutdown();
65 void paused(bool);
66
67public slots:
68 void setController(CoreController* controller, const QString& fname);
69 void selectROM();
70#ifdef USE_SQLITE3
71 void selectROMInArchive();
72 void addDirToLibrary();
73#endif
74 void selectSave(bool temporary);
75 void selectState(bool load);
76 void selectPatch();
77 void enterFullScreen();
78 void exitFullScreen();
79 void toggleFullScreen();
80 void loadConfig();
81 void reloadConfig();
82 void saveConfig();
83
84 void loadCamImage();
85
86 void replaceROM();
87
88 void multiplayerChanged();
89
90 void importSharkport();
91 void exportSharkport();
92
93 void openSettingsWindow();
94
95 void startVideoLog();
96
97#ifdef USE_DEBUGGERS
98 void consoleOpen();
99#endif
100
101#ifdef USE_FFMPEG
102 void openVideoWindow();
103#endif
104
105#ifdef USE_MAGICK
106 void openGIFWindow();
107#endif
108
109#ifdef USE_GDB_STUB
110 void gdbOpen();
111#endif
112
113protected:
114 virtual void keyPressEvent(QKeyEvent* event) override;
115 virtual void keyReleaseEvent(QKeyEvent* event) override;
116 virtual void resizeEvent(QResizeEvent*) override;
117 virtual void showEvent(QShowEvent*) override;
118 virtual void closeEvent(QCloseEvent*) override;
119 virtual void focusInEvent(QFocusEvent*) override;
120 virtual void focusOutEvent(QFocusEvent*) override;
121 virtual void dragEnterEvent(QDragEnterEvent*) override;
122 virtual void dropEvent(QDropEvent*) override;
123 virtual void mouseDoubleClickEvent(QMouseEvent*) override;
124
125private slots:
126 void gameStarted();
127 void gameStopped();
128 void gameCrashed(const QString&);
129 void gameFailed();
130 void unimplementedBiosCall(int);
131
132 void reloadAudioDriver();
133 void reloadDisplayDriver();
134
135 void tryMakePortable();
136 void mustRestart();
137
138 void recordFrame();
139 void showFPS();
140 void focusCheck();
141
142 void updateFrame();
143
144private:
145 static const int FPS_TIMER_INTERVAL = 2000;
146
147 void setupMenu(QMenuBar*);
148 void openStateWindow(LoadSave);
149
150 void attachWidget(QWidget* widget);
151 void detachWidget(QWidget* widget);
152
153 void appendMRU(const QString& fname);
154 void updateMRU();
155
156 void openView(QWidget* widget);
157
158 template <typename T, typename... A> std::function<void()> openTView(A... arg);
159 template <typename T, typename... A> std::function<void()> openControllerTView(A... arg);
160
161 Action* addGameAction(const QString& visibleName, const QString& name, Action::Function action, const QString& menu = {}, const QKeySequence& = {});
162 template<typename T, typename V> Action* addGameAction(const QString& visibleName, const QString& name, T* obj, V (T::*action)(), const QString& menu = {}, const QKeySequence& = {});
163 Action* addGameAction(const QString& visibleName, const QString& name, Action::BooleanFunction action, const QString& menu = {}, const QKeySequence& = {});
164
165 void updateTitle(float fps = -1);
166
167 QString getFilters() const;
168 QString getFiltersArchive() const;
169
170 CoreManager* m_manager;
171 std::shared_ptr<CoreController> m_controller;
172 std::unique_ptr<AudioProcessor> m_audioProcessor;
173
174 std::unique_ptr<Display> m_display;
175 int m_savedScale;
176
177 // TODO: Move these to a new class
178 ActionMapper m_actions;
179 QList<Action*> m_gameActions;
180 QList<Action*> m_nonMpActions;
181#ifdef M_CORE_GBA
182 QMultiMap<mPlatform, Action*> m_platformActions;
183#endif
184 Action* m_multiWindow;
185 QMap<int, Action*> m_frameSizes;
186
187 LogController m_log{0};
188 LogView* m_logView;
189#ifdef USE_DEBUGGERS
190 DebuggerConsoleController* m_console = nullptr;
191#endif
192 LoadSaveState* m_stateWindow = nullptr;
193 WindowBackground* m_screenWidget;
194 QPixmap m_logo{":/res/mgba-1024.png"};
195 ConfigController* m_config;
196 InputController m_inputController;
197 QList<qint64> m_frameList;
198 QElapsedTimer m_frameTimer;
199 QTimer m_fpsTimer;
200 QList<QString> m_mruFiles;
201 ShortcutController* m_shortcutController;
202#if defined(BUILD_GL) || defined(BUILD_GLES2)
203 std::unique_ptr<ShaderSelector> m_shaderView;
204#endif
205 bool m_fullscreenOnStart = false;
206 QTimer m_focusCheck;
207 bool m_autoresume = false;
208 bool m_wasOpened = false;
209 QString m_pendingPatch;
210 QString m_pendingState;
211 bool m_pendingPause = false;
212
213 bool m_hitUnimplementedBiosCall;
214
215 std::unique_ptr<OverrideView> m_overrideView;
216 std::unique_ptr<SensorView> m_sensorView;
217
218#ifdef USE_FFMPEG
219 VideoView* m_videoView = nullptr;
220#endif
221
222#ifdef USE_MAGICK
223 GIFView* m_gifView = nullptr;
224#endif
225
226#ifdef USE_GDB_STUB
227 GDBController* m_gdbController = nullptr;
228#endif
229
230#ifdef USE_SQLITE3
231 LibraryController* m_libraryView;
232#endif
233};
234
235class WindowBackground : public QWidget {
236Q_OBJECT
237
238public:
239 WindowBackground(QWidget* parent = 0);
240
241 void setPixmap(const QPixmap& pixmap);
242 void setSizeHint(const QSize& size);
243 virtual QSize sizeHint() const override;
244 void setDimensions(int width, int height);
245 void setLockIntegerScaling(bool lock);
246 void setLockAspectRatio(bool lock);
247
248 const QPixmap& pixmap() const { return m_pixmap; }
249
250protected:
251 virtual void paintEvent(QPaintEvent*) override;
252
253private:
254 QPixmap m_pixmap;
255 QSize m_sizeHint;
256 int m_aspectWidth;
257 int m_aspectHeight;
258 bool m_lockAspectRatio;
259 bool m_lockIntegerScaling;
260};
261
262}