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 void reload();
56
57private slots:
58 void directoryLoaded(const QString& path);
59
60private:
61 struct LibraryColumn {
62 LibraryColumn();
63 LibraryColumn(const QString&, std::function<QString(const mLibraryEntry&)>, int = Qt::AlignLeft);
64 QString name;
65 std::function<QString(const mLibraryEntry&)> value;
66 int alignment;
67 };
68
69 class LibraryHandle {
70 public:
71 LibraryHandle(mLibrary*, const QString& path = QString());
72 ~LibraryHandle();
73
74 mLibrary* const library;
75 LibraryLoader* const loader;
76 const QString path;
77
78 void ref();
79 bool deref();
80
81 private:
82 QThread m_loaderThread;
83 size_t m_ref;
84 };
85
86 LibraryHandle* m_library;
87 static QMap<QString, LibraryHandle*> s_handles;
88
89 mLibraryEntry m_constraints;
90 mLibraryListing m_listings;
91 QStringList m_queue;
92
93 QList<LibraryColumn> m_columns;
94 static QMap<QString, LibraryColumn> s_columns;
95};
96
97class LibraryLoader : public QObject {
98Q_OBJECT
99
100public:
101 LibraryLoader(mLibrary* library, QObject* parent = nullptr);
102
103public slots:
104 void loadDirectory(const QString& path);
105
106signals:
107 void directoryLoaded(const QString& path);
108
109private:
110 mLibrary* m_library;
111};
112
113}
114
115#endif