src/platform/qt/GBAApp.h (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#pragma once
7
8#include <QApplication>
9#include <QFileDialog>
10#include <QList>
11#include <QMap>
12#include <QMultiMap>
13#include <QObject>
14#include <QRunnable>
15#include <QString>
16#include <QThreadPool>
17
18#include <functional>
19
20#include "CoreManager.h"
21#include "MultiplayerController.h"
22
23struct NoIntroDB;
24
25#include <mgba/core/log.h>
26
27mLOG_DECLARE_CATEGORY(QT);
28
29namespace QGBA {
30
31class ConfigController;
32class CoreController;
33class Window;
34
35#ifdef USE_SQLITE3
36class GameDBParser : public QObject {
37Q_OBJECT
38
39public:
40 GameDBParser(NoIntroDB* db, QObject* parent = nullptr);
41
42public slots:
43 void parseNoIntroDB();
44
45private:
46 NoIntroDB* m_db;
47};
48#endif
49
50class GBAApp : public QApplication {
51Q_OBJECT
52
53public:
54 GBAApp(int& argc, char* argv[], ConfigController*);
55 static GBAApp* app();
56
57 static QString dataDir();
58
59 QList<Window*> windows() { return m_windows; }
60 Window* newWindow();
61
62 QString getOpenFileName(QWidget* owner, const QString& title, const QString& filter = {});
63 QStringList getOpenFileNames(QWidget* owner, const QString& title, const QString& filter = {});
64 QString getSaveFileName(QWidget* owner, const QString& title, const QString& filter = {});
65 QString getOpenDirectoryName(QWidget* owner, const QString& title, const QString& path = {});
66
67 const NoIntroDB* gameDB() const { return m_db; }
68 bool reloadGameDB();
69
70 qint64 submitWorkerJob(std::function<void ()> job, std::function<void ()> callback = {});
71 qint64 submitWorkerJob(std::function<void ()> job, QObject* context, std::function<void ()> callback);
72 bool removeWorkerJob(qint64 jobId);
73 bool waitOnJob(qint64 jobId, QObject* context, std::function<void ()> callback);
74
75signals:
76 void jobFinished(qint64 jobId);
77
78protected:
79 bool event(QEvent*);
80
81private slots:
82 void finishJob(qint64 jobId);
83 void cleanup();
84
85private:
86 class WorkerJob : public QRunnable {
87 public:
88 WorkerJob(qint64 id, std::function<void ()> job, GBAApp* owner);
89
90 public:
91 void run() override;
92
93 private:
94 qint64 m_id;
95 std::function<void ()> m_job;
96 GBAApp* m_owner;
97 };
98
99 Window* newWindowInternal();
100
101 void pauseAll(QList<Window*>* paused);
102 void continueAll(const QList<Window*>& paused);
103
104 ConfigController* m_configController;
105 QList<Window*> m_windows;
106 MultiplayerController m_multiplayer;
107 CoreManager m_manager;
108
109 QMap<qint64, WorkerJob*> m_workerJobs;
110 QMultiMap<qint64, QMetaObject::Connection> m_workerJobCallbacks;
111 QThreadPool m_workerThreads;
112 qint64 m_nextJob = 1;
113
114 NoIntroDB* m_db = nullptr;
115};
116
117}