Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
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
>
23
#include "
./pokemonstartersmodel.h
"
24
25
int
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
34
QVariant
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
70
QHash<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
80
int
PokemonStartersModel::valToIndex
(
int
val)
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
100
PokemonDBEntry
*
PokemonStartersModel::getMon
(
int
ind)
const
101
{
102
return
PokemonDB::inst
()->
getIndAt
(
starters
[ind]);
103
}
PokemonDB::inst
static PokemonDB * inst()
< Number of species.
Definition
pokemon.cpp:183
PokemonDB::getIndAt
PokemonDBEntry * getIndAt(const QString &key) const
Species by name key (for QML).
Definition
pokemon.cpp:199
PokemonStartersModel::starters
QString starters[3]
The three canonical starter species.
Definition
pokemonstartersmodel.h:42
PokemonStartersModel::roleNames
virtual QHash< int, QByteArray > roleNames() const override
Role -> QML name.
Definition
pokemonstartersmodel.cpp:70
PokemonStartersModel::rowCount
virtual int rowCount(const QModelIndex &parent) const override
Row count (3).
Definition
pokemonstartersmodel.cpp:25
PokemonStartersModel::getMon
PokemonDBEntry * getMon(int ind) const
DB entry for row ind.
Definition
pokemonstartersmodel.cpp:100
PokemonStartersModel::IndRole
@ IndRole
Definition
pokemonstartersmodel.h:37
PokemonStartersModel::NameRole
@ NameRole
Definition
pokemonstartersmodel.h:38
PokemonStartersModel::valToIndex
int valToIndex(int val)
Row index for species value val.
Definition
pokemonstartersmodel.cpp:80
PokemonStartersModel::data
virtual QVariant data(const QModelIndex &index, int role) const override
Row+role value.
Definition
pokemonstartersmodel.cpp:34
pokemon.h
pokemonstartersmodel.h
PokemonDBEntry
One species' complete static data – the richest entry in the db layer.
Definition
pokemon.h:98
projects
app
src
mvc
pokemonstartersmodel.cpp
Generated by
1.17.0