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