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