src/platform/qt/LibraryModel.h (view raw)
1/* Copyright (c) 2013-2016 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#ifndef QGBA_LIBRARY_MODEL
7#define QGBA_LIBRARY_MODEL
8
9#include <QAbstractItemModel>
10#include <QStringList>
11#include <QThread>
12
13#include <mgba/core/library.h>
14
15#include <functional>
16
17struct VDir;
18struct VFile;
19struct NoIntroDB;
20
21namespace QGBA {
22
23class LibraryLoader;
24class LibraryModel : public QAbstractItemModel {
25Q_OBJECT
26
27public:
28 LibraryModel(const QString& path, QObject* parent = nullptr);
29 virtual ~LibraryModel();
30
31 bool entryAt(int row, mLibraryEntry* out) const;
32 VFile* openVFile(const QModelIndex& index) const;
33 QString filename(const QModelIndex& index) const;
34 QString location(const QModelIndex& index) const;
35
36 virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
37 virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
38
39 virtual QModelIndex index(int row, int column, const QModelIndex& parent) const override;
40 virtual QModelIndex parent(const QModelIndex& index) const override;
41
42 virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override;
43 virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
44
45 void attachGameDB(const NoIntroDB* gameDB);
46
47signals:
48 void doneLoading();
49
50public slots:
51 void loadDirectory(const QString& path);
52
53 void constrainBase(const QString& path);
54 void clearConstraints();
55
56private slots:
57 void directoryLoaded(const QString& path);
58
59private:
60 struct LibraryColumn {
61 QString name;
62 std::function<QString(const mLibraryEntry&)> value;
63 };
64
65 class LibraryHandle {
66 public:
67 LibraryHandle(mLibrary*, const QString& path = QString());
68 ~LibraryHandle();
69
70 mLibrary* const library;
71 LibraryLoader* const loader;
72 const QString path;
73
74 void ref();
75 bool deref();
76
77 private:
78 QThread m_loaderThread;
79 size_t m_ref;
80 };
81
82 LibraryHandle* m_library;
83 static QMap<QString, LibraryHandle*> s_handles;
84
85 mLibraryEntry m_constraints;
86 QStringList m_queue;
87
88 QList<LibraryColumn> m_columns;
89 static QMap<QString, LibraryColumn> s_columns;
90};
91
92class LibraryLoader : public QObject {
93Q_OBJECT
94
95public:
96 LibraryLoader(mLibrary* library, QObject* parent = nullptr);
97
98public slots:
99 void loadDirectory(const QString& path);
100
101signals:
102 void directoryLoaded(const QString& path);
103
104private:
105 mLibrary* m_library;
106};
107
108}
109
110#endif