Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
Loading...
Searching...
No Matches
pokedexmodel.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
24
#include <
pse-db/pokemon.h
>
25
#include "
./pokedexmodel.h
"
26
#include <
pse-savefile/expanded/player/playerpokedex.h
>
27
#include "
../bridge/router.h
"
28
29
PokedexEntryData::PokedexEntryData
(QString
name
,
int
dex
,
int
id
)
30
:
name
(
name
),
31
dex
(
dex
),
32
id
(
id
)
33
{}
34
35
PokedexModel::PokedexModel
(
PlayerPokedex
*
pokedex
,
Router
*
router
)
36
:
pokedex
(
pokedex
),
37
router
(
router
)
38
{
39
// Populate cache containing dex entries
40
for
(
auto
el :
PokemonDB::inst
()->getStore()) {
41
if
(!el->pokedex)
42
continue
;
43
44
dexListCache
.append(
new
PokedexEntryData
(
45
el->readable,
46
*el->pokedex,
47
el->ind
48
));
49
}
50
51
// Now sort them via Pokedex number
52
dexSort
();
53
54
// Connect dex changes
55
connect(
pokedex
, &
PlayerPokedex::dexItemChanged
,
this
, &
PokedexModel::dataChanged
);
56
connect(
this
, &PokedexModel::dexSortSelectChanged,
this
, &
PokedexModel::dexSort
);
57
58
// When entering the page, it always needs to be in dex order
59
connect(this->router, &Router::closeNonModal,
this
, &
PokedexModel::pageClosing
);
60
connect(this->router, &Router::goHome,
this
, &
PokedexModel::pageClosing
);
61
}
62
63
int
PokedexModel::rowCount
(
const
QModelIndex& parent)
const
64
{
65
// Not a tree, just a list, there's no parent
66
Q_UNUSED(parent)
67
68
// Return list count
69
return
pokemonDexCount
;
70
}
71
72
QVariant
PokedexModel::data
(
const
QModelIndex& index,
int
role)
const
73
{
74
// If index is invalid in any way, return nothing
75
if
(!index.isValid())
76
return
QVariant();
77
78
if
(index.row() >=
pokemonDexCount
)
79
return
QVariant();
80
81
// Get Pokemon and return if invalid
82
//auto mon = PokemonDB::inst()->getIndAt("dex" + QString::number(index.row()), nullptr);
83
auto
mon =
PokemonDB::inst
()->
getIndAt
(
"dex"
+ QString::number(
dexListCache
.at(index.row())->dex));
84
if
(mon ==
nullptr
)
85
return
QVariant();
86
87
// Now return requested information
88
if
(role ==
IndRole
)
89
return
*mon->pokedex;
90
else
if
(role ==
NameRole
) {
91
if
(mon->readable !=
""
)
92
return
mon->readable;
93
94
return
mon->name;
95
}
96
else
if
(role ==
StateRole
)
97
return
pokedex
->getState(*mon->pokedex);
98
99
// All else fails, return nothing
100
return
QVariant();
101
}
102
103
QHash<int, QByteArray>
PokedexModel::roleNames
()
const
104
{
105
QHash<int, QByteArray> roles;
106
107
roles[
IndRole
] =
"dexInd"
;
108
roles[
NameRole
] =
"dexName"
;
109
roles[
StateRole
] =
"dexState"
;
110
111
return
roles;
112
}
113
114
void
PokedexModel::dataChanged
(
int
ind)
115
{
116
auto
indFixed =
dexToListIndex
(ind);
117
118
QAbstractListModel::dataChanged(
119
index(indFixed),
120
index(indFixed)
121
);
122
}
123
124
void
PokedexModel::dexSortCycle
()
125
{
126
dexSortSelect
++;
127
if
(
dexSortSelect
>=
SortEnd
)
128
dexSortSelect
=
SortBegin
+ 1;
129
130
dexSortSelectChanged();
131
}
132
133
void
PokedexModel::dexSort
()
134
{
135
switch
(
dexSortSelect
) {
136
case
SortDex
:
137
dexSortNum
();
138
break
;
139
140
case
SortName
:
141
dexSortName
();
142
break
;
143
144
case
SortInternal
:
145
dexSortInternal
();
146
break
;
147
148
default
:
149
// dexSortSelect is clamped to the Sort* set elsewhere; leave order unchanged
150
// for any unexpected value. (clang-tidy bugprone-switch-missing-default-case.)
151
break
;
152
}
153
154
beginResetModel();
155
endResetModel();
156
}
157
158
void
PokedexModel::dexSortName
()
159
{
160
QCollator collator;
161
162
// Setup Collator
163
collator.setNumericMode(
true
);
164
collator.setIgnorePunctuation(
true
);
165
166
std::sort(
167
dexListCache
.begin(),
168
dexListCache
.end(),
169
[&collator](
PokedexEntryData
* item1,
PokedexEntryData
* item2)
170
{
171
return collator.compare(item1->name, item2->name) < 0;
172
});
173
}
174
175
void
PokedexModel::dexSortNum
()
176
{
177
std::sort(
178
dexListCache
.begin(),
179
dexListCache
.end(),
180
[](
PokedexEntryData
* item1,
PokedexEntryData
* item2) {
181
return item1->dex < item2->dex;
182
});
183
}
184
185
void
PokedexModel::dexSortInternal
()
186
{
187
std::sort(
188
dexListCache
.begin(),
189
dexListCache
.end(),
190
[](
PokedexEntryData
* item1,
PokedexEntryData
* item2) {
191
return item1->id < item2->id;
192
});
193
}
194
195
void
PokedexModel::pageClosing
()
196
{
197
if
(
dexSortSelect
==
SortDex
)
198
return
;
199
200
dexSortSelect
=
SortDex
;
201
dexSort
();
202
}
203
204
int
PokedexModel::dexToListIndex
(
int
ind)
205
{
206
int
ret = -1;
207
208
for
(
int
i = 0; i <
dexListCache
.size(); i++) {
209
if
(ind !=
dexListCache
.at(i)->dex)
210
continue
;
211
212
ret = i;
213
break
;
214
}
215
216
return
ret;
217
}
PlayerPokedex
The player's Pokedex: a seen flag and an owned flag per species.
Definition
playerpokedex.h:37
PlayerPokedex::dexItemChanged
void dexItemChanged(int ind)
A single entry ind changed.
PokedexModel::rowCount
virtual int rowCount(const QModelIndex &parent) const override
Row count.
Definition
pokedexmodel.cpp:63
PokedexModel::roleNames
virtual QHash< int, QByteArray > roleNames() const override
Role -> QML name.
Definition
pokedexmodel.cpp:103
PokedexModel::dexSortSelect
int dexSortSelect
Definition
pokedexmodel.h:87
PokedexModel::pageClosing
void pageClosing()
Hook for when the dex page closes.
Definition
pokedexmodel.cpp:195
PokedexModel::dexSortInternal
void dexSortInternal()
Sort by internal id.
Definition
pokedexmodel.cpp:185
PokedexModel::dexSort
void dexSort()
Apply the current sort.
Definition
pokedexmodel.cpp:133
PokedexModel::IndRole
@ IndRole
Definition
pokedexmodel.h:55
PokedexModel::StateRole
@ StateRole
Definition
pokedexmodel.h:57
PokedexModel::NameRole
@ NameRole
Definition
pokedexmodel.h:56
PokedexModel::dexSortCycle
void dexSortCycle()
Advance to the next sort order.
Definition
pokedexmodel.cpp:124
PokedexModel::data
virtual QVariant data(const QModelIndex &index, int role) const override
Row+role value.
Definition
pokedexmodel.cpp:72
PokedexModel::dexListCache
QVector< PokedexEntryData * > dexListCache
Cached, currently-sorted rows.
Definition
pokedexmodel.h:88
PokedexModel::SortName
@ SortName
Definition
pokedexmodel.h:65
PokedexModel::SortDex
@ SortDex
Definition
pokedexmodel.h:64
PokedexModel::SortInternal
@ SortInternal
Definition
pokedexmodel.h:66
PokedexModel::SortEnd
@ SortEnd
Definition
pokedexmodel.h:68
PokedexModel::SortBegin
@ SortBegin
Definition
pokedexmodel.h:62
PokedexModel::dexToListIndex
int dexToListIndex(int ind)
Row index for species ind under the current sort.
Definition
pokedexmodel.cpp:204
PokedexModel::dexSortName
void dexSortName()
Sort alphabetically.
Definition
pokedexmodel.cpp:158
PokedexModel::PokedexModel
PokedexModel(PlayerPokedex *pokedex, Router *router)
Definition
pokedexmodel.cpp:35
PokedexModel::pokedex
PlayerPokedex * pokedex
The save's dex.
Definition
pokedexmodel.h:89
PokedexModel::router
Router * router
For page open/close hooks.
Definition
pokedexmodel.h:90
PokedexModel::dataChanged
void dataChanged(int ind)
Notify that dex entry ind changed.
Definition
pokedexmodel.cpp:114
PokedexModel::dexSortNum
void dexSortNum()
Sort by dex number.
Definition
pokedexmodel.cpp:175
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
Router
Screen navigation for the UI – the QML StackView's controller.
Definition
router.h:74
playerpokedex.h
pokedexmodel.h
pokemon.h
pokemonDexCount
constexpr var8 pokemonDexCount
Number of species.
Definition
pokemon.h:28
router.h
PokedexEntryData
One Pokedex grid row: name, dex number, internal id.
Definition
pokedexmodel.h:27
PokedexEntryData::name
QString name
Species name.
Definition
pokedexmodel.h:30
PokedexEntryData::id
int id
Internal id.
Definition
pokedexmodel.h:32
PokedexEntryData::PokedexEntryData
PokedexEntryData(QString name, int dex, int id)
Definition
pokedexmodel.cpp:29
PokedexEntryData::dex
int dex
Pokedex number.
Definition
pokedexmodel.h:31
projects
app
src
mvc
pokedexmodel.cpp
Generated by
1.17.0