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_GDB_STUB
109 void gdbOpen();
110#endif
111
112protected:
113 virtual void keyPressEvent(QKeyEvent* event) override;
114 virtual void keyReleaseEvent(QKeyEvent* event) override;
115 virtual void resizeEvent(QResizeEvent*) override;
116 virtual void showEvent(QShowEvent*) override;
117 virtual void hideEvent(QHideEvent*) 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 void attachDisplay();
135 void changeRenderer();
136
137 void tryMakePortable();
138 void mustRestart();
139
140 void recordFrame();
141 void showFPS();
142 void focusCheck();
143
144 void updateFrame();
145
146 void setLogo();
147
148private:
149 static const int FPS_TIMER_INTERVAL = 2000;
150 static const int MUST_RESTART_TIMEOUT = 10000;
151
152 void setupMenu(QMenuBar*);
153 void openStateWindow(LoadSave);
154
155 void attachWidget(QWidget* widget);
156 void detachWidget(QWidget* widget);
157
158 void appendMRU(const QString& fname);
159 void clearMRU();
160 void updateMRU();
161
162 void openView(QWidget* widget);
163
164 template <typename T, typename... A> std::function<void()> openTView(A... arg);
165 template <typename T, typename... A> std::function<void()> openControllerTView(A... arg);
166 template <typename T, typename... A> std::function<void()> openNamedControllerTView(std::unique_ptr<T>*, A... arg);
167
168 Action* addGameAction(const QString& visibleName, const QString& name, Action::Function action, const QString& menu = {}, const QKeySequence& = {});
169 template<typename T, typename V> Action* addGameAction(const QString& visibleName, const QString& name, T* obj, V (T::*action)(), const QString& menu = {}, const QKeySequence& = {});
170 template<typename V> Action* addGameAction(const QString& visibleName, const QString& name, V (CoreController::*action)(), const QString& menu = {}, const QKeySequence& = {});
171 Action* addGameAction(const QString& visibleName, const QString& name, Action::BooleanFunction action, const QString& menu = {}, const QKeySequence& = {});
172
173 void updateTitle(float fps = -1);
174
175 QString getFilters() const;
176 QString getFiltersArchive() const;
177
178 CoreManager* m_manager;
179 std::shared_ptr<CoreController> m_controller;
180 std::unique_ptr<AudioProcessor> m_audioProcessor;
181
182 std::unique_ptr<Display> m_display;
183 int m_savedScale;
184
185 // TODO: Move these to a new class
186 ActionMapper m_actions;
187 QList<Action*> m_gameActions;
188 QList<Action*> m_nonMpActions;
189#ifdef M_CORE_GBA
190 QMultiMap<mPlatform, Action*> m_platformActions;
191#endif
192 Action* m_multiWindow;
193 QMap<int, Action*> m_frameSizes;
194
195 LogController m_log{0};
196 LogView* m_logView;
197#ifdef USE_DEBUGGERS
198 DebuggerConsoleController* m_console = nullptr;
199#endif
200 LoadSaveState* m_stateWindow = nullptr;
201 WindowBackground* m_screenWidget;
202 QPixmap m_logo{":/res/mgba-1024.png"};
203 ConfigController* m_config;
204 InputController m_inputController;
205 QList<qint64> m_frameList;
206 QElapsedTimer m_frameTimer;
207 QTimer m_fpsTimer;
208 QTimer m_mustRestart;
209 QList<QString> m_mruFiles;
210 ShortcutController* m_shortcutController;
211#if defined(BUILD_GL) || defined(BUILD_GLES2)
212 std::unique_ptr<ShaderSelector> m_shaderView;
213#endif
214 bool m_fullscreenOnStart = false;
215 QTimer m_focusCheck;
216 bool m_autoresume = false;
217 bool m_wasOpened = false;
218 QString m_pendingPatch;
219 QString m_pendingState;
220 bool m_pendingPause = false;
221 bool m_pendingClose = false;
222
223 bool m_hitUnimplementedBiosCall;
224
225 std::unique_ptr<OverrideView> m_overrideView;
226 std::unique_ptr<SensorView> m_sensorView;
227 FrameView* m_frameView = nullptr;
228
229#ifdef USE_FFMPEG
230 std::unique_ptr<VideoView> m_videoView;
231 std::unique_ptr<GIFView> m_gifView;
232#endif
233
234#ifdef USE_GDB_STUB
235 GDBController* m_gdbController = nullptr;
236#endif
237
238#ifdef USE_SQLITE3
239 LibraryController* m_libraryView;
240#endif
241};
242
243class WindowBackground : public QWidget {
244Q_OBJECT
245
246public:
247 WindowBackground(QWidget* parent = 0);
248
249 void setPixmap(const QPixmap& pixmap);
250 void setSizeHint(const QSize& size);
251 virtual QSize sizeHint() const override;
252 void setDimensions(int width, int height);
253 void setLockIntegerScaling(bool lock);
254 void setLockAspectRatio(bool lock);
255 void filter(bool filter);
256
257 const QPixmap& pixmap() const { return m_pixmap; }
258
259protected:
260 virtual void paintEvent(QPaintEvent*) override;
261
262private:
263 QPixmap m_pixmap;
264 QSize m_sizeHint;
265 int m_aspectWidth;
266 int m_aspectHeight;
267 bool m_lockAspectRatio;
268 bool m_lockIntegerScaling;
269 bool m_filter;
270};
271
272}