all repos — mgba @ 6b0847c4725c89f937f8107c28cc4fef611a85d4

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