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