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