Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
Loading...
Searching...
No Matches
speciesselectmodel.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 <algorithm>
23
#include <QCollator>
24
25
#include <
pse-db/pokemon.h
>
26
#include "
./speciesselectmodel.h
"
27
28
SpeciesSelectEntry::SpeciesSelectEntry
(QString
name
,
int
ind
)
29
:
name
(
name
),
30
ind
(
ind
)
31
{}
32
33
SpeciesSelectModel::SpeciesSelectModel
()
34
{
35
// Setup Collator
36
QCollator collator;
37
collator.setNumericMode(
true
);
38
collator.setIgnorePunctuation(
true
);
39
40
// Add first category
41
speciesListCache
.append(
new
SpeciesSelectEntry
(
"--- Pokedex Pokemon ---"
, -1));
42
43
// Gather normal repeatable items and sort by name, then add into list
44
QVector<PokemonDBEntry*> tmp;
45
46
for
(
auto
el :
PokemonDB::inst
()->getStore()) {
47
if
(el->pokedex)
48
tmp.append(el);
49
}
50
51
std::sort(
52
tmp.begin(),
53
tmp.end(),
54
[&collator](
const
PokemonDBEntry
* mon1,
const
PokemonDBEntry
* mon2)
55
{
56
return collator.compare(mon1->readable, mon2->readable) < 0;
57
});
58
59
for
(
auto
el : tmp) {
60
speciesListCache
.append(
new
SpeciesSelectEntry
(el->readable, el->ind));
61
}
62
63
tmp.clear();
64
65
// Add 2nd category
66
speciesListCache
.append(
new
SpeciesSelectEntry
(
"--- Glitch Pokemon ---"
, -1));
67
68
for
(
auto
el :
PokemonDB::inst
()->getStore()) {
69
if
(!el->pokedex)
70
tmp.append(el);
71
}
72
73
std::sort(
74
tmp.begin(),
75
tmp.end(),
76
[&collator](
const
PokemonDBEntry
* mon1,
const
PokemonDBEntry
* mon2)
77
{
78
return collator.compare(mon1->readable, mon2->readable) < 0;
79
});
80
81
for
(
auto
el : tmp) {
82
speciesListCache
.append(
new
SpeciesSelectEntry
(el->readable, el->ind));
83
}
84
85
tmp.clear();
86
}
87
88
int
SpeciesSelectModel::rowCount
(
const
QModelIndex& parent)
const
89
{
90
// Not a tree, just a list, there's no parent
91
Q_UNUSED(parent)
92
93
// Return list count
94
return
speciesListCache
.size();
95
}
96
97
QVariant
SpeciesSelectModel::data
(
const
QModelIndex& index,
int
role)
const
98
{
99
// If index is invalid in any way, return nothing
100
if
(!index.isValid())
101
return
QVariant();
102
103
if
(index.row() >=
speciesListCache
.size())
104
return
QVariant();
105
106
// Get Item from Item List Cache
107
auto
item =
speciesListCache
.at(index.row());
108
109
if
(item ==
nullptr
)
110
return
QVariant();
111
112
// Now return requested information
113
if
(role ==
IndRole
)
114
return
item->ind;
115
else
if
(role ==
NameRole
)
116
return
item->name;
117
118
// All else fails, return nothing
119
return
QVariant();
120
}
121
122
QHash<int, QByteArray>
SpeciesSelectModel::roleNames
()
const
123
{
124
QHash<int, QByteArray> roles;
125
126
roles[
IndRole
] =
"speciesInd"
;
127
roles[
NameRole
] =
"speciesName"
;
128
129
return
roles;
130
}
131
132
int
SpeciesSelectModel::speciesToListIndex
(
int
ind)
133
{
134
int
ret = -1;
135
136
for
(
int
i = 0; i <
speciesListCache
.size(); i++) {
137
if
(ind !=
speciesListCache
.at(i)->ind)
138
continue
;
139
140
ret = i;
141
break
;
142
}
143
144
return
ret;
145
}
PokemonDB::inst
static PokemonDB * inst()
< Number of species.
Definition
pokemon.cpp:183
SpeciesSelectModel::data
virtual QVariant data(const QModelIndex &index, int role) const override
Value for a row + role.
Definition
speciesselectmodel.cpp:97
SpeciesSelectModel::rowCount
virtual int rowCount(const QModelIndex &parent) const override
Number of species rows.
Definition
speciesselectmodel.cpp:88
SpeciesSelectModel::speciesListCache
QVector< SpeciesSelectEntry * > speciesListCache
Cached picker rows.
Definition
speciesselectmodel.h:58
SpeciesSelectModel::SpeciesSelectModel
SpeciesSelectModel()
Build the cache from the species DB.
Definition
speciesselectmodel.cpp:33
SpeciesSelectModel::speciesToListIndex
int speciesToListIndex(int ind)
Row index for species ind (for combo highlighting).
Definition
speciesselectmodel.cpp:132
SpeciesSelectModel::roleNames
virtual QHash< int, QByteArray > roleNames() const override
Role -> QML name map.
Definition
speciesselectmodel.cpp:122
SpeciesSelectModel::IndRole
@ IndRole
Definition
speciesselectmodel.h:48
SpeciesSelectModel::NameRole
@ NameRole
Definition
speciesselectmodel.h:49
pokemon.h
speciesselectmodel.h
PokemonDBEntry
One species' complete static data – the richest entry in the db layer.
Definition
pokemon.h:98
SpeciesSelectEntry
One species picker row: display name + species index.
Definition
speciesselectmodel.h:23
SpeciesSelectEntry::name
QString name
Display name.
Definition
speciesselectmodel.h:26
SpeciesSelectEntry::ind
int ind
Species index.
Definition
speciesselectmodel.h:27
SpeciesSelectEntry::SpeciesSelectEntry
SpeciesSelectEntry(QString name, int ind)
Definition
speciesselectmodel.cpp:28
projects
app
src
mvc
speciesselectmodel.cpp
Generated by
1.17.0