all repos — mgba @ 2d2f49c361831a085ec5d008edbc7f2da8c88335

mGBA Game Boy Advance Emulator

src/platform/qt/library/LibraryController.h (view raw)

  1/* Copyright (c) 2014-2017 waddlesplash
  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#pragma once
  7
  8#include <memory>
  9
 10#include <QList>
 11#include <QMap>
 12#include <QStackedWidget>
 13
 14#include <mgba/core/library.h>
 15
 16namespace QGBA {
 17
 18// Predefinitions
 19class LibraryGrid;
 20class LibraryTree;
 21class ConfigController;
 22
 23enum class LibraryStyle {
 24	STYLE_LIST = 0,
 25	STYLE_TREE,
 26	STYLE_GRID,
 27	STYLE_ICON
 28};
 29
 30class LibraryEntry final {
 31public:
 32	LibraryEntry(mLibraryEntry* entry);
 33
 34	QString displayTitle() const { return title().isNull() ? filename() : title(); }
 35
 36	QString base() const { return QString(entry->base); }
 37	QString filename() const { return QString(entry->filename); }
 38	QString fullpath() const { return m_fullpath; }
 39	QString title() const { return QString(entry->title); }
 40	QByteArray internalTitle() const { return QByteArray(entry->internalTitle); }
 41	QByteArray internalCode() const { return QByteArray(entry->internalCode); }
 42	mPlatform platform() const { return entry->platform; }
 43	size_t filesize() const { return entry->filesize; }
 44	uint32_t crc32() const { return entry->crc32; }
 45
 46	const mLibraryEntry* entry;
 47private:
 48	const QString m_fullpath;
 49};
 50typedef std::shared_ptr<LibraryEntry> LibraryEntryRef;
 51
 52class AbstractGameList {
 53public:
 54	virtual LibraryEntryRef selectedEntry() = 0;
 55	virtual void selectEntry(LibraryEntryRef game) = 0;
 56
 57	virtual void setViewStyle(LibraryStyle newStyle) = 0;
 58
 59	virtual void addEntry(LibraryEntryRef item) = 0;
 60	virtual void addEntries(QList<LibraryEntryRef> items);
 61
 62	virtual void removeEntry(LibraryEntryRef item) = 0;
 63	virtual void removeEntries(QList<LibraryEntryRef> items);
 64
 65	virtual QWidget* widget() = 0;
 66};
 67
 68class LibraryController final : public QStackedWidget {
 69Q_OBJECT
 70
 71public:
 72	LibraryController(QWidget* parent = nullptr, const QString& path = QString(),
 73	    ConfigController* config = nullptr);
 74	~LibraryController();
 75
 76	LibraryStyle viewStyle() const { return m_currentStyle; }
 77	void setViewStyle(LibraryStyle newStyle);
 78
 79	void selectEntry(LibraryEntryRef entry);
 80	LibraryEntryRef selectedEntry();
 81	VFile* selectedVFile();
 82	QPair<QString, QString> selectedPath();
 83
 84	void selectLastBootedGame();
 85
 86	void addDirectory(const QString& dir);
 87
 88public slots:
 89	void clear();
 90
 91signals:
 92	void startGame();
 93	void doneLoading();
 94
 95private slots:
 96	void refresh();
 97
 98private:
 99	void loadDirectory(const QString&); // Called on separate thread
100
101	ConfigController* m_config = nullptr;
102	std::shared_ptr<mLibrary> m_library;
103	qint64 m_libraryJob = -1;
104	mLibraryListing m_listing;
105	QMap<QString, LibraryEntryRef> m_entries;
106
107	LibraryStyle m_currentStyle;
108	AbstractGameList* m_currentList = nullptr;
109
110	std::unique_ptr<LibraryGrid> m_libraryGrid;
111	std::unique_ptr<LibraryTree> m_libraryTree;
112};
113
114}