Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
pokemonstartersmodel.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/pokemon.h>
24
25int PokemonStartersModel::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 4;
32}
33
34QVariant PokemonStartersModel::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() >= 4)
41 return QVariant();
42
43 // Deal with index 0 meaning No Starter
44 if(index.row() == 0 && role == IndRole)
45 return 0;
46 else if(index.row() == 0 && role == NameRole)
47 return tr("None");
48 else if(index.row() == 0)
49 return QVariant();
50
51 // Get Pokemon and ensure it's valid to prevent crashing
52 auto mon = getMon(index.row() - 1);
53 if(mon == nullptr)
54 return QVariant();
55
56 // Now return requested information
57 if (role == IndRole)
58 return mon->ind;
59 else if (role == NameRole) {
60 if(mon->readable != "")
61 return mon->readable;
62
63 return mon->name;
64 }
65
66 // All else fails, return nothing
67 return QVariant();
68}
69
70QHash<int, QByteArray> PokemonStartersModel::roleNames() const
71{
72 QHash<int, QByteArray> roles;
73
74 roles[IndRole] = "monInd";
75 roles[NameRole] = "monName";
76
77 return roles;
78}
79
81{
82 // Index 0 = No Starter
83 if(val == 0)
84 return 0;
85
86 auto mon = PokemonDB::inst()->getIndAt(QString::number(val));
87 if(mon == nullptr)
88 return 4;
89
90 if(mon->readable == "Bulbasaur")
91 return 1;
92 else if(mon->readable == "Charmander")
93 return 2;
94 else if(mon->readable == "Squirtle")
95 return 3;
96
97 return 4;
98}
99
101{
102 return PokemonDB::inst()->getIndAt(starters[ind]);
103}
static PokemonDB * inst()
< Number of species.
Definition pokemon.cpp:183
PokemonDBEntry * getIndAt(const QString &key) const
Species by name key (for QML).
Definition pokemon.cpp:199
QString starters[3]
The three canonical starter species.
virtual QHash< int, QByteArray > roleNames() const override
Role -> QML name.
virtual int rowCount(const QModelIndex &parent) const override
Row count (3).
PokemonDBEntry * getMon(int ind) const
DB entry for row ind.
int valToIndex(int val)
Row index for species value val.
virtual QVariant data(const QModelIndex &index, int role) const override
Row+role value.
One species' complete static data – the richest entry in the db layer.
Definition pokemon.h:98