all repos — mgba @ 45844301f6d628654f3b61450b25735dad18bd65

mGBA Game Boy Advance Emulator

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