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