Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
typesmodel.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 <pse-db/types.h>
23#include "./typesmodel.h"
24
25int TypesModel::rowCount(const QModelIndex& parent) const
26{
27 // Not a tree, just a list, there's no parent
28 Q_UNUSED(parent)
29
30 // Return list count
31 return TypesDB::inst()->getStore().size() + 1;
32}
33
34QVariant TypesModel::data(const QModelIndex& index, int role) const
35{
36 // If index is invalid in any way, return nothing
37 if (!index.isValid())
38 return QVariant();
39
40 if (index.row() >= TypesDB::inst()->getStore().size() + 1)
41 return QVariant();
42
43 // Placeholder for no type
44 if(index.row() == 0) {
45 if (role == IndRole)
46 return 0xFF;
47 else if (role == NameRole) {
48 return "-----";
49 }
50 // Any other (undeclared) role on the placeholder row: nothing. Without this the
51 // code below would run at(index.row()-1) == at(-1) and crash. (Matches the
52 // PokemonStartersModel placeholder guard.)
53 return QVariant();
54 }
55
56 // Get Pokemon and ensure it's valid to prevent crashing
57 auto type = TypesDB::inst()->getStore().at(index.row() - 1);
58 if(type == nullptr)
59 return QVariant();
60
61 // Now return requested information
62 if (role == IndRole)
63 return type->ind;
64 else if (role == NameRole) {
65 if(type->readable != "")
66 return type->readable;
67
68 return type->name;
69 }
70
71 // All else fails, return nothing
72 return QVariant();
73}
74
75QHash<int, QByteArray> TypesModel::roleNames() const
76{
77 QHash<int, QByteArray> roles;
78
79 roles[IndRole] = "typeInd";
80 roles[NameRole] = "typeName";
81
82 return roles;
83}
84
86{
87 if(val == 0xFF)
88 return 0;
89
90 int ret = 0;
91
92 for(int i = 0; i < TypesDB::inst()->getStore().size(); i++) {
93 if(TypesDB::inst()->getStore().at(i)->ind == val) {
94 // +1 to skip the "-----" (no-type) placeholder that occupies model row 0,
95 // so store entry i maps to its real model row i+1. Without this every type
96 // combo displayed the type one row early (e.g. Fire showed as Ghost).
97 ret = i + 1;
98 break;
99 }
100 }
101
102 return ret;
103}
static TypesDB * inst()
< Number of types.
Definition types.cpp:37
const QVector< TypeDBEntry * > getStore() const
All types.
Definition types.cpp:43
int valToIndex(int val)
Row index for type value val.
virtual QHash< int, QByteArray > roleNames() const override
Role -> QML name.
virtual int rowCount(const QModelIndex &parent) const override
Row count.
virtual QVariant data(const QModelIndex &index, int role) const override
Row+role value.