src/platform/qt/LibraryModel.cpp (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#include "LibraryModel.h"
7
8#include <mgba-util/vfs.h>
9
10using namespace QGBA;
11
12LibraryModel::LibraryModel(QObject* parent)
13 : QAbstractItemModel(parent)
14{
15 mLibraryInit(&m_library);
16}
17
18LibraryModel::~LibraryModel() {
19 mLibraryDeinit(&m_library);
20}
21
22void LibraryModel::loadDirectory(VDir* dir) {
23 mLibraryLoadDirectory(&m_library, dir);
24}
25
26const mLibraryEntry* LibraryModel::entryAt(int row) const {
27 if ((unsigned) row < mLibraryListingSize(&m_library.listing)) {
28 return mLibraryListingGetConstPointer(&m_library.listing, row);
29 }
30 return nullptr;
31}
32
33QVariant LibraryModel::data(const QModelIndex& index, int role) const {
34 if (!index.isValid()) {
35 return QVariant();
36 }
37 if (role != Qt::DisplayRole) {
38 return QVariant();
39 }
40 const mLibraryEntry* entry = mLibraryListingGetConstPointer(&m_library.listing, index.row());
41 switch (index.column()) {
42 case 0:
43 return entry->filename;
44 case 1:
45 return (unsigned long long) entry->filesize;
46 }
47 return QVariant();
48}
49
50QVariant LibraryModel::headerData(int section, Qt::Orientation orientation, int role) const {
51 if (role != Qt::DisplayRole) {
52 return QAbstractItemModel::headerData(section, orientation, role);
53 }
54 if (orientation == Qt::Horizontal) {
55 switch (section) {
56 case 0:
57 return tr("Filename");
58 case 1:
59 return tr("Size");
60 }
61 }
62 return section;
63}
64
65QModelIndex LibraryModel::index(int row, int column, const QModelIndex& parent) const {
66 if (parent.isValid()) {
67 return QModelIndex();
68 }
69 return createIndex(row, column, nullptr);
70}
71
72QModelIndex LibraryModel::parent(const QModelIndex&) const {
73 return QModelIndex();
74}
75
76int LibraryModel::columnCount(const QModelIndex& parent) const {
77 if (parent.isValid()) {
78 return 0;
79 }
80 return 2;
81}
82
83int LibraryModel::rowCount(const QModelIndex& parent) const {
84 if (parent.isValid()) {
85 return 0;
86 }
87 return mLibraryListingSize(&m_library.listing);
88}