Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
creditsmodel.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 Twilight
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15*/
16
21
22#include "./creditsmodel.h"
23#include <QVariantMap>
24#include <pse-db/creditsdb.h>
26
27void CreditsModel::ensureBuilt() const
28{
29 if (m_built)
30 return;
31
32 // Walk the flat store: a non-empty section name starts a new group; every
33 // other entry is an item under the current group.
34 int cur = -1;
35 for (auto entry : CreditsDB::inst()->getStore())
36 {
37 if (!entry->getSection().isEmpty())
38 {
39 m_sections.append(Section{ entry->getSection(), {} });
40 cur = m_sections.size() - 1;
41 continue;
42 }
43
44 if (cur < 0)
45 continue; // defensive: an entry before any header
46
47 QVariantMap item;
48 item["name"] = entry->getName();
49 item["url"] = entry->getUrl();
50 item["note"] = entry->getNote();
51 item["license"] = entry->getLicense();
52 item["mandated"] = entry->getMandated();
53 m_sections[cur].entries.append(item);
54 }
55
56 m_built = true;
57}
58
59int CreditsModel::rowCount(const QModelIndex& parent) const
60{
61 Q_UNUSED(parent)
62
63 ensureBuilt();
64 return m_sections.size();
65}
66
67QVariant CreditsModel::data(const QModelIndex& index, int role) const
68{
69 ensureBuilt();
70
71 // If index is invalid in any way, return nothing
72 if (!index.isValid())
73 return QVariant();
74
75 if (index.row() < 0 || index.row() >= m_sections.size())
76 return QVariant();
77
78 const Section& section = m_sections.at(index.row());
79
80 if (role == SectionRole)
81 return section.name;
82
83 if (role == EntriesRole)
84 return section.entries;
85
86 // All else fails, return nothing
87 return QVariant();
88}
89
90QHash<int, QByteArray> CreditsModel::roleNames() const
91{
92 QHash<int, QByteArray> roles;
93
94 roles[SectionRole] = "section";
95 roles[EntriesRole] = "entries";
96
97 return roles;
98}
static CreditsDB * inst()
< Number of credit entries.
Definition creditsdb.cpp:33
virtual int rowCount(const QModelIndex &parent) const override
Number of sections.
virtual QHash< int, QByteArray > roleNames() const override
Role -> QML name map.
virtual QVariant data(const QModelIndex &index, int role) const override
Value for a section + role.
@ SectionRole
Category name (the card heading).
@ EntriesRole
QVariantList of this section's entry maps.