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