src/platform/qt/library/LibraryController.h (view raw)
1/* Copyright (c) 2014-2017 waddlesplash
2 * Copyright (c) 2014-2020 Jeffrey Pfau
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7#pragma once
8
9#include <memory>
10
11#include <QList>
12#include <QMap>
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 LibraryController final : public QStackedWidget {
70Q_OBJECT
71
72public:
73 LibraryController(QWidget* parent = nullptr, const QString& path = QString(),
74 ConfigController* config = nullptr);
75 ~LibraryController();
76
77 LibraryStyle viewStyle() const { return m_currentStyle; }
78 void setViewStyle(LibraryStyle newStyle);
79
80 void selectEntry(LibraryEntryRef entry);
81 LibraryEntryRef selectedEntry();
82 VFile* selectedVFile();
83 QPair<QString, QString> selectedPath();
84
85 void selectLastBootedGame();
86
87 void addDirectory(const QString& dir);
88
89public slots:
90 void clear();
91
92signals:
93 void startGame();
94 void doneLoading();
95
96private slots:
97 void refresh();
98
99private:
100 void loadDirectory(const QString&); // Called on separate thread
101 void freeLibrary();
102
103 ConfigController* m_config = nullptr;
104 std::shared_ptr<mLibrary> m_library;
105 qint64 m_libraryJob = -1;
106 mLibraryListing m_listing;
107 QMap<QString, LibraryEntryRef> m_entries;
108
109 LibraryStyle m_currentStyle;
110 AbstractGameList* m_currentList = nullptr;
111
112 std::unique_ptr<LibraryGrid> m_libraryGrid;
113 std::unique_ptr<LibraryTree> m_libraryTree;
114};
115
116}